3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Mailing_Summary
extends CRM_Report_Form
{
19 protected $_summary = NULL;
21 protected $_customGroupExtends = [];
23 protected $_add2groupSupported = FALSE;
25 public $_drilldownReport = ['mailing/detail' => 'Link to Detail Report'];
30 public function __construct() {
33 $this->_columns
['civicrm_mailing'] = [
34 'dao' => 'CRM_Mailing_DAO_Mailing',
38 'title' => ts('Mailing ID'),
43 'title' => ts('Mailing Name'),
47 'title' => ts('Date Created'),
50 'title' => ts('Subject'),
53 'title' => ts('Sender Name'),
56 'title' => ts('Sender Email'),
61 'title' => ts('Mailing Status'),
62 'operatorType' => CRM_Report_Form
::OP_SELECT
,
63 'type' => CRM_Utils_Type
::T_INT
,
68 //'operator' => 'like',
73 'title' => ts('Mailing Name'),
74 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
75 'type' => CRM_Utils_Type
::T_INT
,
76 'options' => CRM_Mailing_BAO_Mailing
::getMailingsList(),
79 'mailing_subject' => [
81 'title' => ts('Mailing Subject'),
82 'type' => CRM_Utils_Type
::T_STRING
,
89 'title' => ts('Mailing Name'),
91 'mailing_subject' => [
93 'title' => ts('Mailing Subject'),
98 $this->_columns
['civicrm_mailing_job'] = [
99 'dao' => 'CRM_Mailing_DAO_MailingJob',
102 'title' => ts('Start Date'),
103 'dbAlias' => 'MIN(mailing_job_civireport.start_date)',
106 'title' => ts('End Date'),
107 'dbAlias' => 'MAX(mailing_job_civireport.end_date)',
112 'type' => CRM_Utils_Type
::T_STRING
,
113 'default' => 'Complete',
114 'no_display' => TRUE,
117 'type' => CRM_Utils_Type
::T_INT
,
119 'no_display' => TRUE,
122 'title' => ts('Start Date'),
123 'default' => 'this.year',
124 'operatorType' => CRM_Report_Form
::OP_DATE
,
125 'type' => CRM_Utils_Type
::T_DATE
,
128 'title' => ts('End Date'),
129 'default' => 'this.year',
130 'operatorType' => CRM_Report_Form
::OP_DATE
,
131 'type' => CRM_Utils_Type
::T_DATE
,
136 'title' => ts('Start Date'),
137 'dbAlias' => 'MIN(mailing_job_civireport.start_date)',
140 'title' => ts('End Date'),
141 'default_weight' => '1',
142 'default_order' => 'DESC',
143 'dbAlias' => 'MAX(mailing_job_civireport.end_date)',
146 'grouping' => 'mailing-fields',
149 $this->_columns
['civicrm_mailing_event_queue'] = [
150 'dao' => 'CRM_Mailing_DAO_Mailing',
154 'title' => ts('Intended Recipients'),
159 $this->_columns
['civicrm_mailing_event_delivered'] = [
160 'dao' => 'CRM_Mailing_DAO_Mailing',
162 'delivered_count' => [
163 'name' => 'event_queue_id',
164 'title' => ts('Delivered'),
167 'title' => ts('Accepted Rate'),
169 'calc' => 'PERCENTAGE',
170 'top' => 'civicrm_mailing_event_delivered.delivered_count',
171 'base' => 'civicrm_mailing_event_queue.queue_count',
177 $this->_columns
['civicrm_mailing_event_bounce'] = [
178 'dao' => 'CRM_Mailing_DAO_Mailing',
181 'name' => 'event_queue_id',
182 'title' => ts('Bounce'),
185 'title' => ts('Bounce Rate'),
187 'calc' => 'PERCENTAGE',
188 'top' => 'civicrm_mailing_event_bounce.bounce_count',
189 'base' => 'civicrm_mailing_event_queue.queue_count',
195 $this->_columns
['civicrm_mailing_event_opened'] = [
196 'dao' => 'CRM_Mailing_DAO_Mailing',
198 'unique_open_count' => [
200 'alias' => 'mailing_event_opened_civireport',
201 'dbAlias' => 'mailing_event_opened_civireport.event_queue_id',
202 'title' => ts('Unique Opens'),
204 'unique_open_rate' => [
205 'title' => ts('Unique Open Rate'),
207 'calc' => 'PERCENTAGE',
208 'top' => 'civicrm_mailing_event_opened.unique_open_count',
209 'base' => 'civicrm_mailing_event_delivered.delivered_count',
213 'name' => 'event_queue_id',
214 'title' => ts('Total Opens'),
217 'title' => ts('Total Open Rate'),
219 'calc' => 'PERCENTAGE',
220 'top' => 'civicrm_mailing_event_opened.open_count',
221 'base' => 'civicrm_mailing_event_delivered.delivered_count',
227 $this->_columns
['civicrm_mailing_event_trackable_url_open'] = [
228 'dao' => 'CRM_Mailing_DAO_Mailing',
231 'name' => 'event_queue_id',
232 'title' => ts('Unique Clicks'),
235 'title' => ts('Click through Rate'),
238 'calc' => 'PERCENTAGE',
239 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
240 'base' => 'civicrm_mailing_event_delivered.delivered_count',
244 'title' => ts('Click to Open Rate'),
247 'calc' => 'PERCENTAGE',
248 'top' => 'civicrm_mailing_event_trackable_url_open.click_count',
249 'base' => 'civicrm_mailing_event_opened.open_count',
255 $this->_columns
['civicrm_mailing_event_unsubscribe'] = [
256 'dao' => 'CRM_Mailing_DAO_Mailing',
258 'unsubscribe_count' => [
260 'title' => ts('Unsubscribe'),
261 'alias' => 'mailing_event_unsubscribe_civireport',
262 'dbAlias' => 'mailing_event_unsubscribe_civireport.event_queue_id',
266 'title' => ts('Opt-outs'),
267 'alias' => 'mailing_event_optout_civireport',
268 'dbAlias' => 'mailing_event_optout_civireport.event_queue_id',
272 $this->_columns
['civicrm_mailing_group'] = [
273 'dao' => 'CRM_Mailing_DAO_MailingGroup',
276 'title' => ts('Groups Included in Mailing'),
277 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
278 'type' => CRM_Utils_Type
::T_INT
,
279 'options' => CRM_Core_PseudoConstant
::group(),
283 // If we have campaigns enabled, add those elements to both the fields, filters.
284 $this->addCampaignFields('civicrm_mailing');
286 // Add charts support
289 'barChart' => ts('Bar Chart'),
292 parent
::__construct();
295 public function preProcess() {
296 $this->assign('chartSupported', TRUE);
297 parent
::preProcess();
301 * manipulate the select function to query count functions.
303 public function select() {
306 'civicrm_mailing_event_queue',
307 'civicrm_mailing_event_delivered',
308 'civicrm_mailing_event_opened',
309 'civicrm_mailing_event_bounce',
310 'civicrm_mailing_event_trackable_url_open',
311 'civicrm_mailing_event_unsubscribe',
314 // Define a list of columns that should be counted with the DISTINCT
315 // keyword. For example, civicrm_mailing_event_opened.unique_open_count
316 // should display the number of unique records, whereas something like
317 // civicrm_mailing_event_opened.open_count should display the total number.
318 // Each string here is in the form $tableName.$fieldName, where $tableName
319 // is the key in $this->_columns, and $fieldName is the key in that array's
321 // Reference: CRM-20660
322 $distinctCountColumns = [
323 'civicrm_mailing_event_queue.queue_count',
324 'civicrm_mailing_event_delivered.delivered_count',
325 'civicrm_mailing_event_bounce.bounce_count',
326 'civicrm_mailing_event_opened.unique_open_count',
327 'civicrm_mailing_event_trackable_url_open.click_count',
328 'civicrm_mailing_event_unsubscribe.unsubscribe_count',
329 'civicrm_mailing_event_unsubscribe.optout_count',
333 $this->_columnHeaders
= [];
334 foreach ($this->_columns
as $tableName => $table) {
335 if (array_key_exists('fields', $table)) {
336 foreach ($table['fields'] as $fieldName => $field) {
337 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
340 if (!empty($field['statistics'])) {
341 switch ($field['statistics']['calc']) {
343 $base_table_column = explode('.', $field['statistics']['base']);
344 $top_table_column = explode('.', $field['statistics']['top']);
346 $select[] = "CONCAT(round(
347 count(DISTINCT {$this->_columns[$top_table_column[0]]['fields'][$top_table_column[1]]['dbAlias']}) /
348 count(DISTINCT {$this->_columns[$base_table_column[0]]['fields'][$base_table_column[1]]['dbAlias']}) * 100, 2
349 ), '%') as {$tableName}_{$fieldName}";
354 if (in_array($tableName, $count_tables)) {
355 // Use the DISTINCT keyword appropriately, based on the contents
356 // of $distinct_count_columns.
358 if (in_array("{$tableName}.{$fieldName}", $distinctCountColumns)) {
359 $distinct = 'DISTINCT';
361 $select[] = "count($distinct {$field['dbAlias']}) as {$tableName}_{$fieldName}";
364 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
367 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
368 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
374 $this->_selectClauses
= $select;
375 $this->_select
= "SELECT " . implode(', ', $select) . " ";
376 //print_r($this->_select);
379 public function from() {
382 FROM civicrm_mailing {$this->_aliases['civicrm_mailing']}
383 LEFT JOIN civicrm_mailing_job {$this->_aliases['civicrm_mailing_job']}
384 ON {$this->_aliases['civicrm_mailing']}.id = {$this->_aliases['civicrm_mailing_job']}.mailing_id
385 LEFT JOIN civicrm_mailing_event_queue {$this->_aliases['civicrm_mailing_event_queue']}
386 ON {$this->_aliases['civicrm_mailing_event_queue']}.job_id = {$this->_aliases['civicrm_mailing_job']}.id
387 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
388 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
389 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
390 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
391 AND {$this->_aliases['civicrm_mailing_event_bounce']}.id IS null
392 LEFT JOIN civicrm_mailing_event_opened {$this->_aliases['civicrm_mailing_event_opened']}
393 ON {$this->_aliases['civicrm_mailing_event_opened']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
394 LEFT JOIN civicrm_mailing_event_trackable_url_open {$this->_aliases['civicrm_mailing_event_trackable_url_open']}
395 ON {$this->_aliases['civicrm_mailing_event_trackable_url_open']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id
396 LEFT JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
397 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}.org_unsubscribe = 0
398 LEFT JOIN civicrm_mailing_event_unsubscribe mailing_event_optout_civireport
399 ON mailing_event_optout_civireport.event_queue_id = {$this->_aliases['civicrm_mailing_event_queue']}.id AND mailing_event_optout_civireport.org_unsubscribe = 1";
401 if ($this->isTableSelected('civicrm_mailing_group')) {
403 LEFT JOIN civicrm_mailing_group {$this->_aliases['civicrm_mailing_group']}
404 ON {$this->_aliases['civicrm_mailing_group']}.mailing_id = {$this->_aliases['civicrm_mailing']}.id";
406 // need group by and order by
408 //print_r($this->_from);
411 public function where() {
413 //to avoid the sms listings
414 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
416 foreach ($this->_columns
as $tableName => $table) {
417 if (array_key_exists('filters', $table)) {
418 foreach ($table['filters'] as $fieldName => $field) {
420 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
421 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
422 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
423 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
425 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
428 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
431 if ($fieldName == 'relationship_type_id') {
432 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
435 $clause = $this->whereClause($field,
437 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
438 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
439 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
445 if (!empty($clause)) {
446 $clauses[] = $clause;
452 if (empty($clauses)) {
453 $this->_where
= "WHERE ( 1 )";
456 $this->_where
= "WHERE " . implode(' AND ', $clauses);
460 public function groupBy() {
462 "{$this->_aliases['civicrm_mailing']}.id",
464 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
467 public function orderBy() {
469 CRM_Contact_BAO_Query
::getGroupByFromOrderBy($this->_groupBy
, $this->_orderByArray
);
472 public function postProcess() {
474 $this->beginPostProcess();
476 // get the acl clauses built before we assemble the query
477 $this->buildACLClause(CRM_Utils_Array
::value('civicrm_contact', $this->_aliases
));
479 $sql = $this->buildQuery(TRUE);
483 $rows = $graphRows = [];
484 $this->buildRows($sql, $rows);
486 $this->formatDisplay($rows);
487 $this->doTemplateAssignment($rows);
488 $this->endPostProcess($rows);
494 public static function getChartCriteria() {
497 'civicrm_mailing_event_delivered_delivered_count' => ts('Delivered'),
498 'civicrm_mailing_event_bounce_bounce_count' => ts('Bounce'),
499 'civicrm_mailing_event_opened_open_count' => ts('Total Opens'),
500 'civicrm_mailing_event_opened_unique_open_count' => ts('Unique Opens'),
501 'civicrm_mailing_event_trackable_url_open_click_count' => ts('Unique Clicks'),
502 'civicrm_mailing_event_unsubscribe_unsubscribe_count' => ts('Unsubscribe'),
505 'civicrm_mailing_event_delivered_accepted_rate' => ts('Accepted Rate'),
506 'civicrm_mailing_event_bounce_bounce_rate' => ts('Bounce Rate'),
507 'civicrm_mailing_event_opened_open_rate' => ts('Total Open Rate'),
508 'civicrm_mailing_event_opened_unique_open_rate' => ts('Unique Open Rate'),
509 'civicrm_mailing_event_trackable_url_open_CTR' => ts('Click through Rate'),
510 'civicrm_mailing_event_trackable_url_open_CTO' => ts('Click to Open Rate'),
522 public static function formRule($fields, $files, $self) {
525 if (empty($fields['charts'])) {
529 $criteria = self
::getChartCriteria();
531 foreach ($fields['fields'] as $fld => $isActive) {
551 $errors['_qf_default'] = ts('For Chart view, please select at least one field from %1 OR %2.', [
552 1 => implode(', ', $criteria['count']),
553 2 => implode(', ', $criteria['rate']),
563 public function buildChart(&$rows) {
568 $criteria = self
::getChartCriteria();
571 'legend' => ts('Mail Summary'),
572 'xname' => ts('Mailing'),
573 'yname' => ts('Statistics'),
578 $plotRate = $plotCount = TRUE;
579 foreach ($rows as $row) {
580 $chartInfo['values'][$row['civicrm_mailing_name']] = [];
582 foreach ($criteria['count'] as $criteriaName => $label) {
583 if (isset($row[$criteriaName])) {
584 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteriaName];
585 $chartInfo['tip'][$label] = "{$label} #val#";
588 elseif (isset($criteria['count'][$criteriaName])) {
589 unset($criteria['count'][$criteriaName]);
594 foreach ($criteria['rate'] as $criteriaName => $label) {
595 if (isset($row[$criteria])) {
596 $chartInfo['values'][$row['civicrm_mailing_name']][$label] = $row[$criteriaName];
597 $chartInfo['tip'][$label] = "{$label} #val#";
600 elseif (isset($criteria['rate'][$criteriaName])) {
601 unset($criteria['rate'][$criteriaName]);
608 $criteria = $criteria['count'];
611 $criteria = $criteria['rate'];
614 $chartInfo['criteria'] = array_values($criteria);
616 // dynamically set the graph size
617 $chartInfo['xSize'] = ((count($rows) * 125) +
(count($rows) * count($criteria) * 40));
620 CRM_Utils_Chart
::buildChart($chartInfo, $this->_params
['charts']);
621 $this->assign('chartType', $this->_params
['charts']);
625 * Alter display of rows.
627 * Iterate through the rows retrieved via SQL and make changes for display purposes,
628 * such as rendering contacts as links.
631 * Rows generated by SQL, with an array for each row.
633 public function alterDisplay(&$rows) {
635 foreach ($rows as $rowNum => $row) {
637 if (array_key_exists('civicrm_mailing_id', $row)) {
638 if (array_key_exists('civicrm_mailing_name', $row)) {
639 $rows[$rowNum]['civicrm_mailing_name_link'] = CRM_Report_Utils_Report
::getNextUrl('mailing/detail',
640 'reset=1&force=1&mailing_id_op=eq&mailing_id_value=' . $row['civicrm_mailing_id'],
641 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
643 $rows[$rowNum]['civicrm_mailing_name_hover'] = ts('View Mailing details for this mailing');
646 if (array_key_exists('civicrm_mailing_event_opened_open_count', $row)) {
647 $rows[$rowNum]['civicrm_mailing_event_opened_open_count'] = CRM_Mailing_Event_BAO_Opened
::getTotalCount($row['civicrm_mailing_id']);
651 // convert campaign_id to campaign title
652 if (array_key_exists('civicrm_mailing_campaign_id', $row)) {
653 if ($value = $row['civicrm_mailing_campaign_id']) {
654 $rows[$rowNum]['civicrm_mailing_campaign_id'] = $this->campaigns
[$value];
658 // skip looking further in rows, if first row itself doesn't
659 // have the column we need