3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
35 class CRM_Report_Form_Mailing_Summary
extends CRM_Report_Form
{
37 protected $_summary = NULL;
39 protected $_customGroupExtends = array();
41 protected $_add2groupSupported = FALSE;
43 public $_drilldownReport = array('mailing/detail' => 'Link to Detail Report');
45 protected $_charts = array(
47 'bar_3dChart' => 'Bar Chart',
50 public $campaignEnabled = FALSE;
56 public function __construct() {
57 $this->_columns
= array();
59 $this->_columns
['civicrm_mailing'] = array(
60 'dao' => 'CRM_Mailing_DAO_Mailing',
64 'title' => ts('Mailing ID'),
69 'title' => ts('Mailing Name'),
72 'created_date' => array(
73 'title' => ts('Date Created'),
76 'title' => ts('Subject'),
80 'is_completed' => array(
81 'title' => ts('Mailing Status'),
82 'operatorType' => CRM_Report_Form
::OP_SELECT
,
83 'type' => CRM_Utils_Type
::T_INT
,
88 //'operator' => 'like',
91 'mailing_name' => array(
93 'title' => ts('Mailing'),
94 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
95 'type' => CRM_Utils_Type
::T_STRING
,
96 'options' => self
::mailing_select(),
102 $this->_columns
['civicrm_mailing_job'] = array(
103 'dao' => 'CRM_Mailing_DAO_MailingJob',
105 'start_date' => array(
106 'title' => ts('Start Date'),
109 'title' => ts('End Date'),
114 'type' => CRM_Utils_Type
::T_STRING
,
115 'default' => 'Complete',
116 'no_display' => TRUE,
119 'type' => CRM_Utils_Type
::T_INT
,
121 'no_display' => TRUE,
123 'start_date' => array(
124 'title' => ts('Start Date'),
125 'default' => 'this.year',
126 'operatorType' => CRM_Report_Form
::OP_DATE
,
127 'type' => CRM_Utils_Type
::T_DATE
,
130 'title' => ts('End Date'),
131 'default' => 'this.year',
132 'operatorType' => CRM_Report_Form
::OP_DATE
,
133 'type' => CRM_Utils_Type
::T_DATE
,
138 $this->_columns
['civicrm_mailing_event_queue'] = array(
139 'dao' => 'CRM_Mailing_DAO_Mailing',
141 'queue_count' => array(
143 'title' => ts('Intended Recipients'),
148 $this->_columns
['civicrm_mailing_event_delivered'] = array(
149 'dao' => 'CRM_Mailing_DAO_Mailing',
151 'delivered_count' => array(
152 'name' => 'event_queue_id',
153 'title' => ts('Delivered'),
155 'accepted_rate' => array(
156 'title' => 'Accepted Rate',
157 'statistics' => array(
158 'calc' => 'PERCENTAGE',
159 'top' => 'civicrm_mailing_event_delivered.delivered_count',
160 'base' => 'civicrm_mailing_event_queue.queue_count',
166 $this->_columns
['civicrm_mailing_event_bounce'] = array(
167 'dao' => 'CRM_Mailing_DAO_Mailing',
169 'bounce_count' => array(
170 'name' => 'event_queue_id',
171 'title' => ts('Bounce'),
173 'bounce_rate' => array(
174 'title' => 'Bounce Rate',
175 'statistics' => array(
176 'calc' => 'PERCENTAGE',
177 'top' => 'civicrm_mailing_event_bounce.bounce_count',
178 'base' => 'civicrm_mailing_event_queue.queue_count',
184 $this->_columns
['civicrm_mailing_event_opened'] = array(
185 'dao' => 'CRM_Mailing_DAO_Mailing',
187 'unique_open_count' => array(
189 'alias' => 'mailing_event_opened_civireport',
190 'dbAlias' => 'mailing_event_opened_civireport.event_queue_id',
191 'title' => ts('Unique Opens'),
193 'unique_open_rate' => array(
194 'title' => 'Unique Open Rate',
195 'statistics' => array(
196 'calc' => 'PERCENTAGE',
197 'top' => 'civicrm_mailing_event_opened.unique_open_count',
198 'base' => 'civicrm_mailing_event_delivered.delivered_count',
201 'open_count' => array(
202 'name' => 'event_queue_id',
203 'title' => ts('Total Opens'),
205 'open_rate' => array(
206 'title' => 'Total Open Rate',
207 'statistics' => array(
208 'calc' => 'PERCENTAGE',
209 'top' => 'civicrm_mailing_event_opened.open_count',
210 'base' => 'civicrm_mailing_event_delivered.delivered_count',
216 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = array(
217 'dao' => 'CRM_Mailing_DAO_Mailing',
219 'click_count' => array(
220 'name' => 'event_queue_id',
221 'title' => ts('Clicks'),
224 'title' => 'Click through Rate',
226 'statistics' => array(
227 'calc' => 'PERCENTAGE',
228 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
229 'base' => 'civicrm_mailing_event_delivered.delivered_count',
233 'title' => 'Click to Open Rate',
235 'statistics' => array(
236 'calc' => 'PERCENTAGE',
237 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
238 'base' => 'civicrm_mailing_event_opened.open_count',
244 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
245 'dao' => 'CRM_Mailing_DAO_Mailing',
247 'unsubscribe_count' => array(
249 'title' => ts('Unsubscribe'),
250 'alias' => 'mailing_event_unsubscribe_civireport',
251 'dbAlias' => 'mailing_event_unsubscribe_civireport.event_queue_id',
253 'optout_count' => array(
255 'title' => ts('Opt-outs'),
256 'alias' => 'mailing_event_optout_civireport',
257 'dbAlias' => 'mailing_event_optout_civireport.event_queue_id',
261 $config = CRM_Core_Config
::singleton();
262 $this->campaignEnabled
= in_array("CiviCampaign", $config->enableComponents
);
263 if ($this->campaignEnabled
) {
264 $this->_columns
['civicrm_campaign'] = array(
265 'dao' => 'CRM_Campaign_DAO_Campaign',
268 'title' => ts('Campaign Name'),
273 'type' => CRM_Utils_Type
::T_STRING
,
278 parent
::__construct();
284 public function mailing_select() {
288 $mailing = new CRM_Mailing_BAO_Mailing();
289 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
290 $mailing->query($query);
292 while ($mailing->fetch()) {
293 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
299 public function preProcess() {
300 $this->assign('chartSupported', TRUE);
301 parent
::preProcess();
305 * manipulate the select function to query count functions.
307 public function select() {
309 $count_tables = array(
310 'civicrm_mailing_event_queue',
311 'civicrm_mailing_event_delivered',
312 'civicrm_mailing_event_opened',
313 'civicrm_mailing_event_bounce',
314 'civicrm_mailing_event_trackable_url_open',
315 'civicrm_mailing_event_unsubscribe',
319 $this->_columnHeaders
= array();
320 foreach ($this->_columns
as $tableName => $table) {
321 if (array_key_exists('fields', $table)) {
322 foreach ($table['fields'] as $fieldName => $field) {
323 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
326 if (!empty($field['statistics'])) {
327 switch ($field['statistics']['calc']) {
329 $base_table_column = explode('.', $field['statistics']['base']);
330 $top_table_column = explode('.', $field['statistics']['top']);
332 $select[] = "CONCAT(round(
333 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
334 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
335 ), '%') as {$tableName}_{$fieldName}";
340 if (in_array($tableName, $count_tables)) {
341 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
344 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
347 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
348 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
354 $this->_select
= "SELECT " . implode(', ', $select) . " ";
355 //print_r($this->_select);
358 public function from() {
361 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
362 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
363 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
364 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
365 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
366 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
367 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
368 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
369 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
370 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
371 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
372 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
373 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
374 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
375 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
376 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}.org_unsubscribe = 0
377 LEFT JOIN civicrm_mailing_event_unsubscribe mailing_event_optout_civireport
378 ON mailing_event_optout_civireport.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id AND mailing_event_optout_civireport.org_unsubscribe = 1";
380 if ($this->campaignEnabled
) {
382 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
383 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
386 // need group by and order by
388 //print_r($this->_from);
391 public function where() {
393 //to avoid the sms listings
394 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
396 foreach ($this->_columns
as $tableName => $table) {
397 if (array_key_exists('filters', $table)) {
398 foreach ($table['filters'] as $fieldName => $field) {
400 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
401 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
402 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
403 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
405 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
408 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
411 if ($fieldName == 'relationship_type_id') {
412 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
415 $clause = $this->whereClause($field,
417 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
418 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
419 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
425 if (!empty($clause)) {
426 $clauses[] = $clause;
432 if (empty($clauses)) {
433 $this->_where
= "WHERE ( 1 )";
436 $this->_where
= "WHERE " . implode(' AND ', $clauses);
439 // if ( $this->_aclWhere ) {
440 // $this->_where .= " AND {$this->_aclWhere} ";
444 public function groupBy() {
445 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
448 public function orderBy() {
449 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
452 public function postProcess() {
454 $this->beginPostProcess();
456 // get the acl clauses built before we assemble the query
457 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
459 $sql = $this->buildQuery(TRUE);
463 $rows = $graphRows = array();
464 $this->buildRows($sql, $rows);
466 $this->formatDisplay($rows);
467 $this->doTemplateAssignment($rows);
468 $this->endPostProcess($rows);
474 public static function getChartCriteria() {
477 'civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
478 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
479 'civicrm_mailing_event_opened_open_count' => ts('Total Opens'),
480 'civicrm_mailing_event_opened_unique_open_count' => ts('Unique Opens'),
481 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
482 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
485 'civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
486 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
487 'civicrm_mailing_event_opened_open_rate' => ts('Total Open Rate'),
488 'civicrm_mailing_event_opened_unique_open_rate' => ts('Unique Open Rate'),
489 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
490 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
502 public static function formRule($fields, $files, $self) {
505 if (empty($fields['charts'])) {
509 $criteria = self
::getChartCriteria();
511 foreach ($fields['fields'] as $fld => $isActive) {
512 if (in_array($fld, array(
531 $errors['_qf_default'] = ts('For Chart view, please select at least one field from %1 OR %2.', array(
532 1 => implode(', ', $criteria['count']),
533 2 => implode(', ', $criteria['rate']),
543 public function buildChart(&$rows) {
548 $criteria = self
::getChartCriteria();
551 'legend' => ts('Mail Summary'),
552 'xname' => ts('Mailing'),
553 'yname' => ts('Statistics'),
558 $plotRate = $plotCount = TRUE;
559 foreach ($rows as $row) {
560 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
562 foreach ($criteria['count'] as $criteria => $label) {
563 if (isset($row[$criteria])) {
564 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
565 $chartInfo['tip'][$label] = "{$label} #val#";
568 elseif (isset($criteria['count'][$criteria])) {
569 unset($criteria['count'][$criteria]);
574 foreach ($criteria['rate'] as $criteria => $label) {
575 if (isset($row[$criteria])) {
576 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
577 $chartInfo['tip'][$label] = "{$label} #val#";
580 elseif (isset($criteria['rate'][$criteria])) {
581 unset($criteria['rate'][$criteria]);
588 $criteria = $criteria['count'];
591 $criteria = $criteria['rate'];
594 $chartInfo['criteria'] = array_values($criteria);
596 // dynamically set the graph size
597 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criteria) * 40));
600 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
601 $this->assign('chartType', $this->_params
['charts']);
605 * Alter display of rows.
607 * Iterate through the rows retrieved via SQL and make changes for display purposes,
608 * such as rendering contacts as links.
611 * Rows generated by SQL, with an array for each row.
613 public function alterDisplay(&$rows) {
615 foreach ($rows as $rowNum => $row) {
617 if (array_key_exists('civicrm_mailing_name', $row) &&
618 array_key_exists('civicrm_mailing_id', $row)
620 $rows[$rowNum]['civicrm_mailing_name_link'] = CRM_Report_Utils_Report
::getNextUrl('mailing/detail',
621 'reset=1&force=1&mailing_id_op=eq&mailing_id_value=' . $row['civicrm_mailing_id'],
622 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
624 $rows[$rowNum]['civicrm_mailing_name_hover'] = ts('View Mailing details for this mailing');
627 // skip looking further in rows, if first row itself doesn't
628 // have the column we need