INFRA-132 - Fix incorrect param types
[civicrm-core.git] / CRM / Mailing / Selector / Event.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42 class CRM_Mailing_Selector_Event extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
43
44 /**
45 * Array of supported links, currenly null
46 *
47 * @var array
48 * @static
49 */
50 static $_links = NULL;
51
52 /**
53 * What event type are we browsing?
54 */
55 private $_event;
56
57 /**
58 * Should we only count distinct contacts?
59 */
60 private $_is_distinct;
61
62 /**
63 * Which mailing are we browsing events from?
64 */
65 private $_mailing_id;
66
67 /**
68 * Do we want events tied to a specific job?
69 */
70 private $_job_id;
71
72 /**
73 * For click-through events, do we only want those from a specific url?
74 */
75 private $_url_id;
76
77 /**
78 * We use desc to remind us what that column is, name is used in the tpl
79 *
80 * @var array
81 */
82 public $_columnHeaders;
83
84 /**
85 * Class constructor
86 *
87 * @param string $event
88 * The event type (queue/delivered/open...).
89 * @param bool $distinct
90 * Count only distinct contact events?.
91 * @param int $mailing
92 * ID of the mailing to query.
93 * @param int $job
94 * ID of the job to query. If null, all jobs from $mailing are queried.
95 * @param int $url
96 * If the event type is a click-through, do we want only those from a specific url?.
97 *
98 * @return \CRM_Mailing_Selector_Event
99 * @access public
100 */
101 public function __construct($event, $distinct, $mailing, $job = NULL, $url = NULL) {
102 $this->_event_type = $event;
103 $this->_is_distinct = $distinct;
104 $this->_mailing_id = $mailing;
105 $this->_job_id = $job;
106 $this->_url_id = $url;
107 }
108
109 /**
110 * This method returns the links that are given for each search row.
111 *
112 * @return array
113 * @static
114 */
115 public static function &links() {
116 return self::$_links;
117 }
118
119 /**
120 * Getter for array of the parameters required for creating pager.
121 *
122 * @param $action
123 * @param array $params
124 */
125 public function getPagerParams($action, &$params) {
126 $params['csvString'] = NULL;
127 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
128 $params['status'] = ts('%1 %%StatusMessage%%', array(1 => $this->eventToTitle()));
129 $params['buttonTop'] = 'PagerTopButton';
130 $params['buttonBottom'] = 'PagerBottomButton';
131 }
132
133 /**
134 * Returns the column headers as an array of tuples:
135 * (name, sortName (key to the sort array))
136 *
137 * @param string $action
138 * The action being performed.
139 * @param string $output
140 * What should the result set include (web/email/csv).
141 *
142 * @return array
143 * the column headers that need to be displayed
144 */
145 public function &getColumnHeaders($action = NULL, $output = NULL) {
146 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
147
148 $contact = CRM_Contact_BAO_Contact::getTableName();
149
150 $email = CRM_Core_BAO_Email::getTableName();
151
152 $job = CRM_Mailing_BAO_MailingJob::getTableName();
153 if (!isset($this->_columnHeaders)) {
154
155 $this->_columnHeaders = array(
156 array(
157 'name' => ts('Contact'),
158 'sort' => $contact . '.sort_name',
159 'direction' => CRM_Utils_Sort::ASCENDING,
160 ),
161 array(
162 'name' => ts('Email Address'),
163 'sort' => $email . '.email',
164 'direction' => CRM_Utils_Sort::DONTCARE,
165 ),
166 );
167
168 switch ($this->_event_type) {
169 case 'queue':
170 $dateSort = $job . '.start_date';
171 break;
172
173 case 'delivered':
174 $dateSort = CRM_Mailing_Event_BAO_Delivered::getTableName() . '.time_stamp';
175 break;
176
177 case 'opened':
178 $dateSort = CRM_Mailing_Event_BAO_Opened::getTableName() . '.time_stamp';
179 break;
180
181 case 'bounce':
182 $dateSort = CRM_Mailing_Event_BAO_Bounce::getTableName() . '.time_stamp';
183 $this->_columnHeaders = array_merge($this->_columnHeaders,
184 array(
185 array(
186 'name' => ts('Bounce Type'),
187 ),
188 array(
189 'name' => ts('Bounce Reason'),
190 ),
191 )
192 );
193 break;
194
195 case 'forward':
196 $dateSort = CRM_Mailing_Event_BAO_Forward::getTableName() . '.time_stamp';
197
198 $this->_columnHeaders = array_merge($this->_columnHeaders,
199 array(
200 array(
201 'name' => ts('Forwarded Email'),
202 ),
203 )
204 );
205 break;
206
207 case 'reply':
208 $dateSort = CRM_Mailing_Event_BAO_Reply::getTableName() . '.time_stamp';
209 break;
210
211 case 'unsubscribe':
212 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp';
213 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
214 array(
215 'name' => ts('Unsubscribe'),
216 ),
217 ));
218 break;
219
220 case 'optout':
221 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp';
222 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
223 array(
224 'name' => ts('Opt-Out'),
225 ),
226 ));
227 break;
228
229 case 'click':
230 $dateSort = CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName() . '.time_stamp';
231 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
232 array(
233 'name' => ts('URL'),
234 ),
235 ));
236 break;
237
238 default:
239 return 0;
240 }
241
242 $this->_columnHeaders = array_merge($this->_columnHeaders,
243 array(
244 array(
245 'name' => ts('Date'),
246 'sort' => $dateSort,
247 'direction' => CRM_Utils_Sort::DESCENDING,
248 ),
249 )
250 );
251 }
252 return $this->_columnHeaders;
253 }
254
255 /**
256 * Returns total number of rows for the query.
257 *
258 * @param
259 *
260 * @return int
261 * Total number of rows
262 */
263 public function getTotalCount($action) {
264 switch ($this->_event_type) {
265 case 'queue':
266 $event = new CRM_Mailing_Event_BAO_Queue();
267 return $event->getTotalCount($this->_mailing_id,
268 $this->_job_id
269 );
270
271 case 'delivered':
272 $event = new CRM_Mailing_Event_BAO_Delivered();
273 return $event->getTotalCount($this->_mailing_id,
274 $this->_job_id,
275 $this->_is_distinct
276 );
277
278 case 'opened':
279 $event = new CRM_Mailing_Event_BAO_Opened();
280 return $event->getTotalCount($this->_mailing_id,
281 $this->_job_id,
282 $this->_is_distinct
283 );
284
285 case 'bounce':
286 $event = new CRM_Mailing_Event_BAO_Bounce();
287 return $event->getTotalCount($this->_mailing_id,
288 $this->_job_id,
289 $this->_is_distinct
290 );
291
292 case 'forward':
293 $event = new CRM_Mailing_Event_BAO_Forward();
294 return $event->getTotalCount($this->_mailing_id,
295 $this->_job_id,
296 $this->_is_distinct
297 );
298
299 case 'reply':
300 $event = new CRM_Mailing_Event_BAO_Reply();
301 return $event->getTotalCount($this->_mailing_id,
302 $this->_job_id,
303 $this->_is_distinct
304 );
305
306 case 'unsubscribe':
307 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
308 return $event->getTotalCount($this->_mailing_id,
309 $this->_job_id,
310 $this->_is_distinct
311 );
312
313 case 'optout':
314 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
315 return $event->getTotalCount($this->_mailing_id,
316 $this->_job_id,
317 $this->_is_distinct,
318 FALSE
319 );
320
321 case 'click':
322 $event = new CRM_Mailing_Event_BAO_TrackableURLOpen();
323 return $event->getTotalCount($this->_mailing_id,
324 $this->_job_id,
325 $this->_is_distinct,
326 $this->_url_id
327 );
328
329 default:
330 return 0;
331 }
332 }
333
334 /**
335 * Returns all the rows in the given offset and rowCount
336 *
337 * @param string $action
338 * The action being performed.
339 * @param int $offset
340 * The row number to start from.
341 * @param int $rowCount
342 * The number of rows to return.
343 * @param string $sort
344 * The sql string that describes the sort order.
345 * @param string $output
346 * What should the result set include (web/email/csv).
347 *
348 * @return int
349 * the total number of rows for this action
350 */
351 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
352 switch ($this->_event_type) {
353 case 'queue':
354 return CRM_Mailing_Event_BAO_Queue::getRows($this->_mailing_id,
355 $this->_job_id, $offset, $rowCount, $sort
356 );
357 break;
358
359 case 'delivered':
360 return CRM_Mailing_Event_BAO_Delivered::getRows($this->_mailing_id,
361 $this->_job_id, $this->_is_distinct,
362 $offset, $rowCount, $sort
363 );
364
365 case 'opened':
366 return CRM_Mailing_Event_BAO_Opened::getRows($this->_mailing_id,
367 $this->_job_id, $this->_is_distinct,
368 $offset, $rowCount, $sort
369 );
370
371 case 'bounce':
372 return CRM_Mailing_Event_BAO_Bounce::getRows($this->_mailing_id,
373 $this->_job_id, $this->_is_distinct,
374 $offset, $rowCount, $sort
375 );
376
377 case 'forward':
378 return CRM_Mailing_Event_BAO_Forward::getRows($this->_mailing_id,
379 $this->_job_id, $this->_is_distinct,
380 $offset, $rowCount, $sort
381 );
382
383 case 'reply':
384 return CRM_Mailing_Event_BAO_Reply::getRows($this->_mailing_id,
385 $this->_job_id, $this->_is_distinct,
386 $offset, $rowCount, $sort
387 );
388
389 case 'unsubscribe':
390 return CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id,
391 $this->_job_id, $this->_is_distinct,
392 $offset, $rowCount, $sort, TRUE
393 );
394
395 case 'optout':
396 return CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id,
397 $this->_job_id, $this->_is_distinct,
398 $offset, $rowCount, $sort, FALSE
399 );
400
401 case 'click':
402 return CRM_Mailing_Event_BAO_TrackableURLOpen::getRows(
403 $this->_mailing_id, $this->_job_id,
404 $this->_is_distinct, $this->_url_id,
405 $offset, $rowCount, $sort
406 );
407
408 default:
409 return NULL;
410 }
411 }
412
413 /**
414 * Name of export file.
415 *
416 * @param string $output
417 * Type of output.
418 *
419 * @return string
420 * name of the file
421 */
422 public function getExportFileName($output = 'csv') {
423 }
424
425 public function eventToTitle() {
426 static $events = NULL;
427
428 if (empty($events)) {
429 $events = array(
430 'queue' => ts('Intended Recipients'),
431 'delivered' => ts('Successful Deliveries'),
432 'bounce' => ts('Bounces'),
433 'forward' => ts('Forwards'),
434 'reply' => $this->_is_distinct
435 ? ts('Unique Replies')
436 : ts('Replies'),
437 'unsubscribe' => ts('Unsubscribe Requests'),
438 'optout' => ts('Opt-out Requests'),
439 'click' => $this->_is_distinct
440 ? ts('Unique Click-throughs')
441 : ts('Click-throughs'),
442 'opened' => $this->_is_distinct
443 ? ts('Unique Tracked Opens')
444 : ts('Tracked Opens'),
445 );
446 }
447 return $events[$this->_event_type];
448 }
449
450 public function getTitle() {
451 return $this->eventToTitle();
452 }
453 }