4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
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 function __construct() {
47 $this->_columns
= array();
49 $this->_columns
['civicrm_contact'] = array(
50 'dao' => 'CRM_Contact_DAO_Contact',
56 'title' => ts('Contact ID'),
62 'title' => ts('Contact Name'),
70 'title' => ts('Contact Name'),
74 'title' => ts('Contact ID'),
81 array('title' => ts('Contact Name'),
83 'default_order' => 'ASC',
86 'grouping' => 'contact-fields',
89 $this->_columns
['civicrm_mailing'] = array(
90 'dao' => 'CRM_Mailing_DAO_Mailing',
96 'title' => ts('Mailing'),
105 'title' => ts('Mailing'),
106 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
107 'type' => CRM_Utils_Type
::T_INT
,
108 'options' => CRM_Mailing_BAO_Mailing
::getMailingsList(),
116 'title' => ts('Mailing'),
119 'grouping' => 'mailing-fields',
122 // adding dao just to have alias
123 $this->_columns
['civicrm_mailing_event_bounce'] = array(
124 'dao' => 'CRM_Mailing_Event_DAO_Bounce',
127 $this->_columns
['civicrm_mailing_event_delivered'] = array(
128 'dao' => 'CRM_Mailing_Event_DAO_Delivered',
134 'title' => ts('Delivery Status'),
142 'name' => 'delivery_status',
143 'title' => ts('Delivery Status'),
144 'operatorType' => CRM_Report_Form
::OP_SELECT
,
145 'type' => CRM_Utils_Type
::T_STRING
,
148 'successful' => 'Successful',
149 'bounced' => 'Bounced',
153 'grouping' => 'mailing-fields',
156 $this->_columns
['civicrm_mailing_event_unsubscribe'] = array(
157 'dao' => 'CRM_Mailing_Event_DAO_Unsubscribe',
163 'title' => ts('Unsubscribe'),
169 'title' => ts('Opt-out'),
171 'alias' => 'mailing_event_unsubscribe_civireport2',
179 'title' => ts('Unsubscribed'),
180 'type' => CRM_Utils_Type
::T_INT
,
181 'operatorType' => CRM_Report_Form
::OP_SELECT
,
182 'options' => array('' => ts('Any'), '0' => ts('No'), '1' => ts('Yes')),
183 'clause' => 'mailing_event_unsubscribe_civireport.id IS NULL',
187 'title' => ts('Opted-out'),
188 'type' => CRM_Utils_Type
::T_INT
,
189 'operatorType' => CRM_Report_Form
::OP_SELECT
,
190 'options' => array('' => ts('Any'), '0' => ts('No'), '1' => ts('Yes')),
191 'clause' => 'mailing_event_unsubscribe_civireport2.id IS NULL',
194 'grouping' => 'mailing-fields',
197 $this->_columns
['civicrm_mailing_event_reply'] = array(
198 'dao' => 'CRM_Mailing_Event_DAO_Reply',
204 'title' => ts('Reply'),
212 'title' => ts('Replied'),
213 'type' => CRM_Utils_Type
::T_INT
,
214 'operatorType' => CRM_Report_Form
::OP_SELECT
,
215 'options' => array('' => ts('Any'), '0' => ts('No'), '1' => ts('Yes')),
216 'clause' => 'mailing_event_reply_civireport.id IS NULL',
219 'grouping' => 'mailing-fields',
222 $this->_columns
['civicrm_mailing_event_forward'] = array(
223 'dao' => 'CRM_Mailing_Event_DAO_Forward',
229 'title' => ts('Forwarded to Email'),
237 'title' => ts('Forwarded'),
238 'type' => CRM_Utils_Type
::T_INT
,
239 'operatorType' => CRM_Report_Form
::OP_SELECT
,
240 'options' => array('' => ts('Any'), '0' => ts('No'), '1' => ts('Yes')),
241 'clause' => 'mailing_event_forward_civireport.id IS NULL',
244 'grouping' => 'mailing-fields',
247 $this->_columns
['civicrm_email'] = array(
248 'dao' => 'CRM_Core_DAO_Email',
253 'title' => ts('Email'),
257 'grouping' => 'contact-fields',
260 $this->_columns
['civicrm_phone'] = array(
261 'dao' => 'CRM_Core_DAO_Phone',
262 'fields' => array('phone' => NULL),
263 'grouping' => 'contact-fields',
266 $this->_columns
['civicrm_group'] = array(
267 'dao' => 'CRM_Contact_DAO_Group',
273 'name' => 'group_id',
274 'title' => ts('Group'),
275 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
277 'options' => CRM_Core_PseudoConstant
::group(),
282 parent
::__construct();
286 $select = $columns = array();
287 foreach ($this->_columns
as $tableName => $table) {
288 if (array_key_exists('fields', $table)) {
289 foreach ($table['fields'] as $fieldName => $field) {
290 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
291 if (in_array($fieldName, array(
292 'unsubscribe_id', 'optout_id', 'forward_id', 'reply_id'))) {
293 $select[] = "IF({$field['dbAlias']} IS NULL, 'No', 'Yes') as {$tableName}_{$fieldName}";
294 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
295 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
296 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
297 unset($this->_columns
[$tableName]['fields'][$fieldName]);
298 $columns[$tableName][$fieldName] = $field;
300 elseif ($fieldName == 'delivery_id') {
301 $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}";
302 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
303 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
304 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
305 unset($this->_columns
[$tableName]['fields'][$fieldName]);
306 $columns[$tableName][$fieldName] = $field;
314 if (!empty($select)) {
315 $this->_select
.= ', ' . implode(', ', $select) . " ";
318 // put the fields that were unset, back in place
319 foreach ($columns as $tableName => $table) {
320 foreach ($table as $fieldName => $fields) {
321 $this->_columns
[$tableName]['fields'][$fieldName] = $fields;
326 ksort($this->_columnHeaders
);
331 FROM civicrm_contact {$this->_aliases['civicrm_contact']}";
334 INNER JOIN civicrm_mailing_event_queue
335 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
336 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
337 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id";
339 if (array_key_exists('delivery_id', $this->_params
['fields'])) {
341 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
342 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = civicrm_mailing_event_queue.id
343 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
344 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id";
345 if (CRM_Utils_Array
::value('delivery_status_value', $this->_params
) == 'bounced') {
346 $this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']['clause'] = "{$this->_aliases['civicrm_mailing_event_bounce']}.id IS NOT NULL";
348 elseif (CRM_Utils_Array
::value('delivery_status_value', $this->_params
) == 'successful') {
349 $this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']['clause'] = "{$this->_aliases['civicrm_mailing_event_delivered']}.id IS NOT NULL";
353 unset($this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']);
356 if (array_key_exists('reply_id', $this->_params
['fields']) ||
357 is_numeric(CRM_Utils_Array
::value('is_replied_value', $this->_params
))
359 if (CRM_Utils_Array
::value('is_replied_value', $this->_params
) == 1) {
361 $this->_columns
['civicrm_mailing_event_reply']['filters']['is_replied']['clause'] = '(1)';
367 {$joinType} JOIN civicrm_mailing_event_reply {$this->_aliases['civicrm_mailing_event_reply']}
368 ON {$this->_aliases['civicrm_mailing_event_reply']}.event_queue_id = civicrm_mailing_event_queue.id";
371 unset($this->_columns
['civicrm_mailing_event_reply']['filters']['is_replied']);
374 if (array_key_exists('unsubscribe_id', $this->_params
['fields']) ||
375 is_numeric(CRM_Utils_Array
::value('is_unsubscribed_value', $this->_params
))
377 if (CRM_Utils_Array
::value('is_unsubscribed_value', $this->_params
) == 1) {
379 $this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_unsubscribed']['clause'] = '(1)';
385 {$joinType} JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
386 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = civicrm_mailing_event_queue.id
387 AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}.org_unsubscribe = 0";
390 unset($this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_unsubscribed']);
393 if (array_key_exists('optout_id', $this->_params
['fields']) ||
394 is_numeric(CRM_Utils_Array
::value('is_optout_value', $this->_params
))
396 if (CRM_Utils_Array
::value('is_optout_value', $this->_params
) == 1) {
398 $this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_optout']['clause'] = '(1)';
404 {$joinType} JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}2
405 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}2.event_queue_id = civicrm_mailing_event_queue.id
406 AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}2.org_unsubscribe = 1";
409 unset($this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_optout']);
412 if (array_key_exists('forward_id', $this->_params
['fields']) ||
413 is_numeric(CRM_Utils_Array
::value('is_forwarded_value', $this->_params
))
415 if (CRM_Utils_Array
::value('is_forwarded_value', $this->_params
) == 1) {
417 $this->_columns
['civicrm_mailing_event_forward']['filters']['is_forwarded']['clause'] = '(1)';
423 {$joinType} JOIN civicrm_mailing_event_forward {$this->_aliases['civicrm_mailing_event_forward']}
424 ON {$this->_aliases['civicrm_mailing_event_forward']}.event_queue_id = civicrm_mailing_event_queue.id";
427 unset($this->_columns
['civicrm_mailing_event_forward']['filters']['is_forwarded']);
431 INNER JOIN civicrm_mailing_job
432 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
433 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
434 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
435 AND civicrm_mailing_job.is_test = 0";
437 if ($this->_phoneField
) {
439 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
440 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
441 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
447 $this->_where
.= " AND {$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
453 function mailingList() {
456 $mailing = new CRM_Mailing_BAO_Mailing();
457 $query = "SELECT name FROM civicrm_mailing ";
458 $mailing->query($query);
460 while ($mailing->fetch()) {
461 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
470 function alterDisplay(&$rows) {
471 // custom code to alter rows
473 foreach ($rows as $rowNum => $row) {
474 // make count columns point to detail report
475 // convert display name to links
476 if (array_key_exists('civicrm_contact_sort_name', $row) &&
477 array_key_exists('civicrm_contact_id', $row)
479 $url = CRM_Utils_System
::url('civicrm/contact/view',
480 'reset=1&cid=' . $row['civicrm_contact_id'],
483 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
484 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
488 // skip looking further in rows, if first row itself doesn't
489 // have the column we need