3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Report_Form_Mailing_Summary
extends CRM_Report_Form
{
37 protected $_summary = NULL;
39 # just a toggle we use to build the from
40 protected $_mailingidField = FALSE;
42 protected $_customGroupExtends = array();
44 protected $_add2groupSupported = FALSE;
46 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
48 protected $_charts = array(
50 'bar_3dChart' => 'Bar Chart',
53 public $campaignEnabled = FALSE;
59 public function __construct() {
60 $this->_columns
= array();
62 $this->_columns
['civicrm_mailing'] = array(
63 'dao' => 'CRM_Mailing_DAO_Mailing',
66 'title' => ts('Mailing Name'),
69 'created_date' => array(
70 'title' => ts('Date Created'),
73 'title' => ts('Subject'),
77 'is_completed' => array(
78 'title' => ts('Mailing Status'),
79 'operatorType' => CRM_Report_Form
::OP_SELECT
,
80 'type' => CRM_Utils_Type
::T_INT
,
85 //'operator' => 'like',
88 'mailing_name' => array(
90 'title' => ts('Mailing'),
91 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
92 'type' => CRM_Utils_Type
::T_STRING
,
93 'options' => self
::mailing_select(),
99 $this->_columns
['civicrm_mailing_job'] = array(
100 'dao' => 'CRM_Mailing_DAO_MailingJob',
102 'start_date' => array(
103 'title' => ts('Start Date'),
106 'title' => ts('End Date'),
111 'type' => CRM_Utils_Type
::T_STRING
,
112 'default' => 'Complete',
113 'no_display' => TRUE,
116 'type' => CRM_Utils_Type
::T_INT
,
118 'no_display' => TRUE,
120 'start_date' => array(
121 'title' => ts('Start Date'),
122 'default' => 'this.year',
123 'operatorType' => CRM_Report_Form
::OP_DATE
,
124 'type' => CRM_Utils_Type
::T_DATE
,
127 'title' => ts('End Date'),
128 'default' => 'this.year',
129 'operatorType' => CRM_Report_Form
::OP_DATE
,
130 'type' => CRM_Utils_Type
::T_DATE
,
135 $this->_columns
['civicrm_mailing_event_queue'] = array(
136 'dao' => 'CRM_Mailing_DAO_Mailing',
138 'queue_count' => array(
140 'title' => ts('Intended Recipients'),
145 $this->_columns
['civicrm_mailing_event_delivered'] = array(
146 'dao' => 'CRM_Mailing_DAO_Mailing',
148 'delivered_count' => array(
150 'title' => ts('Delivered'),
152 'accepted_rate' => array(
153 'title' => 'Accepted Rate',
154 'statistics' => array(
155 'calc' => 'PERCENTAGE',
156 'top' => 'civicrm_mailing_event_delivered.delivered_count',
157 'base' => 'civicrm_mailing_event_queue.queue_count',
163 $this->_columns
['civicrm_mailing_event_bounce'] = array(
164 'dao' => 'CRM_Mailing_DAO_Mailing',
166 'bounce_count' => array(
168 'title' => ts('Bounce'),
170 'bounce_rate' => array(
171 'title' => 'Bounce Rate',
172 'statistics' => array(
173 'calc' => 'PERCENTAGE',
174 'top' => 'civicrm_mailing_event_bounce.bounce_count',
175 'base' => 'civicrm_mailing_event_queue.queue_count',
181 $this->_columns
['civicrm_mailing_event_opened'] = array(
182 'dao' => 'CRM_Mailing_DAO_Mailing',
184 'open_count' => array(
186 'title' => ts('Opened'),
188 'open_rate' => array(
189 'title' => 'Confirmed Open Rate',
190 'statistics' => array(
191 'calc' => 'PERCENTAGE',
192 'top' => 'civicrm_mailing_event_opened.open_count',
193 'base' => 'civicrm_mailing_event_delivered.delivered_count',
199 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = array(
200 'dao' => 'CRM_Mailing_DAO_Mailing',
202 'click_count' => array(
204 'title' => ts('Clicks'),
207 'title' => 'Click through Rate',
209 'statistics' => array(
210 'calc' => 'PERCENTAGE',
211 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
212 'base' => 'civicrm_mailing_event_delivered.delivered_count',
216 'title' => 'Click to Open Rate',
218 'statistics' => array(
219 'calc' => 'PERCENTAGE',
220 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
221 'base' => 'civicrm_mailing_event_opened.open_count',
227 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
228 'dao' => 'CRM_Mailing_DAO_Mailing',
230 'unsubscribe_count' => array(
232 'title' => ts('Unsubscribe'),
236 $config = CRM_Core_Config
::singleton();
237 $this->campaignEnabled
= in_array("CiviCampaign", $config->enableComponents
);
238 if ($this->campaignEnabled
) {
239 $this->_columns
['civicrm_campaign'] = array(
240 'dao' => 'CRM_Campaign_DAO_Campaign',
243 'title' => ts('Campaign Name'),
248 'type' => CRM_Utils_Type
::T_STRING
,
253 parent
::__construct();
259 public function mailing_select() {
263 $mailing = new CRM_Mailing_BAO_Mailing();
264 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
265 $mailing->query($query);
267 while ($mailing->fetch()) {
268 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
274 public function preProcess() {
275 $this->assign('chartSupported', TRUE);
276 parent
::preProcess();
280 * manipulate the select function to query count functions.
282 public function select() {
284 $count_tables = array(
285 'civicrm_mailing_event_queue',
286 'civicrm_mailing_event_delivered',
287 'civicrm_mailing_event_opened',
288 'civicrm_mailing_event_bounce',
289 'civicrm_mailing_event_trackable_url_open',
290 'civicrm_mailing_event_unsubscribe',
294 $this->_columnHeaders
= array();
295 foreach ($this->_columns
as $tableName => $table) {
296 if (array_key_exists('fields', $table)) {
297 foreach ($table['fields'] as $fieldName => $field) {
298 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
301 if (!empty($field['statistics'])) {
302 switch ($field['statistics']['calc']) {
304 $base_table_column = explode('.', $field['statistics']['base']);
305 $top_table_column = explode('.', $field['statistics']['top']);
307 $select[] = "CONCAT(round(
308 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
309 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
310 ), '%') as {$tableName}_{$fieldName}";
315 if (in_array($tableName, $count_tables)) {
316 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
319 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
322 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
323 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
329 $this->_select
= "SELECT " . implode(', ', $select) . " ";
330 //print_r($this->_select);
333 public function from() {
336 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
337 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
338 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
339 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
340 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
341 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
342 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
343 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
344 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
345 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
346 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
347 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
348 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
349 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
350 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
351 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id";
353 if ($this->campaignEnabled
) {
355 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
356 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
359 // need group by and order by
361 //print_r($this->_from);
364 public function where() {
366 //to avoid the sms listings
367 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
369 foreach ($this->_columns
as $tableName => $table) {
370 if (array_key_exists('filters', $table)) {
371 foreach ($table['filters'] as $fieldName => $field) {
373 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
374 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
375 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
376 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
378 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
381 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
384 if ($fieldName == 'relationship_type_id') {
385 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
388 $clause = $this->whereClause($field,
390 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
391 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
392 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
398 if (!empty($clause)) {
399 $clauses[] = $clause;
405 if (empty($clauses)) {
406 $this->_where
= "WHERE ( 1 )";
409 $this->_where
= "WHERE " . implode(' AND ', $clauses);
412 // if ( $this->_aclWhere ) {
413 // $this->_where .= " AND {$this->_aclWhere} ";
417 public function groupBy() {
418 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
421 public function orderBy() {
422 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
425 public function postProcess() {
427 $this->beginPostProcess();
429 // get the acl clauses built before we assemble the query
430 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
432 $sql = $this->buildQuery(TRUE);
436 $rows = $graphRows = array();
437 $this->buildRows($sql, $rows);
439 $this->formatDisplay($rows);
440 $this->doTemplateAssignment($rows);
441 $this->endPostProcess($rows);
447 public static function getChartCriteria() {
450 'civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
451 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
452 'civicrm_mailing_event_opened_open_count' => ts('Opened'),
453 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
454 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
457 'civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
458 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
459 'civicrm_mailing_event_opened_open_rate' => ts('Confirmed Open Rate'),
460 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
461 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
473 public function formRule($fields, $files, $self) {
476 if (empty($fields['charts'])) {
480 $criteria = self
::getChartCriteria();
482 foreach ($fields['fields'] as $fld => $isActive) {
483 if (in_array($fld, array(
500 $errors['_qf_default'] = ts('For Chart view, please select at least one field from %1 OR %2.', array(
501 1 => implode(', ', $criteria['count']),
502 2 => implode(', ', $criteria['rate']),
512 public function buildChart(&$rows) {
517 $criteria = self
::getChartCriteria();
520 'legend' => ts('Mail Summary'),
521 'xname' => ts('Mailing'),
522 'yname' => ts('Statistics'),
527 $plotRate = $plotCount = TRUE;
528 foreach ($rows as $row) {
529 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
531 foreach ($criteria['count'] as $criteria => $label) {
532 if (isset($row[$criteria])) {
533 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
534 $chartInfo['tip'][$label] = "{$label} #val#";
537 elseif (isset($criteria['count'][$criteria])) {
538 unset($criteria['count'][$criteria]);
543 foreach ($criteria['rate'] as $criteria => $label) {
544 if (isset($row[$criteria])) {
545 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
546 $chartInfo['tip'][$label] = "{$label} #val#";
549 elseif (isset($criteria['rate'][$criteria])) {
550 unset($criteria['rate'][$criteria]);
557 $criteria = $criteria['count'];
560 $criteria = $criteria['rate'];
563 $chartInfo['criteria'] = array_values($criteria);
565 // dynamically set the graph size
566 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criteria) * 40));
569 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
570 $this->assign('chartType', $this->_params
['charts']);
574 * Alter display of rows.
576 * Iterate through the rows retrieved via SQL and make changes for display purposes,
577 * such as rendering contacts as links.
580 * Rows generated by SQL, with an array for each row.
582 public function alterDisplay(&$rows) {
584 foreach ($rows as $rowNum => $row) {
585 // make count columns point to detail report
586 // convert display name to links
587 if (array_key_exists('civicrm_contact_display_name', $row) &&
588 array_key_exists('civicrm_contact_id', $row)
590 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
591 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
592 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
594 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
595 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
600 if (array_key_exists('civicrm_address_country_id', $row)) {
601 if ($value = $row['civicrm_address_country_id']) {
602 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
606 if (array_key_exists('civicrm_address_state_province_id', $row)) {
607 if ($value = $row['civicrm_address_state_province_id']) {
608 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
613 // skip looking further in rows, if first row itself doesn't
614 // have the column we need