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 | ||
36 | require_once 'Mail.php'; | |
4c6ce474 EM |
37 | |
38 | /** | |
39 | * Class CRM_Mailing_BAO_MailingJob | |
40 | */ | |
9da8dc8c | 41 | class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob { |
6a488035 TO |
42 | CONST MAX_CONTACTS_TO_PROCESS = 1000; |
43 | ||
44 | /** | |
45 | * class constructor | |
46 | */ | |
47 | function __construct() { | |
48 | parent::__construct(); | |
49 | } | |
50 | ||
af4c09bc EM |
51 | /** |
52 | * @param $params | |
53 | * | |
54 | * @return CRM_Mailing_BAO_MailingJob | |
55 | */ | |
84dd5db6 | 56 | static public function create($params) { |
9da8dc8c | 57 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
58 | $job->mailing_id = $params['mailing_id']; |
59 | $job->status = $params['status']; | |
60 | $job->scheduled_date = $params['scheduled_date']; | |
61 | $job->is_test = $params['is_test']; | |
62 | $job->save(); | |
63 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
84dd5db6 | 64 | $mailing->getRecipients($job->id, $params['mailing_id'], NULL, NULL, TRUE, FALSE); |
6a488035 TO |
65 | return $job; |
66 | } | |
af4c09bc | 67 | |
6a488035 TO |
68 | /** |
69 | * Initiate all pending/ready jobs | |
70 | * | |
af4c09bc EM |
71 | * @param null $testParams |
72 | * @param null $mode | |
73 | * | |
6a488035 TO |
74 | * @return void |
75 | * @access public | |
76 | * @static | |
77 | */ | |
78 | public static function runJobs($testParams = NULL, $mode = NULL) { | |
9da8dc8c | 79 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
80 | |
81 | $config = CRM_Core_Config::singleton(); | |
9da8dc8c | 82 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
83 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
84 | ||
85 | if (!empty($testParams)) { | |
86 | $query = " | |
87 | SELECT * | |
88 | FROM $jobTable | |
89 | WHERE id = {$testParams['job_id']}"; | |
90 | $job->query($query); | |
91 | } | |
92 | else { | |
93 | $currentTime = date('YmdHis'); | |
94 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); | |
95 | $domainID = CRM_Core_Config::domainID(); | |
96 | ||
97 | $modeClause = 'AND m.sms_provider_id IS NULL'; | |
98 | if ($mode == 'sms') { | |
99 | $modeClause = 'AND m.sms_provider_id IS NOT NULL'; | |
100 | } | |
101 | ||
102 | // Select the first child job that is scheduled | |
103 | // CRM-6835 | |
104 | $query = " | |
105 | SELECT j.* | |
106 | FROM $jobTable j, | |
107 | $mailingTable m | |
108 | WHERE m.id = j.mailing_id AND m.domain_id = {$domainID} | |
109 | {$modeClause} | |
110 | AND j.is_test = 0 | |
111 | AND ( ( j.start_date IS null | |
112 | AND j.scheduled_date <= $currentTime | |
113 | AND j.status = 'Scheduled' ) | |
114 | OR ( j.status = 'Running' | |
115 | AND j.end_date IS null ) ) | |
116 | AND (j.job_type = 'child') | |
117 | AND {$mailingACL} | |
118 | ORDER BY j.mailing_id, | |
119 | j.id | |
120 | "; | |
121 | ||
122 | $job->query($query); | |
123 | } | |
124 | ||
125 | ||
126 | while ($job->fetch()) { | |
127 | // still use job level lock for each child job | |
128 | $lockName = "civimail.job.{$job->id}"; | |
129 | ||
130 | $lock = new CRM_Core_Lock($lockName); | |
131 | if (!$lock->isAcquired()) { | |
132 | continue; | |
133 | } | |
134 | ||
135 | // for test jobs we do not change anything, since its on a short-circuit path | |
136 | if (empty($testParams)) { | |
137 | // we've got the lock, but while we were waiting and processing | |
138 | // other emails, this job might have changed under us | |
139 | // lets get the job status again and check | |
1a5727bd | 140 | $job->status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 141 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 142 | $job->id, |
6606b2e1 DL |
143 | 'status', |
144 | 'id', | |
145 | TRUE | |
6a488035 TO |
146 | ); |
147 | ||
1a5727bd DL |
148 | if ( |
149 | $job->status != 'Running' && | |
6a488035 TO |
150 | $job->status != 'Scheduled' |
151 | ) { | |
152 | // this includes Cancelled and other statuses, CRM-4246 | |
153 | $lock->release(); | |
154 | continue; | |
155 | } | |
156 | } | |
157 | ||
158 | /* Queue up recipients for the child job being launched */ | |
159 | ||
160 | if ($job->status != 'Running') { | |
161 | $transaction = new CRM_Core_Transaction(); | |
162 | ||
163 | // have to queue it up based on the offset and limits | |
164 | // get the parent ID, and limit and offset | |
165 | $job->queue($testParams); | |
166 | ||
167 | // Mark up the starting time | |
9da8dc8c | 168 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
169 | $saveJob->id = $job->id; |
170 | $saveJob->start_date = date('YmdHis'); | |
171 | $saveJob->status = 'Running'; | |
172 | $saveJob->save(); | |
173 | ||
174 | $transaction->commit(); | |
175 | } | |
176 | ||
177 | // Get the mailer | |
178 | // make it a persistent connection, CRM-9349 | |
179 | if ($mode === NULL) { | |
180 | $mailer = $config->getMailer(TRUE); | |
181 | } | |
182 | elseif ($mode == 'sms') { | |
183 | $mailer = CRM_SMS_Provider::singleton(array('mailing_id' => $job->mailing_id)); | |
184 | } | |
185 | ||
186 | // Compose and deliver each child job | |
187 | $isComplete = $job->deliver($mailer, $testParams); | |
188 | ||
189 | CRM_Utils_Hook::post('create', 'CRM_Mailing_DAO_Spool', $job->id, $isComplete); | |
190 | ||
191 | // Mark the child complete | |
192 | if ($isComplete) { | |
193 | /* Finish the job */ | |
194 | ||
195 | $transaction = new CRM_Core_Transaction(); | |
196 | ||
9da8dc8c | 197 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
198 | $saveJob->id = $job->id; |
199 | $saveJob->end_date = date('YmdHis'); | |
200 | $saveJob->status = 'Complete'; | |
201 | $saveJob->save(); | |
202 | ||
203 | $transaction->commit(); | |
204 | ||
205 | // don't mark the mailing as complete | |
206 | } | |
207 | ||
208 | // Release the child joblock | |
209 | $lock->release(); | |
210 | ||
211 | if ($testParams) { | |
212 | return $isComplete; | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | // post process to determine if the parent job | |
218 | // as well as the mailing is complete after the run | |
e0ef6999 EM |
219 | /** |
220 | * @param null $mode | |
221 | */ | |
6a488035 TO |
222 | public static function runJobs_post($mode = NULL) { |
223 | ||
9da8dc8c | 224 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
225 | |
226 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
227 | ||
228 | $config = CRM_Core_Config::singleton(); | |
9da8dc8c | 229 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
230 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
231 | ||
232 | $currentTime = date('YmdHis'); | |
233 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); | |
234 | $domainID = CRM_Core_Config::domainID(); | |
235 | ||
236 | $query = " | |
237 | SELECT j.* | |
238 | FROM $jobTable j, | |
239 | $mailingTable m | |
240 | WHERE m.id = j.mailing_id AND m.domain_id = {$domainID} | |
241 | AND j.is_test = 0 | |
242 | AND j.scheduled_date <= $currentTime | |
243 | AND j.status = 'Running' | |
244 | AND j.end_date IS null | |
245 | AND (j.job_type != 'child' OR j.job_type is NULL) | |
246 | ORDER BY j.scheduled_date, | |
247 | j.start_date"; | |
248 | ||
249 | $job->query($query); | |
250 | ||
251 | // For each parent job that is running, let's look at their child jobs | |
252 | while ($job->fetch()) { | |
253 | ||
9da8dc8c | 254 | $child_job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
255 | |
256 | $child_job_sql = " | |
257 | SELECT count(j.id) | |
258 | FROM civicrm_mailing_job j, civicrm_mailing m | |
259 | WHERE m.id = j.mailing_id | |
260 | AND j.job_type = 'child' | |
261 | AND j.parent_id = %1 | |
262 | AND j.status <> 'Complete'"; | |
263 | $params = array(1 => array($job->id, 'Integer')); | |
264 | ||
265 | $anyChildLeft = CRM_Core_DAO::singleValueQuery($child_job_sql, $params); | |
266 | ||
267 | // all of the child jobs are complete, update | |
268 | // the parent job as well as the mailing status | |
269 | if (!$anyChildLeft) { | |
270 | ||
271 | $transaction = new CRM_Core_Transaction(); | |
272 | ||
9da8dc8c | 273 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
274 | $saveJob->id = $job->id; |
275 | $saveJob->end_date = date('YmdHis'); | |
276 | $saveJob->status = 'Complete'; | |
277 | $saveJob->save(); | |
278 | ||
279 | $mailing->reset(); | |
280 | $mailing->id = $job->mailing_id; | |
281 | $mailing->is_completed = TRUE; | |
282 | $mailing->save(); | |
283 | $transaction->commit(); | |
284 | } | |
285 | } | |
286 | } | |
287 | ||
288 | ||
289 | // before we run jobs, we need to split the jobs | |
e0ef6999 EM |
290 | /** |
291 | * @param int $offset | |
292 | * @param null $mode | |
293 | */ | |
6a488035 | 294 | public static function runJobs_pre($offset = 200, $mode = NULL) { |
9da8dc8c | 295 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 | 296 | |
9da8dc8c | 297 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
298 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
299 | ||
300 | $currentTime = date('YmdHis'); | |
301 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); | |
302 | ||
303 | ||
9da8dc8c | 304 | $workflowClause = CRM_Mailing_BAO_MailingJob::workflowClause(); |
6a488035 TO |
305 | |
306 | $domainID = CRM_Core_Config::domainID(); | |
307 | ||
308 | $modeClause = 'AND m.sms_provider_id IS NULL'; | |
309 | if ($mode == 'sms') { | |
310 | $modeClause = 'AND m.sms_provider_id IS NOT NULL'; | |
311 | } | |
312 | ||
313 | // Select all the mailing jobs that are created from | |
314 | // when the mailing is submitted or scheduled. | |
315 | $query = " | |
316 | SELECT j.* | |
317 | FROM $jobTable j, | |
318 | $mailingTable m | |
319 | WHERE m.id = j.mailing_id AND m.domain_id = {$domainID} | |
320 | $workflowClause | |
321 | $modeClause | |
322 | AND j.is_test = 0 | |
323 | AND ( ( j.start_date IS null | |
324 | AND j.scheduled_date <= $currentTime | |
325 | AND j.status = 'Scheduled' | |
326 | AND j.end_date IS null ) ) | |
327 | AND ((j.job_type is NULL) OR (j.job_type <> 'child')) | |
328 | ORDER BY j.scheduled_date, | |
329 | j.start_date"; | |
330 | ||
331 | ||
332 | $job->query($query); | |
333 | ||
334 | ||
335 | // For each of the "Parent Jobs" we find, we split them into | |
336 | // X Number of child jobs | |
337 | while ($job->fetch()) { | |
338 | // still use job level lock for each child job | |
339 | $lockName = "civimail.job.{$job->id}"; | |
340 | ||
341 | $lock = new CRM_Core_Lock($lockName); | |
342 | if (!$lock->isAcquired()) { | |
343 | continue; | |
344 | } | |
345 | ||
346 | // Re-fetch the job status in case things | |
347 | // changed between the first query and now | |
348 | // to avoid race conditions | |
6606b2e1 | 349 | $job->status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 350 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 351 | $job->id, |
6606b2e1 DL |
352 | 'status', |
353 | 'id', | |
354 | TRUE | |
6a488035 TO |
355 | ); |
356 | if ($job->status != 'Scheduled') { | |
357 | $lock->release(); | |
358 | continue; | |
359 | } | |
360 | ||
361 | $job->split_job($offset); | |
362 | ||
363 | // update the status of the parent job | |
364 | $transaction = new CRM_Core_Transaction(); | |
365 | ||
9da8dc8c | 366 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
6a488035 TO |
367 | $saveJob->id = $job->id; |
368 | $saveJob->start_date = date('YmdHis'); | |
369 | $saveJob->status = 'Running'; | |
370 | $saveJob->save(); | |
371 | ||
372 | $transaction->commit(); | |
373 | ||
374 | // Release the job lock | |
375 | $lock->release(); | |
376 | } | |
377 | } | |
378 | ||
379 | // Split the parent job into n number of child job based on an offset | |
380 | // If null or 0 , we create only one child job | |
e0ef6999 EM |
381 | /** |
382 | * @param int $offset | |
383 | */ | |
6a488035 TO |
384 | public function split_job($offset = 200) { |
385 | $recipient_count = CRM_Mailing_BAO_Recipients::mailingSize($this->mailing_id); | |
386 | ||
9da8dc8c | 387 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
388 | |
389 | ||
390 | $dao = new CRM_Core_DAO(); | |
391 | ||
392 | $sql = " | |
393 | INSERT INTO civicrm_mailing_job | |
394 | (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`) | |
395 | VALUES (%1, %2, %3, %4, %5, %6, %7) | |
396 | "; | |
397 | $params = array(1 => array($this->mailing_id, 'Integer'), | |
398 | 2 => array($this->scheduled_date, 'String'), | |
399 | 3 => array('Scheduled', 'String'), | |
400 | 4 => array('child', 'String'), | |
401 | 5 => array($this->id, 'Integer'), | |
402 | 6 => array(0, 'Integer'), | |
403 | 7 => array($recipient_count, 'Integer'), | |
404 | ); | |
405 | ||
406 | // create one child job if the mailing size is less than the offset | |
cbe30f5a | 407 | // probably use a CRM_Mailing_DAO_MailingJob( ); |
6a488035 TO |
408 | if (empty($offset) || |
409 | $recipient_count <= $offset | |
410 | ) { | |
411 | CRM_Core_DAO::executeQuery($sql, $params); | |
412 | } | |
413 | else { | |
414 | // Creating 'child jobs' | |
415 | for ($i = 0; $i < $recipient_count; $i = $i + $offset) { | |
416 | $params[6][0] = $i; | |
417 | $params[7][0] = $offset; | |
418 | CRM_Core_DAO::executeQuery($sql, $params); | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
e0ef6999 EM |
423 | /** |
424 | * @param null $testParams | |
425 | */ | |
6a488035 TO |
426 | public function queue($testParams = NULL) { |
427 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
428 | $mailing->id = $this->mailing_id; | |
429 | if (!empty($testParams)) { | |
430 | $mailing->getTestRecipients($testParams); | |
431 | } | |
432 | else { | |
433 | // We are still getting all the recipients from the parent job | |
434 | // so we don't mess with the include/exclude logic. | |
435 | $recipients = CRM_Mailing_BAO_Recipients::mailingQuery($this->mailing_id, $this->job_offset, $this->job_limit); | |
436 | ||
437 | // FIXME: this is not very smart, we should move this to one DB call | |
438 | // INSERT INTO ... SELECT FROM .. | |
439 | // the thing we need to figure out is how to generate the hash automatically | |
440 | $now = time(); | |
441 | $params = array(); | |
442 | $count = 0; | |
443 | while ($recipients->fetch()) { | |
444 | if ($recipients->phone_id) { | |
445 | $recipients->email_id = "null"; | |
446 | } | |
447 | else { | |
448 | $recipients->phone_id = "null"; | |
449 | } | |
450 | ||
451 | $params[] = array( | |
452 | $this->id, | |
453 | $recipients->email_id, | |
454 | $recipients->contact_id, | |
455 | $recipients->phone_id, | |
456 | ); | |
457 | $count++; | |
458 | if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) { | |
459 | CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now); | |
460 | $count = 0; | |
461 | $params = array(); | |
462 | } | |
463 | } | |
464 | ||
465 | if (!empty($params)) { | |
466 | CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now); | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | /** | |
472 | * Send the mailing | |
473 | * | |
2a6da8d7 EM |
474 | * @param object $mailer A Mail object to send the messages |
475 | * | |
476 | * @param null $testParams | |
6a488035 TO |
477 | * |
478 | * @return void | |
479 | * @access public | |
480 | */ | |
481 | public function deliver(&$mailer, $testParams = NULL) { | |
482 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
483 | $mailing->id = $this->mailing_id; | |
484 | $mailing->find(TRUE); | |
485 | $mailing->free(); | |
486 | ||
487 | $eq = new CRM_Mailing_Event_BAO_Queue(); | |
488 | $eqTable = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
489 | $emailTable = CRM_Core_BAO_Email::getTableName(); | |
490 | $phoneTable = CRM_Core_DAO_Phone::getTableName(); | |
491 | $contactTable = CRM_Contact_BAO_Contact::getTableName(); | |
492 | $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName(); | |
493 | $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); | |
494 | ||
495 | $query = " SELECT $eqTable.id, | |
496 | $emailTable.email as email, | |
497 | $eqTable.contact_id, | |
498 | $eqTable.hash, | |
499 | NULL as phone | |
500 | FROM $eqTable | |
501 | INNER JOIN $emailTable | |
502 | ON $eqTable.email_id = $emailTable.id | |
503 | INNER JOIN $contactTable | |
504 | ON $contactTable.id = $emailTable.contact_id | |
505 | LEFT JOIN $edTable | |
506 | ON $eqTable.id = $edTable.event_queue_id | |
507 | LEFT JOIN $ebTable | |
508 | ON $eqTable.id = $ebTable.event_queue_id | |
509 | WHERE $eqTable.job_id = " . $this->id . " | |
510 | AND $edTable.id IS null | |
511 | AND $ebTable.id IS null | |
512 | AND $contactTable.is_opt_out = 0"; | |
513 | ||
514 | if ($mailing->sms_provider_id) { | |
515 | $query = " | |
516 | SELECT $eqTable.id, | |
517 | $phoneTable.phone as phone, | |
518 | $eqTable.contact_id, | |
519 | $eqTable.hash, | |
520 | NULL as email | |
521 | FROM $eqTable | |
522 | INNER JOIN $phoneTable | |
523 | ON $eqTable.phone_id = $phoneTable.id | |
524 | INNER JOIN $contactTable | |
525 | ON $contactTable.id = $phoneTable.contact_id | |
526 | LEFT JOIN $edTable | |
527 | ON $eqTable.id = $edTable.event_queue_id | |
528 | LEFT JOIN $ebTable | |
529 | ON $eqTable.id = $ebTable.event_queue_id | |
530 | WHERE $eqTable.job_id = " . $this->id . " | |
531 | AND $edTable.id IS null | |
532 | AND $ebTable.id IS null | |
533 | AND $contactTable.is_opt_out = 0"; | |
534 | } | |
535 | $eq->query($query); | |
536 | ||
537 | static $config = NULL; | |
538 | static $mailsProcessed = 0; | |
539 | ||
540 | if ($config == NULL) { | |
541 | $config = CRM_Core_Config::singleton(); | |
542 | } | |
543 | ||
544 | $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date); | |
545 | $fields = array(); | |
546 | ||
547 | if (!empty($testParams)) { | |
a5db8e6f | 548 | $mailing->subject = ts('[CiviMail Draft]') . ' ' . $mailing->subject; |
6a488035 TO |
549 | } |
550 | ||
551 | CRM_Mailing_BAO_Mailing::tokenReplace($mailing); | |
552 | ||
553 | // get and format attachments | |
554 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id); | |
555 | ||
556 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
557 | CRM_Core_Smarty::registerStringResource(); | |
558 | } | |
559 | ||
df7f4ae1 DL |
560 | // CRM-12376 |
561 | // This handles the edge case scenario where all the mails | |
562 | // have been delivered in prior jobs | |
563 | $isDelivered = TRUE; | |
6a488035 TO |
564 | |
565 | // make sure that there's no more than $config->mailerBatchLimit mails processed in a run | |
566 | while ($eq->fetch()) { | |
567 | // if ( ( $mailsProcessed % 100 ) == 0 ) { | |
568 | // CRM_Utils_System::xMemory( "$mailsProcessed: " ); | |
569 | // } | |
570 | ||
df7f4ae1 DL |
571 | if ( |
572 | $config->mailerBatchLimit > 0 && | |
6a488035 TO |
573 | $mailsProcessed >= $config->mailerBatchLimit |
574 | ) { | |
575 | if (!empty($fields)) { | |
576 | $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); | |
577 | } | |
578 | $eq->free(); | |
579 | return FALSE; | |
580 | } | |
581 | $mailsProcessed++; | |
582 | ||
583 | $fields[] = array( | |
584 | 'id' => $eq->id, | |
585 | 'hash' => $eq->hash, | |
586 | 'contact_id' => $eq->contact_id, | |
587 | 'email' => $eq->email, | |
588 | 'phone' => $eq->phone, | |
589 | ); | |
590 | if (count($fields) == self::MAX_CONTACTS_TO_PROCESS) { | |
591 | $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); | |
592 | if (!$isDelivered) { | |
593 | $eq->free(); | |
594 | return $isDelivered; | |
595 | } | |
596 | $fields = array(); | |
597 | } | |
598 | } | |
599 | ||
600 | $eq->free(); | |
601 | ||
602 | if (!empty($fields)) { | |
603 | $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); | |
604 | } | |
605 | return $isDelivered; | |
606 | } | |
607 | ||
e0ef6999 EM |
608 | /** |
609 | * @param $fields | |
610 | * @param $mailing | |
611 | * @param $mailer | |
612 | * @param $job_date | |
613 | * @param $attachments | |
614 | * | |
615 | * @return bool|null | |
616 | * @throws Exception | |
617 | */ | |
6a488035 TO |
618 | public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) { |
619 | static $smtpConnectionErrors = 0; | |
620 | ||
1a5727bd | 621 | if (!is_object($mailer) || empty($fields)) { |
6a488035 TO |
622 | CRM_Core_Error::fatal(); |
623 | } | |
624 | ||
625 | // get the return properties | |
626 | $returnProperties = $mailing->getReturnProperties(); | |
627 | $params = $targetParams = $deliveredParams = array(); | |
628 | $count = 0; | |
629 | ||
630 | foreach ($fields as $key => $field) { | |
631 | $params[] = $field['contact_id']; | |
632 | } | |
633 | ||
634 | $details = CRM_Utils_Token::getTokenDetails( | |
635 | $params, | |
636 | $returnProperties, | |
637 | TRUE, TRUE, NULL, | |
638 | $mailing->getFlattenedTokens(), | |
639 | get_class($this), | |
640 | $this->id | |
641 | ); | |
642 | ||
643 | $config = CRM_Core_Config::singleton(); | |
644 | foreach ($fields as $key => $field) { | |
645 | $contactID = $field['contact_id']; | |
646 | if (!array_key_exists($contactID, $details[0])) { | |
647 | $details[0][$contactID] = array(); | |
648 | } | |
6a488035 | 649 | |
1a5727bd | 650 | /* Compose the mailing */ |
6a488035 TO |
651 | $recipient = $replyToEmail = NULL; |
652 | $replyValue = strcmp($mailing->replyto_email, $mailing->from_email); | |
653 | if ($replyValue) { | |
654 | $replyToEmail = $mailing->replyto_email; | |
655 | } | |
656 | ||
1a5727bd DL |
657 | $message = &$mailing->compose( |
658 | $this->id, $field['id'], $field['hash'], | |
6a488035 TO |
659 | $field['contact_id'], $field['email'], |
660 | $recipient, FALSE, $details[0][$contactID], $attachments, | |
661 | FALSE, NULL, $replyToEmail | |
662 | ); | |
663 | if (empty($message)) { | |
664 | // lets keep the message in the queue | |
665 | // most likely a permissions related issue with smarty templates | |
666 | // or a bad contact id? CRM-9833 | |
667 | continue; | |
668 | } | |
669 | ||
670 | /* Send the mailing */ | |
671 | ||
672 | $body = &$message->get(); | |
673 | $headers = &$message->headers(); | |
674 | ||
675 | if ($mailing->sms_provider_id) { | |
676 | $provider = CRM_SMS_Provider::singleton(array('mailing_id' => $mailing->id)); | |
677 | $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]); | |
678 | $headers = $provider->getRecipientDetails($field, $details[0][$contactID]); | |
679 | } | |
680 | ||
681 | // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743 | |
682 | $recipient = $headers['To']; | |
683 | $result = NULL; | |
684 | ||
685 | // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744 | |
686 | if ($job_date) { | |
6a4257d4 | 687 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 TO |
688 | } |
689 | ||
690 | $result = $mailer->send($recipient, $headers, $body, $this->id); | |
691 | ||
692 | if ($job_date) { | |
6a4257d4 | 693 | unset($errorScope); |
6a488035 TO |
694 | } |
695 | ||
c5a6413b | 696 | if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) { |
6a488035 TO |
697 | // CRM-9191 |
698 | $message = $result->getMessage(); | |
18810158 DL |
699 | if ( |
700 | strpos($message, 'Failed to write to socket') !== FALSE || | |
701 | strpos($message, 'Failed to set sender') !== FALSE | |
702 | ) { | |
6a488035 TO |
703 | // lets log this message and code |
704 | $code = $result->getCode(); | |
18810158 | 705 | CRM_Core_Error::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code"); |
6a488035 TO |
706 | |
707 | // these are socket write errors which most likely means smtp connection errors | |
708 | // lets skip them | |
709 | $smtpConnectionErrors++; | |
710 | if ($smtpConnectionErrors <= 5) { | |
711 | continue; | |
712 | } | |
713 | ||
6a488035 TO |
714 | // seems like we have too many of them in a row, we should |
715 | // write stuff to disk and abort the cron job | |
2ede60ec DL |
716 | $this->writeToDB( |
717 | $deliveredParams, | |
6a488035 TO |
718 | $targetParams, |
719 | $mailing, | |
720 | $job_date | |
721 | ); | |
722 | ||
723 | CRM_Core_Error::debug_log_message("Too many SMTP Socket Errors. Exiting"); | |
724 | CRM_Utils_System::civiExit(); | |
725 | } | |
726 | ||
727 | /* Register the bounce event */ | |
728 | ||
729 | $params = array( | |
730 | 'event_queue_id' => $field['id'], | |
731 | 'job_id' => $this->id, | |
732 | 'hash' => $field['hash'], | |
733 | ); | |
734 | $params = array_merge($params, | |
735 | CRM_Mailing_BAO_BouncePattern::match($result->getMessage()) | |
736 | ); | |
737 | CRM_Mailing_Event_BAO_Bounce::create($params); | |
738 | } | |
39373d13 JM |
739 | elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id) { |
740 | // Handle SMS errors: CRM-15426 | |
741 | $job_id = intval($this->id); | |
742 | $mailing_id = intval($mailing->id); | |
743 | CRM_Core_Error::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows."); | |
744 | CRM_Core_Error::debug_log_message($result->getMessage()); | |
745 | } | |
6a488035 TO |
746 | else { |
747 | /* Register the delivery event */ | |
6a488035 TO |
748 | $deliveredParams[] = $field['id']; |
749 | $targetParams[] = $field['contact_id']; | |
750 | ||
751 | $count++; | |
752 | if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) { | |
2ede60ec DL |
753 | $this->writeToDB( |
754 | $deliveredParams, | |
6a488035 TO |
755 | $targetParams, |
756 | $mailing, | |
757 | $job_date | |
758 | ); | |
759 | $count = 0; | |
760 | ||
761 | // hack to stop mailing job at run time, CRM-4246. | |
762 | // to avoid making too many DB calls for this rare case | |
763 | // lets do it when we snapshot | |
1a5727bd | 764 | $status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 765 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 766 | $this->id, |
1a5727bd DL |
767 | 'status', |
768 | 'id', | |
769 | TRUE | |
6a488035 | 770 | ); |
1a5727bd | 771 | |
6a488035 TO |
772 | if ($status != 'Running') { |
773 | return FALSE; | |
774 | } | |
775 | } | |
776 | } | |
777 | ||
778 | unset($result); | |
779 | ||
780 | // seems like a successful delivery or bounce, lets decrement error count | |
781 | // only if we have smtp connection errors | |
782 | if ($smtpConnectionErrors > 0) { | |
783 | $smtpConnectionErrors--; | |
784 | } | |
785 | ||
786 | // If we have enabled the Throttle option, this is the time to enforce it. | |
787 | if (isset($config->mailThrottleTime) && $config->mailThrottleTime > 0) { | |
788 | usleep((int ) $config->mailThrottleTime); | |
789 | } | |
790 | } | |
791 | ||
2ede60ec DL |
792 | $result = $this->writeToDB( |
793 | $deliveredParams, | |
6a488035 TO |
794 | $targetParams, |
795 | $mailing, | |
796 | $job_date | |
797 | ); | |
798 | ||
799 | return $result; | |
800 | } | |
801 | ||
802 | /** | |
803 | * cancel a mailing | |
804 | * | |
805 | * @param int $mailingId the id of the mailing to be canceled | |
806 | * @static | |
807 | */ | |
808 | public static function cancel($mailingId) { | |
809 | $sql = " | |
810 | SELECT * | |
811 | FROM civicrm_mailing_job | |
812 | WHERE mailing_id = %1 | |
813 | AND is_test = 0 | |
814 | AND ( ( job_type IS NULL ) OR | |
815 | job_type <> 'child' ) | |
816 | "; | |
817 | $params = array(1 => array($mailingId, 'Integer')); | |
818 | $job = CRM_Core_DAO::executeQuery($sql, $params); | |
819 | if ($job->fetch() && | |
820 | in_array($job->status, array('Scheduled', 'Running', 'Paused')) | |
821 | ) { | |
822 | ||
9da8dc8c | 823 | $newJob = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
824 | $newJob->id = $job->id; |
825 | $newJob->end_date = date('YmdHis'); | |
826 | $newJob->status = 'Canceled'; | |
827 | $newJob->save(); | |
828 | ||
829 | // also cancel all child jobs | |
830 | $sql = " | |
831 | UPDATE civicrm_mailing_job | |
832 | SET status = 'Canceled', | |
833 | end_date = %2 | |
834 | WHERE parent_id = %1 | |
835 | AND is_test = 0 | |
836 | AND job_type = 'child' | |
837 | AND status IN ( 'Scheduled', 'Running', 'Paused' ) | |
838 | "; | |
839 | $params = array(1 => array($job->id, 'Integer'), | |
840 | 2 => array(date('YmdHis'), 'Timestamp'), | |
841 | ); | |
842 | CRM_Core_DAO::executeQuery($sql, $params); | |
843 | ||
844 | CRM_Core_Session::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success'); | |
845 | } | |
846 | } | |
847 | ||
848 | /** | |
849 | * Return a translated status enum string | |
850 | * | |
851 | * @param string $status The status enum | |
852 | * | |
853 | * @return string The translated version | |
854 | * @access public | |
855 | * @static | |
856 | */ | |
857 | public static function status($status) { | |
858 | static $translation = NULL; | |
859 | ||
860 | if (empty($translation)) { | |
861 | $translation = array( | |
862 | 'Scheduled' => ts('Scheduled'), | |
863 | 'Running' => ts('Running'), | |
864 | 'Complete' => ts('Complete'), | |
865 | 'Paused' => ts('Paused'), | |
866 | 'Canceled' => ts('Canceled'), | |
867 | ); | |
868 | } | |
869 | return CRM_Utils_Array::value($status, $translation, ts('Not scheduled')); | |
870 | } | |
871 | ||
872 | /** | |
873 | * Return a workflow clause for use in SQL queries, | |
874 | * to only process jobs that are approved. | |
875 | * | |
876 | * @return string For use in a WHERE clause | |
877 | * @access public | |
878 | * @static | |
879 | */ | |
880 | public static function workflowClause() { | |
881 | // add an additional check and only process | |
882 | // jobs that are approved | |
883 | if (CRM_Mailing_Info::workflowEnabled()) { | |
884 | $approveOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status', | |
885 | 'Approved', | |
886 | 'name' | |
887 | ); | |
888 | if ($approveOptionID) { | |
889 | return " AND m.approval_status_id = $approveOptionID "; | |
890 | } | |
891 | } | |
892 | return ''; | |
893 | } | |
894 | ||
e0ef6999 EM |
895 | /** |
896 | * @param $deliveredParams | |
897 | * @param $targetParams | |
898 | * @param $mailing | |
899 | * @param $job_date | |
900 | * | |
901 | * @return bool | |
902 | * @throws CRM_Core_Exception | |
903 | * @throws Exception | |
904 | */ | |
2ede60ec DL |
905 | public function writeToDB( |
906 | &$deliveredParams, | |
6a488035 TO |
907 | &$targetParams, |
908 | &$mailing, | |
909 | $job_date | |
910 | ) { | |
911 | static $activityTypeID = NULL; | |
2ede60ec | 912 | static $writeActivity = NULL; |
6a488035 TO |
913 | |
914 | if (!empty($deliveredParams)) { | |
915 | CRM_Mailing_Event_BAO_Delivered::bulkCreate($deliveredParams); | |
916 | $deliveredParams = array(); | |
917 | } | |
918 | ||
2ede60ec DL |
919 | if ($writeActivity === NULL) { |
920 | $writeActivity = CRM_Core_BAO_Setting::getItem( | |
921 | CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
922 | 'write_activity_record', | |
923 | NULL, | |
924 | TRUE | |
925 | ); | |
926 | } | |
6a488035 | 927 | |
2ede60ec DL |
928 | if (!$writeActivity) { |
929 | return TRUE; | |
930 | } | |
931 | ||
932 | $result = TRUE; | |
933 | if (!empty($targetParams) && !empty($mailing->scheduled_id)) { | |
6a488035 | 934 | if (!$activityTypeID) { |
6a488035 TO |
935 | if ($mailing->sms_provider_id) { |
936 | $mailing->subject = $mailing->name; | |
2ede60ec DL |
937 | $activityTypeID = CRM_Core_OptionGroup::getValue( |
938 | 'activity_type', | |
6a488035 TO |
939 | 'Mass SMS', |
940 | 'name' | |
941 | ); | |
942 | } | |
2ede60ec DL |
943 | else { |
944 | $activityTypeID = CRM_Core_OptionGroup::getValue( | |
945 | 'activity_type', | |
946 | 'Bulk Email', | |
947 | 'name' | |
948 | ); | |
949 | } | |
6a488035 TO |
950 | if (!$activityTypeID) { |
951 | CRM_Core_Error::fatal(); | |
952 | } | |
953 | } | |
954 | ||
6a488035 TO |
955 | $activity = array( |
956 | 'source_contact_id' => $mailing->scheduled_id, | |
957 | // CRM-9519 | |
958 | 'target_contact_id' => array_unique($targetParams), | |
6a488035 TO |
959 | 'activity_type_id' => $activityTypeID, |
960 | 'source_record_id' => $this->mailing_id, | |
961 | 'activity_date_time' => $job_date, | |
962 | 'subject' => $mailing->subject, | |
963 | 'status_id' => 2, | |
964 | 'deleteActivityTarget' => FALSE, | |
965 | 'campaign_id' => $mailing->campaign_id, | |
966 | ); | |
967 | ||
968 | //check whether activity is already created for this mailing. | |
969 | //if yes then create only target contact record. | |
970 | $query = " | |
971 | SELECT id | |
972 | FROM civicrm_activity | |
973 | WHERE civicrm_activity.activity_type_id = %1 | |
2ede60ec DL |
974 | AND civicrm_activity.source_record_id = %2 |
975 | "; | |
6a488035 | 976 | |
2ede60ec DL |
977 | $queryParams = array( |
978 | 1 => array($activityTypeID, 'Integer'), | |
6a488035 TO |
979 | 2 => array($this->mailing_id, 'Integer'), |
980 | ); | |
2ede60ec | 981 | $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams); |
6a488035 TO |
982 | |
983 | if ($activityID) { | |
984 | $activity['id'] = $activityID; | |
985 | ||
986 | // CRM-9519 | |
987 | if (CRM_Core_BAO_Email::isMultipleBulkMail()) { | |
b561230a DL |
988 | static $targetRecordID = NULL; |
989 | if (!$targetRecordID) { | |
990 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); | |
991 | $targetRecordID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
992 | } | |
993 | ||
6a488035 TO |
994 | // make sure we don't attempt to duplicate the target activity |
995 | foreach ($activity['target_contact_id'] as $key => $targetID) { | |
b561230a DL |
996 | $sql = " |
997 | SELECT id | |
998 | FROM civicrm_activity_contact | |
999 | WHERE activity_id = $activityID | |
1000 | AND contact_id = $targetID | |
1001 | AND record_type_id = $targetRecordID | |
1002 | "; | |
6a488035 TO |
1003 | if (CRM_Core_DAO::singleValueQuery($sql)) { |
1004 | unset($activity['target_contact_id'][$key]); | |
1005 | } | |
1006 | } | |
1007 | } | |
1008 | } | |
1009 | ||
2ede60ec | 1010 | if (is_a(CRM_Activity_BAO_Activity::create($activity), 'CRM_Core_Error')) { |
6a488035 TO |
1011 | $result = FALSE; |
1012 | } | |
1013 | ||
1014 | $targetParams = array(); | |
1015 | } | |
1016 | ||
1017 | return $result; | |
1018 | } | |
1019 | } | |
1020 |