Merge pull request #8072 from colemanw/CRM-17965
[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 $config = CRM_Core_Config::singleton();
262 $this->campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
263 if ($this->campaignEnabled) {
264 $this->_columns['civicrm_campaign'] = array(
265 'dao' => 'CRM_Campaign_DAO_Campaign',
266 'fields' => array(
267 'title' => array(
268 'title' => ts('Campaign Name'),
269 ),
270 ),
271 'filters' => array(
272 'title' => array(
273 'type' => CRM_Utils_Type::T_STRING,
274 ),
275 ),
276 );
277 }
278 parent::__construct();
279 }
280
281 /**
282 * @return array
283 */
284 public function mailing_select() {
285
286 $data = array();
287
288 $mailing = new CRM_Mailing_BAO_Mailing();
289 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
290 $mailing->query($query);
291
292 while ($mailing->fetch()) {
293 $data[mysql_real_escape_string($mailing->name)] = $mailing->name;
294 }
295
296 return $data;
297 }
298
299 public function preProcess() {
300 $this->assign('chartSupported', TRUE);
301 parent::preProcess();
302 }
303
304 /**
305 * manipulate the select function to query count functions.
306 */
307 public function select() {
308
309 $count_tables = array(
310 'civicrm_mailing_event_queue',
311 'civicrm_mailing_event_delivered',
312 'civicrm_mailing_event_opened',
313 'civicrm_mailing_event_bounce',
314 'civicrm_mailing_event_trackable_url_open',
315 'civicrm_mailing_event_unsubscribe',
316 );
317
318 $select = array();
319 $this->_columnHeaders = array();
320 foreach ($this->_columns as $tableName => $table) {
321 if (array_key_exists('fields', $table)) {
322 foreach ($table['fields'] as $fieldName => $field) {
323 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
324
325 # for statistics
326 if (!empty($field['statistics'])) {
327 switch ($field['statistics']['calc']) {
328 case 'PERCENTAGE':
329 $base_table_column = explode('.', $field['statistics']['base']);
330 $top_table_column = explode('.', $field['statistics']['top']);
331
332 $select[] = "CONCAT(round(
333 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
334 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
335 ), '%') as {$tableName}_{$fieldName}";
336 break;
337 }
338 }
339 else {
340 if (in_array($tableName, $count_tables)) {
341 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
342 }
343 else {
344 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
345 }
346 }
347 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
348 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
349 }
350 }
351 }
352 }
353
354 $this->_select = "SELECT " . implode(', ', $select) . " ";
355 //print_r($this->_select);
356 }
357
358 public function from() {
359
360 $this->_from = "
361 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
362 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
363 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
364 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
365 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
366 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
367 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
368 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
369 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
370 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
371 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
372 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
373 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
374 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
375 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
376 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
377 LEFT JOIN civicrm_mailing_event_unsubscribe mailing_event_optout_civireport
378 ON mailing_event_optout_civireport.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id AND mailing_event_optout_civireport.org_unsubscribe = 1";
379
380 if ($this->campaignEnabled) {
381 $this->_from .= "
382 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
383 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
384 }
385
386 // need group by and order by
387
388 //print_r($this->_from);
389 }
390
391 public function where() {
392 $clauses = array();
393 //to avoid the sms listings
394 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
395
396 foreach ($this->_columns as $tableName => $table) {
397 if (array_key_exists('filters', $table)) {
398 foreach ($table['filters'] as $fieldName => $field) {
399 $clause = NULL;
400 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
401 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
402 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
403 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
404
405 $clause = $this->dateClause($this->_aliases[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
406 }
407 else {
408 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
409
410 if ($op) {
411 if ($fieldName == 'relationship_type_id') {
412 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
413 }
414 else {
415 $clause = $this->whereClause($field,
416 $op,
417 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
418 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
419 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
420 );
421 }
422 }
423 }
424
425 if (!empty($clause)) {
426 $clauses[] = $clause;
427 }
428 }
429 }
430 }
431
432 if (empty($clauses)) {
433 $this->_where = "WHERE ( 1 )";
434 }
435 else {
436 $this->_where = "WHERE " . implode(' AND ', $clauses);
437 }
438
439 // if ( $this->_aclWhere ) {
440 // $this->_where .= " AND {$this->_aclWhere} ";
441 // }
442 }
443
444 public function groupBy() {
445 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
446 }
447
448 public function orderBy() {
449 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
450 }
451
452 public function postProcess() {
453
454 $this->beginPostProcess();
455
456 // get the acl clauses built before we assemble the query
457 $this->buildACLClause(CRM_Utils_Array::value('civicrm_contact', $this->_aliases));
458
459 $sql = $this->buildQuery(TRUE);
460
461 // print_r($sql);
462
463 $rows = $graphRows = array();
464 $this->buildRows($sql, $rows);
465
466 $this->formatDisplay($rows);
467 $this->doTemplateAssignment($rows);
468 $this->endPostProcess($rows);
469 }
470
471 /**
472 * @return array
473 */
474 public static function getChartCriteria() {
475 return array(
476 'count' => array(
477 'civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
478 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
479 'civicrm_mailing_event_opened_open_count' => ts('Total Opens'),
480 'civicrm_mailing_event_opened_unique_open_count' => ts('Unique Opens'),
481 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
482 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
483 ),
484 'rate' => array(
485 'civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
486 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
487 'civicrm_mailing_event_opened_open_rate' => ts('Total Open Rate'),
488 'civicrm_mailing_event_opened_unique_open_rate' => ts('Unique Open Rate'),
489 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
490 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
491 ),
492 );
493 }
494
495 /**
496 * @param $fields
497 * @param $files
498 * @param $self
499 *
500 * @return array
501 */
502 public static function formRule($fields, $files, $self) {
503 $errors = array();
504
505 if (empty($fields['charts'])) {
506 return $errors;
507 }
508
509 $criteria = self::getChartCriteria();
510 $isError = TRUE;
511 foreach ($fields['fields'] as $fld => $isActive) {
512 if (in_array($fld, array(
513 'delivered_count',
514 'bounce_count',
515 'open_count',
516 'click_count',
517 'unsubscribe_count',
518 'accepted_rate',
519 'bounce_rate',
520 'open_rate',
521 'CTR',
522 'CTO',
523 'unique_open_rate',
524 'unique_open_count',
525 ))) {
526 $isError = FALSE;
527 }
528 }
529
530 if ($isError) {
531 $errors['_qf_default'] = ts('For Chart view, please select at least one field from %1 OR %2.', array(
532 1 => implode(', ', $criteria['count']),
533 2 => implode(', ', $criteria['rate']),
534 ));
535 }
536
537 return $errors;
538 }
539
540 /**
541 * @param $rows
542 */
543 public function buildChart(&$rows) {
544 if (empty($rows)) {
545 return;
546 }
547
548 $criteria = self::getChartCriteria();
549
550 $chartInfo = array(
551 'legend' => ts('Mail Summary'),
552 'xname' => ts('Mailing'),
553 'yname' => ts('Statistics'),
554 'xLabelAngle' => 20,
555 'tip' => array(),
556 );
557
558 $plotRate = $plotCount = TRUE;
559 foreach ($rows as $row) {
560 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
561 if ($plotCount) {
562 foreach ($criteria['count'] as $criteria => $label) {
563 if (isset($row[$criteria])) {
564 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
565 $chartInfo['tip'][$label] = "{$label} #val#";
566 $plotRate = FALSE;
567 }
568 elseif (isset($criteria['count'][$criteria])) {
569 unset($criteria['count'][$criteria]);
570 }
571 }
572 }
573 if ($plotRate) {
574 foreach ($criteria['rate'] as $criteria => $label) {
575 if (isset($row[$criteria])) {
576 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
577 $chartInfo['tip'][$label] = "{$label} #val#";
578 $plotCount = FALSE;
579 }
580 elseif (isset($criteria['rate'][$criteria])) {
581 unset($criteria['rate'][$criteria]);
582 }
583 }
584 }
585 }
586
587 if ($plotCount) {
588 $criteria = $criteria['count'];
589 }
590 else {
591 $criteria = $criteria['rate'];
592 }
593
594 $chartInfo['criteria'] = array_values($criteria);
595
596 // dynamically set the graph size
597 $chartInfo['xSize'] = ((count($rows) * 125) + (count($rows) * count($criteria) * 40));
598
599 // build the chart.
600 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
601 $this->assign('chartType', $this->_params['charts']);
602 }
603
604 /**
605 * Alter display of rows.
606 *
607 * Iterate through the rows retrieved via SQL and make changes for display purposes,
608 * such as rendering contacts as links.
609 *
610 * @param array $rows
611 * Rows generated by SQL, with an array for each row.
612 */
613 public function alterDisplay(&$rows) {
614 $entryFound = FALSE;
615 foreach ($rows as $rowNum => $row) {
616 // CRM-16506
617 if (array_key_exists('civicrm_mailing_name', $row) &&
618 array_key_exists('civicrm_mailing_id', $row)
619 ) {
620 $rows[$rowNum]['civicrm_mailing_name_link'] = CRM_Report_Utils_Report::getNextUrl('mailing/detail',
621 'reset=1&force=1&mailing_id_op=eq&mailing_id_value=' . $row['civicrm_mailing_id'],
622 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
623 );
624 $rows[$rowNum]['civicrm_mailing_name_hover'] = ts('View Mailing details for this mailing');
625 $entryFound = TRUE;
626 }
627 // skip looking further in rows, if first row itself doesn't
628 // have the column we need
629 if (!$entryFound) {
630 break;
631 }
632 }
633 }
634
635 }