Last week, I showed you how to create a Queue overdue list of persons report.
This week, we’re going to work with the same tutorial, yet extend upon the base functionality you developed thus far.
As with any tutorial we extend upon, I’m not going to cover the nitty-gritty details of the previous tutorial. You’ll have a bit of reading and review for areas you find to be challenging.
That said, today’s tutorial focuses on having a Queue overdue persons report emailed to you using Church Community Builder’s API service.
You would right to think that you can already do such reporting by using the existing web front-end interface as provided by CCB.
But what if you wanted to access the data programmatically using a 3rd-party system?
Well, this is where last week’s and today’s tutorials come quite in handy. Let’s get started.
Start with last week’s tutorial codebase
Copy and paste last week’s tutorial codebase into the text editor of your choice, and name and save the queue-overdue-persons-email.php.
Just like previous tutorials, the necessary comments and file inclusions should already exist.
There is one thing you may want to change: functions. Be sure to include the functions in the general.php file for future usage. This should shrink your file size quite a bit.
<?PHP /** * overdue persons in a queue */ // contains resusable globals and functions include("includes/config.php"); $queueID = '107'; $response_object = getQueueIndividuals($queueID); // sort asort($response_object); $response_catch = array(); foreach($response_object as $value){ $response_catch[] = getIndividualQueueStatus($value,$queueID); } usort($response_catch, 'sortByDays'); ?>
Time for some slight code modifications
If you remember from the previous tutorial, the code was executed and a HTML table was displayed via the browser with a list of overdue persons for their respective assigned Queue.
To ensure that the same table is emailed, we’ll need to make some slight changes to the code.
Instead of using inline echo of the HTML table, change echo to be a variable. In this case, I’ve declared the variable to be txtOverDue, replacing each echo.
In addition, notice that I’ve added an additional line of code for capturing the date and time when the report was executed using the built-in PHP date method.
This line appears just below the line of code that includes the Total number of Individuals.
<?PHP // table for displaying data, including styles and html $txtOverDue = ''; $txtOverDue .= '<style> .header{ background: #000; color: #fff;} </style>'; $txtOverDue .= '<table cellpadding="8" cellspacing="0" border="1" width="50%" align="center">'; $txtOverDue .= '<tr><td colspan="3">Total number of Individuals: '.sizeof($response_object).'</td></tr>'; $txtOverDue .= '<tr><td colspan="3">Report Last Run: '.date("m/d/Y H:i:s a").'</td></tr>'; $txtOverDue .= '<tr><td class="header">Name</td><td class="header">Days Overdue</td><td class="header">Process Queue</td></tr>'; $i = 1; foreach($response_catch as $value){ $txtOverDue .= '<tr><td>'.$i.') '.$value[1].'</td><td>'.$value[2].'</td><td>'.$value[3].'</td></tr>'; $i++; } $txtOverDue .= '</table>'; ?>
Preparing and sending the report via email
It’s been a while since I covered email, but you guessed it, there’s a tutorial for it too. The exact tutorial is creating a CBB birthday emailer.
Once you complete the code modifications above, prepare the following variables with their respective values:
- to: the email address or email addresses separated by commas (e.g., “test@gmail.com,test@email.com”)
- from: a from email address
- from_name: name of person or organization the email should appear from
- subject: subject of email
- message: this value should be the txtOverDue variable
Do not make any modifications to the header variable.
Next, declare the moreresults variable and set its value to use the built-in PHP mail method, passing it the following argument variables in their respective order: to, subject, message and headers.
The final step is to determine whether or not the email was successfully sent. For this, we use a simple if statement to check the value returned by the moreresults variable.
Simply put, if the variable returned true, then we echo email was sent successfully. If not, then we echo the email send failed.
And finally, you can echo the message or txtOverDue variable. I put this in place if you choose to execute the code via a web browser and want to see the results (just like last tutorial).
But don’t stop at just emailing the report. Automate this report using a cron job.
That’s it for this tutorial
Now, let’s put everything together, save the file and test the code via a web browser.
Let me know if you have any questions. If not, then I look to see you next week when we’ll take this report one step further.
You’ll have to come back next week to find out the next tutorial’s what, how and why.
<?PHP /** * overdue persons in a queue */ // contains resusable globals and functions include("includes/config.php"); $queueID = '107'; $response_object = getQueueIndividuals($queueID); // sort asort($response_object); $response_catch = array(); foreach($response_object as $value){ $response_catch[] = getIndividualQueueStatus($value,$queueID); } usort($response_catch, 'sortByDays'); // table for displaying data, including styles and html $txtOverDue = ''; $txtOverDue .= '<style> .header{ background: #000; color: #fff;} </style>'; $txtOverDue .= '<table cellpadding="8" cellspacing="0" border="1" width="50%" align="center">'; $txtOverDue .= '<tr><td colspan="3">Total number of Individuals: '.sizeof($response_object).'</td></tr>'; $txtOverDue .= '<tr><td colspan="3">Report Last Run: '.date("m/d/Y H:i:s a").'</td></tr>'; $txtOverDue .= '<tr><td class="header">Name</td><td class="header">Days Overdue</td><td class="header">Process Queue</td></tr>'; $i = 1; foreach($response_catch as $value){ $txtOverDue .= '<tr><td>'.$i.') '.$value[1].'</td><td>'.$value[2].'</td><td>'.$value[3].'</td></tr>'; $i++; } $txtOverDue .= '</table>'; // prepare the email variables $to = 'test@ccbtutorials.com'; $from = 'no-reply@ccbtutorials.com'; $from_name = 'CCB Tutorials'; $subject = "Overdue Persons Report"; $message = $txtOverDue; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Content-Transfer-Encoding: 7bit\r\n"; $headers .= "From: $from_name <" . $from . ">\r\n"; // prepare to send email $moreresults = mail($to, $subject, $message, $headers); if($moreresults){ echo $subject.' Email successfully sent'; } else { echo $subject.' Email failed to send.'; } echo '<p>'.$message.'</p>'; ?>