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';
37 class CRM_Mailing_BAO_MailingJob
extends CRM_Mailing_DAO_MailingJob
{
38 CONST MAX_CONTACTS_TO_PROCESS
= 1000;
43 function __construct() {
44 parent
::__construct();
50 * @return CRM_Mailing_BAO_MailingJob
52 static public function create($params) {
53 $job = new CRM_Mailing_BAO_MailingJob();
54 $job->mailing_id
= $params['mailing_id'];
55 $job->status
= $params['status'];
56 $job->scheduled_date
= $params['scheduled_date'];
57 $job->is_test
= $params['is_test'];
59 $mailing = new CRM_Mailing_BAO_Mailing();
60 $mailing->getRecipients($job->id
, $params['mailing_id'], NULL, NULL, TRUE, FALSE);
65 * Initiate all pending/ready jobs
67 * @param null $testParams
74 public static function runJobs($testParams = NULL, $mode = NULL) {
75 $job = new CRM_Mailing_BAO_MailingJob();
77 $config = CRM_Core_Config
::singleton();
78 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
79 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
81 if (!empty($testParams)) {
85 WHERE id = {$testParams['job_id']}";
89 $currentTime = date('YmdHis');
90 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
91 $domainID = CRM_Core_Config
::domainID();
93 $modeClause = 'AND m.sms_provider_id IS NULL';
95 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
98 // Select the first child job that is scheduled
104 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
107 AND ( ( j.start_date IS null
108 AND j.scheduled_date <= $currentTime
109 AND j.status = 'Scheduled' )
110 OR ( j.status = 'Running'
111 AND j.end_date IS null ) )
112 AND (j.job_type = 'child')
114 ORDER BY j.mailing_id,
122 while ($job->fetch()) {
123 // still use job level lock for each child job
124 $lockName = "civimail.job.{$job->id}";
126 $lock = new CRM_Core_Lock($lockName);
127 if (!$lock->isAcquired()) {
131 // for test jobs we do not change anything, since its on a short-circuit path
132 if (empty($testParams)) {
133 // we've got the lock, but while we were waiting and processing
134 // other emails, this job might have changed under us
135 // lets get the job status again and check
136 $job->status
= CRM_Core_DAO
::getFieldValue(
137 'CRM_Mailing_DAO_MailingJob',
145 $job->status
!= 'Running' &&
146 $job->status
!= 'Scheduled'
148 // this includes Cancelled and other statuses, CRM-4246
154 /* Queue up recipients for the child job being launched */
156 if ($job->status
!= 'Running') {
157 $transaction = new CRM_Core_Transaction();
159 // have to queue it up based on the offset and limits
160 // get the parent ID, and limit and offset
161 $job->queue($testParams);
163 // Mark up the starting time
164 $saveJob = new CRM_Mailing_DAO_MailingJob();
165 $saveJob->id
= $job->id
;
166 $saveJob->start_date
= date('YmdHis');
167 $saveJob->status
= 'Running';
170 $transaction->commit();
174 // make it a persistent connection, CRM-9349
175 if ($mode === NULL) {
176 $mailer = $config->getMailer(TRUE);
178 elseif ($mode == 'sms') {
179 $mailer = CRM_SMS_Provider
::singleton(array('mailing_id' => $job->mailing_id
));
182 // Compose and deliver each child job
183 $isComplete = $job->deliver($mailer, $testParams);
185 CRM_Utils_Hook
::post('create', 'CRM_Mailing_DAO_Spool', $job->id
, $isComplete);
187 // Mark the child complete
191 $transaction = new CRM_Core_Transaction();
193 $saveJob = new CRM_Mailing_DAO_MailingJob();
194 $saveJob->id
= $job->id
;
195 $saveJob->end_date
= date('YmdHis');
196 $saveJob->status
= 'Complete';
199 $transaction->commit();
201 // don't mark the mailing as complete
204 // Release the child joblock
213 // post process to determine if the parent job
214 // as well as the mailing is complete after the run
215 public static function runJobs_post($mode = NULL) {
217 $job = new CRM_Mailing_BAO_MailingJob();
219 $mailing = new CRM_Mailing_BAO_Mailing();
221 $config = CRM_Core_Config
::singleton();
222 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
223 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
225 $currentTime = date('YmdHis');
226 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
227 $domainID = CRM_Core_Config
::domainID();
233 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
235 AND j.scheduled_date <= $currentTime
236 AND j.status = 'Running'
237 AND j.end_date IS null
238 AND (j.job_type != 'child' OR j.job_type is NULL)
239 ORDER BY j.scheduled_date,
244 // For each parent job that is running, let's look at their child jobs
245 while ($job->fetch()) {
247 $child_job = new CRM_Mailing_BAO_MailingJob();
251 FROM civicrm_mailing_job j, civicrm_mailing m
252 WHERE m.id = j.mailing_id
253 AND j.job_type = 'child'
255 AND j.status <> 'Complete'";
256 $params = array(1 => array($job->id
, 'Integer'));
258 $anyChildLeft = CRM_Core_DAO
::singleValueQuery($child_job_sql, $params);
260 // all of the child jobs are complete, update
261 // the parent job as well as the mailing status
262 if (!$anyChildLeft) {
264 $transaction = new CRM_Core_Transaction();
266 $saveJob = new CRM_Mailing_DAO_MailingJob();
267 $saveJob->id
= $job->id
;
268 $saveJob->end_date
= date('YmdHis');
269 $saveJob->status
= 'Complete';
273 $mailing->id
= $job->mailing_id
;
274 $mailing->is_completed
= TRUE;
276 $transaction->commit();
282 // before we run jobs, we need to split the jobs
283 public static function runJobs_pre($offset = 200, $mode = NULL) {
284 $job = new CRM_Mailing_BAO_MailingJob();
286 $config = CRM_Core_Config
::singleton();
287 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
288 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
290 $currentTime = date('YmdHis');
291 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
294 $workflowClause = CRM_Mailing_BAO_MailingJob
::workflowClause();
296 $domainID = CRM_Core_Config
::domainID();
298 $modeClause = 'AND m.sms_provider_id IS NULL';
299 if ($mode == 'sms') {
300 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
303 // Select all the mailing jobs that are created from
304 // when the mailing is submitted or scheduled.
309 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
313 AND ( ( j.start_date IS null
314 AND j.scheduled_date <= $currentTime
315 AND j.status = 'Scheduled'
316 AND j.end_date IS null ) )
317 AND ((j.job_type is NULL) OR (j.job_type <> 'child'))
318 ORDER BY j.scheduled_date,
325 // For each of the "Parent Jobs" we find, we split them into
326 // X Number of child jobs
327 while ($job->fetch()) {
328 // still use job level lock for each child job
329 $lockName = "civimail.job.{$job->id}";
331 $lock = new CRM_Core_Lock($lockName);
332 if (!$lock->isAcquired()) {
336 // Re-fetch the job status in case things
337 // changed between the first query and now
338 // to avoid race conditions
339 $job->status
= CRM_Core_DAO
::getFieldValue(
340 'CRM_Mailing_DAO_MailingJob',
346 if ($job->status
!= 'Scheduled') {
351 $job->split_job($offset);
353 // update the status of the parent job
354 $transaction = new CRM_Core_Transaction();
356 $saveJob = new CRM_Mailing_DAO_MailingJob();
357 $saveJob->id
= $job->id
;
358 $saveJob->start_date
= date('YmdHis');
359 $saveJob->status
= 'Running';
362 $transaction->commit();
364 // Release the job lock
369 // Split the parent job into n number of child job based on an offset
370 // If null or 0 , we create only one child job
371 public function split_job($offset = 200) {
372 $recipient_count = CRM_Mailing_BAO_Recipients
::mailingSize($this->mailing_id
);
374 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
377 $dao = new CRM_Core_DAO();
380 INSERT INTO civicrm_mailing_job
381 (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`)
382 VALUES (%1, %2, %3, %4, %5, %6, %7)
384 $params = array(1 => array($this->mailing_id
, 'Integer'),
385 2 => array($this->scheduled_date
, 'String'),
386 3 => array('Scheduled', 'String'),
387 4 => array('child', 'String'),
388 5 => array($this->id
, 'Integer'),
389 6 => array(0, 'Integer'),
390 7 => array($recipient_count, 'Integer'),
393 // create one child job if the mailing size is less than the offset
394 // probably use a CRM_Mailing_DAO_MailingJob( );
395 if (empty($offset) ||
396 $recipient_count <= $offset
398 CRM_Core_DAO
::executeQuery($sql, $params);
401 // Creating 'child jobs'
402 for ($i = 0; $i < $recipient_count; $i = $i +
$offset) {
404 $params[7][0] = $offset;
405 CRM_Core_DAO
::executeQuery($sql, $params);
410 public function queue($testParams = NULL) {
411 $mailing = new CRM_Mailing_BAO_Mailing();
412 $mailing->id
= $this->mailing_id
;
413 if (!empty($testParams)) {
414 $mailing->getTestRecipients($testParams);
417 // We are still getting all the recipients from the parent job
418 // so we don't mess with the include/exclude logic.
419 $recipients = CRM_Mailing_BAO_Recipients
::mailingQuery($this->mailing_id
, $this->job_offset
, $this->job_limit
);
421 // FIXME: this is not very smart, we should move this to one DB call
422 // INSERT INTO ... SELECT FROM ..
423 // the thing we need to figure out is how to generate the hash automatically
427 while ($recipients->fetch()) {
428 if ($recipients->phone_id
) {
429 $recipients->email_id
= "null";
432 $recipients->phone_id
= "null";
437 $recipients->email_id
,
438 $recipients->contact_id
,
439 $recipients->phone_id
,
442 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
443 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
449 if (!empty($params)) {
450 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
458 * @param object $mailer A Mail object to send the messages
463 public function deliver(&$mailer, $testParams = NULL) {
464 $mailing = new CRM_Mailing_BAO_Mailing();
465 $mailing->id
= $this->mailing_id
;
466 $mailing->find(TRUE);
469 $eq = new CRM_Mailing_Event_BAO_Queue();
470 $eqTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
471 $emailTable = CRM_Core_BAO_Email
::getTableName();
472 $phoneTable = CRM_Core_DAO_Phone
::getTableName();
473 $contactTable = CRM_Contact_BAO_Contact
::getTableName();
474 $edTable = CRM_Mailing_Event_BAO_Delivered
::getTableName();
475 $ebTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
477 $query = " SELECT $eqTable.id,
478 $emailTable.email as email,
483 INNER JOIN $emailTable
484 ON $eqTable.email_id = $emailTable.id
485 INNER JOIN $contactTable
486 ON $contactTable.id = $emailTable.contact_id
488 ON $eqTable.id = $edTable.event_queue_id
490 ON $eqTable.id = $ebTable.event_queue_id
491 WHERE $eqTable.job_id = " . $this->id
. "
492 AND $edTable.id IS null
493 AND $ebTable.id IS null
494 AND $contactTable.is_opt_out = 0";
496 if ($mailing->sms_provider_id
) {
499 $phoneTable.phone as phone,
504 INNER JOIN $phoneTable
505 ON $eqTable.phone_id = $phoneTable.id
506 INNER JOIN $contactTable
507 ON $contactTable.id = $phoneTable.contact_id
509 ON $eqTable.id = $edTable.event_queue_id
511 ON $eqTable.id = $ebTable.event_queue_id
512 WHERE $eqTable.job_id = " . $this->id
. "
513 AND $edTable.id IS null
514 AND $ebTable.id IS null
515 AND $contactTable.is_opt_out = 0";
519 static $config = NULL;
520 static $mailsProcessed = 0;
522 if ($config == NULL) {
523 $config = CRM_Core_Config
::singleton();
526 $job_date = CRM_Utils_Date
::isoToMysql($this->scheduled_date
);
529 if (!empty($testParams)) {
530 $mailing->subject
= ts('[CiviMail Draft]') . ' ' . $mailing->subject
;
533 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
535 // get and format attachments
536 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
538 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
539 CRM_Core_Smarty
::registerStringResource();
543 // This handles the edge case scenario where all the mails
544 // have been delivered in prior jobs
547 // make sure that there's no more than $config->mailerBatchLimit mails processed in a run
548 while ($eq->fetch()) {
549 // if ( ( $mailsProcessed % 100 ) == 0 ) {
550 // CRM_Utils_System::xMemory( "$mailsProcessed: " );
554 $config->mailerBatchLimit
> 0 &&
555 $mailsProcessed >= $config->mailerBatchLimit
557 if (!empty($fields)) {
558 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
568 'contact_id' => $eq->contact_id
,
569 'email' => $eq->email
,
570 'phone' => $eq->phone
,
572 if (count($fields) == self
::MAX_CONTACTS_TO_PROCESS
) {
573 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
584 if (!empty($fields)) {
585 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
590 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
591 static $smtpConnectionErrors = 0;
593 if (!is_object($mailer) ||
empty($fields)) {
594 CRM_Core_Error
::fatal();
597 // get the return properties
598 $returnProperties = $mailing->getReturnProperties();
599 $params = $targetParams = $deliveredParams = array();
603 * CRM-15702: Sending bulk sms to contacts without e-mail addres fails.
604 * Solution is to skip checking for on hold
606 $skipOnHold = true; //do include a statement to check wether e-mail address is on hold
607 if ($mailing->sms_provider_id
) {
608 $skipOnHold = false; //do not include a statement to check wether e-mail address is on hold
611 foreach ($fields as $key => $field) {
612 $params[] = $field['contact_id'];
615 $details = CRM_Utils_Token
::getTokenDetails(
618 $skipOnHold, TRUE, NULL,
619 $mailing->getFlattenedTokens(),
624 $config = CRM_Core_Config
::singleton();
625 foreach ($fields as $key => $field) {
626 $contactID = $field['contact_id'];
627 if (!array_key_exists($contactID, $details[0])) {
628 $details[0][$contactID] = array();
631 /* Compose the mailing */
632 $recipient = $replyToEmail = NULL;
633 $replyValue = strcmp($mailing->replyto_email
, $mailing->from_email
);
635 $replyToEmail = $mailing->replyto_email
;
638 $message = &$mailing->compose(
639 $this->id
, $field['id'], $field['hash'],
640 $field['contact_id'], $field['email'],
641 $recipient, FALSE, $details[0][$contactID], $attachments,
642 FALSE, NULL, $replyToEmail
644 if (empty($message)) {
645 // lets keep the message in the queue
646 // most likely a permissions related issue with smarty templates
647 // or a bad contact id? CRM-9833
651 /* Send the mailing */
653 $body = &$message->get();
654 $headers = &$message->headers();
656 if ($mailing->sms_provider_id
) {
657 $provider = CRM_SMS_Provider
::singleton(array('mailing_id' => $mailing->id
));
658 $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
659 $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
662 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
663 $recipient = $headers['To'];
666 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
668 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
671 $result = $mailer->send($recipient, $headers, $body, $this->id
);
677 // FIXME: for now we skipping bounce handling for sms
678 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id
) {
680 $message = $result->getMessage();
682 'Failed to write to socket'
684 // lets log this message and code
685 $code = $result->getCode();
686 CRM_Core_Error
::debug_log_message("SMTP Socket Error. Message: $message, Code: $code");
688 // these are socket write errors which most likely means smtp connection errors
690 $smtpConnectionErrors++
;
691 if ($smtpConnectionErrors <= 5) {
695 // seems like we have too many of them in a row, we should
696 // write stuff to disk and abort the cron job
704 CRM_Core_Error
::debug_log_message("Too many SMTP Socket Errors. Exiting");
705 CRM_Utils_System
::civiExit();
708 /* Register the bounce event */
711 'event_queue_id' => $field['id'],
712 'job_id' => $this->id
,
713 'hash' => $field['hash'],
715 $params = array_merge($params,
716 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
718 CRM_Mailing_Event_BAO_Bounce
::create($params);
721 /* Register the delivery event */
722 $deliveredParams[] = $field['id'];
723 $targetParams[] = $field['contact_id'];
726 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
735 // hack to stop mailing job at run time, CRM-4246.
736 // to avoid making too many DB calls for this rare case
737 // lets do it when we snapshot
738 $status = CRM_Core_DAO
::getFieldValue(
739 'CRM_Mailing_DAO_MailingJob',
746 if ($status != 'Running') {
754 // seems like a successful delivery or bounce, lets decrement error count
755 // only if we have smtp connection errors
756 if ($smtpConnectionErrors > 0) {
757 $smtpConnectionErrors--;
760 // If we have enabled the Throttle option, this is the time to enforce it.
761 if (isset($config->mailThrottleTime
) && $config->mailThrottleTime
> 0) {
762 usleep((int ) $config->mailThrottleTime
);
766 $result = $this->writeToDB(
779 * @param int $mailingId the id of the mailing to be canceled
782 public static function cancel($mailingId) {
785 FROM civicrm_mailing_job
786 WHERE mailing_id = %1
788 AND ( ( job_type IS NULL ) OR
789 job_type <> 'child' )
791 $params = array(1 => array($mailingId, 'Integer'));
792 $job = CRM_Core_DAO
::executeQuery($sql, $params);
794 in_array($job->status
, array('Scheduled', 'Running', 'Paused'))
797 $newJob = new CRM_Mailing_BAO_MailingJob();
798 $newJob->id
= $job->id
;
799 $newJob->end_date
= date('YmdHis');
800 $newJob->status
= 'Canceled';
803 // also cancel all child jobs
805 UPDATE civicrm_mailing_job
806 SET status = 'Canceled',
810 AND job_type = 'child'
811 AND status IN ( 'Scheduled', 'Running', 'Paused' )
813 $params = array(1 => array($job->id
, 'Integer'),
814 2 => array(date('YmdHis'), 'Timestamp'),
816 CRM_Core_DAO
::executeQuery($sql, $params);
818 CRM_Core_Session
::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
823 * Return a translated status enum string
825 * @param string $status The status enum
827 * @return string The translated version
831 public static function status($status) {
832 static $translation = NULL;
834 if (empty($translation)) {
835 $translation = array(
836 'Scheduled' => ts('Scheduled'),
837 'Running' => ts('Running'),
838 'Complete' => ts('Complete'),
839 'Paused' => ts('Paused'),
840 'Canceled' => ts('Canceled'),
843 return CRM_Utils_Array
::value($status, $translation, ts('Not scheduled'));
847 * Return a workflow clause for use in SQL queries,
848 * to only process jobs that are approved.
850 * @return string For use in a WHERE clause
854 public static function workflowClause() {
855 // add an additional check and only process
856 // jobs that are approved
857 if (CRM_Mailing_Info
::workflowEnabled()) {
858 $approveOptionID = CRM_Core_OptionGroup
::getValue('mail_approval_status',
862 if ($approveOptionID) {
863 return " AND m.approval_status_id = $approveOptionID ";
869 public function writeToDB(
875 static $activityTypeID = NULL;
876 static $writeActivity = NULL;
878 if (!empty($deliveredParams)) {
879 CRM_Mailing_Event_BAO_Delivered
::bulkCreate($deliveredParams);
880 $deliveredParams = array();
883 if ($writeActivity === NULL) {
884 $writeActivity = CRM_Core_BAO_Setting
::getItem(
885 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
886 'write_activity_record',
892 if (!$writeActivity) {
897 if (!empty($targetParams) && !empty($mailing->scheduled_id
)) {
898 if (!$activityTypeID) {
899 if ($mailing->sms_provider_id
) {
900 $mailing->subject
= $mailing->name
;
901 $activityTypeID = CRM_Core_OptionGroup
::getValue(
908 $activityTypeID = CRM_Core_OptionGroup
::getValue(
914 if (!$activityTypeID) {
915 CRM_Core_Error
::fatal();
920 'source_contact_id' => $mailing->scheduled_id
,
922 'target_contact_id' => array_unique($targetParams),
923 'activity_type_id' => $activityTypeID,
924 'source_record_id' => $this->mailing_id
,
925 'activity_date_time' => $job_date,
926 'subject' => $mailing->subject
,
928 'deleteActivityTarget' => FALSE,
929 'campaign_id' => $mailing->campaign_id
,
932 //check whether activity is already created for this mailing.
933 //if yes then create only target contact record.
936 FROM civicrm_activity
937 WHERE civicrm_activity.activity_type_id = %1
938 AND civicrm_activity.source_record_id = %2
941 $queryParams = array(
942 1 => array($activityTypeID, 'Integer'),
943 2 => array($this->mailing_id
, 'Integer'),
945 $activityID = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
948 $activity['id'] = $activityID;
951 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
952 static $targetRecordID = NULL;
953 if (!$targetRecordID) {
954 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
955 $targetRecordID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
958 // make sure we don't attempt to duplicate the target activity
959 foreach ($activity['target_contact_id'] as $key => $targetID) {
962 FROM civicrm_activity_contact
963 WHERE activity_id = $activityID
964 AND contact_id = $targetID
965 AND record_type_id = $targetRecordID
967 if (CRM_Core_DAO
::singleValueQuery($sql)) {
968 unset($activity['target_contact_id'][$key]);
974 if (is_a(CRM_Activity_BAO_Activity
::create($activity), 'CRM_Core_Error')) {
978 $targetParams = array();