Merge pull request #22255 from MegaphoneJon/membership-14-2021
[civicrm-core.git] / CRM / Report / Form / Mailing / Bounce.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form {
18
19 protected $_summary = NULL;
20
21 protected $_emailField = FALSE;
22
23 protected $_phoneField = FALSE;
24
be2fb01f 25 protected $_customGroupExtends = [
9d72cede
EM
26 'Contact',
27 'Individual',
28 'Household',
21dfd5f5 29 'Organization',
be2fb01f 30 ];
6a488035 31
74cf4551 32 /**
1728e9a0 33 * This report has not been optimised for group filtering.
34 *
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.
38 *
1728e9a0 39 * @var bool
0e480632 40 * @see https://issues.civicrm.org/jira/browse/CRM-19170
1728e9a0 41 */
42 protected $groupFilterNotOptimised = TRUE;
43
44 /**
45 * Class constructor.
74cf4551 46 */
00be9182 47 public function __construct() {
be2fb01f 48 $this->_columns = [];
6a488035 49
be2fb01f 50 $this->_columns['civicrm_contact'] = [
6a488035 51 'dao' => 'CRM_Contact_DAO_Contact',
be2fb01f
CW
52 'fields' => [
53 'id' => [
6a488035
TO
54 'title' => ts('Contact ID'),
55 'required' => TRUE,
be2fb01f
CW
56 ],
57 'sort_name' => [
6a488035
TO
58 'title' => ts('Contact Name'),
59 'required' => TRUE,
be2fb01f
CW
60 ],
61 ],
62 'filters' => [
63 'sort_name' => [
6a488035 64 'title' => ts('Contact Name'),
be2fb01f
CW
65 ],
66 'source' => [
6a488035
TO
67 'title' => ts('Contact Source'),
68 'type' => CRM_Utils_Type::T_STRING,
be2fb01f
CW
69 ],
70 'id' => [
6a488035
TO
71 'title' => ts('Contact ID'),
72 'no_display' => TRUE,
be2fb01f
CW
73 ],
74 ],
75 'order_bys' => [
76 'sort_name' => [
9d72cede
EM
77 'title' => ts('Contact Name'),
78 'default' => TRUE,
21dfd5f5 79 'default_order' => 'ASC',
be2fb01f
CW
80 ],
81 ],
6a488035 82 'grouping' => 'contact-fields',
be2fb01f 83 ];
6a488035 84
be2fb01f 85 $this->_columns['civicrm_mailing'] = [
6a488035 86 'dao' => 'CRM_Mailing_DAO_Mailing',
be2fb01f
CW
87 'fields' => [
88 'mailing_name' => [
6a488035 89 'name' => 'name',
fa15bf0d 90 'title' => ts('Mailing Name'),
6a488035 91 'default' => TRUE,
be2fb01f
CW
92 ],
93 'mailing_name_alias' => [
6a488035
TO
94 'name' => 'name',
95 'required' => TRUE,
96 'no_display' => TRUE,
be2fb01f
CW
97 ],
98 'mailing_subject' => [
fa15bf0d
BS
99 'name' => 'subject',
100 'title' => ts('Mailing Subject'),
df28e44e 101 'default' => TRUE,
be2fb01f
CW
102 ],
103 ],
104 'filters' => [
105 'mailing_id' => [
6a488035 106 'name' => 'id',
fa15bf0d 107 'title' => ts('Mailing Name'),
6a488035
TO
108 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
109 'type' => CRM_Utils_Type::T_INT,
110 'options' => CRM_Mailing_BAO_Mailing::getMailingsList(),
111 'operator' => 'like',
be2fb01f
CW
112 ],
113 'mailing_subject' => [
fa15bf0d
BS
114 'name' => 'subject',
115 'title' => ts('Mailing Subject'),
df28e44e 116 'type' => CRM_Utils_Type::T_STRING,
fa15bf0d 117 'operator' => 'like',
be2fb01f
CW
118 ],
119 ],
120 'order_bys' => [
121 'mailing_name' => [
6a488035 122 'name' => 'name',
fa15bf0d 123 'title' => ts('Mailing Name'),
be2fb01f
CW
124 ],
125 'mailing_subject' => [
fa15bf0d
BS
126 'name' => 'subject',
127 'title' => ts('Mailing Subject'),
be2fb01f
CW
128 ],
129 ],
6a488035 130 'grouping' => 'mailing-fields',
be2fb01f 131 ];
6a488035 132
be2fb01f 133 $this->_columns['civicrm_mailing_event_bounce'] = [
6a488035 134 'dao' => 'CRM_Mailing_DAO_Mailing',
be2fb01f
CW
135 'fields' => [
136 'bounce_reason' => [
6a488035 137 'title' => ts('Bounce Reason'),
be2fb01f
CW
138 ],
139 'time_stamp' => [
a32357b9 140 'title' => ts('Bounce Date'),
be2fb01f
CW
141 ],
142 ],
143 'filters' => [
144 'bounce_reason' => [
a32357b9 145 'title' => ts('Bounce Reason'),
8ee006e7 146 'type' => CRM_Utils_Type::T_STRING,
be2fb01f
CW
147 ],
148 'time_stamp' => [
a32357b9
J
149 'title' => ts('Bounce Date'),
150 'operatorType' => CRM_Report_Form::OP_DATE,
a4ce6863 151 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
be2fb01f
CW
152 ],
153 ],
154 'order_bys' => [
155 'bounce_reason' => [
d3f6ce37 156 'title' => ts('Bounce Reason'),
be2fb01f
CW
157 ],
158 'time_stamp' => [
d3f6ce37 159 'title' => ts('Bounce Date'),
be2fb01f
CW
160 ],
161 ],
6a488035 162 'grouping' => 'mailing-fields',
be2fb01f 163 ];
6a488035 164
be2fb01f 165 $this->_columns['civicrm_mailing_bounce_type'] = [
6a488035 166 'dao' => 'CRM_Mailing_DAO_BounceType',
be2fb01f
CW
167 'fields' => [
168 'bounce_name' => [
6a488035
TO
169 'name' => 'name',
170 'title' => ts('Bounce Type'),
be2fb01f
CW
171 ],
172 ],
173 'filters' => [
174 'bounce_type_name' => [
6a488035
TO
175 'name' => 'name',
176 'title' => ts('Bounce Type'),
fa15bf0d 177 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
6a488035
TO
178 'type' => CRM_Utils_Type::T_STRING,
179 'options' => self::bounce_type(),
180 'operator' => 'like',
be2fb01f
CW
181 ],
182 ],
183 'order_bys' => [
184 'bounce_name' => [
6a488035
TO
185 'name' => 'name',
186 'title' => ts('Bounce Type'),
be2fb01f
CW
187 ],
188 ],
6a488035 189 'grouping' => 'mailing-fields',
be2fb01f 190 ];
6a488035 191
be2fb01f 192 $this->_columns['civicrm_email'] = [
6a488035 193 'dao' => 'CRM_Core_DAO_Email',
be2fb01f
CW
194 'fields' => [
195 'email' => [
6a488035
TO
196 'title' => ts('Email'),
197 'no_repeat' => TRUE,
be2fb01f
CW
198 ],
199 'on_hold' => [
a32357b9 200 'title' => ts('On hold'),
be2fb01f
CW
201 ],
202 'hold_date' => [
a32357b9 203 'title' => ts('Hold date'),
be2fb01f
CW
204 ],
205 'reset_date' => [
a32357b9 206 'title' => ts('Hold reset date'),
be2fb01f
CW
207 ],
208 ],
209 'filters' => [
210 'email' => [
9c91ca2e 211 'title' => ts('Email'),
be2fb01f
CW
212 ],
213 'on_hold' => [
a32357b9 214 'title' => ts('On hold'),
be2fb01f
CW
215 ],
216 'hold_date' => [
a32357b9
J
217 'title' => ts('Hold date'),
218 'operatorType' => CRM_Report_Form::OP_DATE,
219 'type' => CRM_Utils_Type::T_DATE,
be2fb01f
CW
220 ],
221 'reset_date' => [
a32357b9
J
222 'title' => ts('Hold reset date'),
223 'operatorType' => CRM_Report_Form::OP_DATE,
224 'type' => CRM_Utils_Type::T_DATE,
be2fb01f
CW
225 ],
226 ],
227 'order_bys' => [
228 'email' => ['title' => ts('Email'), 'default_order' => 'ASC'],
229 ],
6a488035 230 'grouping' => 'contact-fields',
be2fb01f 231 ];
6a488035 232
be2fb01f 233 $this->_columns['civicrm_phone'] = [
6a488035 234 'dao' => 'CRM_Core_DAO_Phone',
be2fb01f 235 'fields' => ['phone' => NULL],
6a488035 236 'grouping' => 'contact-fields',
be2fb01f 237 ];
6a488035 238
f787b3c4
BT
239 // Add charts support
240 $this->_charts = [
241 '' => ts('Tabular'),
242 'barChart' => ts('Bar Chart'),
243 'pieChart' => ts('Pie Chart'),
244 ];
245
16e2e80c 246 $this->_groupFilter = TRUE;
6a488035
TO
247 $this->_tagFilter = TRUE;
248 parent::__construct();
249 }
250
00be9182 251 public function preProcess() {
6a488035
TO
252 $this->assign('chartSupported', TRUE);
253 parent::preProcess();
254 }
255
00be9182 256 public function select() {
be2fb01f
CW
257 $select = [];
258 $this->_columnHeaders = [];
6a488035
TO
259
260 foreach ($this->_columns as $tableName => $table) {
261 if (array_key_exists('fields', $table)) {
262 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
263 if (!empty($field['required']) ||
264 !empty($this->_params['fields'][$fieldName])
265 ) {
6a488035
TO
266 if ($tableName == 'civicrm_email') {
267 $this->_emailField = TRUE;
268 }
269 elseif ($tableName == 'civicrm_phone') {
270 $this->_phoneField = TRUE;
271 }
272
273 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
9c1bc317
CW
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;
6a488035
TO
277 }
278 }
279 }
280 }
281
a7488080 282 if (!empty($this->_params['charts'])) {
6a488035
TO
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');
285 }
286
d1641c51 287 $this->_selectClauses = $select;
6a488035
TO
288 $this->_select = "SELECT " . implode(', ', $select) . " ";
289 }
290
74cf4551
EM
291 /**
292 * @param $fields
293 * @param $files
e8cf95b4 294 * @param self $self
74cf4551
EM
295 *
296 * @return array
297 */
00be9182 298 public static function formRule($fields, $files, $self) {
be2fb01f 299 $errors = $grouping = [];
6a488035
TO
300 return $errors;
301 }
302
00be9182 303 public function from() {
6a488035
TO
304 $this->_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 ) ";
309
310 $this->_from .= "
2f4c2f5d 311 INNER JOIN civicrm_mailing_event_queue
312 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
d2c742e9 313 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
2f4c2f5d 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
323 ";
2a5bea53 324 $this->joinPhoneFromContact();
6a488035
TO
325 }
326
00be9182 327 public function where() {
d3f6ce37 328
be2fb01f 329 $clauses = [];
d3f6ce37 330
a32357b9
J
331 // Exclude SMS mailing type
332 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
d3f6ce37 333
a32357b9
J
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) {
338 $clause = NULL;
339 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
9c1bc317
CW
340 $relative = $this->_params["{$fieldName}_relative"] ?? NULL;
341 $from = $this->_params["{$fieldName}_from"] ?? NULL;
342 $to = $this->_params["{$fieldName}_to"] ?? NULL;
a32357b9
J
343
344 $clause = $this->dateClause($this->_aliases[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
345 }
346 else {
9c1bc317 347 $op = $this->_params["{$fieldName}_op"] ?? NULL;
a32357b9
J
348
349 if ($op) {
350 $clause = $this->whereClause($field,
351 $op,
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)
355 );
356 }
357
358 }
359
360 if (!empty($clause)) {
361 $clauses[] = $clause;
362 }
363 }
364 }
365 }
366
367 $this->_where = "WHERE " . implode(' AND ', $clauses);
6a488035
TO
368 }
369
00be9182 370 public function groupBy() {
a7488080 371 if (!empty($this->_params['charts'])) {
d1641c51 372 $groupBy = "{$this->_aliases['civicrm_mailing']}.id";
6a488035
TO
373 }
374 else {
d1641c51 375 $groupBy = "{$this->_aliases['civicrm_mailing_event_bounce']}.id";
6a488035 376 }
b708c08d 377 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
6a488035
TO
378 }
379
00be9182 380 public function postProcess() {
6a488035
TO
381 $this->beginPostProcess();
382
383 // get the acl clauses built before we assemble the query
384 $this->buildACLClause($this->_aliases['civicrm_contact']);
385
386 $sql = $this->buildQuery(TRUE);
387
be2fb01f 388 $rows = $graphRows = [];
6a488035
TO
389 $this->buildRows($sql, $rows);
390
391 $this->formatDisplay($rows);
392 $this->doTemplateAssignment($rows);
393 $this->endPostProcess($rows);
394 }
395
74cf4551
EM
396 /**
397 * @param $rows
398 */
00be9182 399 public function buildChart(&$rows) {
6a488035
TO
400 if (empty($rows)) {
401 return;
402 }
403
be2fb01f 404 $chartInfo = [
9d72cede 405 'legend' => ts('Mail Bounce Report'),
6a488035
TO
406 'xname' => ts('Mailing'),
407 'yname' => ts('Bounce'),
408 'xLabelAngle' => 20,
be2fb01f
CW
409 'tip' => ts('Mail Bounce: %1', [1 => '#val#']),
410 ];
6a488035
TO
411 foreach ($rows as $row) {
412 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_bounce_count'];
413 }
414
415 // build the chart.
dc61ee93 416 CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']);
6a488035
TO
417 $this->assign('chartType', $this->_params['charts']);
418 }
419
74cf4551
EM
420 /**
421 * @return array
422 */
00be9182 423 public function bounce_type() {
6a488035 424
be2fb01f 425 $data = [];
6a488035
TO
426
427 $bounce_type = new CRM_Mailing_DAO_BounceType();
428 $query = "SELECT name FROM civicrm_mailing_bounce_type";
429 $bounce_type->query($query);
430
431 while ($bounce_type->fetch()) {
432 $data[$bounce_type->name] = $bounce_type->name;
433 }
434
435 return $data;
436 }
437
74cf4551 438 /**
ced9bfed
EM
439 * Alter display of rows.
440 *
441 * Iterate through the rows retrieved via SQL and make changes for display purposes,
442 * such as rendering contacts as links.
443 *
444 * @param array $rows
445 * Rows generated by SQL, with an array for each row.
74cf4551 446 */
00be9182 447 public function alterDisplay(&$rows) {
d3f6ce37 448
a32357b9 449 $config = CRM_Core_Config::Singleton();
d3f6ce37 450
6a488035
TO
451 $entryFound = FALSE;
452 foreach ($rows as $rowNum => $row) {
d2c742e9
J
453
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'])) {
81ca3560 457 $rows[$rowNum]['civicrm_email_email'] = '<del>' . ts('Email address deleted.') . '</del>';
d2c742e9
J
458 }
459 $entryFound = TRUE;
460 }
461
6a488035
TO
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)
466 ) {
467 $url = CRM_Utils_System::url('civicrm/contact/view',
468 'reset=1&cid=' . $row['civicrm_contact_id'],
469 $this->_absoluteUrl
470 );
471 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
472 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
473 $entryFound = TRUE;
474 }
d3f6ce37 475
a32357b9
J
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';
479 $entryFound = TRUE;
480 }
d3f6ce37 481
a32357b9 482 // Convert datetime values to custom date and time format
be2fb01f 483 $dateFields = [
a32357b9
J
484 'civicrm_mailing_event_bounce_time_stamp',
485 'civicrm_email_hold_date',
486 'civicrm_email_reset_date',
be2fb01f 487 ];
d3f6ce37
J
488
489 foreach ($dateFields as $dateField) {
a32357b9
J
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);
493 }
494 $entryFound = TRUE;
495 }
496 }
6a488035
TO
497
498 // skip looking further in rows, if first row itself doesn't
499 // have the column we need
500 if (!$entryFound) {
501 break;
502 }
503 }
504 }
96025800 505
6a488035 506}