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