4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
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 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;
56 function __construct() {
57 $this->_columns
= array();
59 $this->_columns
['civicrm_mailing'] = array(
60 'dao' => 'CRM_Mailing_DAO_Mailing',
63 'title' => ts('Mailing Name'),
66 'created_date' => array(
67 'title' => ts('Date Created'),
70 'title' => ts('Subject'),
74 'is_completed' => array(
75 'title' => ts('Mailing Status'),
76 'operatorType' => CRM_Report_Form
::OP_SELECT
,
77 'type' => CRM_Utils_Type
::T_INT
,
82 //'operator' => 'like',
85 'mailing_name' => array(
87 'title' => ts('Mailing'),
88 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
89 'type' => CRM_Utils_Type
::T_STRING
,
90 'options' => self
::mailing_select(),
96 $this->_columns
['civicrm_mailing_job'] = array(
97 'dao' => 'CRM_Mailing_DAO_MailingJob',
99 'start_date' => array(
100 'title' => ts('Start Date'),
103 'title' => ts('End Date'),
108 'type' => CRM_Utils_Type
::T_STRING
,
109 'default' => 'Complete',
110 'no_display' => TRUE,
113 'type' => CRM_Utils_Type
::T_INT
,
115 'no_display' => TRUE,
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
,
124 'title' => ts('End Date'),
125 'default' => 'this.year',
126 'operatorType' => CRM_Report_Form
::OP_DATE
,
127 'type' => CRM_Utils_Type
::T_DATE
,
132 $this->_columns
['civicrm_mailing_event_queue'] = array(
133 'dao' => 'CRM_Mailing_DAO_Mailing',
135 'queue_count' => array(
137 'title' => ts('Intended Recipients'),
142 $this->_columns
['civicrm_mailing_event_delivered'] = array(
143 'dao' => 'CRM_Mailing_DAO_Mailing',
145 'delivered_count' => array(
147 'title' => ts('Delivered'),
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',
160 $this->_columns
['civicrm_mailing_event_bounce'] = array(
161 'dao' => 'CRM_Mailing_DAO_Mailing',
163 'bounce_count' => array(
165 'title' => ts('Bounce'),
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',
178 $this->_columns
['civicrm_mailing_event_opened'] = array(
179 'dao' => 'CRM_Mailing_DAO_Mailing',
181 'open_count' => array(
183 'title' => ts('Opened'),
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',
196 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = array(
197 'dao' => 'CRM_Mailing_DAO_Mailing',
199 'click_count' => array(
201 'title' => ts('Clicks'),
204 'title' => 'Click through Rate',
206 'statistics' => array(
207 'calc' => 'PERCENTAGE',
208 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
209 'base' => 'civicrm_mailing_event_delivered.delivered_count',
213 'title' => 'Click to Open Rate',
215 'statistics' => array(
216 'calc' => 'PERCENTAGE',
217 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
218 'base' => 'civicrm_mailing_event_opened.open_count',
224 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
225 'dao' => 'CRM_Mailing_DAO_Mailing',
227 'unsubscribe_count' => array(
229 'title' => ts('Unsubscribe'),
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',
240 'title' => ts('Campaign Name'),
245 'type' => CRM_Utils_Type
::T_STRING
,
250 parent
::__construct();
253 function mailing_select() {
257 $mailing = new CRM_Mailing_BAO_Mailing();
258 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
259 $mailing->query($query);
261 while ($mailing->fetch()) {
262 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
268 function preProcess() {
269 $this->assign('chartSupported', TRUE);
270 parent
::preProcess();
273 // manipulate the select function to query count functions
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',
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 (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
293 if (!empty($field['statistics'])) {
294 switch ($field['statistics']['calc']) {
296 $base_table_column = explode('.', $field['statistics']['base']);
297 $top_table_column = explode('.', $field['statistics']['top']);
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}";
307 if (in_array($tableName, $count_tables)) {
308 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
311 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
314 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
315 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
321 $this->_select
= "SELECT " . implode(', ', $select) . " ";
322 //print_r($this->_select);
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";
345 if ($this->campaignEnabled
) {
347 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
348 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
351 // need group by and order by
353 //print_r($this->_from);
358 //to avoid the sms listings
359 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
361 foreach ($this->_columns
as $tableName => $table) {
362 if (array_key_exists('filters', $table)) {
363 foreach ($table['filters'] as $fieldName => $field) {
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
);
370 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
373 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
376 if ($fieldName == 'relationship_type_id') {
377 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
380 $clause = $this->whereClause($field,
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
)
390 if (!empty($clause)) {
391 $clauses[] = $clause;
397 if (empty($clauses)) {
398 $this->_where
= "WHERE ( 1 )";
401 $this->_where
= "WHERE " . implode(' AND ', $clauses);
404 // if ( $this->_aclWhere ) {
405 // $this->_where .= " AND {$this->_aclWhere} ";
410 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
414 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
417 function postProcess() {
419 $this->beginPostProcess();
421 // get the acl clauses built before we assemble the query
422 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
424 $sql = $this->buildQuery(TRUE);
428 $rows = $graphRows = array();
429 $this->buildRows($sql, $rows);
431 $this->formatDisplay($rows);
432 $this->doTemplateAssignment($rows);
433 $this->endPostProcess($rows);
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'),
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'),
452 function formRule($fields, $files, $self) {
455 if (empty($fields['charts'])) {
459 $criterias = self
::getChartCriteria();
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'))) {
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'])));
475 function buildChart(&$rows) {
480 $criterias = self
::getChartCriteria();
482 $chartInfo = array('legend' => ts('Mail Summary'),
483 'xname' => ts('Mailing'),
484 'yname' => ts('Statistics'),
489 $plotRate = $plotCount = TRUE;
490 foreach ($rows as $row) {
491 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
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#";
499 elseif (isset($criterias['count'][$criteria])) {
500 unset($criterias['count'][$criteria]);
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#";
511 elseif (isset($criterias['rate'][$criteria])) {
512 unset($criterias['rate'][$criteria]);
519 $criterias = $criterias['count'];
522 $criterias = $criterias['rate'];
525 $chartInfo['criteria'] = array_values($criterias);
527 // dynamically set the graph size
528 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criterias) * 40));
531 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
532 $this->assign('chartType', $this->_params
['charts']);
535 function alterDisplay(&$rows) {
536 // custom code to alter rows
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)
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
548 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
549 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
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);
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);
568 // skip looking further in rows, if first row itself doesn't
569 // have the column we need