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