4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
47 protected $_charts = array(
49 'bar_3dChart' => 'Bar Chart',
52 function __construct() {
53 $this->_columns
= array();
55 $this->_columns
['civicrm_mailing'] = array(
56 'dao' => 'CRM_Mailing_DAO_Mailing',
59 'title' => ts('Mailing Name'),
62 'created_date' => array(
63 'title' => ts('Date Created'),
67 'is_completed' => array(
68 'title' => ts('Mailing Status'),
69 'operatorType' => CRM_Report_Form
::OP_SELECT
,
70 'type' => CRM_Utils_Type
::T_INT
,
75 //'operator' => 'like',
78 'mailing_name' => array(
80 'title' => ts('Mailing'),
81 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
82 'type' => CRM_Utils_Type
::T_STRING
,
83 'options' => self
::mailing_select(),
89 $this->_columns
['civicrm_mailing_job'] = array(
90 'dao' => 'CRM_Mailing_DAO_Job',
92 'start_date' => array(
93 'title' => ts('Start Date'),
96 'title' => ts('End Date'),
101 'type' => CRM_Utils_Type
::T_STRING
,
102 'default' => 'Complete',
103 'no_display' => TRUE,
106 'type' => CRM_Utils_Type
::T_INT
,
108 'no_display' => TRUE,
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
,
117 'title' => ts('End Date'),
118 'default' => 'this.year',
119 'operatorType' => CRM_Report_Form
::OP_DATE
,
120 'type' => CRM_Utils_Type
::T_DATE
,
125 $this->_columns
['civicrm_mailing_event_queue'] = array(
126 'dao' => 'CRM_Mailing_DAO_Mailing',
128 'queue_count' => array(
130 'title' => ts('Intended Recipients'),
135 $this->_columns
['civicrm_mailing_event_delivered'] = array(
136 'dao' => 'CRM_Mailing_DAO_Mailing',
138 'delivered_count' => array(
140 'title' => ts('Delivered'),
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',
153 $this->_columns
['civicrm_mailing_event_bounce'] = array(
154 'dao' => 'CRM_Mailing_DAO_Mailing',
156 'bounce_count' => array(
158 'title' => ts('Bounce'),
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',
171 $this->_columns
['civicrm_mailing_event_opened'] = array(
172 'dao' => 'CRM_Mailing_DAO_Mailing',
174 'open_count' => array(
176 'title' => ts('Opened'),
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',
189 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = array(
190 'dao' => 'CRM_Mailing_DAO_Mailing',
192 'click_count' => array(
194 'title' => ts('Clicks'),
197 'title' => 'Click through Rate',
199 'statistics' => array(
200 'calc' => 'PERCENTAGE',
201 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
202 'base' => 'civicrm_mailing_event_delivered.delivered_count',
206 'title' => 'Click to Open Rate',
208 'statistics' => array(
209 'calc' => 'PERCENTAGE',
210 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
211 'base' => 'civicrm_mailing_event_opened.open_count',
217 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
218 'dao' => 'CRM_Mailing_DAO_Mailing',
220 'unsubscribe_count' => array(
222 'title' => ts('Unsubscribe'),
227 parent
::__construct();
230 function mailing_select() {
234 $mailing = new CRM_Mailing_BAO_Mailing();
235 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
236 $mailing->query($query);
238 while ($mailing->fetch()) {
239 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
245 function preProcess() {
246 $this->assign('chartSupported', TRUE);
247 parent
::preProcess();
250 // manipulate the select function to query count functions
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',
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'])
272 if (CRM_Utils_Array
::value('statistics', $field)) {
273 switch ($field['statistics']['calc']) {
275 $base_table_column = explode('.', $field['statistics']['base']);
276 $top_table_column = explode('.', $field['statistics']['top']);
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}";
286 if (in_array($tableName, $count_tables)) {
287 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
290 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
293 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
294 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
300 $this->_select
= "SELECT " . implode(', ', $select) . " ";
301 //print_r($this->_select);
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
325 //print_r($this->_from);
330 //to avoid the sms listings
331 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
333 foreach ($this->_columns
as $tableName => $table) {
334 if (array_key_exists('filters', $table)) {
335 foreach ($table['filters'] as $fieldName => $field) {
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
);
342 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
345 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
348 if ($fieldName == 'relationship_type_id') {
349 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
352 $clause = $this->whereClause($field,
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
)
362 if (!empty($clause)) {
363 $clauses[] = $clause;
369 if (empty($clauses)) {
370 $this->_where
= "WHERE ( 1 )";
373 $this->_where
= "WHERE " . implode(' AND ', $clauses);
376 // if ( $this->_aclWhere ) {
377 // $this->_where .= " AND {$this->_aclWhere} ";
382 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
386 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
389 function postProcess() {
391 $this->beginPostProcess();
393 // get the acl clauses built before we assemble the query
394 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
396 $sql = $this->buildQuery(TRUE);
400 $rows = $graphRows = array();
401 $this->buildRows($sql, $rows);
403 $this->formatDisplay($rows);
404 $this->doTemplateAssignment($rows);
405 $this->endPostProcess($rows);
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'),
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'),
424 function formRule($fields, $files, $self) {
427 if (!CRM_Utils_Array
::value('charts', $fields)) {
431 $criterias = self
::getChartCriteria();
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'))) {
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'])));
447 function buildChart(&$rows) {
452 $criterias = self
::getChartCriteria();
454 $chartInfo = array('legend' => ts('Mail Summary'),
455 'xname' => ts('Mailing'),
456 'yname' => ts('Statistics'),
461 $plotRate = $plotCount = TRUE;
462 foreach ($rows as $row) {
463 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
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#";
471 elseif (isset($criterias['count'][$criteria])) {
472 unset($criterias['count'][$criteria]);
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#";
483 elseif (isset($criterias['rate'][$criteria])) {
484 unset($criterias['rate'][$criteria]);
491 $criterias = $criterias['count'];
494 $criterias = $criterias['rate'];
497 $chartInfo['criteria'] = array_values($criterias);
499 // dynamically set the graph size
500 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criterias) * 40));
503 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
504 $this->assign('chartType', $this->_params
['charts']);
507 function alterDisplay(&$rows) {
508 // custom code to alter rows
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)
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
520 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
521 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
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);
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);
540 // skip looking further in rows, if first row itself doesn't
541 // have the column we need