4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Mailing_Detail
extends CRM_Report_Form
{
38 protected $_exposeContactID = FALSE;
46 public function __construct() {
47 $this->_columns
= array();
49 $this->_columns
['civicrm_contact'] = array(
50 'dao' => 'CRM_Contact_DAO_Contact',
54 'title' => ts('Contact ID'),
59 'title' => ts('Contact Name'),
65 'title' => ts('Contact Name'),
68 'title' => ts('Contact ID'),
74 'title' => ts('Contact Name'),
76 'default_order' => 'ASC',
79 'grouping' => 'contact-fields',
82 $this->_columns
['civicrm_mailing'] = array(
83 'dao' => 'CRM_Mailing_DAO_Mailing',
85 'mailing_name' => array(
87 'title' => ts('Mailing'),
92 'mailing_id' => array(
94 'title' => ts('Mailing'),
95 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
96 'type' => CRM_Utils_Type
::T_INT
,
97 'options' => CRM_Mailing_BAO_Mailing
::getMailingsList(),
100 'order_bys' => array(
101 'mailing_name' => array(
103 'title' => ts('Mailing'),
106 'grouping' => 'mailing-fields',
109 // adding dao just to have alias
110 $this->_columns
['civicrm_mailing_event_bounce'] = array(
111 'dao' => 'CRM_Mailing_Event_DAO_Bounce',
114 $this->_columns
['civicrm_mailing_event_delivered'] = array(
115 'dao' => 'CRM_Mailing_Event_DAO_Delivered',
117 'delivery_id' => array(
119 'title' => ts('Delivery Status'),
124 'delivery_status' => array(
125 'name' => 'delivery_status',
126 'title' => ts('Delivery Status'),
127 'operatorType' => CRM_Report_Form
::OP_SELECT
,
128 'type' => CRM_Utils_Type
::T_STRING
,
131 'successful' => 'Successful',
132 'bounced' => 'Bounced',
136 'grouping' => 'mailing-fields',
139 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
140 'dao' => 'CRM_Mailing_Event_DAO_Unsubscribe',
142 'unsubscribe_id' => array(
144 'title' => ts('Unsubscribe'),
147 'optout_id' => array(
149 'title' => ts('Opt-out'),
151 'alias' => 'mailing_event_unsubscribe_civireport2',
155 'is_unsubscribed' => array(
157 'title' => ts('Unsubscribed'),
158 'type' => CRM_Utils_Type
::T_INT
,
159 'operatorType' => CRM_Report_Form
::OP_SELECT
,
165 'clause' => 'mailing_event_unsubscribe_civireport.id IS NULL',
167 'is_optout' => array(
168 'title' => ts('Opted-out'),
169 'type' => CRM_Utils_Type
::T_INT
,
170 'operatorType' => CRM_Report_Form
::OP_SELECT
,
176 'clause' => 'mailing_event_unsubscribe_civireport2.id IS NULL',
179 'grouping' => 'mailing-fields',
182 $this->_columns
['civicrm_mailing_event_reply'] = array(
183 'dao' => 'CRM_Mailing_Event_DAO_Reply',
187 'title' => ts('Reply'),
191 'is_replied' => array(
193 'title' => ts('Replied'),
194 'type' => CRM_Utils_Type
::T_INT
,
195 'operatorType' => CRM_Report_Form
::OP_SELECT
,
201 'clause' => 'mailing_event_reply_civireport.id IS NULL',
204 'grouping' => 'mailing-fields',
207 $this->_columns
['civicrm_mailing_event_forward'] = array(
208 'dao' => 'CRM_Mailing_Event_DAO_Forward',
210 'forward_id' => array(
212 'title' => ts('Forwarded to Email'),
216 'is_forwarded' => array(
218 'title' => ts('Forwarded'),
219 'type' => CRM_Utils_Type
::T_INT
,
220 'operatorType' => CRM_Report_Form
::OP_SELECT
,
226 'clause' => 'mailing_event_forward_civireport.id IS NULL',
229 'grouping' => 'mailing-fields',
232 $this->_columns
['civicrm_email'] = array(
233 'dao' => 'CRM_Core_DAO_Email',
236 'title' => ts('Email'),
240 'grouping' => 'contact-fields',
243 $this->_columns
['civicrm_phone'] = array(
244 'dao' => 'CRM_Core_DAO_Phone',
245 'fields' => array('phone' => NULL),
246 'grouping' => 'contact-fields',
249 $this->_groupFilter
= TRUE;
250 $this->_tagFilter
= TRUE;
252 parent
::__construct();
255 public function select() {
256 $select = $columns = array();
257 foreach ($this->_columns
as $tableName => $table) {
258 if (array_key_exists('fields', $table)) {
259 foreach ($table['fields'] as $fieldName => $field) {
260 if (!empty($field['required']) ||
261 !empty($this->_params
['fields'][$fieldName])
263 if (in_array($fieldName, array(
269 $select[] = "IF({$field['dbAlias']} IS NULL, 'No', 'Yes') as {$tableName}_{$fieldName}";
270 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
271 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
272 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
273 unset($this->_columns
[$tableName]['fields'][$fieldName]);
274 $columns[$tableName][$fieldName] = $field;
276 elseif ($fieldName == 'delivery_id') {
277 $select[] = "IF(mailing_event_delivered_civireport.id IS NOT NULL, 'Successful', IF(mailing_event_bounce_civireport.id IS NOT NULL, 'Bounced ', 'Unknown')) as {$tableName}_{$fieldName}";
278 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
279 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
280 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
281 unset($this->_columns
[$tableName]['fields'][$fieldName]);
282 $columns[$tableName][$fieldName] = $field;
290 if (!empty($select)) {
291 $this->_select
.= ', ' . implode(', ', $select) . " ";
294 // put the fields that were unset, back in place
295 foreach ($columns as $tableName => $table) {
296 foreach ($table as $fieldName => $fields) {
297 $this->_columns
[$tableName]['fields'][$fieldName] = $fields;
302 ksort($this->_columnHeaders
);
305 public function from() {
307 FROM civicrm_contact {$this->_aliases['civicrm_contact']}";
310 INNER JOIN civicrm_mailing_event_queue
311 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
312 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
313 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id";
315 if (array_key_exists('delivery_id', $this->_params
['fields'])) {
317 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
318 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = civicrm_mailing_event_queue.id
319 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
320 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id";
321 if (CRM_Utils_Array
::value('delivery_status_value', $this->_params
) ==
324 $this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']['clause'] = "{$this->_aliases['civicrm_mailing_event_bounce']}.id IS NOT NULL";
326 elseif (CRM_Utils_Array
::value('delivery_status_value', $this->_params
) ==
329 $this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']['clause'] = "{$this->_aliases['civicrm_mailing_event_delivered']}.id IS NOT NULL";
333 unset($this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']);
336 if (array_key_exists('reply_id', $this->_params
['fields']) ||
337 is_numeric(CRM_Utils_Array
::value('is_replied_value', $this->_params
))
339 if (CRM_Utils_Array
::value('is_replied_value', $this->_params
) == 1) {
341 $this->_columns
['civicrm_mailing_event_reply']['filters']['is_replied']['clause'] = '(1)';
347 {$joinType} JOIN civicrm_mailing_event_reply {$this->_aliases['civicrm_mailing_event_reply']}
348 ON {$this->_aliases['civicrm_mailing_event_reply']}.event_queue_id = civicrm_mailing_event_queue.id";
351 unset($this->_columns
['civicrm_mailing_event_reply']['filters']['is_replied']);
354 if (array_key_exists('unsubscribe_id', $this->_params
['fields']) ||
355 is_numeric(CRM_Utils_Array
::value('is_unsubscribed_value', $this->_params
))
357 if (CRM_Utils_Array
::value('is_unsubscribed_value', $this->_params
) == 1
360 $this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_unsubscribed']['clause'] = '(1)';
366 {$joinType} JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
367 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = civicrm_mailing_event_queue.id
368 AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}.org_unsubscribe = 0";
371 unset($this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_unsubscribed']);
374 if (array_key_exists('optout_id', $this->_params
['fields']) ||
375 is_numeric(CRM_Utils_Array
::value('is_optout_value', $this->_params
))
377 if (CRM_Utils_Array
::value('is_optout_value', $this->_params
) == 1) {
379 $this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_optout']['clause'] = '(1)';
385 {$joinType} JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}2
386 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}2.event_queue_id = civicrm_mailing_event_queue.id
387 AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}2.org_unsubscribe = 1";
390 unset($this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_optout']);
393 if (array_key_exists('forward_id', $this->_params
['fields']) ||
394 is_numeric(CRM_Utils_Array
::value('is_forwarded_value', $this->_params
))
396 if (CRM_Utils_Array
::value('is_forwarded_value', $this->_params
) == 1) {
398 $this->_columns
['civicrm_mailing_event_forward']['filters']['is_forwarded']['clause'] = '(1)';
404 {$joinType} JOIN civicrm_mailing_event_forward {$this->_aliases['civicrm_mailing_event_forward']}
405 ON {$this->_aliases['civicrm_mailing_event_forward']}.event_queue_id = civicrm_mailing_event_queue.id";
408 unset($this->_columns
['civicrm_mailing_event_forward']['filters']['is_forwarded']);
412 INNER JOIN civicrm_mailing_job
413 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
414 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
415 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
416 AND civicrm_mailing_job.is_test = 0";
418 if ($this->_phoneField
) {
420 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
421 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
422 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
426 public function where() {
428 $this->_where
.= " AND {$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
434 public function mailingList() {
437 $mailing = new CRM_Mailing_BAO_Mailing();
438 $query = "SELECT name FROM civicrm_mailing ";
439 $mailing->query($query);
441 while ($mailing->fetch()) {
442 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
451 public function alterDisplay(&$rows) {
452 // custom code to alter rows
454 foreach ($rows as $rowNum => $row) {
455 // make count columns point to detail report
456 // convert display name to links
457 if (array_key_exists('civicrm_contact_sort_name', $row) &&
458 array_key_exists('civicrm_contact_id', $row)
460 $url = CRM_Utils_System
::url('civicrm/contact/view',
461 'reset=1&cid=' . $row['civicrm_contact_id'],
464 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
465 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
469 // skip looking further in rows, if first row itself doesn't
470 // have the column we need