3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Report_Form_Mailing_Bounce
extends CRM_Report_Form
{
35 protected $_summary = NULL;
37 protected $_emailField = FALSE;
39 protected $_phoneField = FALSE;
41 protected $_customGroupExtends = [
48 protected $_charts = [
50 'barChart' => 'Bar Chart',
51 'pieChart' => 'Pie Chart',
55 * This report has not been optimised for group filtering.
57 * The functionality for group filtering has been improved but not
58 * all reports have been adjusted to take care of it. This report has not
59 * and will run an inefficient query until fixed.
65 protected $groupFilterNotOptimised = TRUE;
70 public function __construct() {
73 $this->_columns
['civicrm_contact'] = [
74 'dao' => 'CRM_Contact_DAO_Contact',
77 'title' => ts('Contact ID'),
81 'title' => ts('Contact Name'),
87 'title' => ts('Contact Name'),
90 'title' => ts('Contact Source'),
91 'type' => CRM_Utils_Type
::T_STRING
,
94 'title' => ts('Contact ID'),
100 'title' => ts('Contact Name'),
102 'default_order' => 'ASC',
105 'grouping' => 'contact-fields',
108 $this->_columns
['civicrm_mailing'] = [
109 'dao' => 'CRM_Mailing_DAO_Mailing',
113 'title' => ts('Mailing Name'),
116 'mailing_name_alias' => [
119 'no_display' => TRUE,
121 'mailing_subject' => [
123 'title' => ts('Mailing Subject'),
130 'title' => ts('Mailing Name'),
131 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
132 'type' => CRM_Utils_Type
::T_INT
,
133 'options' => CRM_Mailing_BAO_Mailing
::getMailingsList(),
134 'operator' => 'like',
136 'mailing_subject' => [
138 'title' => ts('Mailing Subject'),
139 'type' => CRM_Utils_Type
::T_STRING
,
140 'operator' => 'like',
146 'title' => ts('Mailing Name'),
148 'mailing_subject' => [
150 'title' => ts('Mailing Subject'),
153 'grouping' => 'mailing-fields',
156 $this->_columns
['civicrm_mailing_event_bounce'] = [
157 'dao' => 'CRM_Mailing_DAO_Mailing',
160 'title' => ts('Bounce Reason'),
163 'title' => ts('Bounce Date'),
168 'title' => ts('Bounce Reason'),
169 'type' => CRM_Utils_Type
::T_STRING
,
172 'title' => ts('Bounce Date'),
173 'operatorType' => CRM_Report_Form
::OP_DATE
,
174 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
179 'title' => ts('Bounce Reason'),
182 'title' => ts('Bounce Date'),
185 'grouping' => 'mailing-fields',
188 $this->_columns
['civicrm_mailing_bounce_type'] = [
189 'dao' => 'CRM_Mailing_DAO_BounceType',
193 'title' => ts('Bounce Type'),
197 'bounce_type_name' => [
199 'title' => ts('Bounce Type'),
200 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
201 'type' => CRM_Utils_Type
::T_STRING
,
202 'options' => self
::bounce_type(),
203 'operator' => 'like',
209 'title' => ts('Bounce Type'),
212 'grouping' => 'mailing-fields',
215 $this->_columns
['civicrm_email'] = [
216 'dao' => 'CRM_Core_DAO_Email',
219 'title' => ts('Email'),
223 'title' => ts('On hold'),
226 'title' => ts('Hold date'),
229 'title' => ts('Hold reset date'),
234 'title' => ts('Email'),
237 'title' => ts('On hold'),
240 'title' => ts('Hold date'),
241 'operatorType' => CRM_Report_Form
::OP_DATE
,
242 'type' => CRM_Utils_Type
::T_DATE
,
245 'title' => ts('Hold reset date'),
246 'operatorType' => CRM_Report_Form
::OP_DATE
,
247 'type' => CRM_Utils_Type
::T_DATE
,
251 'email' => ['title' => ts('Email'), 'default_order' => 'ASC'],
253 'grouping' => 'contact-fields',
256 $this->_columns
['civicrm_phone'] = [
257 'dao' => 'CRM_Core_DAO_Phone',
258 'fields' => ['phone' => NULL],
259 'grouping' => 'contact-fields',
262 $this->_groupFilter
= TRUE;
263 $this->_tagFilter
= TRUE;
264 parent
::__construct();
267 public function preProcess() {
268 $this->assign('chartSupported', TRUE);
269 parent
::preProcess();
272 public function select() {
274 $this->_columnHeaders
= [];
276 foreach ($this->_columns
as $tableName => $table) {
277 if (array_key_exists('fields', $table)) {
278 foreach ($table['fields'] as $fieldName => $field) {
279 if (!empty($field['required']) ||
280 !empty($this->_params
['fields'][$fieldName])
282 if ($tableName == 'civicrm_email') {
283 $this->_emailField
= TRUE;
285 elseif ($tableName == 'civicrm_phone') {
286 $this->_phoneField
= TRUE;
289 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
290 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
291 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
292 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
298 if (!empty($this->_params
['charts'])) {
299 $select[] = "COUNT({$this->_aliases['civicrm_mailing_event_bounce']}.id) as civicrm_mailing_bounce_count";
300 $this->_columnHeaders
["civicrm_mailing_bounce_count"]['title'] = ts('Bounce Count');
303 $this->_selectClauses
= $select;
304 $this->_select
= "SELECT " . implode(', ', $select) . " ";
314 public static function formRule($fields, $files, $self) {
315 $errors = $grouping = [];
319 public function from() {
321 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
322 // LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
323 // ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
324 // {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
327 INNER JOIN civicrm_mailing_event_queue
328 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
329 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
330 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
331 INNER JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
332 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id
333 LEFT JOIN civicrm_mailing_bounce_type {$this->_aliases['civicrm_mailing_bounce_type']}
334 ON {$this->_aliases['civicrm_mailing_event_bounce']}.bounce_type_id = {$this->_aliases['civicrm_mailing_bounce_type']}.id
335 INNER JOIN civicrm_mailing_job
336 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
337 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
338 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
340 $this->joinPhoneFromContact();
343 public function where() {
347 // Exclude SMS mailing type
348 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
350 // Build date filter clauses
351 foreach ($this->_columns
as $tableName => $table) {
352 if (array_key_exists('filters', $table)) {
353 foreach ($table['filters'] as $fieldName => $field) {
355 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
356 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
357 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
358 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
360 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
363 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
366 $clause = $this->whereClause($field,
368 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
369 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
370 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
376 if (!empty($clause)) {
377 $clauses[] = $clause;
383 $this->_where
= "WHERE " . implode(' AND ', $clauses);
386 public function groupBy() {
387 if (!empty($this->_params
['charts'])) {
388 $groupBy = "{$this->_aliases['civicrm_mailing']}.id";
391 $groupBy = "{$this->_aliases['civicrm_mailing_event_bounce']}.id";
393 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
396 public function postProcess() {
397 $this->beginPostProcess();
399 // get the acl clauses built before we assemble the query
400 $this->buildACLClause($this->_aliases
['civicrm_contact']);
402 $sql = $this->buildQuery(TRUE);
404 $rows = $graphRows = [];
405 $this->buildRows($sql, $rows);
407 $this->formatDisplay($rows);
408 $this->doTemplateAssignment($rows);
409 $this->endPostProcess($rows);
415 public function buildChart(&$rows) {
421 'legend' => ts('Mail Bounce Report'),
422 'xname' => ts('Mailing'),
423 'yname' => ts('Bounce'),
425 'tip' => ts('Mail Bounce: %1', [1 => '#val#']),
427 foreach ($rows as $row) {
428 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_bounce_count'];
432 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
433 $this->assign('chartType', $this->_params
['charts']);
439 public function bounce_type() {
443 $bounce_type = new CRM_Mailing_DAO_BounceType();
444 $query = "SELECT name FROM civicrm_mailing_bounce_type";
445 $bounce_type->query($query);
447 while ($bounce_type->fetch()) {
448 $data[$bounce_type->name
] = $bounce_type->name
;
455 * Alter display of rows.
457 * Iterate through the rows retrieved via SQL and make changes for display purposes,
458 * such as rendering contacts as links.
461 * Rows generated by SQL, with an array for each row.
463 public function alterDisplay(&$rows) {
465 $config = CRM_Core_Config
::Singleton();
468 foreach ($rows as $rowNum => $row) {
470 // If the email address has been deleted
471 if (array_key_exists('civicrm_email_email', $row)) {
472 if (empty($rows[$rowNum]['civicrm_email_email'])) {
473 $rows[$rowNum]['civicrm_email_email'] = '<del>' . ts('Email address deleted.') . '</del>';
478 // make count columns point to detail report
479 // convert display name to links
480 if (array_key_exists('civicrm_contact_sort_name', $row) &&
481 array_key_exists('civicrm_contact_id', $row)
483 $url = CRM_Utils_System
::url('civicrm/contact/view',
484 'reset=1&cid=' . $row['civicrm_contact_id'],
487 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
488 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
492 // Handle on_hold boolean display
493 if (array_key_exists('civicrm_email_on_hold', $row)) {
494 $rows[$rowNum]['civicrm_email_on_hold'] = (!empty($row['civicrm_email_on_hold'])) ?
'Yes' : 'No';
498 // Convert datetime values to custom date and time format
500 'civicrm_mailing_event_bounce_time_stamp',
501 'civicrm_email_hold_date',
502 'civicrm_email_reset_date',
505 foreach ($dateFields as $dateField) {
506 if (array_key_exists($dateField, $row)) {
507 if (!empty($rows[$rowNum][$dateField])) {
508 $rows[$rowNum][$dateField] = CRM_Utils_Date
::customFormat($row[$dateField], $config->dateformatDatetime
);
514 // skip looking further in rows, if first row itself doesn't
515 // have the column we need