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