Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Mailing_Event_BAO_Queue extends CRM_Mailing_Event_DAO_Queue { | |
18 | ||
6a488035 | 19 | /** |
fe482240 | 20 | * Queue a new recipient. |
6a488035 | 21 | * |
90c8230e TO |
22 | * @param array $params |
23 | * Values of the new EventQueue. | |
6a488035 | 24 | * |
16b10e64 CW |
25 | * @return CRM_Mailing_Event_BAO_Queue |
26 | * The new EventQueue | |
6a488035 | 27 | */ |
07c09ae4 | 28 | public static function create($params) { |
6a488035 TO |
29 | $eq = new CRM_Mailing_Event_BAO_Queue(); |
30 | $eq->copyValues($params); | |
bd6658bd TO |
31 | if (empty($params['id']) && empty($params['hash'])) { |
32 | $eq->hash = self::hash($params); | |
33 | } | |
6a488035 TO |
34 | $eq->save(); |
35 | return $eq; | |
36 | } | |
37 | ||
38 | /** | |
54957108 | 39 | * Create a security hash from the job, email and contact ids. |
40 | * | |
41 | * @param array $params | |
6a488035 | 42 | * |
a2f24340 | 43 | * @return string |
a6c01b45 | 44 | * The hash |
6a488035 TO |
45 | */ |
46 | public static function hash($params) { | |
353ffa53 TO |
47 | $jobId = $params['job_id']; |
48 | $emailId = CRM_Utils_Array::value('email_id', $params, ''); | |
6a488035 TO |
49 | $contactId = $params['contact_id']; |
50 | ||
51 | return substr(sha1("{$jobId}:{$emailId}:{$contactId}:" . time()), | |
52 | 0, 16 | |
53 | ); | |
54 | } | |
55 | ||
56 | /** | |
54957108 | 57 | * Verify that a queue event exists with the specified id/job id/hash. |
6a488035 | 58 | * |
90c8230e TO |
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. | |
6a488035 | 65 | * |
72b3a70c CW |
66 | * @return object|null |
67 | * The queue event if verified, or null | |
6a488035 TO |
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)) { | |
353ffa53 | 73 | $q->id = $queue_id; |
6a488035 | 74 | $q->job_id = $job_id; |
353ffa53 | 75 | $q->hash = $hash; |
6a488035 TO |
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 | * | |
90c8230e TO |
86 | * @param int $queue_id |
87 | * The queue event ID. | |
6a488035 | 88 | * |
a6c01b45 CW |
89 | * @return string |
90 | * The email address | |
6a488035 TO |
91 | */ |
92 | public static function getEmailAddress($queue_id) { | |
93 | $email = CRM_Core_BAO_Email::getTableName(); | |
353ffa53 | 94 | $eq = self::getTableName(); |
03e04002 | 95 | $query = " SELECT $email.email as email |
96 | FROM $email | |
97 | INNER JOIN $eq | |
98 | ON $eq.email_id = $email.id | |
6a488035 TO |
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 | /** | |
fe482240 | 111 | * Count up events given a mailing id and optional job id. |
6a488035 | 112 | * |
90c8230e TO |
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. | |
6a488035 | 117 | * |
a6c01b45 CW |
118 | * @return int |
119 | * Number of matching events | |
6a488035 TO |
120 | */ |
121 | public static function getTotalCount($mailing_id, $job_id = NULL) { | |
122 | $dao = new CRM_Core_DAO(); | |
123 | ||
353ffa53 | 124 | $queue = self::getTableName(); |
6a488035 | 125 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 126 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
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 | /** | |
fe482240 | 146 | * Get rows for the event browser. |
6a488035 | 147 | * |
90c8230e TO |
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. | |
6a488035 | 158 | * |
a6c01b45 CW |
159 | * @return array |
160 | * Result set | |
6a488035 | 161 | */ |
a3d7e8ee TO |
162 | public static function &getRows( |
163 | $mailing_id, $job_id = NULL, $offset = NULL, | |
6a488035 TO |
164 | $rowCount = NULL, $sort = NULL |
165 | ) { | |
0fc59d7a | 166 | $dao = new CRM_Core_DAO(); |
6a488035 | 167 | |
353ffa53 | 168 | $queue = self::getTableName(); |
6a488035 | 169 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 170 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 | 171 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
353ffa53 | 172 | $email = CRM_Core_BAO_Email::getTableName(); |
6a488035 TO |
173 | |
174 | $orderBy = "sort_name ASC, {$job}.start_date DESC"; | |
175 | if ($sort) { | |
176 | if (is_string($sort)) { | |
21d32567 | 177 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
178 | $orderBy = $sort; |
179 | } | |
180 | else { | |
181 | $orderBy = trim($sort->orderBy()); | |
182 | } | |
183 | } | |
184 | ||
185 | $query = " | |
4d20926e JP |
186 | SELECT $queue.id as queue_id, |
187 | $contact.display_name as display_name, | |
6a488035 TO |
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 | ||
be2fb01f | 216 | $results = []; |
6a488035 TO |
217 | |
218 | while ($dao->fetch()) { | |
219 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
220 | "reset=1&cid={$dao->contact_id}" | |
221 | ); | |
be2fb01f | 222 | $results[$dao->queue_id] = [ |
6a488035 TO |
223 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", |
224 | 'email' => $dao->email, | |
225 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
be2fb01f | 226 | ]; |
6a488035 TO |
227 | } |
228 | return $results; | |
229 | } | |
230 | ||
231 | /** | |
fe482240 | 232 | * Get the mailing object for this queue event instance. |
6a488035 TO |
233 | * |
234 | * @param | |
235 | * | |
a6c01b45 CW |
236 | * @return object |
237 | * Mailing BAO | |
6a488035 TO |
238 | */ |
239 | public function &getMailing() { | |
353ffa53 TO |
240 | $mailing = new CRM_Mailing_BAO_Mailing(); |
241 | $jobs = CRM_Mailing_BAO_MailingJob::getTableName(); | |
6a488035 | 242 | $mailings = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 243 | $queue = self::getTableName(); |
6a488035 TO |
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 | ||
e0ef6999 | 257 | /** |
100fef9d | 258 | * @param int $queueID |
e0ef6999 EM |
259 | * |
260 | * @return array | |
261 | */ | |
6a488035 TO |
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 | ||
33621c4f | 274 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
275 | |
276 | $displayName = 'Unknown'; | |
277 | $email = 'Unknown'; | |
278 | if ($dao->fetch()) { | |
279 | $displayName = $dao->display_name; | |
280 | $email = $dao->email; | |
fabc9b13 | 281 | $contact_id = $dao->contact_id; |
6a488035 TO |
282 | } |
283 | ||
fabc9b13 | 284 | return [$displayName, $email, $contact_id]; |
6a488035 TO |
285 | } |
286 | ||
e0ef6999 | 287 | /** |
c490a46a | 288 | * @param array $params |
e0ef6999 EM |
289 | * @param null $now |
290 | */ | |
00be9182 | 291 | public static function bulkCreate($params, $now = NULL) { |
6a488035 TO |
292 | if (!$now) { |
293 | $now = time(); | |
294 | } | |
295 | ||
296 | // construct a bulk insert statement | |
be2fb01f | 297 | $values = []; |
6a488035 TO |
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}"), | |
353ffa53 TO |
300 | 0, 16 |
301 | ) . "' )"; | |
6a488035 TO |
302 | } |
303 | ||
304 | while (!empty($values)) { | |
305 | $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT); | |
353ffa53 TO |
306 | $str = implode(',', $input); |
307 | $sql = "INSERT INTO civicrm_mailing_event_queue ( job_id, email_id, contact_id, phone_id, hash ) VALUES $str;"; | |
6a488035 TO |
308 | CRM_Core_DAO::executeQuery($sql); |
309 | } | |
310 | } | |
96025800 | 311 | |
6a488035 | 312 | } |