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