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