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