3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 require_once 'Mail.php';
39 * Class CRM_Mailing_BAO_MailingJob
41 class CRM_Mailing_BAO_MailingJob
extends CRM_Mailing_DAO_MailingJob
{
42 const MAX_CONTACTS_TO_PROCESS
= 1000;
45 * (Dear God Why) Keep a global count of mails processed within the current
50 static $mailsProcessed = 0;
55 public function __construct() {
56 parent
::__construct();
60 * @param array $params
62 * @return CRM_Mailing_BAO_MailingJob
64 static public function create($params) {
65 $job = new CRM_Mailing_BAO_MailingJob();
66 $job->mailing_id
= $params['mailing_id'];
67 $job->status
= $params['status'];
68 $job->scheduled_date
= $params['scheduled_date'];
69 $job->is_test
= $params['is_test'];
71 $mailing = new CRM_Mailing_BAO_Mailing();
72 $mailing->id
= $params['mailing_id'];
73 if ($mailing->id
&& $mailing->find(TRUE)) {
74 $mailing->getRecipients($job->id
, $params['mailing_id'], NULL, NULL, TRUE, $mailing->dedupe_email
);
78 throw new CRM_Core_Exception("Failed to create job: Unknown mailing ID");
83 * Initiate all pending/ready jobs
85 * @param array $testParams
90 public static function runJobs($testParams = NULL, $mode = NULL) {
91 $job = new CRM_Mailing_BAO_MailingJob();
93 $config = CRM_Core_Config
::singleton();
94 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
95 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
97 if (!empty($testParams)) {
101 WHERE id = {$testParams['job_id']}";
105 $currentTime = date('YmdHis');
106 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
107 $domainID = CRM_Core_Config
::domainID();
109 $modeClause = 'AND m.sms_provider_id IS NULL';
110 if ($mode == 'sms') {
111 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
114 // Select the first child job that is scheduled
120 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
123 AND ( ( j.start_date IS null
124 AND j.scheduled_date <= $currentTime
125 AND j.status = 'Scheduled' )
126 OR ( j.status = 'Running'
127 AND j.end_date IS null ) )
128 AND (j.job_type = 'child')
130 ORDER BY j.mailing_id,
137 while ($job->fetch()) {
138 // still use job level lock for each child job
139 $lockName = "civimail.job.{$job->id}";
141 $lock = new CRM_Core_Lock($lockName);
142 if (!$lock->isAcquired()) {
146 // for test jobs we do not change anything, since its on a short-circuit path
147 if (empty($testParams)) {
148 // we've got the lock, but while we were waiting and processing
149 // other emails, this job might have changed under us
150 // lets get the job status again and check
151 $job->status
= CRM_Core_DAO
::getFieldValue(
152 'CRM_Mailing_DAO_MailingJob',
160 $job->status
!= 'Running' &&
161 $job->status
!= 'Scheduled'
163 // this includes Cancelled and other statuses, CRM-4246
169 /* Queue up recipients for the child job being launched */
171 if ($job->status
!= 'Running') {
172 $transaction = new CRM_Core_Transaction();
174 // have to queue it up based on the offset and limits
175 // get the parent ID, and limit and offset
176 $job->queue($testParams);
178 // Mark up the starting time
179 $saveJob = new CRM_Mailing_DAO_MailingJob();
180 $saveJob->id
= $job->id
;
181 $saveJob->start_date
= date('YmdHis');
182 $saveJob->status
= 'Running';
185 $transaction->commit();
189 // make it a persistent connection, CRM-9349
190 if ($mode === NULL) {
191 $mailer = $config->getMailer(TRUE);
193 elseif ($mode == 'sms') {
194 $mailer = CRM_SMS_Provider
::singleton(array('mailing_id' => $job->mailing_id
));
197 // Compose and deliver each child job
198 $isComplete = $job->deliver($mailer, $testParams);
200 CRM_Utils_Hook
::post('create', 'CRM_Mailing_DAO_Spool', $job->id
, $isComplete);
202 // Mark the child complete
206 $transaction = new CRM_Core_Transaction();
208 $saveJob = new CRM_Mailing_DAO_MailingJob();
209 $saveJob->id
= $job->id
;
210 $saveJob->end_date
= date('YmdHis');
211 $saveJob->status
= 'Complete';
214 $transaction->commit();
216 // don't mark the mailing as complete
219 // Release the child joblock
229 * post process to determine if the parent job.
230 * as well as the mailing is complete after the run
233 public static function runJobs_post($mode = NULL) {
235 $job = new CRM_Mailing_BAO_MailingJob();
237 $mailing = new CRM_Mailing_BAO_Mailing();
239 $config = CRM_Core_Config
::singleton();
240 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
241 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
243 $currentTime = date('YmdHis');
244 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
245 $domainID = CRM_Core_Config
::domainID();
251 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
253 AND j.scheduled_date <= $currentTime
254 AND j.status = 'Running'
255 AND j.end_date IS null
256 AND (j.job_type != 'child' OR j.job_type is NULL)
257 ORDER BY j.scheduled_date,
262 // For each parent job that is running, let's look at their child jobs
263 while ($job->fetch()) {
265 $child_job = new CRM_Mailing_BAO_MailingJob();
269 FROM civicrm_mailing_job j, civicrm_mailing m
270 WHERE m.id = j.mailing_id
271 AND j.job_type = 'child'
273 AND j.status <> 'Complete'";
274 $params = array(1 => array($job->id
, 'Integer'));
276 $anyChildLeft = CRM_Core_DAO
::singleValueQuery($child_job_sql, $params);
278 // all of the child jobs are complete, update
279 // the parent job as well as the mailing status
280 if (!$anyChildLeft) {
282 $transaction = new CRM_Core_Transaction();
284 $saveJob = new CRM_Mailing_DAO_MailingJob();
285 $saveJob->id
= $job->id
;
286 $saveJob->end_date
= date('YmdHis');
287 $saveJob->status
= 'Complete';
291 $mailing->id
= $job->mailing_id
;
292 $mailing->is_completed
= TRUE;
294 $transaction->commit();
301 * before we run jobs, we need to split the jobs
305 public static function runJobs_pre($offset = 200, $mode = NULL) {
306 $job = new CRM_Mailing_BAO_MailingJob();
308 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
309 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
311 $currentTime = date('YmdHis');
312 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
314 $workflowClause = CRM_Mailing_BAO_MailingJob
::workflowClause();
316 $domainID = CRM_Core_Config
::domainID();
318 $modeClause = 'AND m.sms_provider_id IS NULL';
319 if ($mode == 'sms') {
320 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
323 // Select all the mailing jobs that are created from
324 // when the mailing is submitted or scheduled.
329 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
333 AND ( ( j.start_date IS null
334 AND j.scheduled_date <= $currentTime
335 AND j.status = 'Scheduled'
336 AND j.end_date IS null ) )
337 AND ((j.job_type is NULL) OR (j.job_type <> 'child'))
338 ORDER BY j.scheduled_date,
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}";
349 $lock = new CRM_Core_Lock($lockName);
350 if (!$lock->isAcquired()) {
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',
364 if ($job->status
!= 'Scheduled') {
369 $job->split_job($offset);
371 // update the status of the parent job
372 $transaction = new CRM_Core_Transaction();
374 $saveJob = new CRM_Mailing_DAO_MailingJob();
375 $saveJob->id
= $job->id
;
376 $saveJob->start_date
= date('YmdHis');
377 $saveJob->status
= 'Running';
380 $transaction->commit();
382 // Release the job lock
388 * Split the parent job into n number of child job based on an offset.
389 * If null or 0 , we create only one child job
392 public function split_job($offset = 200) {
393 $recipient_count = CRM_Mailing_BAO_Recipients
::mailingSize($this->mailing_id
);
395 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
397 $dao = new CRM_Core_DAO();
400 INSERT INTO civicrm_mailing_job
401 (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`)
402 VALUES (%1, %2, %3, %4, %5, %6, %7)
405 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'),
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
419 CRM_Core_DAO
::executeQuery($sql, $params);
422 // Creating 'child jobs'
423 for ($i = 0; $i < $recipient_count; $i = $i +
$offset) {
425 $params[7][0] = $offset;
426 CRM_Core_DAO
::executeQuery($sql, $params);
432 * @param array $testParams
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);
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
);
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
451 while ($recipients->fetch()) {
452 if ($recipients->phone_id
) {
453 $recipients->email_id
= "null";
456 $recipients->phone_id
= "null";
461 $recipients->email_id
,
462 $recipients->contact_id
,
463 $recipients->phone_id
,
466 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
467 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
473 if (!empty($params)) {
474 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
482 * @param object $mailer
483 * A Mail object to send the messages.
485 * @param array $testParams
489 public function deliver(&$mailer, $testParams = NULL) {
490 $mailing = new CRM_Mailing_BAO_Mailing();
491 $mailing->id
= $this->mailing_id
;
492 $mailing->find(TRUE);
495 $eq = new CRM_Mailing_Event_BAO_Queue();
496 $eqTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
497 $emailTable = CRM_Core_BAO_Email
::getTableName();
498 $phoneTable = CRM_Core_DAO_Phone
::getTableName();
499 $contactTable = CRM_Contact_BAO_Contact
::getTableName();
500 $edTable = CRM_Mailing_Event_BAO_Delivered
::getTableName();
501 $ebTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
503 $query = " SELECT $eqTable.id,
504 $emailTable.email as email,
509 INNER JOIN $emailTable
510 ON $eqTable.email_id = $emailTable.id
511 INNER JOIN $contactTable
512 ON $contactTable.id = $emailTable.contact_id
514 ON $eqTable.id = $edTable.event_queue_id
516 ON $eqTable.id = $ebTable.event_queue_id
517 WHERE $eqTable.job_id = " . $this->id
. "
518 AND $edTable.id IS null
519 AND $ebTable.id IS null
520 AND $contactTable.is_opt_out = 0";
522 if ($mailing->sms_provider_id
) {
525 $phoneTable.phone as phone,
530 INNER JOIN $phoneTable
531 ON $eqTable.phone_id = $phoneTable.id
532 INNER JOIN $contactTable
533 ON $contactTable.id = $phoneTable.contact_id
535 ON $eqTable.id = $edTable.event_queue_id
537 ON $eqTable.id = $ebTable.event_queue_id
538 WHERE $eqTable.job_id = " . $this->id
. "
539 AND $edTable.id IS null
540 AND $ebTable.id IS null
541 AND ( $contactTable.is_opt_out = 0
542 OR $contactTable.do_not_sms = 0 )";
548 if ($config == NULL) {
549 $config = CRM_Core_Config
::singleton();
552 $job_date = CRM_Utils_Date
::isoToMysql($this->scheduled_date
);
555 if (!empty($testParams)) {
556 $mailing->subject
= ts('[CiviMail Draft]') . ' ' . $mailing->subject
;
559 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
561 // get and format attachments
562 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
564 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
565 CRM_Core_Smarty
::registerStringResource();
569 // This handles the edge case scenario where all the mails
570 // have been delivered in prior jobs
573 // make sure that there's no more than $config->mailerBatchLimit mails processed in a run
574 while ($eq->fetch()) {
575 // if ( ( $mailsProcessed % 100 ) == 0 ) {
576 // CRM_Utils_System::xMemory( "$mailsProcessed: " );
580 $config->mailerBatchLimit
> 0 &&
581 self
::$mailsProcessed >= $config->mailerBatchLimit
583 if (!empty($fields)) {
584 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
589 self
::$mailsProcessed++
;
594 'contact_id' => $eq->contact_id
,
595 'email' => $eq->email
,
596 'phone' => $eq->phone
,
598 if (count($fields) == self
::MAX_CONTACTS_TO_PROCESS
) {
599 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
610 if (!empty($fields)) {
611 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
621 * @param $attachments
626 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
627 static $smtpConnectionErrors = 0;
629 if (!is_object($mailer) ||
empty($fields)) {
630 CRM_Core_Error
::fatal();
633 // get the return properties
634 $returnProperties = $mailing->getReturnProperties();
635 $params = $targetParams = $deliveredParams = array();
639 * CRM-15702: Sending bulk sms to contacts without e-mail address fails.
640 * Solution is to skip checking for on hold
642 $skipOnHold = TRUE; //do include a statement to check wether e-mail address is on hold
643 if ($mailing->sms_provider_id
) {
644 $skipOnHold = FALSE; //do not include a statement to check wether e-mail address is on hold
647 foreach ($fields as $key => $field) {
648 $params[] = $field['contact_id'];
651 $details = CRM_Utils_Token
::getTokenDetails(
654 $skipOnHold, TRUE, NULL,
655 $mailing->getFlattenedTokens(),
660 $config = CRM_Core_Config
::singleton();
661 foreach ($fields as $key => $field) {
662 $contactID = $field['contact_id'];
663 if (!array_key_exists($contactID, $details[0])) {
664 $details[0][$contactID] = array();
667 /* Compose the mailing */
668 $recipient = $replyToEmail = NULL;
669 $replyValue = strcmp($mailing->replyto_email
, $mailing->from_email
);
671 $replyToEmail = $mailing->replyto_email
;
674 $message = &$mailing->compose(
675 $this->id
, $field['id'], $field['hash'],
676 $field['contact_id'], $field['email'],
677 $recipient, FALSE, $details[0][$contactID], $attachments,
678 FALSE, NULL, $replyToEmail
680 if (empty($message)) {
681 // lets keep the message in the queue
682 // most likely a permissions related issue with smarty templates
683 // or a bad contact id? CRM-9833
687 /* Send the mailing */
689 $body = &$message->get();
690 $headers = &$message->headers();
692 if ($mailing->sms_provider_id
) {
693 $provider = CRM_SMS_Provider
::singleton(array('mailing_id' => $mailing->id
));
694 $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
695 $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
698 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
699 $recipient = $headers['To'];
702 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
704 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
707 $result = $mailer->send($recipient, $headers, $body, $this->id
);
713 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id
) {
715 $message = $result->getMessage();
717 strpos($message, 'Failed to write to socket') !== FALSE ||
718 strpos($message, 'Failed to set sender') !== FALSE
720 // lets log this message and code
721 $code = $result->getCode();
722 CRM_Core_Error
::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code");
724 // these are socket write errors which most likely means smtp connection errors
726 $smtpConnectionErrors++
;
727 if ($smtpConnectionErrors <= 5) {
731 // seems like we have too many of them in a row, we should
732 // write stuff to disk and abort the cron job
740 CRM_Core_Error
::debug_log_message("Too many SMTP Socket Errors. Exiting");
741 CRM_Utils_System
::civiExit();
744 /* Register the bounce event */
747 'event_queue_id' => $field['id'],
748 'job_id' => $this->id
,
749 'hash' => $field['hash'],
751 $params = array_merge($params,
752 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
754 CRM_Mailing_Event_BAO_Bounce
::create($params);
756 elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id
) {
757 // Handle SMS errors: CRM-15426
758 $job_id = intval($this->id
);
759 $mailing_id = intval($mailing->id
);
760 CRM_Core_Error
::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows.");
761 CRM_Core_Error
::debug_log_message($result->getMessage());
764 /* Register the delivery event */
765 $deliveredParams[] = $field['id'];
766 $targetParams[] = $field['contact_id'];
769 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
778 // hack to stop mailing job at run time, CRM-4246.
779 // to avoid making too many DB calls for this rare case
780 // lets do it when we snapshot
781 $status = CRM_Core_DAO
::getFieldValue(
782 'CRM_Mailing_DAO_MailingJob',
789 if ($status != 'Running') {
797 // seems like a successful delivery or bounce, lets decrement error count
798 // only if we have smtp connection errors
799 if ($smtpConnectionErrors > 0) {
800 $smtpConnectionErrors--;
803 // If we have enabled the Throttle option, this is the time to enforce it.
804 if (isset($config->mailThrottleTime
) && $config->mailThrottleTime
> 0) {
805 usleep((int ) $config->mailThrottleTime
);
809 $result = $this->writeToDB(
822 * @param int $mailingId
823 * The id of the mailing to be canceled.
825 public static function cancel($mailingId) {
828 FROM civicrm_mailing_job
829 WHERE mailing_id = %1
831 AND ( ( job_type IS NULL ) OR
832 job_type <> 'child' )
834 $params = array(1 => array($mailingId, 'Integer'));
835 $job = CRM_Core_DAO
::executeQuery($sql, $params);
837 in_array($job->status
, array('Scheduled', 'Running', 'Paused'))
840 $newJob = new CRM_Mailing_BAO_MailingJob();
841 $newJob->id
= $job->id
;
842 $newJob->end_date
= date('YmdHis');
843 $newJob->status
= 'Canceled';
846 // also cancel all child jobs
848 UPDATE civicrm_mailing_job
849 SET status = 'Canceled',
853 AND job_type = 'child'
854 AND status IN ( 'Scheduled', 'Running', 'Paused' )
857 1 => array($job->id
, 'Integer'),
858 2 => array(date('YmdHis'), 'Timestamp'),
860 CRM_Core_DAO
::executeQuery($sql, $params);
862 CRM_Core_Session
::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
867 * Return a translated status enum string.
869 * @param string $status
873 * The translated version
875 public static function status($status) {
876 static $translation = NULL;
878 if (empty($translation)) {
879 $translation = array(
880 'Scheduled' => ts('Scheduled'),
881 'Running' => ts('Running'),
882 'Complete' => ts('Complete'),
883 'Paused' => ts('Paused'),
884 'Canceled' => ts('Canceled'),
887 return CRM_Utils_Array
::value($status, $translation, ts('Not scheduled'));
891 * Return a workflow clause for use in SQL queries,
892 * to only process jobs that are approved.
895 * For use in a WHERE clause
897 public static function workflowClause() {
898 // add an additional check and only process
899 // jobs that are approved
900 if (CRM_Mailing_Info
::workflowEnabled()) {
901 $approveOptionID = CRM_Core_OptionGroup
::getValue('mail_approval_status',
905 if ($approveOptionID) {
906 return " AND m.approval_status_id = $approveOptionID ";
913 * @param array $deliveredParams
914 * @param array $targetParams
919 * @throws CRM_Core_Exception
922 public function writeToDB(
928 static $activityTypeID = NULL;
929 static $writeActivity = NULL;
931 if (!empty($deliveredParams)) {
932 CRM_Mailing_Event_BAO_Delivered
::bulkCreate($deliveredParams);
933 $deliveredParams = array();
936 if ($writeActivity === NULL) {
937 $writeActivity = CRM_Core_BAO_Setting
::getItem(
938 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
939 'write_activity_record',
945 if (!$writeActivity) {
950 if (!empty($targetParams) && !empty($mailing->scheduled_id
)) {
951 if (!$activityTypeID) {
952 if ($mailing->sms_provider_id
) {
953 $mailing->subject
= $mailing->name
;
954 $activityTypeID = CRM_Core_OptionGroup
::getValue(
961 $activityTypeID = CRM_Core_OptionGroup
::getValue(
967 if (!$activityTypeID) {
968 CRM_Core_Error
::fatal();
973 'source_contact_id' => $mailing->scheduled_id
,
975 'target_contact_id' => array_unique($targetParams),
976 'activity_type_id' => $activityTypeID,
977 'source_record_id' => $this->mailing_id
,
978 'activity_date_time' => $job_date,
979 'subject' => $mailing->subject
,
981 'deleteActivityTarget' => FALSE,
982 'campaign_id' => $mailing->campaign_id
,
985 //check whether activity is already created for this mailing.
986 //if yes then create only target contact record.
989 FROM civicrm_activity
990 WHERE civicrm_activity.activity_type_id = %1
991 AND civicrm_activity.source_record_id = %2
994 $queryParams = array(
995 1 => array($activityTypeID, 'Integer'),
996 2 => array($this->mailing_id
, 'Integer'),
998 $activityID = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
1001 $activity['id'] = $activityID;
1004 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
1005 static $targetRecordID = NULL;
1006 if (!$targetRecordID) {
1007 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1008 $targetRecordID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
1011 // make sure we don't attempt to duplicate the target activity
1012 foreach ($activity['target_contact_id'] as $key => $targetID) {
1015 FROM civicrm_activity_contact
1016 WHERE activity_id = $activityID
1017 AND contact_id = $targetID
1018 AND record_type_id = $targetRecordID
1020 if (CRM_Core_DAO
::singleValueQuery($sql)) {
1021 unset($activity['target_contact_id'][$key]);
1027 if (is_a(CRM_Activity_BAO_Activity
::create($activity), 'CRM_Core_Error')) {
1031 $targetParams = array();