3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Mailing_Bounce
extends CRM_Report_Form
{
19 protected $_summary = NULL;
21 protected $_emailField = FALSE;
23 protected $_phoneField = FALSE;
25 protected $_customGroupExtends = [
33 * This report has not been optimised for group filtering.
35 * The functionality for group filtering has been improved but not
36 * all reports have been adjusted to take care of it. This report has not
37 * and will run an inefficient query until fixed.
40 * @see https://issues.civicrm.org/jira/browse/CRM-19170
42 protected $groupFilterNotOptimised = TRUE;
47 public function __construct() {
50 $this->_columns
['civicrm_contact'] = [
51 'dao' => 'CRM_Contact_DAO_Contact',
54 'title' => ts('Contact ID'),
58 'title' => ts('Contact Name'),
64 'title' => ts('Contact Name'),
67 'title' => ts('Contact Source'),
68 'type' => CRM_Utils_Type
::T_STRING
,
71 'title' => ts('Contact ID'),
77 'title' => ts('Contact Name'),
79 'default_order' => 'ASC',
82 'grouping' => 'contact-fields',
85 $this->_columns
['civicrm_mailing'] = [
86 'dao' => 'CRM_Mailing_DAO_Mailing',
90 'title' => ts('Mailing Name'),
93 'mailing_name_alias' => [
98 'mailing_subject' => [
100 'title' => ts('Mailing Subject'),
107 'title' => ts('Mailing Name'),
108 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
109 'type' => CRM_Utils_Type
::T_INT
,
110 'options' => CRM_Mailing_BAO_Mailing
::getMailingsList(),
111 'operator' => 'like',
113 'mailing_subject' => [
115 'title' => ts('Mailing Subject'),
116 'type' => CRM_Utils_Type
::T_STRING
,
117 'operator' => 'like',
123 'title' => ts('Mailing Name'),
125 'mailing_subject' => [
127 'title' => ts('Mailing Subject'),
130 'grouping' => 'mailing-fields',
133 $this->_columns
['civicrm_mailing_event_bounce'] = [
134 'dao' => 'CRM_Mailing_DAO_Mailing',
137 'title' => ts('Bounce Reason'),
140 'title' => ts('Bounce Date'),
145 'title' => ts('Bounce Reason'),
146 'type' => CRM_Utils_Type
::T_STRING
,
149 'title' => ts('Bounce Date'),
150 'operatorType' => CRM_Report_Form
::OP_DATE
,
151 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
156 'title' => ts('Bounce Reason'),
159 'title' => ts('Bounce Date'),
162 'grouping' => 'mailing-fields',
165 $this->_columns
['civicrm_mailing_bounce_type'] = [
166 'dao' => 'CRM_Mailing_DAO_BounceType',
170 'title' => ts('Bounce Type'),
174 'bounce_type_name' => [
176 'title' => ts('Bounce Type'),
177 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
178 'type' => CRM_Utils_Type
::T_STRING
,
179 'options' => self
::bounce_type(),
180 'operator' => 'like',
186 'title' => ts('Bounce Type'),
189 'grouping' => 'mailing-fields',
192 $this->_columns
['civicrm_email'] = [
193 'dao' => 'CRM_Core_DAO_Email',
196 'title' => ts('Email'),
200 'title' => ts('On hold'),
203 'title' => ts('Hold date'),
206 'title' => ts('Hold reset date'),
211 'title' => ts('Email'),
214 'title' => ts('On hold'),
217 'title' => ts('Hold date'),
218 'operatorType' => CRM_Report_Form
::OP_DATE
,
219 'type' => CRM_Utils_Type
::T_DATE
,
222 'title' => ts('Hold reset date'),
223 'operatorType' => CRM_Report_Form
::OP_DATE
,
224 'type' => CRM_Utils_Type
::T_DATE
,
228 'email' => ['title' => ts('Email'), 'default_order' => 'ASC'],
230 'grouping' => 'contact-fields',
233 $this->_columns
['civicrm_phone'] = [
234 'dao' => 'CRM_Core_DAO_Phone',
235 'fields' => ['phone' => NULL],
236 'grouping' => 'contact-fields',
239 // Add charts support
242 'barChart' => ts('Bar Chart'),
243 'pieChart' => ts('Pie Chart'),
246 $this->_groupFilter
= TRUE;
247 $this->_tagFilter
= TRUE;
248 parent
::__construct();
251 public function preProcess() {
252 $this->assign('chartSupported', TRUE);
253 parent
::preProcess();
256 public function select() {
258 $this->_columnHeaders
= [];
260 foreach ($this->_columns
as $tableName => $table) {
261 if (array_key_exists('fields', $table)) {
262 foreach ($table['fields'] as $fieldName => $field) {
263 if (!empty($field['required']) ||
264 !empty($this->_params
['fields'][$fieldName])
266 if ($tableName == 'civicrm_email') {
267 $this->_emailField
= TRUE;
269 elseif ($tableName == 'civicrm_phone') {
270 $this->_phoneField
= TRUE;
273 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
274 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
275 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = $field['no_display'] ??
NULL;
276 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
282 if (!empty($this->_params
['charts'])) {
283 $select[] = "COUNT({$this->_aliases['civicrm_mailing_event_bounce']}.id) as civicrm_mailing_bounce_count";
284 $this->_columnHeaders
["civicrm_mailing_bounce_count"]['title'] = ts('Bounce Count');
287 $this->_selectClauses
= $select;
288 $this->_select
= "SELECT " . implode(', ', $select) . " ";
298 public static function formRule($fields, $files, $self) {
299 $errors = $grouping = [];
303 public function from() {
305 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
306 // LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
307 // ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
308 // {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
311 INNER JOIN civicrm_mailing_event_queue
312 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
313 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
314 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
315 INNER JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
316 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id
317 LEFT JOIN civicrm_mailing_bounce_type {$this->_aliases['civicrm_mailing_bounce_type']}
318 ON {$this->_aliases['civicrm_mailing_event_bounce']}.bounce_type_id = {$this->_aliases['civicrm_mailing_bounce_type']}.id
319 INNER JOIN civicrm_mailing_job
320 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
321 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
322 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
324 $this->joinPhoneFromContact();
327 public function where() {
331 // Exclude SMS mailing type
332 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
334 // Build date filter clauses
335 foreach ($this->_columns
as $tableName => $table) {
336 if (array_key_exists('filters', $table)) {
337 foreach ($table['filters'] as $fieldName => $field) {
339 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
340 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
341 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
342 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
344 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
347 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
350 $clause = $this->whereClause($field,
352 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
353 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
354 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
360 if (!empty($clause)) {
361 $clauses[] = $clause;
367 $this->_where
= "WHERE " . implode(' AND ', $clauses);
370 public function groupBy() {
371 if (!empty($this->_params
['charts'])) {
372 $groupBy = "{$this->_aliases['civicrm_mailing']}.id";
375 $groupBy = "{$this->_aliases['civicrm_mailing_event_bounce']}.id";
377 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
380 public function postProcess() {
381 $this->beginPostProcess();
383 // get the acl clauses built before we assemble the query
384 $this->buildACLClause($this->_aliases
['civicrm_contact']);
386 $sql = $this->buildQuery(TRUE);
388 $rows = $graphRows = [];
389 $this->buildRows($sql, $rows);
391 $this->formatDisplay($rows);
392 $this->doTemplateAssignment($rows);
393 $this->endPostProcess($rows);
399 public function buildChart(&$rows) {
405 'legend' => ts('Mail Bounce Report'),
406 'xname' => ts('Mailing'),
407 'yname' => ts('Bounce'),
409 'tip' => ts('Mail Bounce: %1', [1 => '#val#']),
411 foreach ($rows as $row) {
412 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_bounce_count'];
416 CRM_Utils_Chart
::buildChart($chartInfo, $this->_params
['charts']);
417 $this->assign('chartType', $this->_params
['charts']);
423 public function bounce_type() {
427 $bounce_type = new CRM_Mailing_DAO_BounceType();
428 $query = "SELECT name FROM civicrm_mailing_bounce_type";
429 $bounce_type->query($query);
431 while ($bounce_type->fetch()) {
432 $data[$bounce_type->name
] = $bounce_type->name
;
439 * Alter display of rows.
441 * Iterate through the rows retrieved via SQL and make changes for display purposes,
442 * such as rendering contacts as links.
445 * Rows generated by SQL, with an array for each row.
447 public function alterDisplay(&$rows) {
449 $config = CRM_Core_Config
::Singleton();
452 foreach ($rows as $rowNum => $row) {
454 // If the email address has been deleted
455 if (array_key_exists('civicrm_email_email', $row)) {
456 if (empty($rows[$rowNum]['civicrm_email_email'])) {
457 $rows[$rowNum]['civicrm_email_email'] = '<del>' . ts('Email address deleted.') . '</del>';
462 // make count columns point to detail report
463 // convert display name to links
464 if (array_key_exists('civicrm_contact_sort_name', $row) &&
465 array_key_exists('civicrm_contact_id', $row)
467 $url = CRM_Utils_System
::url('civicrm/contact/view',
468 'reset=1&cid=' . $row['civicrm_contact_id'],
471 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
472 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
476 // Handle on_hold boolean display
477 if (array_key_exists('civicrm_email_on_hold', $row)) {
478 $rows[$rowNum]['civicrm_email_on_hold'] = (!empty($row['civicrm_email_on_hold'])) ?
'Yes' : 'No';
482 // Convert datetime values to custom date and time format
484 'civicrm_mailing_event_bounce_time_stamp',
485 'civicrm_email_hold_date',
486 'civicrm_email_reset_date',
489 foreach ($dateFields as $dateField) {
490 if (array_key_exists($dateField, $row)) {
491 if (!empty($rows[$rowNum][$dateField])) {
492 $rows[$rowNum][$dateField] = CRM_Utils_Date
::customFormat($row[$dateField], $config->dateformatDatetime
);
498 // skip looking further in rows, if first row itself doesn't
499 // have the column we need