Merge pull request #4892 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Mailing / Event / BAO / Queue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Mailing_Event_BAO_Queue extends CRM_Mailing_Event_DAO_Queue {
36
37 /**
100fef9d 38 * Class constructor
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
45 * Queue a new recipient
46 *
90c8230e
TO
47 * @param array $params
48 * Values of the new EventQueue.
6a488035 49 *
07c09ae4 50 * @return CRM_Mailing_Event_BAO_Queue The new EventQueue
6a488035
TO
51 * @static
52 */
07c09ae4 53 public static function create($params) {
6a488035
TO
54 $eq = new CRM_Mailing_Event_BAO_Queue();
55 $eq->copyValues($params);
bd6658bd
TO
56 if (empty($params['id']) && empty($params['hash'])) {
57 $eq->hash = self::hash($params);
58 }
6a488035
TO
59 $eq->save();
60 return $eq;
61 }
62
63 /**
64 * Create a security hash from the job, email and contact ids
65 *
90c8230e 66 * @param array The ids to be hashed
6a488035
TO
67 *
68 * @return int The hash
6a488035
TO
69 * @static
70 */
71 public static function hash($params) {
72 $jobId = $params['job_id'];
bd6658bd 73 $emailId = CRM_Utils_Array::value('email_id', $params, '');
6a488035
TO
74 $contactId = $params['contact_id'];
75
76 return substr(sha1("{$jobId}:{$emailId}:{$contactId}:" . time()),
77 0, 16
78 );
79 }
80
81 /**
82 * Verify that a queue event exists with the specified id/job id/hash
83 *
90c8230e
TO
84 * @param int $job_id
85 * The job ID of the event to find.
86 * @param int $queue_id
87 * The Queue Event ID to find.
88 * @param string $hash
89 * The hash to validate against.
6a488035
TO
90 *
91 * @return object|null The queue event if verified, or null
6a488035
TO
92 * @static
93 */
94 public static function &verify($job_id, $queue_id, $hash) {
95 $success = NULL;
96 $q = new CRM_Mailing_Event_BAO_Queue();
97 if (!empty($job_id) && !empty($queue_id) && !empty($hash)) {
98 $q->id = $queue_id;
99 $q->job_id = $job_id;
100 $q->hash = $hash;
101 if ($q->find(TRUE)) {
102 $success = $q;
103 }
104 }
105 return $success;
106 }
107
108 /**
109 * Given a queue event ID, find the corresponding email address.
110 *
90c8230e
TO
111 * @param int $queue_id
112 * The queue event ID.
6a488035
TO
113 *
114 * @return string The email address
6a488035
TO
115 * @static
116 */
117 public static function getEmailAddress($queue_id) {
118 $email = CRM_Core_BAO_Email::getTableName();
119 $eq = self::getTableName();
03e04002 120 $query = " SELECT $email.email as email
121 FROM $email
122 INNER JOIN $eq
123 ON $eq.email_id = $email.id
6a488035
TO
124 WHERE $eq.id = " . CRM_Utils_Type::rule($queue_id, 'Integer');
125
126 $q = new CRM_Mailing_Event_BAO_Queue();
127 $q->query($query);
128 if (!$q->fetch()) {
129 return NULL;
130 }
131
132 return $q->email;
133 }
134
135 /**
136 * Count up events given a mailing id and optional job id
137 *
90c8230e
TO
138 * @param int $mailing_id
139 * ID of the mailing to count.
140 * @param int $job_id
141 * Optional ID of a job to limit results.
6a488035
TO
142 *
143 * @return int Number of matching events
6a488035
TO
144 * @static
145 */
146 public static function getTotalCount($mailing_id, $job_id = NULL) {
147 $dao = new CRM_Core_DAO();
148
149 $queue = self::getTableName();
150 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
9da8dc8c 151 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
152
153 $dao->query("
154 SELECT COUNT(*) as queued
155 FROM $queue
156 INNER JOIN $job
157 ON $queue.job_id = $job.id
158 INNER JOIN $mailing
159 ON $job.mailing_id = $mailing.id
160 AND $job.is_test = 0
161 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer') . ($job_id ? " AND $job.id = " . CRM_Utils_Type::escape($job_id,
162 'Integer'
163 ) : '')
164 );
165
166 $dao->fetch();
167 return $dao->queued;
168 }
169
170 /**
171 * Get rows for the event browser
172 *
90c8230e
TO
173 * @param int $mailing_id
174 * ID of the mailing.
175 * @param int $job_id
176 * Optional ID of the job.
177 * @param int $offset
178 * Offset.
179 * @param int $rowCount
180 * Number of rows.
181 * @param array $sort
182 * Sort array.
6a488035
TO
183 *
184 * @return array Result set
6a488035
TO
185 * @static
186 */
a3d7e8ee
TO
187 public static function &getRows(
188 $mailing_id, $job_id = NULL, $offset = NULL,
6a488035
TO
189 $rowCount = NULL, $sort = NULL
190 ) {
191 $dao = new CRM_Core_Dao();
192
193 $queue = self::getTableName();
194 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
9da8dc8c 195 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
196 $contact = CRM_Contact_BAO_Contact::getTableName();
197 $email = CRM_Core_BAO_Email::getTableName();
198
199 $orderBy = "sort_name ASC, {$job}.start_date DESC";
200 if ($sort) {
201 if (is_string($sort)) {
21d32567 202 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
203 $orderBy = $sort;
204 }
205 else {
206 $orderBy = trim($sort->orderBy());
207 }
208 }
209
210 $query = "
211 SELECT $contact.display_name as display_name,
212 $contact.id as contact_id,
213 $email.email as email,
214 $job.start_date as date
215 FROM $contact
216 INNER JOIN $queue
217 ON $queue.contact_id = $contact.id
218 INNER JOIN $email
219 ON $queue.email_id = $email.id
220 INNER JOIN $job
221 ON $queue.job_id = $job.id
222 INNER JOIN $mailing
223 ON $job.mailing_id = $mailing.id
224 AND $job.is_test = 0
225 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
226
227 if (!empty($job_id)) {
228 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
229 }
230
231 $query .= " ORDER BY {$orderBy} ";
232
233 if ($offset || $rowCount) {
234 //Added "||$rowCount" to avoid displaying all records on first page
235 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
236 }
237
238 $dao->query($query);
239
240 $results = array();
241
242 while ($dao->fetch()) {
243 $url = CRM_Utils_System::url('civicrm/contact/view',
244 "reset=1&cid={$dao->contact_id}"
245 );
246 $results[] = array(
247 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
248 'email' => $dao->email,
249 'date' => CRM_Utils_Date::customFormat($dao->date),
250 );
251 }
252 return $results;
253 }
254
255 /**
256 * Get the mailing object for this queue event instance
257 *
258 * @param
259 *
260 * @return object Mailing BAO
6a488035
TO
261 */
262 public function &getMailing() {
263 $mailing = new CRM_Mailing_BAO_Mailing();
9da8dc8c 264 $jobs = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
265 $mailings = CRM_Mailing_BAO_Mailing::getTableName();
266 $queue = self::getTableName();
267
268 $mailing->query("
269 SELECT $mailings.*
270 FROM $mailings
271 INNER JOIN $jobs
272 ON $jobs.mailing_id = $mailings.id
273 INNER JOIN $queue
274 ON $queue.job_id = $jobs.id
275 WHERE $queue.id = {$this->id}");
276 $mailing->fetch();
277 return $mailing;
278 }
279
e0ef6999 280 /**
100fef9d 281 * @param int $queueID
e0ef6999
EM
282 *
283 * @return array
284 */
6a488035
TO
285 public static function getContactInfo($queueID) {
286 $query = "
287SELECT DISTINCT(civicrm_mailing_event_queue.contact_id) as contact_id,
288 civicrm_contact.display_name as display_name,
289 civicrm_email.email as email
290 FROM civicrm_mailing_event_queue,
291 civicrm_contact,
292 civicrm_email
293 WHERE civicrm_mailing_event_queue.contact_id = civicrm_contact.id
294 AND civicrm_mailing_event_queue.email_id = civicrm_email.id
295 AND civicrm_mailing_event_queue.id = " . CRM_Utils_Type::escape($queueID, 'Integer');
296
297 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
298
299 $displayName = 'Unknown';
300 $email = 'Unknown';
301 if ($dao->fetch()) {
302 $displayName = $dao->display_name;
303 $email = $dao->email;
304 }
305
306 return array($displayName, $email);
307 }
308
e0ef6999 309 /**
c490a46a 310 * @param array $params
e0ef6999
EM
311 * @param null $now
312 */
00be9182 313 public static function bulkCreate($params, $now = NULL) {
6a488035
TO
314 if (!$now) {
315 $now = time();
316 }
317
318 // construct a bulk insert statement
319 $values = array();
320 foreach ($params as $param) {
321 $values[] = "( {$param[0]}, {$param[1]}, {$param[2]}, {$param[3]}, '" . substr(sha1("{$param[0]}:{$param[1]}:{$param[2]}:{$param[3]}:{$now}"),
322 0, 16
323 ) . "' )";
324 }
325
326 while (!empty($values)) {
327 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
328 $str = implode(',', $input);
329 $sql = "INSERT INTO civicrm_mailing_event_queue ( job_id, email_id, contact_id, phone_id, hash ) VALUES $str;";
330 CRM_Core_DAO::executeQuery($sql);
331 }
332 }
333}