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