/**
* Recent Campaigns - This function is used for AJAX requests to obtain information on
* recent campaigns, or a set of campaigns filtered by date and/or list.
*
* @param
* start_date (optional) - The start date of the campaign information.
* Defaults to 7 days before end date, or 7 days before current date if end date not set.
* end_date (optional) - The end date of the campaign information.
* Defaults to 7 days after the start date, or the current date if start date not set.
* list (optional) - The list used to filter campaigns by. Defaults to all lists.
* stat_1 - The first statistic queried for the set of campaigns (e.g. open %, click %, bounce % ).
* stat_2 - The second statistic queried for the set of campaigns (e.g. open %, click %, bounce % ).
* page - the page to request information for.
* @return
* Returns a table of the information requested in html form.
*/
public function reports_recent_campaigns()
{
$sailthruClient = new Sailthru_Client(API_KEY, API_SECRET);
// If both start and end date are not set, set to default values.
if ($this->params['pass'][0] == 'null' && $this->params['pass'][1] == 'null') {
$end_date = date("m/d/y");
$temp = strtotime($end_date) - DEFAULT_DAYS * 86400;
// 86400 = seconds in a day
$start_date = date("m/d/y", $temp);
// If start is not set, default it to 7 days prior to end date.
} else {
if ($this->params['pass'][0] == 'null') {
$temp = strtotime($this->params['pass'][1]);
$end_date = date("m/d/y", $temp);
$temp = strtotime($end_date) - DEFAULT_DAYS * 86400;
$start_date = date("m/d/y", $temp);
// If end date is not set, default it to 7 days after the start date.
} else {
if ($this->params['pass'][1] == 'null') {
$temp = strtotime($this->params['pass'][0]);
$start_date = date("m/d/y", $temp);
$temp = strtotime($start_date) + DEFAULT_DAYS * 86400;
$end_date = date("m/d/y", $temp);
// Else, both days are set. Format them for the API call.
} else {
$temp = strtotime($this->params['pass'][0]);
$start_date = date("m/d/y", $temp);
$temp = strtotime($this->params['pass'][1]);
$end_date = date("m/d/y", $temp);
}
}
}
//str_replace("-","/",$end_date);
//str_replace("-","/",$start_date);
// Check if parameters for the first statistics is set. Default to "Clicks".
if ($this->params['pass'][3] == 'null') {
$stat_1 = 'click';
} else {
$stat_1 = $this->params['pass'][3];
}
// Check if parameters for the second statistics is set. Default to "Est. Opens".
if ($this->params['pass'][4] == 'null') {
$stat_2 = 'estopens';
} else {
$stat_2 = $this->params['pass'][4];
}
// If the set start date is not before the end date chronologically, return with an error message.
if (strtotime($start_date) > strtotime($end_date)) {
echo 'Invalid date range.';
$this->autoLayout = $this->autoRender = false;
return;
}
//Prepare array of options to be passed into the API call.
$options['start_date'] = $start_date;
$options['end_date'] = $end_date;
$options['status'] = 'sent';
// If list is specified, add it to the options array.
if ($this->params['pass'][2] != 'null') {
$options['list'] = $this->params['pass'][2];
}
try {
//Retrieve the blast id via API call.
$response = $sailthruClient->getBlasts($options);
$results = $response['blasts'];
// Calculate the number pages by dividing the total blasts by the results per page constant.
$total_count = count($results);
$pages = ceil($total_count / RESULTS_PER_PAGE);
// Get the page requested from the URL of this request.
if ($this->params['pass'][5] == 'null') {
$page = 1;
} else {
$page = $this->params['pass'][5];
}
$start = ($page - 1) * RESULTS_PER_PAGE;
$end = $page * RESULTS_PER_PAGE;
if (!isset($response['error'])) {
// Prepare option array for secondary API call.
$data['beacon_times'] = 1;
$data['click_times'] = 1;
$data['engagement'] = 1;
$toReturn = array();
// For each of the blasts on the current page
for ($i = $start; $i < $end; $i++) {
if (array_key_exists($i, $results)) {
$toReturn[$i]['name'] = $results[$i]['name'];
$toReturn[$i]['blast_id'] = $results[$i]['blast_id'];
$toReturn[$i]['list'] = $results[$i]['list'];
//Make the secondary API call to retrieve the stats specified by the $stats_1 and $stats_2 parameters
$blast_stats = $sailthruClient->stats_blast($results[$i]['blast_id'], null, null, $data);
$toReturn[$i]['stat_1'] = isset($blast_stats[$stat_1]) ? $blast_stats[$stat_1] : 0;
$toReturn[$i]['stat_2'] = isset($blast_stats[$stat_2]) ? $blast_stats[$stat_2] : 0;
}
}
if (count($toReturn) == 0) {
echo "Nothing to Display.";
}
//set variables and render view.
$test = $start_date;
$test2 = $end_date;
$this->set('test', $test);
$this->set('test2', $test2);
$this->set('page', $page);
$this->set('pages', $pages);
$this->set('results', array_reverse($toReturn));
$this->layout = 'campaign_table';
//this layout simply echos the content of the View.
$this->render('reports_recent_campaigns');
} else {
echo 'error';
}
} catch (Sailthru_Client_Exception $e) {
echo $e->getMessage();
}
}