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