3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
33 class CRM_Mailing_Event_BAO_Queue
extends CRM_Mailing_Event_DAO_Queue
{
38 public function __construct() {
39 parent
::__construct();
43 * Queue a new recipient.
45 * @param array $params
46 * Values of the new EventQueue.
48 * @return CRM_Mailing_Event_BAO_Queue
51 public static function create($params) {
52 $eq = new CRM_Mailing_Event_BAO_Queue();
53 $eq->copyValues($params);
54 if (empty($params['id']) && empty($params['hash'])) {
55 $eq->hash
= self
::hash($params);
62 * Create a security hash from the job, email and contact ids.
64 * @param array $params
69 public static function hash($params) {
70 $jobId = $params['job_id'];
71 $emailId = CRM_Utils_Array
::value('email_id', $params, '');
72 $contactId = $params['contact_id'];
74 return substr(sha1("{$jobId}:{$emailId}:{$contactId}:" . time()),
80 * Verify that a queue event exists with the specified id/job id/hash.
83 * The job ID of the event to find.
84 * @param int $queue_id
85 * The Queue Event ID to find.
87 * The hash to validate against.
90 * The queue event if verified, or null
92 public static function &verify($job_id, $queue_id, $hash) {
94 $q = new CRM_Mailing_Event_BAO_Queue();
95 if (!empty($job_id) && !empty($queue_id) && !empty($hash)) {
107 * Given a queue event ID, find the corresponding email address.
109 * @param int $queue_id
110 * The queue event ID.
115 public static function getEmailAddress($queue_id) {
116 $email = CRM_Core_BAO_Email
::getTableName();
117 $eq = self
::getTableName();
118 $query = " SELECT $email.email as email
121 ON $eq.email_id = $email.id
122 WHERE $eq.id = " . CRM_Utils_Type
::rule($queue_id, 'Integer');
124 $q = new CRM_Mailing_Event_BAO_Queue();
134 * Count up events given a mailing id and optional job id.
136 * @param int $mailing_id
137 * ID of the mailing to count.
139 * Optional ID of a job to limit results.
142 * Number of matching events
144 public static function getTotalCount($mailing_id, $job_id = NULL) {
145 $dao = new CRM_Core_DAO();
147 $queue = self
::getTableName();
148 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
149 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
152 SELECT COUNT(*) as queued
155 ON $queue.job_id = $job.id
157 ON $job.mailing_id = $mailing.id
159 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer') . ($job_id ?
" AND $job.id = " . CRM_Utils_Type
::escape($job_id,
169 * Get rows for the event browser.
171 * @param int $mailing_id
174 * Optional ID of the job.
177 * @param int $rowCount
185 public static function &getRows(
186 $mailing_id, $job_id = NULL, $offset = NULL,
187 $rowCount = NULL, $sort = NULL
189 $dao = new CRM_Core_Dao();
191 $queue = self
::getTableName();
192 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
193 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
194 $contact = CRM_Contact_BAO_Contact
::getTableName();
195 $email = CRM_Core_BAO_Email
::getTableName();
197 $orderBy = "sort_name ASC, {$job}.start_date DESC";
199 if (is_string($sort)) {
200 $sort = CRM_Utils_Type
::escape($sort, 'String');
204 $orderBy = trim($sort->orderBy());
209 SELECT $contact.display_name as display_name,
210 $contact.id as contact_id,
211 $email.email as email,
212 $job.start_date as date
215 ON $queue.contact_id = $contact.id
217 ON $queue.email_id = $email.id
219 ON $queue.job_id = $job.id
221 ON $job.mailing_id = $mailing.id
223 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
225 if (!empty($job_id)) {
226 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
229 $query .= " ORDER BY {$orderBy} ";
231 if ($offset ||
$rowCount) {
232 //Added "||$rowCount" to avoid displaying all records on first page
233 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
240 while ($dao->fetch()) {
241 $url = CRM_Utils_System
::url('civicrm/contact/view',
242 "reset=1&cid={$dao->contact_id}"
245 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
246 'email' => $dao->email
,
247 'date' => CRM_Utils_Date
::customFormat($dao->date
),
254 * Get the mailing object for this queue event instance.
261 public function &getMailing() {
262 $mailing = new CRM_Mailing_BAO_Mailing();
263 $jobs = CRM_Mailing_BAO_MailingJob
::getTableName();
264 $mailings = CRM_Mailing_BAO_Mailing
::getTableName();
265 $queue = self
::getTableName();
271 ON $jobs.mailing_id = $mailings.id
273 ON $queue.job_id = $jobs.id
274 WHERE $queue.id = {$this->id}");
280 * @param int $queueID
284 public static function getContactInfo($queueID) {
286 SELECT DISTINCT(civicrm_mailing_event_queue.contact_id) as contact_id,
287 civicrm_contact.display_name as display_name,
288 civicrm_email.email as email
289 FROM civicrm_mailing_event_queue,
292 WHERE civicrm_mailing_event_queue.contact_id = civicrm_contact.id
293 AND civicrm_mailing_event_queue.email_id = civicrm_email.id
294 AND civicrm_mailing_event_queue.id = " . CRM_Utils_Type
::escape($queueID, 'Integer');
296 $dao = CRM_Core_DAO
::executeQuery($query);
298 $displayName = 'Unknown';
301 $displayName = $dao->display_name
;
302 $email = $dao->email
;
305 return array($displayName, $email);
309 * @param array $params
312 public static function bulkCreate($params, $now = NULL) {
317 // construct a bulk insert statement
319 foreach ($params as $param) {
320 $values[] = "( {$param[0]}, {$param[1]}, {$param[2]}, {$param[3]}, '" . substr(sha1("{$param[0]}:{$param[1]}:{$param[2]}:{$param[3]}:{$now}"),
325 while (!empty($values)) {
326 $input = array_splice($values, 0, CRM_Core_DAO
::BULK_INSERT_COUNT
);
327 $str = implode(',', $input);
328 $sql = "INSERT INTO civicrm_mailing_event_queue ( job_id, email_id, contact_id, phone_id, hash ) VALUES $str;";
329 CRM_Core_DAO
::executeQuery($sql);