Created
July 11, 2018 19:26
-
-
Save coolsoftwaretyler/97b66b150753909de6a4b70511eef83f to your computer and use it in GitHub Desktop.
Income Log Reporting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// NationBuilder functions | |
// It may eventually make sense to make these methods in OOP model | |
// Could turn into a NationBuilder PHP library or package | |
// Make a GET request using curl | |
// Takes one argument, $endpoint, which is the URL to hit with GET | |
// Expects a response in JSON format, decodes to array, returns the decoded JSON array | |
function nationbuilderGet($endpoint) { | |
// Make the curl request | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $endpoint, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"cache-control: no-cache" | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
$response = json_decode($response, true); //because of true, it's in an array | |
return $response; | |
} | |
// - Constructs an endpoint using the $next endpoint from a NationBuilder response JSON array | |
// - Takes three arguments: | |
// 1. $slug: nation slug | |
// 2. $token: API token | |
// 3. $next: next string from NationBuilder JSON response array | |
function nationbuilderNextEndpoint($slug, $token, $next) { | |
$endpoint = "https://". $slug . ".nationbuilder.com/" . $next . "&access_token=" . $token; | |
return $endpoint; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// PHP script to create SSDP's Sensible Society Monthly Report | |
//// Betty's manual process, described in comments | |
// | |
// | |
// In nationBuilder, run the OK to list donor filter. | |
//// https://ssdp.nationbuilder.com/admin/signups?token=5c153abc06f22e5977f55faedc1602f0 | |
// Determine if filtered gifts are anon or should have something listed. | |
//// Update this information in Ok to list donor custom field: | |
////// First Last (with "xx for alums), Anon?XXXXX for those which are unclear, AnonXXXXXX for those who don't want to be listed (where XXXXX is the NationBuilder ID) | |
// Export financial transactions in the last 365 days | |
//// https://ssdp.nationbuilder.com/admin/financial_transactions?token=82a97bbe976acc558070bfceb9a01156 | |
// Paste appropriate cells to the tab "export data 365" in the income log | |
// Delete all "Jean L Sullivan Donations" | |
// Fix BPG/Sean Luse '00 gifts. ($250+ are BPG) | |
// Adjust pivot tables to include all rows in "export data 365" | |
// Skim "sorted list" in "current society" for anomalies | |
//// Fix in "export data 365" & NationBuilder | |
// Copy "Sorted List" to "SSDP Supporters" in fundraising tools folder | |
// Copy "YTD" list to income log, fundraising totals sheet | |
// | |
// | |
//// End manual process description | |
// Pull in configuration | |
require_once 'config.php'; | |
// Pull in functions.php | |
require_once 'functions.php'; | |
// NationBuilder configuration | |
// NATION_SLUG is the nation slug required for NationBuilder endpoints | |
// NB_ACCESS_TOKEN is the API token required for NationBuilder endpoints | |
// Get the list of all donations made in the last 365 days | |
// $last_year is 365 days ago, should be a string for concatenation in the $endpoint variable | |
// Returns in format: 2018-07-10T07:43:37+02:00 | |
$last_year = date(DATE_ATOM, mktime(0, 0, 0, date("m"), date("d"), date("Y")-1)); | |
// remove everything after the T for formatting in the API endpoint | |
$last_year = explode("T", $last_year)[0]; | |
// Here's the specific endpoint to use, in $endpoint | |
// We're only ever going to use one endpoint variable to keep us from making any mistakes or bad requests | |
$endpoint = "https://". $NATION_SLUG . ".nationbuilder.com/api/v1/donations/search?succeeded_since=". $last_year . "&access_token=" . $NB_ACCESS_TOKEN; | |
// Create an empty array to store all results | |
$search_results = array(); | |
// Use the GET function from functions.php | |
$response = nationbuilderGet($endpoint); | |
// Merge the first GET response with the empty array | |
$search_results = array_merge($search_results, $response['results']); | |
// Check $response['results'] and $search_results are identical | |
$spot_check = array_diff($response['results'], $search_results); | |
// var_dump of an OK to list donor field for a specific person in results | |
// var_dump($response['results'][0]['ok_to_list_donor']); | |
// While there is a 'next' token in the result array, keep on building the array | |
while (!empty($response['next'])) { | |
// Build the correct endpoint | |
$endpoint = nationbuilderNextEndpoint($NATION_SLUG, $NB_ACCESS_TOKEN, $response['next']); | |
// GET the endpoint | |
$response = nationbuilderGet($endpoint); | |
// Merge the results with $search_results | |
// Sometimes it doesn't seem like $response['results'] is an array, so don't try to merge if you can't | |
// Although if the results are just one person, it may not be an array, and we may be missing a value | |
// TODO: Validate this better | |
if (is_array($response['results'])) { | |
$search_results = array_merge($search_results, $response['results']); | |
} | |
} | |
// Loop through results | |
foreach ($search_results as $fields) { | |
// If they don't have an "ok to list donor" value, check if we have permission | |
if (empty($fields["donor"]["ok_to_list_donor"])) { | |
// If we don't have permission, update as anon | |
if ((bool)$fields["permisson"] === (bool)fase) { | |
var_dump($fields["permission"]); | |
echo "This donation is: " . $fields["donor"]["id"]; | |
echo "\n"; | |
echo "Update with anon here"; | |
echo "\n"; | |
// AnonXXXXXX for those who don't want to be listed (where XXXXX is the NationBuilder ID) | |
} | |
// If we do have permission to list them as donor, update the field with the correct value | |
elseif ((bool)$fields["permission"] === (bool)true) { | |
var_dump($fields["permission"]); | |
echo "This donation is: " . $fields["donor"]["id"]; | |
echo "\n"; | |
echo "Update with proper name here"; | |
echo "\n"; | |
//First Last (with "xx for alums), | |
} | |
// The only possible case left | |
// If permission is null, it's unclear whether or not they want to be anonymous | |
else { | |
var_dump($fields["permission"]); | |
echo "This donation is: " . $fields["donor"]["id"]; | |
echo "\n"; | |
echo "Update with anon with a question mark here"; | |
echo "\n"; | |
//Anon?XXXXX for those which are unclear,\ | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment