3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Mailing_Event_BAO_Queue
extends CRM_Mailing_Event_DAO_Queue
{
20 * Queue a new recipient.
22 * @param array $params
23 * Values of the new EventQueue.
25 * @return CRM_Mailing_Event_BAO_Queue
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);
39 * Create a security hash from the job, email and contact ids.
41 * @param array $params
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'];
51 return substr(sha1("{$jobId}:{$emailId}:{$contactId}:" . time()),
57 * Verify that a queue event exists with the specified id/job id/hash.
60 * The job ID of the event to find.
61 * @param int $queue_id
62 * The Queue Event ID to find.
64 * The hash to validate against.
67 * The queue event if verified, or null
69 public static function &verify($job_id, $queue_id, $hash) {
71 $q = new CRM_Mailing_Event_BAO_Queue();
72 if (!empty($job_id) && !empty($queue_id) && !empty($hash)) {
84 * Given a queue event ID, find the corresponding email address.
86 * @param int $queue_id
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
98 ON $eq.email_id = $email.id
99 WHERE $eq.id = " . CRM_Utils_Type
::rule($queue_id, 'Integer');
101 $q = new CRM_Mailing_Event_BAO_Queue();
111 * Count up events given a mailing id and optional job id.
113 * @param int $mailing_id
114 * ID of the mailing to count.
116 * Optional ID of a job to limit results.
119 * Number of matching events
121 public static function getTotalCount($mailing_id, $job_id = NULL) {
122 $dao = new CRM_Core_DAO();
124 $queue = self
::getTableName();
125 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
126 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
129 SELECT COUNT(*) as queued
132 ON $queue.job_id = $job.id
134 ON $job.mailing_id = $mailing.id
136 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer') . ($job_id ?
" AND $job.id = " . CRM_Utils_Type
::escape($job_id,
146 * Get rows for the event browser.
148 * @param int $mailing_id
151 * Optional ID of the job.
154 * @param int $rowCount
162 public static function &getRows(
163 $mailing_id, $job_id = NULL, $offset = NULL,
164 $rowCount = NULL, $sort = NULL
166 $dao = new CRM_Core_DAO();
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();
174 $orderBy = "sort_name ASC, {$job}.start_date DESC";
176 if (is_string($sort)) {
177 $sort = CRM_Utils_Type
::escape($sort, 'String');
181 $orderBy = trim($sort->orderBy());
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
193 ON $queue.contact_id = $contact.id
195 ON $queue.email_id = $email.id
197 ON $queue.job_id = $job.id
199 ON $job.mailing_id = $mailing.id
201 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
203 if (!empty($job_id)) {
204 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
207 $query .= " ORDER BY {$orderBy} ";
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');
218 while ($dao->fetch()) {
219 $url = CRM_Utils_System
::url('civicrm/contact/view',
220 "reset=1&cid={$dao->contact_id}"
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
),
232 * Get the mailing object for this queue event instance.
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();
249 ON $jobs.mailing_id = $mailings.id
251 ON $queue.job_id = $jobs.id
252 WHERE $queue.id = {$this->id}");
258 * @param int $queueID
262 public static function getContactInfo($queueID) {
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,
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');
274 $dao = CRM_Core_DAO
::executeQuery($query);
276 $displayName = 'Unknown';
279 $displayName = $dao->display_name
;
280 $email = $dao->email
;
281 $contact_id = $dao->contact_id
;
284 return [$displayName, $email, $contact_id];
288 * @param array $params
291 public static function bulkCreate($params, $now = NULL) {
296 // construct a bulk insert statement
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}"),
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);