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 function __construct() {
55 $this->_columns
= array();
57 $this->_columns
['civicrm_mailing'] = array(
58 'dao' => 'CRM_Mailing_DAO_Mailing',
61 'title' => ts('Mailing Name'),
64 'created_date' => array(
65 'title' => ts('Date Created'),
68 'title' => ts('Subject'),
72 'is_completed' => array(
73 'title' => ts('Mailing Status'),
74 'operatorType' => CRM_Report_Form
::OP_SELECT
,
75 'type' => CRM_Utils_Type
::T_INT
,
80 //'operator' => 'like',
83 'mailing_name' => array(
85 'title' => ts('Mailing'),
86 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
87 'type' => CRM_Utils_Type
::T_STRING
,
88 'options' => self
::mailing_select(),
94 $this->_columns
['civicrm_mailing_job'] = array(
95 'dao' => 'CRM_Mailing_DAO_MailingJob',
97 'start_date' => array(
98 'title' => ts('Start Date'),
101 'title' => ts('End Date'),
106 'type' => CRM_Utils_Type
::T_STRING
,
107 'default' => 'Complete',
108 'no_display' => TRUE,
111 'type' => CRM_Utils_Type
::T_INT
,
113 'no_display' => TRUE,
115 'start_date' => array(
116 'title' => ts('Start Date'),
117 'default' => 'this.year',
118 'operatorType' => CRM_Report_Form
::OP_DATE
,
119 'type' => CRM_Utils_Type
::T_DATE
,
122 'title' => ts('End Date'),
123 'default' => 'this.year',
124 'operatorType' => CRM_Report_Form
::OP_DATE
,
125 'type' => CRM_Utils_Type
::T_DATE
,
130 $this->_columns
['civicrm_mailing_event_queue'] = array(
131 'dao' => 'CRM_Mailing_DAO_Mailing',
133 'queue_count' => array(
135 'title' => ts('Intended Recipients'),
140 $this->_columns
['civicrm_mailing_event_delivered'] = array(
141 'dao' => 'CRM_Mailing_DAO_Mailing',
143 'delivered_count' => array(
145 'title' => ts('Delivered'),
147 'accepted_rate' => array(
148 'title' => 'Accepted Rate',
149 'statistics' => array(
150 'calc' => 'PERCENTAGE',
151 'top' => 'civicrm_mailing_event_delivered.delivered_count',
152 'base' => 'civicrm_mailing_event_queue.queue_count',
158 $this->_columns
['civicrm_mailing_event_bounce'] = array(
159 'dao' => 'CRM_Mailing_DAO_Mailing',
161 'bounce_count' => array(
163 'title' => ts('Bounce'),
165 'bounce_rate' => array(
166 'title' => 'Bounce Rate',
167 'statistics' => array(
168 'calc' => 'PERCENTAGE',
169 'top' => 'civicrm_mailing_event_bounce.bounce_count',
170 'base' => 'civicrm_mailing_event_queue.queue_count',
176 $this->_columns
['civicrm_mailing_event_opened'] = array(
177 'dao' => 'CRM_Mailing_DAO_Mailing',
179 'open_count' => array(
181 'title' => ts('Opened'),
183 'open_rate' => array(
184 'title' => 'Confirmed Open Rate',
185 'statistics' => array(
186 'calc' => 'PERCENTAGE',
187 'top' => 'civicrm_mailing_event_opened.open_count',
188 'base' => 'civicrm_mailing_event_delivered.delivered_count',
194 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = array(
195 'dao' => 'CRM_Mailing_DAO_Mailing',
197 'click_count' => array(
199 'title' => ts('Clicks'),
202 'title' => 'Click through Rate',
204 'statistics' => array(
205 'calc' => 'PERCENTAGE',
206 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
207 'base' => 'civicrm_mailing_event_delivered.delivered_count',
211 'title' => 'Click to Open Rate',
213 'statistics' => array(
214 'calc' => 'PERCENTAGE',
215 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
216 'base' => 'civicrm_mailing_event_opened.open_count',
222 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
223 'dao' => 'CRM_Mailing_DAO_Mailing',
225 'unsubscribe_count' => array(
227 'title' => ts('Unsubscribe'),
232 parent
::__construct();
235 function mailing_select() {
239 $mailing = new CRM_Mailing_BAO_Mailing();
240 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
241 $mailing->query($query);
243 while ($mailing->fetch()) {
244 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
250 function preProcess() {
251 $this->assign('chartSupported', TRUE);
252 parent
::preProcess();
255 // manipulate the select function to query count functions
258 $count_tables = array(
259 'civicrm_mailing_event_queue',
260 'civicrm_mailing_event_delivered',
261 'civicrm_mailing_event_opened',
262 'civicrm_mailing_event_bounce',
263 'civicrm_mailing_event_trackable_url_open',
264 'civicrm_mailing_event_unsubscribe',
268 $this->_columnHeaders
= array();
269 foreach ($this->_columns
as $tableName => $table) {
270 if (array_key_exists('fields', $table)) {
271 foreach ($table['fields'] as $fieldName => $field) {
272 if (CRM_Utils_Array
::value('required', $field) ||
273 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
277 if (CRM_Utils_Array
::value('statistics', $field)) {
278 switch ($field['statistics']['calc']) {
280 $base_table_column = explode('.', $field['statistics']['base']);
281 $top_table_column = explode('.', $field['statistics']['top']);
283 $select[] = "CONCAT(round(
284 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
285 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
286 ), '%') as {$tableName}_{$fieldName}";
291 if (in_array($tableName, $count_tables)) {
292 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
295 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
298 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
299 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
305 $this->_select
= "SELECT " . implode(', ', $select) . " ";
306 //print_r($this->_select);
312 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
313 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
314 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
315 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
316 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
317 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
318 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
319 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
320 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
321 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
322 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
323 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
324 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
325 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
326 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
327 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id";
328 // need group by and order by
330 //print_r($this->_from);
335 //to avoid the sms listings
336 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
338 foreach ($this->_columns
as $tableName => $table) {
339 if (array_key_exists('filters', $table)) {
340 foreach ($table['filters'] as $fieldName => $field) {
342 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
343 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
344 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
345 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
347 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
350 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
353 if ($fieldName == 'relationship_type_id') {
354 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
357 $clause = $this->whereClause($field,
359 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
360 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
361 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
367 if (!empty($clause)) {
368 $clauses[] = $clause;
374 if (empty($clauses)) {
375 $this->_where
= "WHERE ( 1 )";
378 $this->_where
= "WHERE " . implode(' AND ', $clauses);
381 // if ( $this->_aclWhere ) {
382 // $this->_where .= " AND {$this->_aclWhere} ";
387 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
391 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
394 function postProcess() {
396 $this->beginPostProcess();
398 // get the acl clauses built before we assemble the query
399 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
401 $sql = $this->buildQuery(TRUE);
405 $rows = $graphRows = array();
406 $this->buildRows($sql, $rows);
408 $this->formatDisplay($rows);
409 $this->doTemplateAssignment($rows);
410 $this->endPostProcess($rows);
413 static function getChartCriteria() {
414 return array('count' => array('civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
415 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
416 'civicrm_mailing_event_opened_open_count' => ts('Opened'),
417 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
418 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
420 'rate' => array('civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
421 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
422 'civicrm_mailing_event_opened_open_rate' => ts('Confirmed Open Rate'),
423 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
424 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
429 function formRule($fields, $files, $self) {
432 if (!CRM_Utils_Array
::value('charts', $fields)) {
436 $criterias = self
::getChartCriteria();
438 foreach ($fields['fields'] as $fld => $isActive) {
439 if (in_array($fld, array(
440 'delivered_count', 'bounce_count', 'open_count', 'click_count', 'unsubscribe_count', 'accepted_rate', 'bounce_rate', 'open_rate', 'CTR', 'CTO'))) {
446 $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'])));
452 function buildChart(&$rows) {
457 $criterias = self
::getChartCriteria();
459 $chartInfo = array('legend' => ts('Mail Summary'),
460 'xname' => ts('Mailing'),
461 'yname' => ts('Statistics'),
466 $plotRate = $plotCount = TRUE;
467 foreach ($rows as $row) {
468 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
470 foreach ($criterias['count'] as $criteria => $label) {
471 if (isset($row[$criteria])) {
472 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
473 $chartInfo['tip'][$label] = "{$label} #val#";
476 elseif (isset($criterias['count'][$criteria])) {
477 unset($criterias['count'][$criteria]);
482 foreach ($criterias['rate'] as $criteria => $label) {
483 if (isset($row[$criteria])) {
484 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
485 $chartInfo['tip'][$label] = "{$label} #val#";
488 elseif (isset($criterias['rate'][$criteria])) {
489 unset($criterias['rate'][$criteria]);
496 $criterias = $criterias['count'];
499 $criterias = $criterias['rate'];
502 $chartInfo['criteria'] = array_values($criterias);
504 // dynamically set the graph size
505 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criterias) * 40));
508 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
509 $this->assign('chartType', $this->_params
['charts']);
512 function alterDisplay(&$rows) {
513 // custom code to alter rows
515 foreach ($rows as $rowNum => $row) {
516 // make count columns point to detail report
517 // convert display name to links
518 if (array_key_exists('civicrm_contact_display_name', $row) &&
519 array_key_exists('civicrm_contact_id', $row)
521 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
522 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
523 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
525 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
526 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
531 if (array_key_exists('civicrm_address_country_id', $row)) {
532 if ($value = $row['civicrm_address_country_id']) {
533 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
537 if (array_key_exists('civicrm_address_state_province_id', $row)) {
538 if ($value = $row['civicrm_address_state_province_id']) {
539 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
545 // skip looking further in rows, if first row itself doesn't
546 // have the column we need