3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Mailing_Event_BAO_Queue
extends CRM_Mailing_Event_DAO_Queue
{
40 public function __construct() {
41 parent
::__construct();
45 * Queue a new recipient
47 * @param array $params
48 * Values of the new EventQueue.
50 * @return CRM_Mailing_Event_BAO_Queue The new EventQueue
53 public static function create($params) {
54 $eq = new CRM_Mailing_Event_BAO_Queue();
55 $eq->copyValues($params);
56 if (empty($params['id']) && empty($params['hash'])) {
57 $eq->hash
= self
::hash($params);
64 * Create a security hash from the job, email and contact ids
66 * @param array The ids to be hashed
72 public static function hash($params) {
73 $jobId = $params['job_id'];
74 $emailId = CRM_Utils_Array
::value('email_id', $params, '');
75 $contactId = $params['contact_id'];
77 return substr(sha1("{$jobId}:{$emailId}:{$contactId}:" . time()),
83 * Verify that a queue event exists with the specified id/job id/hash
86 * The job ID of the event to find.
87 * @param int $queue_id
88 * The Queue Event ID to find.
90 * The hash to validate against.
93 * The queue event if verified, or null
96 public static function &verify($job_id, $queue_id, $hash) {
98 $q = new CRM_Mailing_Event_BAO_Queue();
99 if (!empty($job_id) && !empty($queue_id) && !empty($hash)) {
101 $q->job_id
= $job_id;
103 if ($q->find(TRUE)) {
111 * Given a queue event ID, find the corresponding email address.
113 * @param int $queue_id
114 * The queue event ID.
120 public static function getEmailAddress($queue_id) {
121 $email = CRM_Core_BAO_Email
::getTableName();
122 $eq = self
::getTableName();
123 $query = " SELECT $email.email as email
126 ON $eq.email_id = $email.id
127 WHERE $eq.id = " . CRM_Utils_Type
::rule($queue_id, 'Integer');
129 $q = new CRM_Mailing_Event_BAO_Queue();
139 * Count up events given a mailing id and optional job id
141 * @param int $mailing_id
142 * ID of the mailing to count.
144 * Optional ID of a job to limit results.
147 * Number of matching events
150 public static function getTotalCount($mailing_id, $job_id = NULL) {
151 $dao = new CRM_Core_DAO();
153 $queue = self
::getTableName();
154 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
155 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
158 SELECT COUNT(*) as queued
161 ON $queue.job_id = $job.id
163 ON $job.mailing_id = $mailing.id
165 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer') . ($job_id ?
" AND $job.id = " . CRM_Utils_Type
::escape($job_id,
175 * Get rows for the event browser
177 * @param int $mailing_id
180 * Optional ID of the job.
183 * @param int $rowCount
192 public static function &getRows(
193 $mailing_id, $job_id = NULL, $offset = NULL,
194 $rowCount = NULL, $sort = NULL
196 $dao = new CRM_Core_Dao();
198 $queue = self
::getTableName();
199 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
200 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
201 $contact = CRM_Contact_BAO_Contact
::getTableName();
202 $email = CRM_Core_BAO_Email
::getTableName();
204 $orderBy = "sort_name ASC, {$job}.start_date DESC";
206 if (is_string($sort)) {
207 $sort = CRM_Utils_Type
::escape($sort, 'String');
211 $orderBy = trim($sort->orderBy());
216 SELECT $contact.display_name as display_name,
217 $contact.id as contact_id,
218 $email.email as email,
219 $job.start_date as date
222 ON $queue.contact_id = $contact.id
224 ON $queue.email_id = $email.id
226 ON $queue.job_id = $job.id
228 ON $job.mailing_id = $mailing.id
230 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
232 if (!empty($job_id)) {
233 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
236 $query .= " ORDER BY {$orderBy} ";
238 if ($offset ||
$rowCount) {
239 //Added "||$rowCount" to avoid displaying all records on first page
240 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
247 while ($dao->fetch()) {
248 $url = CRM_Utils_System
::url('civicrm/contact/view',
249 "reset=1&cid={$dao->contact_id}"
252 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
253 'email' => $dao->email
,
254 'date' => CRM_Utils_Date
::customFormat($dao->date
),
261 * Get the mailing object for this queue event instance
268 public function &getMailing() {
269 $mailing = new CRM_Mailing_BAO_Mailing();
270 $jobs = CRM_Mailing_BAO_MailingJob
::getTableName();
271 $mailings = CRM_Mailing_BAO_Mailing
::getTableName();
272 $queue = self
::getTableName();
278 ON $jobs.mailing_id = $mailings.id
280 ON $queue.job_id = $jobs.id
281 WHERE $queue.id = {$this->id}");
287 * @param int $queueID
291 public static function getContactInfo($queueID) {
293 SELECT DISTINCT(civicrm_mailing_event_queue.contact_id) as contact_id,
294 civicrm_contact.display_name as display_name,
295 civicrm_email.email as email
296 FROM civicrm_mailing_event_queue,
299 WHERE civicrm_mailing_event_queue.contact_id = civicrm_contact.id
300 AND civicrm_mailing_event_queue.email_id = civicrm_email.id
301 AND civicrm_mailing_event_queue.id = " . CRM_Utils_Type
::escape($queueID, 'Integer');
303 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
305 $displayName = 'Unknown';
308 $displayName = $dao->display_name
;
309 $email = $dao->email
;
312 return array($displayName, $email);
316 * @param array $params
319 public static function bulkCreate($params, $now = NULL) {
324 // construct a bulk insert statement
326 foreach ($params as $param) {
327 $values[] = "( {$param[0]}, {$param[1]}, {$param[2]}, {$param[3]}, '" . substr(sha1("{$param[0]}:{$param[1]}:{$param[2]}:{$param[3]}:{$now}"),
332 while (!empty($values)) {
333 $input = array_splice($values, 0, CRM_Core_DAO
::BULK_INSERT_COUNT
);
334 $str = implode(',', $input);
335 $sql = "INSERT INTO civicrm_mailing_event_queue ( job_id, email_id, contact_id, phone_id, hash ) VALUES $str;";
336 CRM_Core_DAO
::executeQuery($sql);