CRM-18439 - Report Fixes to include Full Group by clause
[civicrm-core.git] / CRM / Report / Form / Mailing / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Mailing_Summary extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38
39 protected $_customGroupExtends = array();
40
41 protected $_add2groupSupported = FALSE;
42
43 public $_drilldownReport = array('mailing/detail' => 'Link to Detail Report');
44
45 protected $_charts = array(
46 '' => 'Tabular',
47 'bar_3dChart' => 'Bar Chart',
48 );
49
50 public $campaignEnabled = FALSE;
51
52 /**
53 */
54 /**
55 */
56 public function __construct() {
57 $this->_columns = array();
58
59 $this->_columns['civicrm_mailing'] = array(
60 'dao' => 'CRM_Mailing_DAO_Mailing',
61 'fields' => array(
62 'id' => array(
63 'name' => 'id',
64 'title' => ts('Mailing ID'),
65 'required' => TRUE,
66 'no_display' => TRUE,
67 ),
68 'name' => array(
69 'title' => ts('Mailing Name'),
70 'required' => TRUE,
71 ),
72 'created_date' => array(
73 'title' => ts('Date Created'),
74 ),
75 'subject' => array(
76 'title' => ts('Subject'),
77 ),
78 ),
79 'filters' => array(
80 'is_completed' => array(
81 'title' => ts('Mailing Status'),
82 'operatorType' => CRM_Report_Form::OP_SELECT,
83 'type' => CRM_Utils_Type::T_INT,
84 'options' => array(
85 0 => 'Incomplete',
86 1 => 'Complete',
87 ),
88 //'operator' => 'like',
89 'default' => 1,
90 ),
91 'mailing_name' => array(
92 'name' => 'name',
93 'title' => ts('Mailing'),
94 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
95 'type' => CRM_Utils_Type::T_STRING,
96 'options' => self::mailing_select(),
97 'operator' => 'like',
98 ),
99 ),
100 );
101
102 $this->_columns['civicrm_mailing_job'] = array(
103 'dao' => 'CRM_Mailing_DAO_MailingJob',
104 'fields' => array(
105 'start_date' => array(
106 'title' => ts('Start Date'),
107 ),
108 'end_date' => array(
109 'title' => ts('End Date'),
110 ),
111 ),
112 'filters' => array(
113 'status' => array(
114 'type' => CRM_Utils_Type::T_STRING,
115 'default' => 'Complete',
116 'no_display' => TRUE,
117 ),
118 'is_test' => array(
119 'type' => CRM_Utils_Type::T_INT,
120 'default' => 0,
121 'no_display' => TRUE,
122 ),
123 'start_date' => array(
124 'title' => ts('Start Date'),
125 'default' => 'this.year',
126 'operatorType' => CRM_Report_Form::OP_DATE,
127 'type' => CRM_Utils_Type::T_DATE,
128 ),
129 'end_date' => array(
130 'title' => ts('End Date'),
131 'default' => 'this.year',
132 'operatorType' => CRM_Report_Form::OP_DATE,
133 'type' => CRM_Utils_Type::T_DATE,
134 ),
135 ),
136 );
137
138 $this->_columns['civicrm_mailing_event_queue'] = array(
139 'dao' => 'CRM_Mailing_DAO_Mailing',
140 'fields' => array(
141 'queue_count' => array(
142 'name' => 'id',
143 'title' => ts('Intended Recipients'),
144 ),
145 ),
146 );
147
148 $this->_columns['civicrm_mailing_event_delivered'] = array(
149 'dao' => 'CRM_Mailing_DAO_Mailing',
150 'fields' => array(
151 'delivered_count' => array(
152 'name' => 'event_queue_id',
153 'title' => ts('Delivered'),
154 ),
155 'accepted_rate' => array(
156 'title' => 'Accepted Rate',
157 'statistics' => array(
158 'calc' => 'PERCENTAGE',
159 'top' => 'civicrm_mailing_event_delivered.delivered_count',
160 'base' => 'civicrm_mailing_event_queue.queue_count',
161 ),
162 ),
163 ),
164 );
165
166 $this->_columns['civicrm_mailing_event_bounce'] = array(
167 'dao' => 'CRM_Mailing_DAO_Mailing',
168 'fields' => array(
169 'bounce_count' => array(
170 'name' => 'event_queue_id',
171 'title' => ts('Bounce'),
172 ),
173 'bounce_rate' => array(
174 'title' => 'Bounce Rate',
175 'statistics' => array(
176 'calc' => 'PERCENTAGE',
177 'top' => 'civicrm_mailing_event_bounce.bounce_count',
178 'base' => 'civicrm_mailing_event_queue.queue_count',
179 ),
180 ),
181 ),
182 );
183
184 $this->_columns['civicrm_mailing_event_opened'] = array(
185 'dao' => 'CRM_Mailing_DAO_Mailing',
186 'fields' => array(
187 'unique_open_count' => array(
188 'name' => 'id',
189 'alias' => 'mailing_event_opened_civireport',
190 'dbAlias' => 'mailing_event_opened_civireport.event_queue_id',
191 'title' => ts('Unique Opens'),
192 ),
193 'unique_open_rate' => array(
194 'title' => 'Unique Open Rate',
195 'statistics' => array(
196 'calc' => 'PERCENTAGE',
197 'top' => 'civicrm_mailing_event_opened.unique_open_count',
198 'base' => 'civicrm_mailing_event_delivered.delivered_count',
199 ),
200 ),
201 'open_count' => array(
202 'name' => 'event_queue_id',
203 'title' => ts('Total Opens'),
204 ),
205 'open_rate' => array(
206 'title' => 'Total Open Rate',
207 'statistics' => array(
208 'calc' => 'PERCENTAGE',
209 'top' => 'civicrm_mailing_event_opened.open_count',
210 'base' => 'civicrm_mailing_event_delivered.delivered_count',
211 ),
212 ),
213 ),
214 );
215
216 $this->_columns['civicrm_mailing_event_trackable_url_open'] = array(
217 'dao' => 'CRM_Mailing_DAO_Mailing',
218 'fields' => array(
219 'click_count' => array(
220 'name' => 'event_queue_id',
221 'title' => ts('Clicks'),
222 ),
223 'CTR' => array(
224 'title' => 'Click through Rate',
225 'default' => 0,
226 'statistics' => array(
227 'calc' => 'PERCENTAGE',
228 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
229 'base' => 'civicrm_mailing_event_delivered.delivered_count',
230 ),
231 ),
232 'CTO' => array(
233 'title' => 'Click to Open Rate',
234 'default' => 0,
235 'statistics' => array(
236 'calc' => 'PERCENTAGE',
237 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
238 'base' => 'civicrm_mailing_event_opened.open_count',
239 ),
240 ),
241 ),
242 );
243
244 $this->_columns['civicrm_mailing_event_unsubscribe'] = array(
245 'dao' => 'CRM_Mailing_DAO_Mailing',
246 'fields' => array(
247 'unsubscribe_count' => array(
248 'name' => 'id',
249 'title' => ts('Unsubscribe'),
250 'alias' => 'mailing_event_unsubscribe_civireport',
251 'dbAlias' => 'mailing_event_unsubscribe_civireport.event_queue_id',
252 ),
253 'optout_count' => array(
254 'name' => 'id',
255 'title' => ts('Opt-outs'),
256 'alias' => 'mailing_event_optout_civireport',
257 'dbAlias' => 'mailing_event_optout_civireport.event_queue_id',
258 ),
259 ),
260 );
261 $this->_columns['civicrm_mailing_group'] = array(
262 'dao' => 'CRM_Mailing_DAO_MailingGroup',
263 'filters' => array(
264 'entity_id' => array(
265 'title' => ts('Groups Included in Mailing'),
266 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
267 'type' => CRM_Utils_Type::T_INT,
268 'options' => CRM_Core_PseudoConstant::group(),
269 ),
270 ),
271 );
272 $config = CRM_Core_Config::singleton();
273 $this->campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
274 if ($this->campaignEnabled) {
275 $this->_columns['civicrm_campaign'] = array(
276 'dao' => 'CRM_Campaign_DAO_Campaign',
277 'fields' => array(
278 'title' => array(
279 'title' => ts('Campaign Name'),
280 ),
281 ),
282 'filters' => array(
283 'title' => array(
284 'type' => CRM_Utils_Type::T_STRING,
285 ),
286 ),
287 );
288 }
289 parent::__construct();
290 }
291
292 /**
293 * @return array
294 */
295 public function mailing_select() {
296
297 $data = array();
298
299 $mailing = new CRM_Mailing_BAO_Mailing();
300 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
301 $mailing->query($query);
302
303 while ($mailing->fetch()) {
304 $data[mysql_real_escape_string($mailing->name)] = $mailing->name;
305 }
306
307 return $data;
308 }
309
310 public function preProcess() {
311 $this->assign('chartSupported', TRUE);
312 parent::preProcess();
313 }
314
315 /**
316 * manipulate the select function to query count functions.
317 */
318 public function select() {
319
320 $count_tables = array(
321 'civicrm_mailing_event_queue',
322 'civicrm_mailing_event_delivered',
323 'civicrm_mailing_event_opened',
324 'civicrm_mailing_event_bounce',
325 'civicrm_mailing_event_trackable_url_open',
326 'civicrm_mailing_event_unsubscribe',
327 );
328
329 $select = array();
330 $this->_columnHeaders = array();
331 foreach ($this->_columns as $tableName => $table) {
332 if (array_key_exists('fields', $table)) {
333 foreach ($table['fields'] as $fieldName => $field) {
334 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
335
336 # for statistics
337 if (!empty($field['statistics'])) {
338 switch ($field['statistics']['calc']) {
339 case 'PERCENTAGE':
340 $base_table_column = explode('.', $field['statistics']['base']);
341 $top_table_column = explode('.', $field['statistics']['top']);
342
343 $select[] = "CONCAT(round(
344 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
345 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
346 ), '%') as {$tableName}_{$fieldName}";
347 break;
348 }
349 }
350 else {
351 if (in_array($tableName, $count_tables)) {
352 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
353 }
354 else {
355 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
356 }
357 }
358 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
359 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
360 }
361 }
362 }
363 }
364
365 $this->_selectClauses = $select;
366 $this->_select = "SELECT " . implode(', ', $select) . " ";
367 //print_r($this->_select);
368 }
369
370 public function from() {
371
372 $this->_from = "
373 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
374 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
375 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
376 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
377 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
378 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
379 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
380 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
381 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
382 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
383 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
384 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
385 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
386 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
387 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
388 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}.org_unsubscribe = 0
389 LEFT JOIN civicrm_mailing_event_unsubscribe mailing_event_optout_civireport
390 ON mailing_event_optout_civireport.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id AND mailing_event_optout_civireport.org_unsubscribe = 1";
391
392 if ($this->isTableSelected('civicrm_mailing_group')) {
393 $this->_from .= "
394 LEFT JOIN civicrm_mailing_group {$this->_aliases['civicrm_mailing_group']}
395 ON {$this->_aliases['civicrm_mailing_group']}.mailing_id = {$this->_aliases['civicrm_mailing']}.id";
396 }
397 if ($this->campaignEnabled) {
398 $this->_from .= "
399 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
400 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
401 }
402
403 // need group by and order by
404
405 //print_r($this->_from);
406 }
407
408 public function where() {
409 $clauses = array();
410 //to avoid the sms listings
411 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
412
413 foreach ($this->_columns as $tableName => $table) {
414 if (array_key_exists('filters', $table)) {
415 foreach ($table['filters'] as $fieldName => $field) {
416 $clause = NULL;
417 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
418 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
419 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
420 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
421
422 $clause = $this->dateClause($this->_aliases[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
423 }
424 else {
425 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
426
427 if ($op) {
428 if ($fieldName == 'relationship_type_id') {
429 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
430 }
431 else {
432 $clause = $this->whereClause($field,
433 $op,
434 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
435 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
436 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
437 );
438 }
439 }
440 }
441
442 if (!empty($clause)) {
443 $clauses[] = $clause;
444 }
445 }
446 }
447 }
448
449 if (empty($clauses)) {
450 $this->_where = "WHERE ( 1 )";
451 }
452 else {
453 $this->_where = "WHERE " . implode(' AND ', $clauses);
454 }
455
456 // if ( $this->_aclWhere ) {
457 // $this->_where .= " AND {$this->_aclWhere} ";
458 // }
459 }
460
461 public function groupBy() {
462 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
463 $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_mailing']}.id");
464 }
465
466 public function orderBy() {
467 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
468 }
469
470 public function postProcess() {
471
472 $this->beginPostProcess();
473
474 // get the acl clauses built before we assemble the query
475 $this->buildACLClause(CRM_Utils_Array::value('civicrm_contact', $this->_aliases));
476
477 $sql = $this->buildQuery(TRUE);
478
479 // print_r($sql);
480
481 $rows = $graphRows = array();
482 $this->buildRows($sql, $rows);
483
484 $this->formatDisplay($rows);
485 $this->doTemplateAssignment($rows);
486 $this->endPostProcess($rows);
487 }
488
489 /**
490 * @return array
491 */
492 public static function getChartCriteria() {
493 return array(
494 'count' => array(
495 'civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
496 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
497 'civicrm_mailing_event_opened_open_count' => ts('Total Opens'),
498 'civicrm_mailing_event_opened_unique_open_count' => ts('Unique Opens'),
499 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
500 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
501 ),
502 'rate' => array(
503 'civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
504 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
505 'civicrm_mailing_event_opened_open_rate' => ts('Total Open Rate'),
506 'civicrm_mailing_event_opened_unique_open_rate' => ts('Unique Open Rate'),
507 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
508 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
509 ),
510 );
511 }
512
513 /**
514 * @param $fields
515 * @param $files
516 * @param $self
517 *
518 * @return array
519 */
520 public static function formRule($fields, $files, $self) {
521 $errors = array();
522
523 if (empty($fields['charts'])) {
524 return $errors;
525 }
526
527 $criteria = self::getChartCriteria();
528 $isError = TRUE;
529 foreach ($fields['fields'] as $fld => $isActive) {
530 if (in_array($fld, array(
531 'delivered_count',
532 'bounce_count',
533 'open_count',
534 'click_count',
535 'unsubscribe_count',
536 'accepted_rate',
537 'bounce_rate',
538 'open_rate',
539 'CTR',
540 'CTO',
541 'unique_open_rate',
542 'unique_open_count',
543 ))) {
544 $isError = FALSE;
545 }
546 }
547
548 if ($isError) {
549 $errors['_qf_default'] = ts('For Chart view, please select at least one field from %1 OR %2.', array(
550 1 => implode(', ', $criteria['count']),
551 2 => implode(', ', $criteria['rate']),
552 ));
553 }
554
555 return $errors;
556 }
557
558 /**
559 * @param $rows
560 */
561 public function buildChart(&$rows) {
562 if (empty($rows)) {
563 return;
564 }
565
566 $criteria = self::getChartCriteria();
567
568 $chartInfo = array(
569 'legend' => ts('Mail Summary'),
570 'xname' => ts('Mailing'),
571 'yname' => ts('Statistics'),
572 'xLabelAngle' => 20,
573 'tip' => array(),
574 );
575
576 $plotRate = $plotCount = TRUE;
577 foreach ($rows as $row) {
578 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
579 if ($plotCount) {
580 foreach ($criteria['count'] as $criteria => $label) {
581 if (isset($row[$criteria])) {
582 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
583 $chartInfo['tip'][$label] = "{$label} #val#";
584 $plotRate = FALSE;
585 }
586 elseif (isset($criteria['count'][$criteria])) {
587 unset($criteria['count'][$criteria]);
588 }
589 }
590 }
591 if ($plotRate) {
592 foreach ($criteria['rate'] as $criteria => $label) {
593 if (isset($row[$criteria])) {
594 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
595 $chartInfo['tip'][$label] = "{$label} #val#";
596 $plotCount = FALSE;
597 }
598 elseif (isset($criteria['rate'][$criteria])) {
599 unset($criteria['rate'][$criteria]);
600 }
601 }
602 }
603 }
604
605 if ($plotCount) {
606 $criteria = $criteria['count'];
607 }
608 else {
609 $criteria = $criteria['rate'];
610 }
611
612 $chartInfo['criteria'] = array_values($criteria);
613
614 // dynamically set the graph size
615 $chartInfo['xSize'] = ((count($rows) * 125) + (count($rows) * count($criteria) * 40));
616
617 // build the chart.
618 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
619 $this->assign('chartType', $this->_params['charts']);
620 }
621
622 /**
623 * Alter display of rows.
624 *
625 * Iterate through the rows retrieved via SQL and make changes for display purposes,
626 * such as rendering contacts as links.
627 *
628 * @param array $rows
629 * Rows generated by SQL, with an array for each row.
630 */
631 public function alterDisplay(&$rows) {
632 $entryFound = FALSE;
633 foreach ($rows as $rowNum => $row) {
634 // CRM-16506
635 if (array_key_exists('civicrm_mailing_name', $row) &&
636 array_key_exists('civicrm_mailing_id', $row)
637 ) {
638 $rows[$rowNum]['civicrm_mailing_name_link'] = CRM_Report_Utils_Report::getNextUrl('mailing/detail',
639 'reset=1&force=1&mailing_id_op=eq&mailing_id_value=' . $row['civicrm_mailing_id'],
640 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
641 );
642 $rows[$rowNum]['civicrm_mailing_name_hover'] = ts('View Mailing details for this mailing');
643 $entryFound = TRUE;
644 }
645 // skip looking further in rows, if first row itself doesn't
646 // have the column we need
647 if (!$entryFound) {
648 break;
649 }
650 }
651 }
652
653 }