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