CRM/SMS add missing comment blocks
[civicrm-core.git] / CRM / Report / Form / Mailing / Summary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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) {
8cc574cf 290 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
6a488035
TO
291
292 # for statistics
a7488080 293 if (!empty($field['statistics'])) {
6a488035
TO
294 switch ($field['statistics']['calc']) {
295 case 'PERCENTAGE':
296 $base_table_column = explode('.', $field['statistics']['base']);
297 $top_table_column = explode('.', $field['statistics']['top']);
298
299 $select[] = "CONCAT(round(
300 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
301 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
302 ), '%') as {$tableName}_{$fieldName}";
303 break;
304 }
305 }
306 else {
307 if (in_array($tableName, $count_tables)) {
308 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
309 }
310 else {
311 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
312 }
313 }
314 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
315 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
316 }
317 }
318 }
319 }
320
321 $this->_select = "SELECT " . implode(', ', $select) . " ";
322 //print_r($this->_select);
323 }
324
325 function from() {
326
327 $this->_from = "
328 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
329 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
330 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
331 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
332 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
333 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
334 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
335 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
336 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
337 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
338 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
339 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
340 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
341 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
342 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
343 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id";
baff85dd 344
345 if ($this->campaignEnabled) {
346 $this->_from .= "
347 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
348 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
349 }
350
6a488035
TO
351 // need group by and order by
352
353 //print_r($this->_from);
354 }
355
356 function where() {
357 $clauses = array();
358 //to avoid the sms listings
359 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
360
361 foreach ($this->_columns as $tableName => $table) {
362 if (array_key_exists('filters', $table)) {
363 foreach ($table['filters'] as $fieldName => $field) {
364 $clause = NULL;
365 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
366 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
367 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
368 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
369
baff85dd 370 $clause = $this->dateClause($this->_aliases[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
6a488035
TO
371 }
372 else {
373 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
374
375 if ($op) {
376 if ($fieldName == 'relationship_type_id') {
377 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
378 }
379 else {
380 $clause = $this->whereClause($field,
381 $op,
382 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
383 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
384 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
385 );
386 }
387 }
388 }
389
390 if (!empty($clause)) {
391 $clauses[] = $clause;
392 }
393 }
394 }
395 }
396
397 if (empty($clauses)) {
398 $this->_where = "WHERE ( 1 )";
399 }
400 else {
401 $this->_where = "WHERE " . implode(' AND ', $clauses);
402 }
403
404 // if ( $this->_aclWhere ) {
405 // $this->_where .= " AND {$this->_aclWhere} ";
406 // }
407 }
408
409 function groupBy() {
410 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
411 }
412
413 function orderBy() {
414 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
415 }
416
417 function postProcess() {
418
419 $this->beginPostProcess();
420
421 // get the acl clauses built before we assemble the query
422 $this->buildACLClause(CRM_Utils_Array::value('civicrm_contact', $this->_aliases));
423
424 $sql = $this->buildQuery(TRUE);
425
426 // print_r($sql);
427
428 $rows = $graphRows = array();
429 $this->buildRows($sql, $rows);
430
431 $this->formatDisplay($rows);
432 $this->doTemplateAssignment($rows);
433 $this->endPostProcess($rows);
434 }
435
436 static function getChartCriteria() {
437 return array('count' => array('civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
438 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
439 'civicrm_mailing_event_opened_open_count' => ts('Opened'),
440 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
441 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
442 ),
443 'rate' => array('civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
444 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
445 'civicrm_mailing_event_opened_open_rate' => ts('Confirmed Open Rate'),
446 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
447 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
448 ),
449 );
450 }
451
452 function formRule($fields, $files, $self) {
453 $errors = array();
454
a7488080 455 if (empty($fields['charts'])) {
6a488035
TO
456 return $errors;
457 }
458
459 $criterias = self::getChartCriteria();
460 $isError = TRUE;
461 foreach ($fields['fields'] as $fld => $isActive) {
462 if (in_array($fld, array(
463 'delivered_count', 'bounce_count', 'open_count', 'click_count', 'unsubscribe_count', 'accepted_rate', 'bounce_rate', 'open_rate', 'CTR', 'CTO'))) {
464 $isError = FALSE;
465 }
466 }
467
468 if ($isError) {
469 $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'])));
470 }
471
472 return $errors;
473 }
474
475 function buildChart(&$rows) {
476 if (empty($rows)) {
477 return;
478 }
479
480 $criterias = self::getChartCriteria();
481
482 $chartInfo = array('legend' => ts('Mail Summary'),
483 'xname' => ts('Mailing'),
484 'yname' => ts('Statistics'),
485 'xLabelAngle' => 20,
486 'tip' => array(),
487 );
488
489 $plotRate = $plotCount = TRUE;
490 foreach ($rows as $row) {
491 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
492 if ($plotCount) {
493 foreach ($criterias['count'] as $criteria => $label) {
494 if (isset($row[$criteria])) {
495 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
496 $chartInfo['tip'][$label] = "{$label} #val#";
497 $plotRate = FALSE;
498 }
499 elseif (isset($criterias['count'][$criteria])) {
500 unset($criterias['count'][$criteria]);
501 }
502 }
503 }
504 if ($plotRate) {
505 foreach ($criterias['rate'] as $criteria => $label) {
506 if (isset($row[$criteria])) {
507 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
508 $chartInfo['tip'][$label] = "{$label} #val#";
509 $plotCount = FALSE;
510 }
511 elseif (isset($criterias['rate'][$criteria])) {
512 unset($criterias['rate'][$criteria]);
513 }
514 }
515 }
516 }
517
518 if ($plotCount) {
519 $criterias = $criterias['count'];
520 }
521 else {
522 $criterias = $criterias['rate'];
523 }
524
525 $chartInfo['criteria'] = array_values($criterias);
526
527 // dynamically set the graph size
528 $chartInfo['xSize'] = ((count($rows) * 125) + (count($rows) * count($criterias) * 40));
529
530 // build the chart.
531 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
532 $this->assign('chartType', $this->_params['charts']);
533 }
534
535 function alterDisplay(&$rows) {
536 // custom code to alter rows
537 $entryFound = FALSE;
538 foreach ($rows as $rowNum => $row) {
539 // make count columns point to detail report
540 // convert display name to links
541 if (array_key_exists('civicrm_contact_display_name', $row) &&
542 array_key_exists('civicrm_contact_id', $row)
543 ) {
544 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
545 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
546 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
547 );
548 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
549 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
550 $entryFound = TRUE;
551 }
552
553 // handle country
554 if (array_key_exists('civicrm_address_country_id', $row)) {
555 if ($value = $row['civicrm_address_country_id']) {
556 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
557 }
558 $entryFound = TRUE;
559 }
560 if (array_key_exists('civicrm_address_state_province_id', $row)) {
561 if ($value = $row['civicrm_address_state_province_id']) {
562 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
563 }
564 $entryFound = TRUE;
565 }
566
567
568 // skip looking further in rows, if first row itself doesn't
569 // have the column we need
570 if (!$entryFound) {
571 break;
572 }
573 }
574 }
575}
576