3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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(
149 'name' => 'event_queue_id',
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(
167 'name' => 'event_queue_id',
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 'unique_open_count' => array(
186 'alias' => 'mailing_event_opened_civireport',
187 'dbAlias' => 'mailing_event_opened_civireport.event_queue_id',
188 'title' => ts('Unique Opens'),
190 'unique_open_rate' => array(
191 'title' => 'Unique Open Rate',
192 'statistics' => array(
193 'calc' => 'PERCENTAGE',
194 'top' => 'civicrm_mailing_event_opened.unique_open_count',
195 'base' => 'civicrm_mailing_event_delivered.delivered_count',
198 'open_count' => array(
199 'name' => 'event_queue_id',
200 'title' => ts('Total Opens'),
202 'open_rate' => array(
203 'title' => 'Total Open Rate',
204 'statistics' => array(
205 'calc' => 'PERCENTAGE',
206 'top' => 'civicrm_mailing_event_opened.open_count',
207 'base' => 'civicrm_mailing_event_delivered.delivered_count',
213 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = array(
214 'dao' => 'CRM_Mailing_DAO_Mailing',
216 'click_count' => array(
217 'name' => 'event_queue_id',
218 'title' => ts('Clicks'),
221 'title' => 'Click through Rate',
223 'statistics' => array(
224 'calc' => 'PERCENTAGE',
225 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
226 'base' => 'civicrm_mailing_event_delivered.delivered_count',
230 'title' => 'Click to Open Rate',
232 'statistics' => array(
233 'calc' => 'PERCENTAGE',
234 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
235 'base' => 'civicrm_mailing_event_opened.open_count',
241 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
242 'dao' => 'CRM_Mailing_DAO_Mailing',
244 'unsubscribe_count' => array(
246 'title' => ts('Unsubscribe'),
247 'alias' => 'mailing_event_unsubscribe_civireport',
248 'dbAlias' => 'mailing_event_unsubscribe_civireport.event_queue_id',
252 $config = CRM_Core_Config
::singleton();
253 $this->campaignEnabled
= in_array("CiviCampaign", $config->enableComponents
);
254 if ($this->campaignEnabled
) {
255 $this->_columns
['civicrm_campaign'] = array(
256 'dao' => 'CRM_Campaign_DAO_Campaign',
259 'title' => ts('Campaign Name'),
264 'type' => CRM_Utils_Type
::T_STRING
,
269 parent
::__construct();
275 public function mailing_select() {
279 $mailing = new CRM_Mailing_BAO_Mailing();
280 $query = "SELECT name FROM civicrm_mailing WHERE sms_provider_id IS NULL";
281 $mailing->query($query);
283 while ($mailing->fetch()) {
284 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
290 public function preProcess() {
291 $this->assign('chartSupported', TRUE);
292 parent
::preProcess();
296 * manipulate the select function to query count functions.
298 public function select() {
300 $count_tables = array(
301 'civicrm_mailing_event_queue',
302 'civicrm_mailing_event_delivered',
303 'civicrm_mailing_event_opened',
304 'civicrm_mailing_event_bounce',
305 'civicrm_mailing_event_trackable_url_open',
306 'civicrm_mailing_event_unsubscribe',
310 $this->_columnHeaders
= array();
311 foreach ($this->_columns
as $tableName => $table) {
312 if (array_key_exists('fields', $table)) {
313 foreach ($table['fields'] as $fieldName => $field) {
314 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
317 if (!empty($field['statistics'])) {
318 switch ($field['statistics']['calc']) {
320 $base_table_column = explode('.', $field['statistics']['base']);
321 $top_table_column = explode('.', $field['statistics']['top']);
323 $select[] = "CONCAT(round(
324 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
325 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
326 ), '%') as {$tableName}_{$fieldName}";
331 if (in_array($tableName, $count_tables)) {
332 $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}";
335 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
338 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
339 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
345 $this->_select
= "SELECT " . implode(', ', $select) . " ";
346 //print_r($this->_select);
349 public function from() {
352 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
353 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
354 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
355 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
356 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
357 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
358 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
359 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
360 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
361 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
362 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
363 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
364 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
365 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
366 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
367 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id";
369 if ($this->campaignEnabled
) {
371 LEFT JOIN civicrm_campaign {$this->_aliases['civicrm_campaign']}
372 ON {$this->_aliases['civicrm_campaign']}.id = {$this->_aliases['civicrm_mailing']}.campaign_id";
375 // need group by and order by
377 //print_r($this->_from);
380 public function where() {
382 //to avoid the sms listings
383 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
385 foreach ($this->_columns
as $tableName => $table) {
386 if (array_key_exists('filters', $table)) {
387 foreach ($table['filters'] as $fieldName => $field) {
389 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
390 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
391 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
392 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
394 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
397 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
400 if ($fieldName == 'relationship_type_id') {
401 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
404 $clause = $this->whereClause($field,
406 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
407 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
408 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
414 if (!empty($clause)) {
415 $clauses[] = $clause;
421 if (empty($clauses)) {
422 $this->_where
= "WHERE ( 1 )";
425 $this->_where
= "WHERE " . implode(' AND ', $clauses);
428 // if ( $this->_aclWhere ) {
429 // $this->_where .= " AND {$this->_aclWhere} ";
433 public function groupBy() {
434 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
437 public function orderBy() {
438 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_mailing_job']}.end_date DESC ";
441 public function postProcess() {
443 $this->beginPostProcess();
445 // get the acl clauses built before we assemble the query
446 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
448 $sql = $this->buildQuery(TRUE);
452 $rows = $graphRows = array();
453 $this->buildRows($sql, $rows);
455 $this->formatDisplay($rows);
456 $this->doTemplateAssignment($rows);
457 $this->endPostProcess($rows);
463 public static function getChartCriteria() {
466 'civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
467 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
468 'civicrm_mailing_event_opened_open_count' => ts('Total Opens'),
469 'civicrm_mailing_event_opened_unique_open_count' => ts('Unique Opens'),
470 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Clicks'),
471 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
474 'civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
475 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
476 'civicrm_mailing_event_opened_open_rate' => ts('Total Open Rate'),
477 'civicrm_mailing_event_opened_unique_open_rate' => ts('Unique Open Rate'),
478 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
479 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
491 public function formRule($fields, $files, $self) {
494 if (empty($fields['charts'])) {
498 $criteria = self
::getChartCriteria();
500 foreach ($fields['fields'] as $fld => $isActive) {
501 if (in_array($fld, array(
520 $errors['_qf_default'] = ts('For Chart view, please select at least one field from %1 OR %2.', array(
521 1 => implode(', ', $criteria['count']),
522 2 => implode(', ', $criteria['rate']),
532 public function buildChart(&$rows) {
537 $criteria = self
::getChartCriteria();
540 'legend' => ts('Mail Summary'),
541 'xname' => ts('Mailing'),
542 'yname' => ts('Statistics'),
547 $plotRate = $plotCount = TRUE;
548 foreach ($rows as $row) {
549 $chartInfo['values'][$row['civicrm_mailing_name']] = array();
551 foreach ($criteria['count'] as $criteria => $label) {
552 if (isset($row[$criteria])) {
553 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
554 $chartInfo['tip'][$label] = "{$label} #val#";
557 elseif (isset($criteria['count'][$criteria])) {
558 unset($criteria['count'][$criteria]);
563 foreach ($criteria['rate'] as $criteria => $label) {
564 if (isset($row[$criteria])) {
565 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteria];
566 $chartInfo['tip'][$label] = "{$label} #val#";
569 elseif (isset($criteria['rate'][$criteria])) {
570 unset($criteria['rate'][$criteria]);
577 $criteria = $criteria['count'];
580 $criteria = $criteria['rate'];
583 $chartInfo['criteria'] = array_values($criteria);
585 // dynamically set the graph size
586 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criteria) * 40));
589 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
590 $this->assign('chartType', $this->_params
['charts']);
594 * Alter display of rows.
596 * Iterate through the rows retrieved via SQL and make changes for display purposes,
597 * such as rendering contacts as links.
600 * Rows generated by SQL, with an array for each row.
602 public function alterDisplay(&$rows) {
604 foreach ($rows as $rowNum => $row) {
605 // make count columns point to detail report
606 // convert display name to links
607 if (array_key_exists('civicrm_contact_display_name', $row) &&
608 array_key_exists('civicrm_contact_id', $row)
610 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
611 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
612 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
614 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
615 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
620 if (array_key_exists('civicrm_address_country_id', $row)) {
621 if ($value = $row['civicrm_address_country_id']) {
622 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
626 if (array_key_exists('civicrm_address_state_province_id', $row)) {
627 if ($value = $row['civicrm_address_state_province_id']) {
628 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
633 // skip looking further in rows, if first row itself doesn't
634 // have the column we need