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