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