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