3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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'),
140 'type' => CRM_Utils_Type
::T_STRING
,
142 'time_stamp' => array(
143 'title' => ts('Bounce Date'),
144 'operatorType' => CRM_Report_Form
::OP_DATE
,
145 'type' => CRM_Utils_Type
::T_DATE
,
148 'order_bys' => array(
149 'bounce_reason' => array(
150 'title' => ts('Bounce Reason'),
152 'time_stamp' => array(
153 'title' => ts('Bounce Date'),
156 'grouping' => 'mailing-fields',
159 $this->_columns
['civicrm_mailing_bounce_type'] = array(
160 'dao' => 'CRM_Mailing_DAO_BounceType',
162 'bounce_name' => array(
164 'title' => ts('Bounce Type'),
168 'bounce_type_name' => array(
170 'title' => ts('Bounce Type'),
171 'operatorType' => CRM_Report_Form
::OP_SELECT
,
172 'type' => CRM_Utils_Type
::T_STRING
,
173 'options' => self
::bounce_type(),
174 'operator' => 'like',
177 'order_bys' => array(
178 'bounce_name' => array(
180 'title' => ts('Bounce Type'),
183 'grouping' => 'mailing-fields',
186 $this->_columns
['civicrm_email'] = array(
187 'dao' => 'CRM_Core_DAO_Email',
190 'title' => ts('Email'),
195 'title' => ts('On hold'),
197 'hold_date' => array(
198 'title' => ts('Hold date'),
200 'reset_date' => array(
201 'title' => ts('Hold reset date'),
206 'title' => ts('On hold'),
208 'hold_date' => array(
209 'title' => ts('Hold date'),
210 'operatorType' => CRM_Report_Form
::OP_DATE
,
211 'type' => CRM_Utils_Type
::T_DATE
,
213 'reset_date' => array(
214 'title' => ts('Hold reset date'),
215 'operatorType' => CRM_Report_Form
::OP_DATE
,
216 'type' => CRM_Utils_Type
::T_DATE
,
219 'order_bys' => array(
220 'email' => array('title' => ts('Email'), 'default_order' => 'ASC'),
222 'grouping' => 'contact-fields',
225 $this->_columns
['civicrm_phone'] = array(
226 'dao' => 'CRM_Core_DAO_Phone',
227 'fields' => array('phone' => NULL),
228 'grouping' => 'contact-fields',
231 $this->_groupFilter
= TRUE;
232 $this->_tagFilter
= TRUE;
233 parent
::__construct();
236 public function preProcess() {
237 $this->assign('chartSupported', TRUE);
238 parent
::preProcess();
241 public function select() {
243 $this->_columnHeaders
= array();
245 foreach ($this->_columns
as $tableName => $table) {
246 if (array_key_exists('fields', $table)) {
247 foreach ($table['fields'] as $fieldName => $field) {
248 if (!empty($field['required']) ||
249 !empty($this->_params
['fields'][$fieldName])
251 if ($tableName == 'civicrm_email') {
252 $this->_emailField
= TRUE;
254 elseif ($tableName == 'civicrm_phone') {
255 $this->_phoneField
= TRUE;
258 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
259 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
260 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
261 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
267 if (!empty($this->_params
['charts'])) {
268 $select[] = "COUNT({$this->_aliases['civicrm_mailing_event_bounce']}.id) as civicrm_mailing_bounce_count";
269 $this->_columnHeaders
["civicrm_mailing_bounce_count"]['title'] = ts('Bounce Count');
272 $this->_select
= "SELECT " . implode(', ', $select) . " ";
282 public static function formRule($fields, $files, $self) {
283 $errors = $grouping = array();
287 public function from() {
289 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
290 // LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
291 // ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
292 // {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
295 INNER JOIN civicrm_mailing_event_queue
296 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
297 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
298 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
299 INNER JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
300 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id
301 LEFT JOIN civicrm_mailing_bounce_type {$this->_aliases['civicrm_mailing_bounce_type']}
302 ON {$this->_aliases['civicrm_mailing_event_bounce']}.bounce_type_id = {$this->_aliases['civicrm_mailing_bounce_type']}.id
303 INNER JOIN civicrm_mailing_job
304 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
305 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
306 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
309 if ($this->_phoneField
) {
311 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
312 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
313 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
317 public function where() {
321 // Exclude SMS mailing type
322 $clauses[] = "{$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
324 // Build date filter clauses
325 foreach ($this->_columns
as $tableName => $table) {
326 if (array_key_exists('filters', $table)) {
327 foreach ($table['filters'] as $fieldName => $field) {
329 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
330 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
331 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
332 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
334 $clause = $this->dateClause($this->_aliases
[$tableName] . '.' . $field['name'], $relative, $from, $to, $field['type']);
337 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
340 $clause = $this->whereClause($field,
342 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
343 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
344 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
350 if (!empty($clause)) {
351 $clauses[] = $clause;
357 $this->_where
= "WHERE " . implode(' AND ', $clauses);
360 public function groupBy() {
361 if (!empty($this->_params
['charts'])) {
362 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_mailing']}.id";
365 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_mailing_event_bounce']}.id";
369 public function postProcess() {
370 $this->beginPostProcess();
372 // get the acl clauses built before we assemble the query
373 $this->buildACLClause($this->_aliases
['civicrm_contact']);
375 $sql = $this->buildQuery(TRUE);
377 $rows = $graphRows = array();
378 $this->buildRows($sql, $rows);
380 $this->formatDisplay($rows);
381 $this->doTemplateAssignment($rows);
382 $this->endPostProcess($rows);
388 public function buildChart(&$rows) {
394 'legend' => ts('Mail Bounce Report'),
395 'xname' => ts('Mailing'),
396 'yname' => ts('Bounce'),
398 'tip' => ts('Mail Bounce: %1', array(1 => '#val#')),
400 foreach ($rows as $row) {
401 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_bounce_count'];
405 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
406 $this->assign('chartType', $this->_params
['charts']);
412 public function bounce_type() {
414 $data = array('' => '--Please Select--');
416 $bounce_type = new CRM_Mailing_DAO_BounceType();
417 $query = "SELECT name FROM civicrm_mailing_bounce_type";
418 $bounce_type->query($query);
420 while ($bounce_type->fetch()) {
421 $data[$bounce_type->name
] = $bounce_type->name
;
428 * Alter display of rows.
430 * Iterate through the rows retrieved via SQL and make changes for display purposes,
431 * such as rendering contacts as links.
434 * Rows generated by SQL, with an array for each row.
436 public function alterDisplay(&$rows) {
438 $config = CRM_Core_Config
::Singleton();
441 foreach ($rows as $rowNum => $row) {
442 // make count columns point to detail report
443 // convert display name to links
444 if (array_key_exists('civicrm_contact_sort_name', $row) &&
445 array_key_exists('civicrm_contact_id', $row)
447 $url = CRM_Utils_System
::url('civicrm/contact/view',
448 'reset=1&cid=' . $row['civicrm_contact_id'],
451 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
452 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
456 // Handle on_hold boolean display
457 if (array_key_exists('civicrm_email_on_hold', $row)) {
458 $rows[$rowNum]['civicrm_email_on_hold'] = (!empty($row['civicrm_email_on_hold'])) ?
'Yes' : 'No';
462 // Convert datetime values to custom date and time format
464 'civicrm_mailing_event_bounce_time_stamp',
465 'civicrm_email_hold_date',
466 'civicrm_email_reset_date',
469 foreach ($dateFields as $dateField) {
470 if (array_key_exists($dateField, $row)) {
471 if (!empty($rows[$rowNum][$dateField])) {
472 $rows[$rowNum][$dateField] = CRM_Utils_Date
::customFormat($row[$dateField], $config->dateformatDatetime
);
478 // skip looking further in rows, if first row itself doesn't
479 // have the column we need