Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_Event_BAO_Queue extends CRM_Mailing_Event_DAO_Queue { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
6a488035 | 37 | */ |
00be9182 | 38 | public function __construct() { |
6a488035 TO |
39 | parent::__construct(); |
40 | } | |
41 | ||
42 | /** | |
fe482240 | 43 | * Queue a new recipient. |
6a488035 | 44 | * |
90c8230e TO |
45 | * @param array $params |
46 | * Values of the new EventQueue. | |
6a488035 | 47 | * |
16b10e64 CW |
48 | * @return CRM_Mailing_Event_BAO_Queue |
49 | * The new EventQueue | |
6a488035 | 50 | */ |
07c09ae4 | 51 | public static function create($params) { |
6a488035 TO |
52 | $eq = new CRM_Mailing_Event_BAO_Queue(); |
53 | $eq->copyValues($params); | |
bd6658bd TO |
54 | if (empty($params['id']) && empty($params['hash'])) { |
55 | $eq->hash = self::hash($params); | |
56 | } | |
6a488035 TO |
57 | $eq->save(); |
58 | return $eq; | |
59 | } | |
60 | ||
61 | /** | |
54957108 | 62 | * Create a security hash from the job, email and contact ids. |
63 | * | |
64 | * @param array $params | |
6a488035 | 65 | * |
a6c01b45 CW |
66 | * @return int |
67 | * The hash | |
6a488035 TO |
68 | */ |
69 | public static function hash($params) { | |
353ffa53 TO |
70 | $jobId = $params['job_id']; |
71 | $emailId = CRM_Utils_Array::value('email_id', $params, ''); | |
6a488035 TO |
72 | $contactId = $params['contact_id']; |
73 | ||
74 | return substr(sha1("{$jobId}:{$emailId}:{$contactId}:" . time()), | |
75 | 0, 16 | |
76 | ); | |
77 | } | |
78 | ||
79 | /** | |
54957108 | 80 | * Verify that a queue event exists with the specified id/job id/hash. |
6a488035 | 81 | * |
90c8230e TO |
82 | * @param int $job_id |
83 | * The job ID of the event to find. | |
84 | * @param int $queue_id | |
85 | * The Queue Event ID to find. | |
86 | * @param string $hash | |
87 | * The hash to validate against. | |
6a488035 | 88 | * |
72b3a70c CW |
89 | * @return object|null |
90 | * The queue event if verified, or null | |
6a488035 TO |
91 | */ |
92 | public static function &verify($job_id, $queue_id, $hash) { | |
93 | $success = NULL; | |
94 | $q = new CRM_Mailing_Event_BAO_Queue(); | |
95 | if (!empty($job_id) && !empty($queue_id) && !empty($hash)) { | |
353ffa53 | 96 | $q->id = $queue_id; |
6a488035 | 97 | $q->job_id = $job_id; |
353ffa53 | 98 | $q->hash = $hash; |
6a488035 TO |
99 | if ($q->find(TRUE)) { |
100 | $success = $q; | |
101 | } | |
102 | } | |
103 | return $success; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Given a queue event ID, find the corresponding email address. | |
108 | * | |
90c8230e TO |
109 | * @param int $queue_id |
110 | * The queue event ID. | |
6a488035 | 111 | * |
a6c01b45 CW |
112 | * @return string |
113 | * The email address | |
6a488035 TO |
114 | */ |
115 | public static function getEmailAddress($queue_id) { | |
116 | $email = CRM_Core_BAO_Email::getTableName(); | |
353ffa53 | 117 | $eq = self::getTableName(); |
03e04002 | 118 | $query = " SELECT $email.email as email |
119 | FROM $email | |
120 | INNER JOIN $eq | |
121 | ON $eq.email_id = $email.id | |
6a488035 TO |
122 | WHERE $eq.id = " . CRM_Utils_Type::rule($queue_id, 'Integer'); |
123 | ||
124 | $q = new CRM_Mailing_Event_BAO_Queue(); | |
125 | $q->query($query); | |
126 | if (!$q->fetch()) { | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | return $q->email; | |
131 | } | |
132 | ||
133 | /** | |
fe482240 | 134 | * Count up events given a mailing id and optional job id. |
6a488035 | 135 | * |
90c8230e TO |
136 | * @param int $mailing_id |
137 | * ID of the mailing to count. | |
138 | * @param int $job_id | |
139 | * Optional ID of a job to limit results. | |
6a488035 | 140 | * |
a6c01b45 CW |
141 | * @return int |
142 | * Number of matching events | |
6a488035 TO |
143 | */ |
144 | public static function getTotalCount($mailing_id, $job_id = NULL) { | |
145 | $dao = new CRM_Core_DAO(); | |
146 | ||
353ffa53 | 147 | $queue = self::getTableName(); |
6a488035 | 148 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 149 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
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 | /** | |
fe482240 | 169 | * Get rows for the event browser. |
6a488035 | 170 | * |
90c8230e TO |
171 | * @param int $mailing_id |
172 | * ID of the mailing. | |
173 | * @param int $job_id | |
174 | * Optional ID of the job. | |
175 | * @param int $offset | |
176 | * Offset. | |
177 | * @param int $rowCount | |
178 | * Number of rows. | |
179 | * @param array $sort | |
180 | * Sort array. | |
6a488035 | 181 | * |
a6c01b45 CW |
182 | * @return array |
183 | * Result set | |
6a488035 | 184 | */ |
a3d7e8ee TO |
185 | public static function &getRows( |
186 | $mailing_id, $job_id = NULL, $offset = NULL, | |
6a488035 TO |
187 | $rowCount = NULL, $sort = NULL |
188 | ) { | |
189 | $dao = new CRM_Core_Dao(); | |
190 | ||
353ffa53 | 191 | $queue = self::getTableName(); |
6a488035 | 192 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 193 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 | 194 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
353ffa53 | 195 | $email = CRM_Core_BAO_Email::getTableName(); |
6a488035 TO |
196 | |
197 | $orderBy = "sort_name ASC, {$job}.start_date DESC"; | |
198 | if ($sort) { | |
199 | if (is_string($sort)) { | |
21d32567 | 200 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
201 | $orderBy = $sort; |
202 | } | |
203 | else { | |
204 | $orderBy = trim($sort->orderBy()); | |
205 | } | |
206 | } | |
207 | ||
208 | $query = " | |
4d20926e JP |
209 | SELECT $queue.id as queue_id, |
210 | $contact.display_name as display_name, | |
6a488035 TO |
211 | $contact.id as contact_id, |
212 | $email.email as email, | |
213 | $job.start_date as date | |
214 | FROM $contact | |
215 | INNER JOIN $queue | |
216 | ON $queue.contact_id = $contact.id | |
217 | INNER JOIN $email | |
218 | ON $queue.email_id = $email.id | |
219 | INNER JOIN $job | |
220 | ON $queue.job_id = $job.id | |
221 | INNER JOIN $mailing | |
222 | ON $job.mailing_id = $mailing.id | |
223 | AND $job.is_test = 0 | |
224 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
225 | ||
226 | if (!empty($job_id)) { | |
227 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
228 | } | |
229 | ||
230 | $query .= " ORDER BY {$orderBy} "; | |
231 | ||
232 | if ($offset || $rowCount) { | |
233 | //Added "||$rowCount" to avoid displaying all records on first page | |
234 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
235 | } | |
236 | ||
237 | $dao->query($query); | |
238 | ||
239 | $results = array(); | |
240 | ||
241 | while ($dao->fetch()) { | |
242 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
243 | "reset=1&cid={$dao->contact_id}" | |
244 | ); | |
4d20926e | 245 | $results[$dao->queue_id] = array( |
6a488035 TO |
246 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", |
247 | 'email' => $dao->email, | |
248 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
249 | ); | |
250 | } | |
251 | return $results; | |
252 | } | |
253 | ||
254 | /** | |
fe482240 | 255 | * Get the mailing object for this queue event instance. |
6a488035 TO |
256 | * |
257 | * @param | |
258 | * | |
a6c01b45 CW |
259 | * @return object |
260 | * Mailing BAO | |
6a488035 TO |
261 | */ |
262 | public function &getMailing() { | |
353ffa53 TO |
263 | $mailing = new CRM_Mailing_BAO_Mailing(); |
264 | $jobs = CRM_Mailing_BAO_MailingJob::getTableName(); | |
6a488035 | 265 | $mailings = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 266 | $queue = self::getTableName(); |
6a488035 TO |
267 | |
268 | $mailing->query(" | |
269 | SELECT $mailings.* | |
270 | FROM $mailings | |
271 | INNER JOIN $jobs | |
272 | ON $jobs.mailing_id = $mailings.id | |
273 | INNER JOIN $queue | |
274 | ON $queue.job_id = $jobs.id | |
275 | WHERE $queue.id = {$this->id}"); | |
276 | $mailing->fetch(); | |
277 | return $mailing; | |
278 | } | |
279 | ||
e0ef6999 | 280 | /** |
100fef9d | 281 | * @param int $queueID |
e0ef6999 EM |
282 | * |
283 | * @return array | |
284 | */ | |
6a488035 TO |
285 | public static function getContactInfo($queueID) { |
286 | $query = " | |
287 | SELECT DISTINCT(civicrm_mailing_event_queue.contact_id) as contact_id, | |
288 | civicrm_contact.display_name as display_name, | |
289 | civicrm_email.email as email | |
290 | FROM civicrm_mailing_event_queue, | |
291 | civicrm_contact, | |
292 | civicrm_email | |
293 | WHERE civicrm_mailing_event_queue.contact_id = civicrm_contact.id | |
294 | AND civicrm_mailing_event_queue.email_id = civicrm_email.id | |
295 | AND civicrm_mailing_event_queue.id = " . CRM_Utils_Type::escape($queueID, 'Integer'); | |
296 | ||
33621c4f | 297 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
298 | |
299 | $displayName = 'Unknown'; | |
300 | $email = 'Unknown'; | |
301 | if ($dao->fetch()) { | |
302 | $displayName = $dao->display_name; | |
303 | $email = $dao->email; | |
304 | } | |
305 | ||
306 | return array($displayName, $email); | |
307 | } | |
308 | ||
e0ef6999 | 309 | /** |
c490a46a | 310 | * @param array $params |
e0ef6999 EM |
311 | * @param null $now |
312 | */ | |
00be9182 | 313 | public static function bulkCreate($params, $now = NULL) { |
6a488035 TO |
314 | if (!$now) { |
315 | $now = time(); | |
316 | } | |
317 | ||
318 | // construct a bulk insert statement | |
319 | $values = array(); | |
320 | foreach ($params as $param) { | |
321 | $values[] = "( {$param[0]}, {$param[1]}, {$param[2]}, {$param[3]}, '" . substr(sha1("{$param[0]}:{$param[1]}:{$param[2]}:{$param[3]}:{$now}"), | |
353ffa53 TO |
322 | 0, 16 |
323 | ) . "' )"; | |
6a488035 TO |
324 | } |
325 | ||
326 | while (!empty($values)) { | |
327 | $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT); | |
353ffa53 TO |
328 | $str = implode(',', $input); |
329 | $sql = "INSERT INTO civicrm_mailing_event_queue ( job_id, email_id, contact_id, phone_id, hash ) VALUES $str;"; | |
6a488035 TO |
330 | CRM_Core_DAO::executeQuery($sql); |
331 | } | |
332 | } | |
96025800 | 333 | |
6a488035 | 334 | } |