copyright and version fixes
[civicrm-core.git] / CRM / Report / Form / Mailing / Opened.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
33 * $Id$
34 *
35 */
36 class CRM_Report_Form_Mailing_Opened 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 );
51
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' =>
92 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 'order_bys' =>
135 array(
136 'email' =>
137 array('title' => ts('Email'), 'default_order' => 'ASC'),
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_group'] = array(
149 'dao' => 'CRM_Contact_DAO_Group',
150 'alias' => 'cgroup',
151 'filters' => array(
152 'gid' => array(
153 'name' => 'group_id',
154 'title' => ts('Group'),
155 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
156 'group' => TRUE,
157 'options' => CRM_Core_PseudoConstant::group(),
158 ),
159 ),
160 );
161
162 $this->_tagFilter = TRUE;
163 parent::__construct();
164 }
165
166 function preProcess() {
167 $this->assign('chartSupported', TRUE);
168 parent::preProcess();
169 }
170
171 function select() {
172 $select = array();
173 $this->_columnHeaders = array();
174 foreach ($this->_columns as $tableName => $table) {
175 if (array_key_exists('fields', $table)) {
176 foreach ($table['fields'] as $fieldName => $field) {
177 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
178 if ($tableName == 'civicrm_email') {
179 $this->_emailField = TRUE;
180 }
181 elseif ($tableName == 'civicrm_phone') {
182 $this->_phoneField = TRUE;
183 }
184
185 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
186 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
187 $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field);
188 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
189 }
190 }
191 }
192 }
193
194 if (!empty($this->_params['charts'])) {
195 $select[] = "COUNT(civicrm_mailing_event_opened.id) as civicrm_mailing_opened_count";
196 $this->_columnHeaders["civicrm_mailing_opened_count"]['title'] = ts('Opened Count');
197 }
198
199 $this->_select = "SELECT " . implode(', ', $select) . " ";
200 }
201
202 static function formRule($fields, $files, $self) {
203 $errors = $grouping = array();
204 return $errors;
205 }
206
207 function from() {
208 $this->_from = "
209 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
210
211 $this->_from .= "
212 INNER JOIN civicrm_mailing_event_queue
213 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
214 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
215 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
216 INNER JOIN civicrm_mailing_event_opened
217 ON civicrm_mailing_event_opened.event_queue_id = civicrm_mailing_event_queue.id
218 INNER JOIN civicrm_mailing_job
219 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
220 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
221 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
222 AND civicrm_mailing_job.is_test = 0
223 ";
224
225 if ($this->_phoneField) {
226 $this->_from .= "
227 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
228 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
229 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
230 }
231 }
232
233 function where() {
234 parent::where();
235 $this->_where .= " AND {$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
236 }
237
238 function groupBy() {
239 if (!empty($this->_params['charts'])) {
240 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_mailing']}.id";
241 }
242 else {
243 $this->_groupBy = " GROUP BY civicrm_mailing_event_queue.email_id";
244 }
245 }
246
247 function postProcess() {
248
249 $this->beginPostProcess();
250
251 // get the acl clauses built before we assemble the query
252 $this->buildACLClause($this->_aliases['civicrm_contact']);
253
254 $sql = $this->buildQuery(TRUE);
255
256 $rows = $graphRows = array();
257 $this->buildRows($sql, $rows);
258
259 $this->formatDisplay($rows);
260 $this->doTemplateAssignment($rows);
261 $this->endPostProcess($rows);
262 }
263
264 function buildChart(&$rows) {
265 if (empty($rows)) {
266 return;
267 }
268
269 $chartInfo = array('legend' => ts('Mail Opened Report'),
270 'xname' => ts('Mailing'),
271 'yname' => ts('Opened'),
272 'xLabelAngle' => 20,
273 'tip' => ts('Mail Opened: %1', array(1 => '#val#')),
274 );
275 foreach ($rows as $row) {
276 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_opened_count'];
277 }
278
279 // build the chart.
280 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
281 $this->assign('chartType', $this->_params['charts']);
282 }
283
284 function alterDisplay(&$rows) {
285 // custom code to alter rows
286 $entryFound = FALSE;
287 foreach ($rows as $rowNum => $row) {
288 // make count columns point to detail report
289 // convert display name to links
290 if (array_key_exists('civicrm_contact_sort_name', $row) &&
291 array_key_exists('civicrm_contact_id', $row)
292 ) {
293 $url = CRM_Utils_System::url('civicrm/contact/view',
294 'reset=1&cid=' . $row['civicrm_contact_id'],
295 $this->_absoluteUrl
296 );
297 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
298 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
299 $entryFound = TRUE;
300 }
301
302 // skip looking further in rows, if first row itself doesn't
303 // have the column we need
304 if (!$entryFound) {
305 break;
306 }
307 }
308 }
309 }
310