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