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
33 class CRM_Report_Form_Mailing_Bounce
extends CRM_Report_Form
{
35 protected $_summary = NULL;
37 protected $_emailField = FALSE;
39 protected $_phoneField = FALSE;
41 protected $_customGroupExtends = array(
48 protected $_charts = array(
50 'barChart' => 'Bar Chart',
51 'pieChart' => 'Pie Chart',
56 public function __construct() {
57 $this->_columns
= array();
59 $this->_columns
['civicrm_contact'] = array(
60 'dao' => 'CRM_Contact_DAO_Contact',
63 'title' => ts('Contact ID'),
67 'title' => ts('Contact Name'),
73 'title' => ts('Contact Name'),
76 'title' => ts('Contact Source'),
77 'type' => CRM_Utils_Type
::T_STRING
,
80 'title' => ts('Contact ID'),
86 'title' => ts('Contact Name'),
88 'default_order' => 'ASC',
91 'grouping' => 'contact-fields',
94 $this->_columns
['civicrm_mailing'] = array(
95 'dao' => 'CRM_Mailing_DAO_Mailing',
97 'mailing_name' => array(
99 'title' => ts('Mailing'),
102 'mailing_name_alias' => array(
105 'no_display' => TRUE,
109 'mailing_id' => array(
111 'title' => ts('Mailing'),
112 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
113 'type' => CRM_Utils_Type
::T_INT
,
114 'options' => CRM_Mailing_BAO_Mailing
::getMailingsList(),
115 'operator' => 'like',
118 'order_bys' => array(
119 'mailing_name' => array(
121 'title' => ts('Mailing'),
124 'grouping' => 'mailing-fields',
127 $this->_columns
['civicrm_mailing_event_bounce'] = array(
128 'dao' => 'CRM_Mailing_DAO_Mailing',
130 'bounce_reason' => array(
131 'title' => ts('Bounce Reason'),
133 'time_stamp' => array(
134 'title' => ts('Bounce Date'),
138 'bounce_reason' => array(
139 'title' => ts('Bounce Reason'),
141 'time_stamp' => array(
142 'title' => ts('Bounce Date'),
143 'operatorType' => CRM_Report_Form
::OP_DATE
,
144 'type' => CRM_Utils_Type
::T_DATE
,
147 'order_bys' => array(
148 'bounce_reason' => array(
149 'title' => ts('Bounce Reason'),
151 'time_stamp' => array(
152 'title' => ts('Bounce Date'),
155 'grouping' => 'mailing-fields',
158 $this->_columns
['civicrm_mailing_bounce_type'] = array(
159 'dao' => 'CRM_Mailing_DAO_BounceType',
161 'bounce_name' => array(
163 'title' => ts('Bounce Type'),
167 'bounce_type_name' => array(
169 'title' => ts('Bounce Type'),
170 'operatorType' => CRM_Report_Form
::OP_SELECT
,
171 'type' => CRM_Utils_Type
::T_STRING
,
172 'options' => self
::bounce_type(),
173 'operator' => 'like',
176 'order_bys' => array(
177 'bounce_name' => array(
179 'title' => ts('Bounce Type'),
182 'grouping' => 'mailing-fields',
185 $this->_columns
['civicrm_email'] = array(
186 'dao' => 'CRM_Core_DAO_Email',
189 'title' => ts('Email'),
194 'title' => ts('On hold'),
196 'hold_date' => array(
197 'title' => ts('Hold date'),
199 'reset_date' => array(
200 'title' => ts('Hold reset date'),
205 'title' => ts('On hold'),
207 'hold_date' => array(
208 'title' => ts('Hold date'),
209 'operatorType' => CRM_Report_Form
::OP_DATE
,
210 'type' => CRM_Utils_Type
::T_DATE
,
212 'reset_date' => array(
213 'title' => ts('Hold reset date'),
214 'operatorType' => CRM_Report_Form
::OP_DATE
,
215 'type' => CRM_Utils_Type
::T_DATE
,
218 'order_bys' => array(
219 'email' => array('title' => ts('Email'), 'default_order' => 'ASC'),
221 'grouping' => 'contact-fields',
224 $this->_columns
['civicrm_phone'] = array(
225 'dao' => 'CRM_Core_DAO_Phone',
226 'fields' => array('phone' => NULL),
227 'grouping' => 'contact-fields',
230 $this->_groupFilter
= TRUE;
231 $this->_tagFilter
= TRUE;
232 parent
::__construct();
235 public function preProcess() {
236 $this->assign('chartSupported', TRUE);
237 parent
::preProcess();
240 public function select() {
242 $this->_columnHeaders
= array();
244 foreach ($this->_columns
as $tableName => $table) {
245 if (array_key_exists('fields', $table)) {
246 foreach ($table['fields'] as $fieldName => $field) {
247 if (!empty($field['required']) ||
248 !empty($this->_params
['fields'][$fieldName])
250 if ($tableName == 'civicrm_email') {
251 $this->_emailField
= TRUE;
253 elseif ($tableName == 'civicrm_phone') {
254 $this->_phoneField
= TRUE;
257 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
258 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
259 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
260 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
266 if (!empty($this->_params
['charts'])) {
267 $select[] = "COUNT({$this->_aliases['civicrm_mailing_event_bounce']}.id) as civicrm_mailing_bounce_count";
268 $this->_columnHeaders
["civicrm_mailing_bounce_count"]['title'] = ts('Bounce Count');
271 $this->_select
= "SELECT " . implode(', ', $select) . " ";
281 public static function formRule($fields, $files, $self) {
282 $errors = $grouping = array();
286 public function from() {
288 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
289 // LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
290 // ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
291 // {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
294 INNER JOIN civicrm_mailing_event_queue
295 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
296 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
297 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
298 INNER JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
299 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id
300 LEFT JOIN civicrm_mailing_bounce_type {$this->_aliases['civicrm_mailing_bounce_type']}
301 ON {$this->_aliases['civicrm_mailing_event_bounce']}.bounce_type_id = {$this->_aliases['civicrm_mailing_bounce_type']}.id
302 INNER JOIN civicrm_mailing_job
303 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
304 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
305 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
308 if ($this->_phoneField
) {
310 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
311 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
312 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
316 public function where() {
320 // Exclude SMS mailing type
321 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
323 // Build date filter clauses
324 foreach ($this->_columns
as $tableName => $table) {
325 if (array_key_exists('filters', $table)) {
326 foreach ($table['filters'] as $fieldName => $field) {
328 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
329 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
330 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
331 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
333 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
336 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
339 $clause = $this->whereClause($field,
341 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
342 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
343 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
349 if (!empty($clause)) {
350 $clauses[] = $clause;
356 $this->_where
= "WHERE " . implode(' AND ', $clauses);
359 public function groupBy() {
360 if (!empty($this->_params
['charts'])) {
361 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_mailing']}.id";
364 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_mailing_event_bounce']}.id";
368 public function postProcess() {
369 $this->beginPostProcess();
371 // get the acl clauses built before we assemble the query
372 $this->buildACLClause($this->_aliases
['civicrm_contact']);
374 $sql = $this->buildQuery(TRUE);
376 $rows = $graphRows = array();
377 $this->buildRows($sql, $rows);
379 $this->formatDisplay($rows);
380 $this->doTemplateAssignment($rows);
381 $this->endPostProcess($rows);
387 public function buildChart(&$rows) {
393 'legend' => ts('Mail Bounce Report'),
394 'xname' => ts('Mailing'),
395 'yname' => ts('Bounce'),
397 'tip' => ts('Mail Bounce: %1', array(1 => '#val#')),
399 foreach ($rows as $row) {
400 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_bounce_count'];
404 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
405 $this->assign('chartType', $this->_params
['charts']);
411 public function bounce_type() {
413 $data = array('' => '--Please Select--');
415 $bounce_type = new CRM_Mailing_DAO_BounceType();
416 $query = "SELECT name FROM civicrm_mailing_bounce_type";
417 $bounce_type->query($query);
419 while ($bounce_type->fetch()) {
420 $data[$bounce_type->name
] = $bounce_type->name
;
427 * Alter display of rows.
429 * Iterate through the rows retrieved via SQL and make changes for display purposes,
430 * such as rendering contacts as links.
433 * Rows generated by SQL, with an array for each row.
435 public function alterDisplay(&$rows) {
437 $config = CRM_Core_Config
::Singleton();
440 foreach ($rows as $rowNum => $row) {
441 // make count columns point to detail report
442 // convert display name to links
443 if (array_key_exists('civicrm_contact_sort_name', $row) &&
444 array_key_exists('civicrm_contact_id', $row)
446 $url = CRM_Utils_System
::url('civicrm/contact/view',
447 'reset=1&cid=' . $row['civicrm_contact_id'],
450 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
451 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
455 // Handle on_hold boolean display
456 if (array_key_exists('civicrm_email_on_hold', $row)) {
457 $rows[$rowNum]['civicrm_email_on_hold'] = (!empty($row['civicrm_email_on_hold'])) ?
'Yes' : 'No';
461 // Convert datetime values to custom date and time format
463 'civicrm_mailing_event_bounce_time_stamp',
464 'civicrm_email_hold_date',
465 'civicrm_email_reset_date',
468 foreach ($dateFields as $dateField) {
469 if (array_key_exists($dateField, $row)) {
470 if (!empty($rows[$rowNum][$dateField])) {
471 $rows[$rowNum][$dateField] = CRM_Utils_Date
::customFormat($row[$dateField], $config->dateformatDatetime
);
477 // skip looking further in rows, if first row itself doesn't
478 // have the column we need