mandrill_simplenews_report.mandrill.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Class file that extends mandrill api class.
  5. */
  6. class MandrillSimplenewsReport extends Mandrill {
  7. /**
  8. * Request mandrill to prepare report and import the same to local table
  9. */
  10. public function activity_report_import($date_from = NULL, $date_to = NULL) {
  11. // Part #1: Initiate request to export most recent report
  12. // (part #2: To save the same in local table
  13. // when request state changes from "waiting" to "completed").
  14. // By default request for last 24 hours report.
  15. if (!isset($data_from)) {
  16. $date_from = REQUEST_TIME - 86400;
  17. }
  18. if (!isset($date_to)) {
  19. $date_to = REQUEST_TIME;
  20. }
  21. // Request report per newsletter category, as the report returned is not
  22. // very structured. This helps us to see the total sent/open count by
  23. // newsletter for the instance, when same email address is subscribed
  24. // to more than one newsletter.
  25. $tids = db_query('SELECT tid, from_address FROM simplenews_category');
  26. foreach ($tids as $tid) {
  27. $_params = array(
  28. 'date_from' => format_date($date_from, 'custom', 'Y-m-d H:i:s'),
  29. 'date_to' => format_date($date_to, 'custom', 'Y-m-d H:i:s'),
  30. 'senders' => array($tid->from_address),
  31. 'states' => array('sent'),
  32. 'tags' => array('simplenews_node'),
  33. );
  34. // Initiate report export request
  35. $new_request = $this->request('exports/activity.json', $_params);
  36. if (is_array($new_request) && empty($new_request['id'])) {
  37. $message = 'Unexpected response from Mandrill "exports/activity.json" api call';
  38. $message .= '<br/><pre>' . print_r($new_request, TRUE) . '</pre>';
  39. watchdog('mandrill_simplenews_report', $message, array(), WATCHDOG_ERROR);
  40. continue;
  41. }
  42. // Save the request id to queue table until it changes from
  43. // "waiting" to "completed" state.
  44. db_insert('mandrill_simplenews_report_newsletter_report')
  45. ->fields(array(
  46. 'report_id' => $new_request['id'],
  47. 'tid' => $tid->tid,
  48. 'created' => strtotime($new_request['created_at']),
  49. ))
  50. ->execute();
  51. }
  52. // End of part #1.
  53. // Part #2: Check if activity.csv is ready for already requested reports.
  54. // Fetch the requets id from local table, download the report, if ready
  55. // import the same data to {mandrill_simplenews_report_newsletter_sent} table.
  56. $reports = db_query_range('SELECT report_id, tid
  57. FROM {mandrill_simplenews_report_newsletter_report}
  58. WHERE created < :one_hour
  59. ORDER BY created ASC', 0, 10, array(':one_hour' => REQUEST_TIME - 3600));
  60. $import_count = 0;
  61. foreach ($reports as $report) {
  62. if ($import_count >= 4) {
  63. // To prevent overloadding the server with too many imports.
  64. watchdog('mandrill_simplenews_report', 'Reports import job completed Successfully.');
  65. return;
  66. }
  67. $info = $this->request('exports/info.json', array('id' => $report->report_id));
  68. if (isset($info['state']) && $info['state'] != 'complete') {
  69. // Report is not ready yet.
  70. continue;
  71. }
  72. if ($info['state'] == 'complete' && !valid_url($info['result_url'])) {
  73. $message = 'Unexpected response from Mandrill "exports/info.json" api call';
  74. $message .= '<br/><pre>' . print_r($info, TRUE) . '</pre>';
  75. watchdog('mandrill_simplenews_report', $message, array(), WATCHDOG_ERROR);
  76. continue;
  77. }
  78. // Converts Remote URL to Drupal stream wrapper path
  79. $file_srp = system_retrieve_file($info['result_url']);
  80. // Get Drupal stream wrapper path to local file path
  81. $local_file = drupal_realpath($file_srp);
  82. $za = new ZipArchive();
  83. $res = $za->open($local_file);
  84. if ($res != TRUE) {
  85. $message = 'ZipArchive is unable to open file (local path) !local_file
  86. (file stream url !file_srp, mandrill export url !result_url)';
  87. $watchdog_variables = array(
  88. '!local_file' => $local_file,
  89. '!file_srp' => $file_srp,
  90. '!result_url' => $info['result_url'],
  91. );
  92. watchdog('mandrill_simplenews_report', $message, $watchdog_variables, WATCHDOG_ERROR);
  93. continue;
  94. }
  95. $extract_to = str_replace('.zip', '', $local_file);
  96. $za->extractTo($extract_to);
  97. $za->close();
  98. $csv_file = $extract_to . "/activity.csv";
  99. if (($handle = fopen($csv_file, "r")) == FALSE) {
  100. $message = 'Unable to open activity.csv file (local path) !local_file
  101. (file stream url !file_srp, mandrill export url !result_url, extracted to
  102. folder !extracted_to, csv file full path !csv_file)';
  103. $watchdog_variables = array(
  104. '!local_file' => $local_file,
  105. '!file_srp' => $file_srp,
  106. '!result_url' => $info['result_url'],
  107. '!extracted_to' => $extract_to,
  108. '!csv_file' => $csv_file,
  109. );
  110. watchdog('mandrill_simplenews_report', $message, $watchdog_variables, WATCHDOG_ERROR);
  111. continue;
  112. }
  113. $line = 0;
  114. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  115. if ($line != 0) {
  116. db_merge('mandrill_simplenews_report_newsletter_sent')
  117. ->key(array(
  118. 'sent_timestamp' => strtotime($data[0]),
  119. 'sent_to_mail' => $data[1],
  120. 'tid' => $report->tid,
  121. ))
  122. ->fields(array(
  123. 'sent_timestamp' => strtotime($data[0]),
  124. 'sent_to_mail' => $data[1],
  125. 'from_mail' => $data[2],
  126. 'subject' => $data[3],
  127. 'tid' => $report->tid,
  128. 'open_count' => $data[7],
  129. 'click_count' => $data[8],
  130. ))
  131. ->execute();
  132. }
  133. $line++;
  134. }
  135. fclose($handle);
  136. // Delete the report .zip file and extracted folder.
  137. file_unmanaged_delete_recursive($file_srp);
  138. file_unmanaged_delete_recursive($extract_to);
  139. db_delete('mandrill_simplenews_report_newsletter_report')
  140. ->condition('report_id', $report->report_id)
  141. ->execute();
  142. $import_count++;
  143. }
  144. }
  145. }