Add case ID and make subject optional for case detail report
[civicrm-core.git] / CRM / Report / Form / Mailing / Bounce.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class 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
25 protected $_customGroupExtends = [
26 'Contact',
27 'Individual',
28 'Household',
29 'Organization',
30 ];
31
32 /**
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 *
39 * @var bool
40 * @see https://issues.civicrm.org/jira/browse/CRM-19170
41 */
42 protected $groupFilterNotOptimised = TRUE;
43
44 /**
45 * Class constructor.
46 */
47 public function __construct() {
48 $this->_columns = [];
49
50 $this->_columns['civicrm_contact'] = [
51 'dao' => 'CRM_Contact_DAO_Contact',
52 'fields' => [
53 'id' => [
54 'title' => ts('Contact ID'),
55 'required' => TRUE,
56 ],
57 'sort_name' => [
58 'title' => ts('Contact Name'),
59 'required' => TRUE,
60 ],
61 ],
62 'filters' => [
63 'sort_name' => [
64 'title' => ts('Contact Name'),
65 ],
66 'source' => [
67 'title' => ts('Contact Source'),
68 'type' => CRM_Utils_Type::T_STRING,
69 ],
70 'id' => [
71 'title' => ts('Contact ID'),
72 'no_display' => TRUE,
73 ],
74 ],
75 'order_bys' => [
76 'sort_name' => [
77 'title' => ts('Contact Name'),
78 'default' => TRUE,
79 'default_order' => 'ASC',
80 ],
81 ],
82 'grouping' => 'contact-fields',
83 ];
84
85 $this->_columns['civicrm_mailing'] = [
86 'dao' => 'CRM_Mailing_DAO_Mailing',
87 'fields' => [
88 'mailing_name' => [
89 'name' => 'name',
90 'title' => ts('Mailing Name'),
91 'default' => TRUE,
92 ],
93 'mailing_name_alias' => [
94 'name' => 'name',
95 'required' => TRUE,
96 'no_display' => TRUE,
97 ],
98 'mailing_subject' => [
99 'name' => 'subject',
100 'title' => ts('Mailing Subject'),
101 'default' => TRUE,
102 ],
103 ],
104 'filters' => [
105 'mailing_id' => [
106 'name' => 'id',
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',
112 ],
113 'mailing_subject' => [
114 'name' => 'subject',
115 'title' => ts('Mailing Subject'),
116 'type' => CRM_Utils_Type::T_STRING,
117 'operator' => 'like',
118 ],
119 ],
120 'order_bys' => [
121 'mailing_name' => [
122 'name' => 'name',
123 'title' => ts('Mailing Name'),
124 ],
125 'mailing_subject' => [
126 'name' => 'subject',
127 'title' => ts('Mailing Subject'),
128 ],
129 ],
130 'grouping' => 'mailing-fields',
131 ];
132
133 $this->_columns['civicrm_mailing_event_bounce'] = [
134 'dao' => 'CRM_Mailing_DAO_Mailing',
135 'fields' => [
136 'bounce_reason' => [
137 'title' => ts('Bounce Reason'),
138 ],
139 'time_stamp' => [
140 'title' => ts('Bounce Date'),
141 ],
142 ],
143 'filters' => [
144 'bounce_reason' => [
145 'title' => ts('Bounce Reason'),
146 'type' => CRM_Utils_Type::T_STRING,
147 ],
148 'time_stamp' => [
149 'title' => ts('Bounce Date'),
150 'operatorType' => CRM_Report_Form::OP_DATE,
151 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
152 ],
153 ],
154 'order_bys' => [
155 'bounce_reason' => [
156 'title' => ts('Bounce Reason'),
157 ],
158 'time_stamp' => [
159 'title' => ts('Bounce Date'),
160 ],
161 ],
162 'grouping' => 'mailing-fields',
163 ];
164
165 $this->_columns['civicrm_mailing_bounce_type'] = [
166 'dao' => 'CRM_Mailing_DAO_BounceType',
167 'fields' => [
168 'bounce_name' => [
169 'name' => 'name',
170 'title' => ts('Bounce Type'),
171 ],
172 ],
173 'filters' => [
174 'bounce_type_name' => [
175 'name' => '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',
181 ],
182 ],
183 'order_bys' => [
184 'bounce_name' => [
185 'name' => 'name',
186 'title' => ts('Bounce Type'),
187 ],
188 ],
189 'grouping' => 'mailing-fields',
190 ];
191
192 $this->_columns['civicrm_email'] = [
193 'dao' => 'CRM_Core_DAO_Email',
194 'fields' => [
195 'email' => [
196 'title' => ts('Email'),
197 'no_repeat' => TRUE,
198 ],
199 'on_hold' => [
200 'title' => ts('On hold'),
201 ],
202 'hold_date' => [
203 'title' => ts('Hold date'),
204 ],
205 'reset_date' => [
206 'title' => ts('Hold reset date'),
207 ],
208 ],
209 'filters' => [
210 'email' => [
211 'title' => ts('Email'),
212 ],
213 'on_hold' => [
214 'title' => ts('On hold'),
215 ],
216 'hold_date' => [
217 'title' => ts('Hold date'),
218 'operatorType' => CRM_Report_Form::OP_DATE,
219 'type' => CRM_Utils_Type::T_DATE,
220 ],
221 'reset_date' => [
222 'title' => ts('Hold reset date'),
223 'operatorType' => CRM_Report_Form::OP_DATE,
224 'type' => CRM_Utils_Type::T_DATE,
225 ],
226 ],
227 'order_bys' => [
228 'email' => ['title' => ts('Email'), 'default_order' => 'ASC'],
229 ],
230 'grouping' => 'contact-fields',
231 ];
232
233 $this->_columns['civicrm_phone'] = [
234 'dao' => 'CRM_Core_DAO_Phone',
235 'fields' => ['phone' => NULL],
236 'grouping' => 'contact-fields',
237 ];
238
239 // Add charts support
240 $this->_charts = [
241 '' => ts('Tabular'),
242 'barChart' => ts('Bar Chart'),
243 'pieChart' => ts('Pie Chart'),
244 ];
245
246 $this->_groupFilter = TRUE;
247 $this->_tagFilter = TRUE;
248 parent::__construct();
249 }
250
251 public function preProcess() {
252 $this->assign('chartSupported', TRUE);
253 parent::preProcess();
254 }
255
256 public function select() {
257 $select = [];
258 $this->_columnHeaders = [];
259
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])
265 ) {
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}";
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;
277 }
278 }
279 }
280 }
281
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');
285 }
286
287 $this->_selectClauses = $select;
288 $this->_select = "SELECT " . implode(', ', $select) . " ";
289 }
290
291 /**
292 * @param $fields
293 * @param $files
294 * @param self $self
295 *
296 * @return array
297 */
298 public static function formRule($fields, $files, $self) {
299 $errors = $grouping = [];
300 return $errors;
301 }
302
303 public function from() {
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 .= "
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
323 ";
324 $this->joinPhoneFromContact();
325 }
326
327 public function where() {
328
329 $clauses = [];
330
331 // Exclude SMS mailing type
332 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
333
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) {
340 $relative = $this->_params["{$fieldName}_relative"] ?? NULL;
341 $from = $this->_params["{$fieldName}_from"] ?? NULL;
342 $to = $this->_params["{$fieldName}_to"] ?? NULL;
343
344 $clause = $this->dateClause($this->_aliases[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
345 }
346 else {
347 $op = $this->_params["{$fieldName}_op"] ?? NULL;
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);
368 }
369
370 public function groupBy() {
371 if (!empty($this->_params['charts'])) {
372 $groupBy = "{$this->_aliases['civicrm_mailing']}.id";
373 }
374 else {
375 $groupBy = "{$this->_aliases['civicrm_mailing_event_bounce']}.id";
376 }
377 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
378 }
379
380 public function postProcess() {
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
388 $rows = $graphRows = [];
389 $this->buildRows($sql, $rows);
390
391 $this->formatDisplay($rows);
392 $this->doTemplateAssignment($rows);
393 $this->endPostProcess($rows);
394 }
395
396 /**
397 * @param $rows
398 */
399 public function buildChart(&$rows) {
400 if (empty($rows)) {
401 return;
402 }
403
404 $chartInfo = [
405 'legend' => ts('Mail Bounce Report'),
406 'xname' => ts('Mailing'),
407 'yname' => ts('Bounce'),
408 'xLabelAngle' => 20,
409 'tip' => ts('Mail Bounce: %1', [1 => '#val#']),
410 ];
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.
416 CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']);
417 $this->assign('chartType', $this->_params['charts']);
418 }
419
420 /**
421 * @return array
422 */
423 public function bounce_type() {
424
425 $data = [];
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
438 /**
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.
446 */
447 public function alterDisplay(&$rows) {
448
449 $config = CRM_Core_Config::Singleton();
450
451 $entryFound = FALSE;
452 foreach ($rows as $rowNum => $row) {
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'])) {
457 $rows[$rowNum]['civicrm_email_email'] = '<del>' . ts('Email address deleted.') . '</del>';
458 }
459 $entryFound = TRUE;
460 }
461
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 }
475
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 }
481
482 // Convert datetime values to custom date and time format
483 $dateFields = [
484 'civicrm_mailing_event_bounce_time_stamp',
485 'civicrm_email_hold_date',
486 'civicrm_email_reset_date',
487 ];
488
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);
493 }
494 $entryFound = TRUE;
495 }
496 }
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 }
505
506 }