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