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