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 (CRM_Utils_Array
::value('required', $field) ||
291 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
295 if (CRM_Utils_Array
::value('statistics', $field)) {
296 switch ($field['statistics']['calc']) {
298 $base_table_column = explode('.', $field['statistics']['base']);
299 $top_table_column = explode('.', $field['statistics']['top']);
301 $select[] = "CONCAT(round(
302 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
303 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
304 ), '%') as {$tableName}_{$fieldName}";
309 if (in_array($tableName, $count_tables)) {
310 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
313 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
316 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
317 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
323 $this->_select
= "SELECT " . implode(', ', $select) . " ";
324 //print_r($this->_select);
330 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
331 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
332 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
333 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
334 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
335 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
336 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
337 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
338 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
339 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
340 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
341 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
342 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
343 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
344 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
345 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id";
347 if ($this->campaignEnabled
) {
349 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
350 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
353 // need group by and order by
355 //print_r($this->_from);
360 //to avoid the sms listings
361 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
363 foreach ($this->_columns
as $tableName => $table) {
364 if (array_key_exists('filters', $table)) {
365 foreach ($table['filters'] as $fieldName => $field) {
367 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
368 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
369 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
370 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
372 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
375 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
378 if ($fieldName == 'relationship_type_id') {
379 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
382 $clause = $this->whereClause($field,
384 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
385 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
386 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
392 if (!empty($clause)) {
393 $clauses[] = $clause;
399 if (empty($clauses)) {
400 $this->_where
= "WHERE ( 1 )";
403 $this->_where
= "WHERE " . implode(' AND ', $clauses);
406 // if ( $this->_aclWhere ) {
407 // $this->_where .= " AND {$this->_aclWhere} ";
412 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
416 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
419 function postProcess() {
421 $this->beginPostProcess();
423 // get the acl clauses built before we assemble the query
424 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
426 $sql = $this->buildQuery(TRUE);
430 $rows = $graphRows = array();
431 $this->buildRows($sql, $rows);
433 $this->formatDisplay($rows);
434 $this->doTemplateAssignment($rows);
435 $this->endPostProcess($rows);
438 static function getChartCriteria() {
439 return array('count' => array('civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
440 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
441 'civicrm_mailing_event_opened_open_count' => ts('Opened'),
442 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
443 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
445 'rate' => array('civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
446 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
447 'civicrm_mailing_event_opened_open_rate' => ts('Confirmed Open Rate'),
448 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
449 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
454 function formRule($fields, $files, $self) {
457 if (!CRM_Utils_Array
::value('charts', $fields)) {
461 $criterias = self
::getChartCriteria();
463 foreach ($fields['fields'] as $fld => $isActive) {
464 if (in_array($fld, array(
465 'delivered_count', 'bounce_count', 'open_count', 'click_count', 'unsubscribe_count', 'accepted_rate', 'bounce_rate', 'open_rate', 'CTR', 'CTO'))) {
471 $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'])));
477 function buildChart(&$rows) {
482 $criterias = self
::getChartCriteria();
484 $chartInfo = array('legend' => ts('Mail Summary'),
485 'xname' => ts('Mailing'),
486 'yname' => ts('Statistics'),
491 $plotRate = $plotCount = TRUE;
492 foreach ($rows as $row) {
493 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
495 foreach ($criterias['count'] as $criteria => $label) {
496 if (isset($row[$criteria])) {
497 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
498 $chartInfo['tip'][$label] = "{$label} #val#";
501 elseif (isset($criterias['count'][$criteria])) {
502 unset($criterias['count'][$criteria]);
507 foreach ($criterias['rate'] as $criteria => $label) {
508 if (isset($row[$criteria])) {
509 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
510 $chartInfo['tip'][$label] = "{$label} #val#";
513 elseif (isset($criterias['rate'][$criteria])) {
514 unset($criterias['rate'][$criteria]);
521 $criterias = $criterias['count'];
524 $criterias = $criterias['rate'];
527 $chartInfo['criteria'] = array_values($criterias);
529 // dynamically set the graph size
530 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criterias) * 40));
533 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
534 $this->assign('chartType', $this->_params
['charts']);
537 function alterDisplay(&$rows) {
538 // custom code to alter rows
540 foreach ($rows as $rowNum => $row) {
541 // make count columns point to detail report
542 // convert display name to links
543 if (array_key_exists('civicrm_contact_display_name', $row) &&
544 array_key_exists('civicrm_contact_id', $row)
546 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
547 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
548 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
550 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
551 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
556 if (array_key_exists('civicrm_address_country_id', $row)) {
557 if ($value = $row['civicrm_address_country_id']) {
558 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
562 if (array_key_exists('civicrm_address_state_province_id', $row)) {
563 if ($value = $row['civicrm_address_state_province_id']) {
564 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
570 // skip looking further in rows, if first row itself doesn't
571 // have the column we need