4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Mailing_Summary
extends CRM_Report_Form
{
38 protected $_summary = NULL;
40 # just a toggle we use to build the from
41 protected $_mailingidField = FALSE;
43 protected $_customGroupExtends = array();
45 protected $_add2groupSupported = FALSE;
47 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
49 protected $_charts = array(
51 'bar_3dChart' => 'Bar Chart',
54 public $campaignEnabled = False;
62 function __construct() {
63 $this->_columns
= array();
65 $this->_columns
['civicrm_mailing'] = array(
66 'dao' => 'CRM_Mailing_DAO_Mailing',
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(
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(
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 'open_count' => array(
189 'title' => ts('Opened'),
191 'open_rate' => array(
192 'title' => 'Confirmed Open Rate',
193 'statistics' => array(
194 'calc' => 'PERCENTAGE',
195 'top' => 'civicrm_mailing_event_opened.open_count',
196 'base' => 'civicrm_mailing_event_delivered.delivered_count',
202 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = array(
203 'dao' => 'CRM_Mailing_DAO_Mailing',
205 'click_count' => array(
207 'title' => ts('Clicks'),
210 'title' => 'Click through Rate',
212 'statistics' => array(
213 'calc' => 'PERCENTAGE',
214 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
215 'base' => 'civicrm_mailing_event_delivered.delivered_count',
219 'title' => 'Click to Open Rate',
221 'statistics' => array(
222 'calc' => 'PERCENTAGE',
223 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
224 'base' => 'civicrm_mailing_event_opened.open_count',
230 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
231 'dao' => 'CRM_Mailing_DAO_Mailing',
233 'unsubscribe_count' => array(
235 'title' => ts('Unsubscribe'),
239 $config = CRM_Core_Config
::singleton();
240 $this->campaignEnabled
= in_array("CiviCampaign", $config->enableComponents
);
241 if ($this->campaignEnabled
) {
242 $this->_columns
['civicrm_campaign'] = array(
243 'dao' => 'CRM_Campaign_DAO_Campaign',
246 'title' => ts('Campaign Name'),
251 'type' => CRM_Utils_Type
::T_STRING
,
256 parent
::__construct();
262 function mailing_select() {
266 $mailing = new CRM_Mailing_BAO_Mailing();
267 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
268 $mailing->query($query);
270 while ($mailing->fetch()) {
271 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
277 function preProcess() {
278 $this->assign('chartSupported', TRUE);
279 parent
::preProcess();
282 // manipulate the select function to query count functions
285 $count_tables = array(
286 'civicrm_mailing_event_queue',
287 'civicrm_mailing_event_delivered',
288 'civicrm_mailing_event_opened',
289 'civicrm_mailing_event_bounce',
290 'civicrm_mailing_event_trackable_url_open',
291 'civicrm_mailing_event_unsubscribe',
295 $this->_columnHeaders
= array();
296 foreach ($this->_columns
as $tableName => $table) {
297 if (array_key_exists('fields', $table)) {
298 foreach ($table['fields'] as $fieldName => $field) {
299 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
302 if (!empty($field['statistics'])) {
303 switch ($field['statistics']['calc']) {
305 $base_table_column = explode('.', $field['statistics']['base']);
306 $top_table_column = explode('.', $field['statistics']['top']);
308 $select[] = "CONCAT(round(
309 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
310 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
311 ), '%') as {$tableName}_{$fieldName}";
316 if (in_array($tableName, $count_tables)) {
317 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
320 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
323 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
324 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
330 $this->_select
= "SELECT " . implode(', ', $select) . " ";
331 //print_r($this->_select);
337 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
338 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
339 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
340 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
341 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
342 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
343 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
344 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
345 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
346 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
347 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
348 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
349 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
350 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
351 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
352 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id";
354 if ($this->campaignEnabled
) {
356 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
357 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
360 // need group by and order by
362 //print_r($this->_from);
367 //to avoid the sms listings
368 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
370 foreach ($this->_columns
as $tableName => $table) {
371 if (array_key_exists('filters', $table)) {
372 foreach ($table['filters'] as $fieldName => $field) {
374 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
375 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
376 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
377 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
379 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
382 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
385 if ($fieldName == 'relationship_type_id') {
386 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
389 $clause = $this->whereClause($field,
391 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
392 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
393 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
399 if (!empty($clause)) {
400 $clauses[] = $clause;
406 if (empty($clauses)) {
407 $this->_where
= "WHERE ( 1 )";
410 $this->_where
= "WHERE " . implode(' AND ', $clauses);
413 // if ( $this->_aclWhere ) {
414 // $this->_where .= " AND {$this->_aclWhere} ";
419 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
423 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
426 function postProcess() {
428 $this->beginPostProcess();
430 // get the acl clauses built before we assemble the query
431 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
433 $sql = $this->buildQuery(TRUE);
437 $rows = $graphRows = array();
438 $this->buildRows($sql, $rows);
440 $this->formatDisplay($rows);
441 $this->doTemplateAssignment($rows);
442 $this->endPostProcess($rows);
448 static function getChartCriteria() {
449 return array('count' => array('civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
450 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
451 'civicrm_mailing_event_opened_open_count' => ts('Opened'),
452 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
453 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
455 'rate' => array('civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
456 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
457 'civicrm_mailing_event_opened_open_rate' => ts('Confirmed Open Rate'),
458 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
459 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
471 function formRule($fields, $files, $self) {
474 if (empty($fields['charts'])) {
478 $criterias = self
::getChartCriteria();
480 foreach ($fields['fields'] as $fld => $isActive) {
481 if (in_array($fld, array(
482 'delivered_count', 'bounce_count', 'open_count', 'click_count', 'unsubscribe_count', 'accepted_rate', 'bounce_rate', 'open_rate', 'CTR', 'CTO'))) {
488 $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'])));
497 function buildChart(&$rows) {
502 $criterias = self
::getChartCriteria();
504 $chartInfo = array('legend' => ts('Mail Summary'),
505 'xname' => ts('Mailing'),
506 'yname' => ts('Statistics'),
511 $plotRate = $plotCount = TRUE;
512 foreach ($rows as $row) {
513 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
515 foreach ($criterias['count'] as $criteria => $label) {
516 if (isset($row[$criteria])) {
517 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
518 $chartInfo['tip'][$label] = "{$label} #val#";
521 elseif (isset($criterias['count'][$criteria])) {
522 unset($criterias['count'][$criteria]);
527 foreach ($criterias['rate'] as $criteria => $label) {
528 if (isset($row[$criteria])) {
529 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
530 $chartInfo['tip'][$label] = "{$label} #val#";
533 elseif (isset($criterias['rate'][$criteria])) {
534 unset($criterias['rate'][$criteria]);
541 $criterias = $criterias['count'];
544 $criterias = $criterias['rate'];
547 $chartInfo['criteria'] = array_values($criterias);
549 // dynamically set the graph size
550 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criterias) * 40));
553 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
554 $this->assign('chartType', $this->_params
['charts']);
560 function alterDisplay(&$rows) {
561 // custom code to alter rows
563 foreach ($rows as $rowNum => $row) {
564 // make count columns point to detail report
565 // convert display name to links
566 if (array_key_exists('civicrm_contact_display_name', $row) &&
567 array_key_exists('civicrm_contact_id', $row)
569 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
570 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
571 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
573 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
574 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
579 if (array_key_exists('civicrm_address_country_id', $row)) {
580 if ($value = $row['civicrm_address_country_id']) {
581 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
585 if (array_key_exists('civicrm_address_state_province_id', $row)) {
586 if ($value = $row['civicrm_address_state_province_id']) {
587 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
593 // skip looking further in rows, if first row itself doesn't
594 // have the column we need