3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
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;
47 function __construct() {
48 parent
::__construct();
52 * @param array $params
54 * @return CRM_Mailing_BAO_MailingJob
56 static public function create($params) {
57 $job = new CRM_Mailing_BAO_MailingJob();
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'];
63 $mailing = new CRM_Mailing_BAO_Mailing();
64 $mailing->getRecipients($job->id
, $params['mailing_id'], NULL, NULL, TRUE, FALSE);
69 * Initiate all pending/ready jobs
71 * @param array $testParams
78 public static function runJobs($testParams = NULL, $mode = NULL) {
79 $job = new CRM_Mailing_BAO_MailingJob();
81 $config = CRM_Core_Config
::singleton();
82 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
83 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
85 if (!empty($testParams)) {
89 WHERE id = {$testParams['job_id']}";
93 $currentTime = date('YmdHis');
94 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
95 $domainID = CRM_Core_Config
::domainID();
97 $modeClause = 'AND m.sms_provider_id IS NULL';
99 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
102 // Select the first child job that is scheduled
108 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
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')
118 ORDER BY j.mailing_id,
126 while ($job->fetch()) {
127 // still use job level lock for each child job
128 $lockName = "civimail.job.{$job->id}";
130 $lock = new CRM_Core_Lock($lockName);
131 if (!$lock->isAcquired()) {
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
140 $job->status
= CRM_Core_DAO
::getFieldValue(
141 'CRM_Mailing_DAO_MailingJob',
149 $job->status
!= 'Running' &&
150 $job->status
!= 'Scheduled'
152 // this includes Cancelled and other statuses, CRM-4246
158 /* Queue up recipients for the child job being launched */
160 if ($job->status
!= 'Running') {
161 $transaction = new CRM_Core_Transaction();
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);
167 // Mark up the starting time
168 $saveJob = new CRM_Mailing_DAO_MailingJob();
169 $saveJob->id
= $job->id
;
170 $saveJob->start_date
= date('YmdHis');
171 $saveJob->status
= 'Running';
174 $transaction->commit();
178 // make it a persistent connection, CRM-9349
179 if ($mode === NULL) {
180 $mailer = $config->getMailer(TRUE);
182 elseif ($mode == 'sms') {
183 $mailer = CRM_SMS_Provider
::singleton(array('mailing_id' => $job->mailing_id
));
186 // Compose and deliver each child job
187 $isComplete = $job->deliver($mailer, $testParams);
189 CRM_Utils_Hook
::post('create', 'CRM_Mailing_DAO_Spool', $job->id
, $isComplete);
191 // Mark the child complete
195 $transaction = new CRM_Core_Transaction();
197 $saveJob = new CRM_Mailing_DAO_MailingJob();
198 $saveJob->id
= $job->id
;
199 $saveJob->end_date
= date('YmdHis');
200 $saveJob->status
= 'Complete';
203 $transaction->commit();
205 // don't mark the mailing as complete
208 // Release the child joblock
217 // post process to determine if the parent job
218 // as well as the mailing is complete after the run
222 public static function runJobs_post($mode = NULL) {
224 $job = new CRM_Mailing_BAO_MailingJob();
226 $mailing = new CRM_Mailing_BAO_Mailing();
228 $config = CRM_Core_Config
::singleton();
229 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
230 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
232 $currentTime = date('YmdHis');
233 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
234 $domainID = CRM_Core_Config
::domainID();
240 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
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,
251 // For each parent job that is running, let's look at their child jobs
252 while ($job->fetch()) {
254 $child_job = new CRM_Mailing_BAO_MailingJob();
258 FROM civicrm_mailing_job j, civicrm_mailing m
259 WHERE m.id = j.mailing_id
260 AND j.job_type = 'child'
262 AND j.status <> 'Complete'";
263 $params = array(1 => array($job->id
, 'Integer'));
265 $anyChildLeft = CRM_Core_DAO
::singleValueQuery($child_job_sql, $params);
267 // all of the child jobs are complete, update
268 // the parent job as well as the mailing status
269 if (!$anyChildLeft) {
271 $transaction = new CRM_Core_Transaction();
273 $saveJob = new CRM_Mailing_DAO_MailingJob();
274 $saveJob->id
= $job->id
;
275 $saveJob->end_date
= date('YmdHis');
276 $saveJob->status
= 'Complete';
280 $mailing->id
= $job->mailing_id
;
281 $mailing->is_completed
= TRUE;
283 $transaction->commit();
289 // before we run jobs, we need to split the jobs
294 public static function runJobs_pre($offset = 200, $mode = NULL) {
295 $job = new CRM_Mailing_BAO_MailingJob();
297 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
298 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
300 $currentTime = date('YmdHis');
301 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
304 $workflowClause = CRM_Mailing_BAO_MailingJob
::workflowClause();
306 $domainID = CRM_Core_Config
::domainID();
308 $modeClause = 'AND m.sms_provider_id IS NULL';
309 if ($mode == 'sms') {
310 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
313 // Select all the mailing jobs that are created from
314 // when the mailing is submitted or scheduled.
319 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
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,
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}";
341 $lock = new CRM_Core_Lock($lockName);
342 if (!$lock->isAcquired()) {
346 // Re-fetch the job status in case things
347 // changed between the first query and now
348 // to avoid race conditions
349 $job->status
= CRM_Core_DAO
::getFieldValue(
350 'CRM_Mailing_DAO_MailingJob',
356 if ($job->status
!= 'Scheduled') {
361 $job->split_job($offset);
363 // update the status of the parent job
364 $transaction = new CRM_Core_Transaction();
366 $saveJob = new CRM_Mailing_DAO_MailingJob();
367 $saveJob->id
= $job->id
;
368 $saveJob->start_date
= date('YmdHis');
369 $saveJob->status
= 'Running';
372 $transaction->commit();
374 // Release the job lock
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
384 public function split_job($offset = 200) {
385 $recipient_count = CRM_Mailing_BAO_Recipients
::mailingSize($this->mailing_id
);
387 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
390 $dao = new CRM_Core_DAO();
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)
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'),
406 // create one child job if the mailing size is less than the offset
407 // probably use a CRM_Mailing_DAO_MailingJob( );
408 if (empty($offset) ||
409 $recipient_count <= $offset
411 CRM_Core_DAO
::executeQuery($sql, $params);
414 // Creating 'child jobs'
415 for ($i = 0; $i < $recipient_count; $i = $i +
$offset) {
417 $params[7][0] = $offset;
418 CRM_Core_DAO
::executeQuery($sql, $params);
424 * @param array $testParams
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);
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
);
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
443 while ($recipients->fetch()) {
444 if ($recipients->phone_id
) {
445 $recipients->email_id
= "null";
448 $recipients->phone_id
= "null";
453 $recipients->email_id
,
454 $recipients->contact_id
,
455 $recipients->phone_id
,
458 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
459 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
465 if (!empty($params)) {
466 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
474 * @param object $mailer A Mail object to send the messages
476 * @param array $testParams
481 public function deliver(&$mailer, $testParams = NULL) {
482 $mailing = new CRM_Mailing_BAO_Mailing();
483 $mailing->id
= $this->mailing_id
;
484 $mailing->find(TRUE);
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();
495 $query = " SELECT $eqTable.id,
496 $emailTable.email as email,
501 INNER JOIN $emailTable
502 ON $eqTable.email_id = $emailTable.id
503 INNER JOIN $contactTable
504 ON $contactTable.id = $emailTable.contact_id
506 ON $eqTable.id = $edTable.event_queue_id
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";
514 if ($mailing->sms_provider_id
) {
517 $phoneTable.phone as phone,
522 INNER JOIN $phoneTable
523 ON $eqTable.phone_id = $phoneTable.id
524 INNER JOIN $contactTable
525 ON $contactTable.id = $phoneTable.contact_id
527 ON $eqTable.id = $edTable.event_queue_id
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";
537 static $config = NULL;
538 static $mailsProcessed = 0;
540 if ($config == NULL) {
541 $config = CRM_Core_Config
::singleton();
544 $job_date = CRM_Utils_Date
::isoToMysql($this->scheduled_date
);
547 if (!empty($testParams)) {
548 $mailing->subject
= ts('[CiviMail Draft]') . ' ' . $mailing->subject
;
551 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
553 // get and format attachments
554 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
556 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
557 CRM_Core_Smarty
::registerStringResource();
561 // This handles the edge case scenario where all the mails
562 // have been delivered in prior jobs
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: " );
572 $config->mailerBatchLimit
> 0 &&
573 $mailsProcessed >= $config->mailerBatchLimit
575 if (!empty($fields)) {
576 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
586 'contact_id' => $eq->contact_id
,
587 'email' => $eq->email
,
588 'phone' => $eq->phone
,
590 if (count($fields) == self
::MAX_CONTACTS_TO_PROCESS
) {
591 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
602 if (!empty($fields)) {
603 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
613 * @param $attachments
618 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
619 static $smtpConnectionErrors = 0;
621 if (!is_object($mailer) ||
empty($fields)) {
622 CRM_Core_Error
::fatal();
625 // get the return properties
626 $returnProperties = $mailing->getReturnProperties();
627 $params = $targetParams = $deliveredParams = array();
631 * CRM-15702: Sending bulk sms to contacts without e-mail addres fails.
632 * Solution is to skip checking for on hold
634 $skipOnHold = true; //do include a statement to check wether e-mail address is on hold
635 if ($mailing->sms_provider_id
) {
636 $skipOnHold = false; //do not include a statement to check wether e-mail address is on hold
639 foreach ($fields as $key => $field) {
640 $params[] = $field['contact_id'];
643 $details = CRM_Utils_Token
::getTokenDetails(
646 $skipOnHold, TRUE, NULL,
647 $mailing->getFlattenedTokens(),
652 $config = CRM_Core_Config
::singleton();
653 foreach ($fields as $key => $field) {
654 $contactID = $field['contact_id'];
655 if (!array_key_exists($contactID, $details[0])) {
656 $details[0][$contactID] = array();
659 /* Compose the mailing */
660 $recipient = $replyToEmail = NULL;
661 $replyValue = strcmp($mailing->replyto_email
, $mailing->from_email
);
663 $replyToEmail = $mailing->replyto_email
;
666 $message = &$mailing->compose(
667 $this->id
, $field['id'], $field['hash'],
668 $field['contact_id'], $field['email'],
669 $recipient, FALSE, $details[0][$contactID], $attachments,
670 FALSE, NULL, $replyToEmail
672 if (empty($message)) {
673 // lets keep the message in the queue
674 // most likely a permissions related issue with smarty templates
675 // or a bad contact id? CRM-9833
679 /* Send the mailing */
681 $body = &$message->get();
682 $headers = &$message->headers();
684 if ($mailing->sms_provider_id
) {
685 $provider = CRM_SMS_Provider
::singleton(array('mailing_id' => $mailing->id
));
686 $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
687 $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
690 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
691 $recipient = $headers['To'];
694 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
696 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
699 $result = $mailer->send($recipient, $headers, $body, $this->id
);
705 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id
) {
707 $message = $result->getMessage();
709 strpos($message, 'Failed to write to socket') !== FALSE ||
710 strpos($message, 'Failed to set sender') !== FALSE
712 // lets log this message and code
713 $code = $result->getCode();
714 CRM_Core_Error
::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code");
716 // these are socket write errors which most likely means smtp connection errors
718 $smtpConnectionErrors++
;
719 if ($smtpConnectionErrors <= 5) {
723 // seems like we have too many of them in a row, we should
724 // write stuff to disk and abort the cron job
732 CRM_Core_Error
::debug_log_message("Too many SMTP Socket Errors. Exiting");
733 CRM_Utils_System
::civiExit();
736 /* Register the bounce event */
739 'event_queue_id' => $field['id'],
740 'job_id' => $this->id
,
741 'hash' => $field['hash'],
743 $params = array_merge($params,
744 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
746 CRM_Mailing_Event_BAO_Bounce
::create($params);
748 elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id
) {
749 // Handle SMS errors: CRM-15426
750 $job_id = intval($this->id
);
751 $mailing_id = intval($mailing->id
);
752 CRM_Core_Error
::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows.");
753 CRM_Core_Error
::debug_log_message($result->getMessage());
756 /* Register the delivery event */
757 $deliveredParams[] = $field['id'];
758 $targetParams[] = $field['contact_id'];
761 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
770 // hack to stop mailing job at run time, CRM-4246.
771 // to avoid making too many DB calls for this rare case
772 // lets do it when we snapshot
773 $status = CRM_Core_DAO
::getFieldValue(
774 'CRM_Mailing_DAO_MailingJob',
781 if ($status != 'Running') {
789 // seems like a successful delivery or bounce, lets decrement error count
790 // only if we have smtp connection errors
791 if ($smtpConnectionErrors > 0) {
792 $smtpConnectionErrors--;
795 // If we have enabled the Throttle option, this is the time to enforce it.
796 if (isset($config->mailThrottleTime
) && $config->mailThrottleTime
> 0) {
797 usleep((int ) $config->mailThrottleTime
);
801 $result = $this->writeToDB(
814 * @param int $mailingId the id of the mailing to be canceled
817 public static function cancel($mailingId) {
820 FROM civicrm_mailing_job
821 WHERE mailing_id = %1
823 AND ( ( job_type IS NULL ) OR
824 job_type <> 'child' )
826 $params = array(1 => array($mailingId, 'Integer'));
827 $job = CRM_Core_DAO
::executeQuery($sql, $params);
829 in_array($job->status
, array('Scheduled', 'Running', 'Paused'))
832 $newJob = new CRM_Mailing_BAO_MailingJob();
833 $newJob->id
= $job->id
;
834 $newJob->end_date
= date('YmdHis');
835 $newJob->status
= 'Canceled';
838 // also cancel all child jobs
840 UPDATE civicrm_mailing_job
841 SET status = 'Canceled',
845 AND job_type = 'child'
846 AND status IN ( 'Scheduled', 'Running', 'Paused' )
848 $params = array(1 => array($job->id
, 'Integer'),
849 2 => array(date('YmdHis'), 'Timestamp'),
851 CRM_Core_DAO
::executeQuery($sql, $params);
853 CRM_Core_Session
::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
858 * Return a translated status enum string
860 * @param string $status The status enum
862 * @return string The translated version
866 public static function status($status) {
867 static $translation = NULL;
869 if (empty($translation)) {
870 $translation = array(
871 'Scheduled' => ts('Scheduled'),
872 'Running' => ts('Running'),
873 'Complete' => ts('Complete'),
874 'Paused' => ts('Paused'),
875 'Canceled' => ts('Canceled'),
878 return CRM_Utils_Array
::value($status, $translation, ts('Not scheduled'));
882 * Return a workflow clause for use in SQL queries,
883 * to only process jobs that are approved.
885 * @return string For use in a WHERE clause
889 public static function workflowClause() {
890 // add an additional check and only process
891 // jobs that are approved
892 if (CRM_Mailing_Info
::workflowEnabled()) {
893 $approveOptionID = CRM_Core_OptionGroup
::getValue('mail_approval_status',
897 if ($approveOptionID) {
898 return " AND m.approval_status_id = $approveOptionID ";
905 * @param array $deliveredParams
906 * @param array $targetParams
911 * @throws CRM_Core_Exception
914 public function writeToDB(
920 static $activityTypeID = NULL;
921 static $writeActivity = NULL;
923 if (!empty($deliveredParams)) {
924 CRM_Mailing_Event_BAO_Delivered
::bulkCreate($deliveredParams);
925 $deliveredParams = array();
928 if ($writeActivity === NULL) {
929 $writeActivity = CRM_Core_BAO_Setting
::getItem(
930 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
931 'write_activity_record',
937 if (!$writeActivity) {
942 if (!empty($targetParams) && !empty($mailing->scheduled_id
)) {
943 if (!$activityTypeID) {
944 if ($mailing->sms_provider_id
) {
945 $mailing->subject
= $mailing->name
;
946 $activityTypeID = CRM_Core_OptionGroup
::getValue(
953 $activityTypeID = CRM_Core_OptionGroup
::getValue(
959 if (!$activityTypeID) {
960 CRM_Core_Error
::fatal();
965 'source_contact_id' => $mailing->scheduled_id
,
967 'target_contact_id' => array_unique($targetParams),
968 'activity_type_id' => $activityTypeID,
969 'source_record_id' => $this->mailing_id
,
970 'activity_date_time' => $job_date,
971 'subject' => $mailing->subject
,
973 'deleteActivityTarget' => FALSE,
974 'campaign_id' => $mailing->campaign_id
,
977 //check whether activity is already created for this mailing.
978 //if yes then create only target contact record.
981 FROM civicrm_activity
982 WHERE civicrm_activity.activity_type_id = %1
983 AND civicrm_activity.source_record_id = %2
986 $queryParams = array(
987 1 => array($activityTypeID, 'Integer'),
988 2 => array($this->mailing_id
, 'Integer'),
990 $activityID = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
993 $activity['id'] = $activityID;
996 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
997 static $targetRecordID = NULL;
998 if (!$targetRecordID) {
999 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1000 $targetRecordID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
1003 // make sure we don't attempt to duplicate the target activity
1004 foreach ($activity['target_contact_id'] as $key => $targetID) {
1007 FROM civicrm_activity_contact
1008 WHERE activity_id = $activityID
1009 AND contact_id = $targetID
1010 AND record_type_id = $targetRecordID
1012 if (CRM_Core_DAO
::singleValueQuery($sql)) {
1013 unset($activity['target_contact_id'][$key]);
1019 if (is_a(CRM_Activity_BAO_Activity
::create($activity), 'CRM_Core_Error')) {
1023 $targetParams = array();