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