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->_groupFilter
= TRUE;
267 $this->_tagFilter
= TRUE;
269 parent
::__construct();
273 $select = $columns = array();
274 foreach ($this->_columns
as $tableName => $table) {
275 if (array_key_exists('fields', $table)) {
276 foreach ($table['fields'] as $fieldName => $field) {
277 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
278 if (in_array($fieldName, array(
279 'unsubscribe_id', 'optout_id', 'forward_id', 'reply_id'))) {
280 $select[] = "IF({$field['dbAlias']} IS NULL, 'No', 'Yes') as {$tableName}_{$fieldName}";
281 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
282 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
283 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
284 unset($this->_columns
[$tableName]['fields'][$fieldName]);
285 $columns[$tableName][$fieldName] = $field;
287 elseif ($fieldName == 'delivery_id') {
288 $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}";
289 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
290 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
291 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
292 unset($this->_columns
[$tableName]['fields'][$fieldName]);
293 $columns[$tableName][$fieldName] = $field;
301 if (!empty($select)) {
302 $this->_select
.= ', ' . implode(', ', $select) . " ";
305 // put the fields that were unset, back in place
306 foreach ($columns as $tableName => $table) {
307 foreach ($table as $fieldName => $fields) {
308 $this->_columns
[$tableName]['fields'][$fieldName] = $fields;
313 ksort($this->_columnHeaders
);
318 FROM civicrm_contact {$this->_aliases['civicrm_contact']}";
321 INNER JOIN civicrm_mailing_event_queue
322 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
323 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
324 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id";
326 if (array_key_exists('delivery_id', $this->_params
['fields'])) {
328 LEFT JOIN civicrm_mailing_event_delivered {$this->_aliases['civicrm_mailing_event_delivered']}
329 ON {$this->_aliases['civicrm_mailing_event_delivered']}.event_queue_id = civicrm_mailing_event_queue.id
330 LEFT JOIN civicrm_mailing_event_bounce {$this->_aliases['civicrm_mailing_event_bounce']}
331 ON {$this->_aliases['civicrm_mailing_event_bounce']}.event_queue_id = civicrm_mailing_event_queue.id";
332 if (CRM_Utils_Array
::value('delivery_status_value', $this->_params
) == 'bounced') {
333 $this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']['clause'] = "{$this->_aliases['civicrm_mailing_event_bounce']}.id IS NOT NULL";
335 elseif (CRM_Utils_Array
::value('delivery_status_value', $this->_params
) == 'successful') {
336 $this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']['clause'] = "{$this->_aliases['civicrm_mailing_event_delivered']}.id IS NOT NULL";
340 unset($this->_columns
['civicrm_mailing_event_delivered']['filters']['delivery_status']);
343 if (array_key_exists('reply_id', $this->_params
['fields']) ||
344 is_numeric(CRM_Utils_Array
::value('is_replied_value', $this->_params
))
346 if (CRM_Utils_Array
::value('is_replied_value', $this->_params
) == 1) {
348 $this->_columns
['civicrm_mailing_event_reply']['filters']['is_replied']['clause'] = '(1)';
354 {$joinType} JOIN civicrm_mailing_event_reply {$this->_aliases['civicrm_mailing_event_reply']}
355 ON {$this->_aliases['civicrm_mailing_event_reply']}.event_queue_id = civicrm_mailing_event_queue.id";
358 unset($this->_columns
['civicrm_mailing_event_reply']['filters']['is_replied']);
361 if (array_key_exists('unsubscribe_id', $this->_params
['fields']) ||
362 is_numeric(CRM_Utils_Array
::value('is_unsubscribed_value', $this->_params
))
364 if (CRM_Utils_Array
::value('is_unsubscribed_value', $this->_params
) == 1) {
366 $this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_unsubscribed']['clause'] = '(1)';
372 {$joinType} JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}
373 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}.event_queue_id = civicrm_mailing_event_queue.id
374 AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}.org_unsubscribe = 0";
377 unset($this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_unsubscribed']);
380 if (array_key_exists('optout_id', $this->_params
['fields']) ||
381 is_numeric(CRM_Utils_Array
::value('is_optout_value', $this->_params
))
383 if (CRM_Utils_Array
::value('is_optout_value', $this->_params
) == 1) {
385 $this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_optout']['clause'] = '(1)';
391 {$joinType} JOIN civicrm_mailing_event_unsubscribe {$this->_aliases['civicrm_mailing_event_unsubscribe']}2
392 ON {$this->_aliases['civicrm_mailing_event_unsubscribe']}2.event_queue_id = civicrm_mailing_event_queue.id
393 AND {$this->_aliases['civicrm_mailing_event_unsubscribe']}2.org_unsubscribe = 1";
396 unset($this->_columns
['civicrm_mailing_event_unsubscribe']['filters']['is_optout']);
399 if (array_key_exists('forward_id', $this->_params
['fields']) ||
400 is_numeric(CRM_Utils_Array
::value('is_forwarded_value', $this->_params
))
402 if (CRM_Utils_Array
::value('is_forwarded_value', $this->_params
) == 1) {
404 $this->_columns
['civicrm_mailing_event_forward']['filters']['is_forwarded']['clause'] = '(1)';
410 {$joinType} JOIN civicrm_mailing_event_forward {$this->_aliases['civicrm_mailing_event_forward']}
411 ON {$this->_aliases['civicrm_mailing_event_forward']}.event_queue_id = civicrm_mailing_event_queue.id";
414 unset($this->_columns
['civicrm_mailing_event_forward']['filters']['is_forwarded']);
418 INNER JOIN civicrm_mailing_job
419 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
420 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
421 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
422 AND civicrm_mailing_job.is_test = 0";
424 if ($this->_phoneField
) {
426 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
427 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
428 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
434 $this->_where
.= " AND {$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
440 function mailingList() {
443 $mailing = new CRM_Mailing_BAO_Mailing();
444 $query = "SELECT name FROM civicrm_mailing ";
445 $mailing->query($query);
447 while ($mailing->fetch()) {
448 $data[mysql_real_escape_string($mailing->name
)] = $mailing->name
;
457 function alterDisplay(&$rows) {
458 // custom code to alter rows
460 foreach ($rows as $rowNum => $row) {
461 // make count columns point to detail report
462 // convert display name to links
463 if (array_key_exists('civicrm_contact_sort_name', $row) &&
464 array_key_exists('civicrm_contact_id', $row)
466 $url = CRM_Utils_System
::url('civicrm/contact/view',
467 'reset=1&cid=' . $row['civicrm_contact_id'],
470 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
471 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
475 // skip looking further in rows, if first row itself doesn't
476 // have the column we need