CRM-14106 - Regex targeting inline conditonal statements
[civicrm-core.git] / CRM / Report / Form / Mailing / Clicks.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
232624b1 5 | CiviCRM version 4.4 |
6a488035
TO
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36class CRM_Report_Form_Mailing_Clicks extends CRM_Report_Form {
37
38 protected $_summary = NULL;
39
40 protected $_emailField = FALSE;
41
42 protected $_phoneField = FALSE;
43
44 protected $_customGroupExtends = array('Contact', 'Individual', 'Household', 'Organization');
45
46 protected $_charts = array(
47 '' => 'Tabular',
48 'barChart' => 'Bar Chart',
49 'pieChart' => 'Pie Chart',
50 );
2f4c2f5d 51
6a488035
TO
52 function __construct() {
53 $this->_columns = array();
54
55 $this->_columns['civicrm_contact'] = array(
56 'dao' => 'CRM_Contact_DAO_Contact',
57 'fields' => array(
58 'id' => array(
59 'title' => ts('Contact ID'),
60 'required' => TRUE,
61 ),
62 'sort_name' =>
63 array(
64 'title' => ts('Contact Name'),
65 'required' => TRUE,
66 ),
67 ),
68 'filters' => array(
69 'sort_name' => array(
70 'title' => ts('Contact Name'),
71 ),
72 'source' => array(
73 'title' => ts('Contact Source'),
74 'type' => CRM_Utils_Type::T_STRING,
75 ),
76 'id' => array(
77 'title' => ts('Contact ID'),
78 'no_display' => TRUE,
79 ),
80 ),
81 'order_bys' =>
82 array(
83 'sort_name' =>
84 array('title' => ts('Contact Name'), 'default' => TRUE, 'default_order' => 'ASC'),
85 ),
86 'grouping' => 'contact-fields',
87 );
88
89 $this->_columns['civicrm_mailing'] = array(
90 'dao' => 'CRM_Mailing_DAO_Mailing',
91 'fields' => array(
92 'mailing_name' => array(
93 'name' => 'name',
94 'title' => ts('Mailing'),
95 'default' => TRUE,
96 ),
97 'mailing_name_alias' => array(
98 'name' => 'name',
99 'required' => TRUE,
100 'no_display' => TRUE,
101 ),
102 ),
103 'filters' => array(
104 'mailing_id' => array(
105 'name' => 'id',
106 'title' => ts('Mailing'),
107 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
108 'type' => CRM_Utils_Type::T_INT,
109 'options' => CRM_Mailing_BAO_Mailing::getMailingsList(),
110 'operator' => 'like',
111 ),
112 ),
113 'order_bys' =>
114 array(
115 'mailing_name' =>
116 array(
117 'name' => 'name',
118 'title' => ts('Mailing'),
119 ),
120 ),
121 'grouping' => 'mailing-fields',
122 );
123
124 $this->_columns['civicrm_email'] = array(
125 'dao' => 'CRM_Core_DAO_Email',
126 'fields' => array(
127 'email' => array(
128 'title' => ts('Email'),
129 'no_repeat' => TRUE,
130 'required' => TRUE,
131 ),
132 ),
133 'grouping' => 'contact-fields',
134 );
135
136 $this->_columns['civicrm_phone'] = array(
137 'dao' => 'CRM_Core_DAO_Phone',
138 'fields' => array('phone' => NULL),
139 'grouping' => 'contact-fields',
140 );
141
142 $this->_columns['civicrm_mailing_trackable_url'] = array(
143 'dao' => 'CRM_Mailing_DAO_TrackableURL',
144 'fields' => array(
145 'url' => array(
146 'title' => ts('Click through URL'),
147 ),
148 ),
149 'order_bys' =>
150 array(
151 'url' =>
152 array('title' => ts('Click through URL')),
153 ),
154 'grouping' => 'mailing-fields',
155 );
156
157 $this->_columns['civicrm_group'] = array(
158 'dao' => 'CRM_Contact_DAO_Group',
159 'alias' => 'cgroup',
160 'filters' => array(
161 'gid' => array(
162 'name' => 'group_id',
163 'title' => ts('Group'),
164 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
165 'group' => TRUE,
166 'options' => CRM_Core_PseudoConstant::group(),
167 ),
168 ),
169 );
170
171 $this->_tagFilter = TRUE;
172 parent::__construct();
173 }
174
175 function preProcess() {
176 $this->assign('chartSupported', TRUE);
177 parent::preProcess();
178 }
179
180 function select() {
181 $select = array();
182 $this->_columnHeaders = array();
183 foreach ($this->_columns as $tableName => $table) {
184 if (array_key_exists('fields', $table)) {
185 foreach ($table['fields'] as $fieldName => $field) {
186 if (CRM_Utils_Array::value('required', $field) ||
187 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
188 ) {
189 if ($tableName == 'civicrm_email') {
190 $this->_emailField = TRUE;
191 }
192 elseif ($tableName == 'civicrm_phone') {
193 $this->_phoneField = TRUE;
194 }
195
196 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
197 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
198 $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field);
199 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
200 }
201 }
202 }
203 }
204
205 if (CRM_Utils_Array::value('charts', $this->_params)) {
206 $select[] = "COUNT(civicrm_mailing_event_trackable_url_open.id) as civicrm_mailing_click_count";
207 $this->_columnHeaders["civicrm_mailing_click_count"]['title'] = ts('Click Count');
208 }
209
210 $this->_select = "SELECT " . implode(', ', $select) . " ";
211 }
212
213 static function formRule($fields, $files, $self) {
214 $errors = $grouping = array();
215 return $errors;
216 }
217
218 function from() {
219 $this->_from = "
220 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
221
222 $this->_from .= "
2f4c2f5d 223 INNER JOIN civicrm_mailing_event_queue
224 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
225 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
226 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
227 INNER JOIN civicrm_mailing_event_trackable_url_open
228 ON civicrm_mailing_event_trackable_url_open.event_queue_id = civicrm_mailing_event_queue.id
229 INNER JOIN civicrm_mailing_trackable_url {$this->_aliases['civicrm_mailing_trackable_url']}
230 ON civicrm_mailing_event_trackable_url_open.trackable_url_id = {$this->_aliases['civicrm_mailing_trackable_url']}.id
231 INNER JOIN civicrm_mailing_job
232 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
233 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
234 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
235 AND civicrm_mailing_job.is_test = 0
236 ";
6a488035
TO
237 if ($this->_phoneField) {
238 $this->_from .= "
2f4c2f5d 239 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
240 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
6a488035
TO
241 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
242 }
243 }
244
245 function where() {
246 parent::where();
247 $this->_where .= " AND {$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
248 }
249
250 function groupBy() {
251
252 $this->_groupBy = '';
253 if (CRM_Utils_Array::value('charts', $this->_params)) {
254 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_mailing']}.id";
255 }
256 else {
257 $this->_groupBy = " GROUP BY civicrm_mailing_event_trackable_url_open.id";
258 }
259 }
260
261 function postProcess() {
262
263 $this->beginPostProcess();
264
265 // get the acl clauses built before we assemble the query
266 $this->buildACLClause($this->_aliases['civicrm_contact']);
267
268 $sql = $this->buildQuery(TRUE);
269
270 $rows = $graphRows = array();
271 $this->buildRows($sql, $rows);
272
273 $this->formatDisplay($rows);
274 $this->doTemplateAssignment($rows);
275 $this->endPostProcess($rows);
276 }
277
278 function buildChart(&$rows) {
279 if (empty($rows)) {
280 return;
281 }
282
283 $chartInfo = array('legend' => ts('Mail Clickthrough Report'),
284 'xname' => ts('Mailing'),
285 'yname' => ts('Clicks'),
286 'xLabelAngle' => 20,
287 'tip' => ts('Clicks: %1', array(1 => '#val#')),
288 );
289 foreach ($rows as $row) {
290 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_click_count'];
291 }
292
293 // build the chart.
294 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
295 $this->assign('chartType', $this->_params['charts']);
296 }
297
298 function alterDisplay(&$rows) {
299 // custom code to alter rows
300 $entryFound = FALSE;
301 foreach ($rows as $rowNum => $row) {
302 // make count columns point to detail report
303 // convert display name to links
304 if (array_key_exists('civicrm_contact_sort_name', $row) &&
305 array_key_exists('civicrm_contact_id', $row)
306 ) {
307 $url = CRM_Utils_System::url('civicrm/contact/view',
308 'reset=1&cid=' . $row['civicrm_contact_id'],
309 $this->_absoluteUrl
310 );
311 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
312 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
313 $entryFound = TRUE;
314 }
315
316 // skip looking further in rows, if first row itself doesn't
317 // have the column we need
318 if (!$entryFound) {
319 break;
320 }
321 }
322 }
323}
324