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