3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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();
47 function create ($params){
48 $job = new CRM_Mailing_BAO_MailingJob();
49 $job->mailing_id
= $params['mailing_id'];
50 $job->status
= $params['status'];
51 $job->scheduled_date
= $params['scheduled_date'];
52 $job->is_test
= $params['is_test'];
54 $mailing = new CRM_Mailing_BAO_Mailing();
55 $eq = $mailing->getRecipients($job->id
, $params['mailing_id'], NULL, NULL, true, false);
59 * Initiate all pending/ready jobs
65 public static function runJobs($testParams = NULL, $mode = NULL) {
66 $job = new CRM_Mailing_BAO_MailingJob();
68 $config = CRM_Core_Config
::singleton();
69 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
70 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
72 if (!empty($testParams)) {
76 WHERE id = {$testParams['job_id']}";
80 $currentTime = date('YmdHis');
81 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
82 $domainID = CRM_Core_Config
::domainID();
84 $modeClause = 'AND m.sms_provider_id IS NULL';
86 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
89 // Select the first child job that is scheduled
95 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
98 AND ( ( j.start_date IS null
99 AND j.scheduled_date <= $currentTime
100 AND j.status = 'Scheduled' )
101 OR ( j.status = 'Running'
102 AND j.end_date IS null ) )
103 AND (j.job_type = 'child')
105 ORDER BY j.mailing_id,
113 while ($job->fetch()) {
114 // still use job level lock for each child job
115 $lockName = "civimail.job.{$job->id}";
117 $lock = new CRM_Core_Lock($lockName);
118 if (!$lock->isAcquired()) {
122 // for test jobs we do not change anything, since its on a short-circuit path
123 if (empty($testParams)) {
124 // we've got the lock, but while we were waiting and processing
125 // other emails, this job might have changed under us
126 // lets get the job status again and check
127 $job->status
= CRM_Core_DAO
::getFieldValue(
128 'CRM_Mailing_DAO_MailingJob',
136 $job->status
!= 'Running' &&
137 $job->status
!= 'Scheduled'
139 // this includes Cancelled and other statuses, CRM-4246
145 /* Queue up recipients for the child job being launched */
147 if ($job->status
!= 'Running') {
148 $transaction = new CRM_Core_Transaction();
150 // have to queue it up based on the offset and limits
151 // get the parent ID, and limit and offset
152 $job->queue($testParams);
154 // Mark up the starting time
155 $saveJob = new CRM_Mailing_DAO_MailingJob();
156 $saveJob->id
= $job->id
;
157 $saveJob->start_date
= date('YmdHis');
158 $saveJob->status
= 'Running';
161 $transaction->commit();
165 // make it a persistent connection, CRM-9349
166 if ($mode === NULL) {
167 $mailer = $config->getMailer(TRUE);
169 elseif ($mode == 'sms') {
170 $mailer = CRM_SMS_Provider
::singleton(array('mailing_id' => $job->mailing_id
));
173 // Compose and deliver each child job
174 $isComplete = $job->deliver($mailer, $testParams);
176 CRM_Utils_Hook
::post('create', 'CRM_Mailing_DAO_Spool', $job->id
, $isComplete);
178 // Mark the child complete
182 $transaction = new CRM_Core_Transaction();
184 $saveJob = new CRM_Mailing_DAO_MailingJob();
185 $saveJob->id
= $job->id
;
186 $saveJob->end_date
= date('YmdHis');
187 $saveJob->status
= 'Complete';
190 $transaction->commit();
192 // don't mark the mailing as complete
195 // Release the child joblock
204 // post process to determine if the parent job
205 // as well as the mailing is complete after the run
206 public static function runJobs_post($mode = NULL) {
208 $job = new CRM_Mailing_BAO_MailingJob();
210 $mailing = new CRM_Mailing_BAO_Mailing();
212 $config = CRM_Core_Config
::singleton();
213 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
214 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
216 $currentTime = date('YmdHis');
217 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
218 $domainID = CRM_Core_Config
::domainID();
224 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
226 AND j.scheduled_date <= $currentTime
227 AND j.status = 'Running'
228 AND j.end_date IS null
229 AND (j.job_type != 'child' OR j.job_type is NULL)
230 ORDER BY j.scheduled_date,
235 // For each parent job that is running, let's look at their child jobs
236 while ($job->fetch()) {
238 $child_job = new CRM_Mailing_BAO_MailingJob();
242 FROM civicrm_mailing_job j, civicrm_mailing m
243 WHERE m.id = j.mailing_id
244 AND j.job_type = 'child'
246 AND j.status <> 'Complete'";
247 $params = array(1 => array($job->id
, 'Integer'));
249 $anyChildLeft = CRM_Core_DAO
::singleValueQuery($child_job_sql, $params);
251 // all of the child jobs are complete, update
252 // the parent job as well as the mailing status
253 if (!$anyChildLeft) {
255 $transaction = new CRM_Core_Transaction();
257 $saveJob = new CRM_Mailing_DAO_MailingJob();
258 $saveJob->id
= $job->id
;
259 $saveJob->end_date
= date('YmdHis');
260 $saveJob->status
= 'Complete';
264 $mailing->id
= $job->mailing_id
;
265 $mailing->is_completed
= TRUE;
267 $transaction->commit();
273 // before we run jobs, we need to split the jobs
274 public static function runJobs_pre($offset = 200, $mode = NULL) {
275 $job = new CRM_Mailing_BAO_MailingJob();
277 $config = CRM_Core_Config
::singleton();
278 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
279 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
281 $currentTime = date('YmdHis');
282 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
285 $workflowClause = CRM_Mailing_BAO_MailingJob
::workflowClause();
287 $domainID = CRM_Core_Config
::domainID();
289 $modeClause = 'AND m.sms_provider_id IS NULL';
290 if ($mode == 'sms') {
291 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
294 // Select all the mailing jobs that are created from
295 // when the mailing is submitted or scheduled.
300 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
304 AND ( ( j.start_date IS null
305 AND j.scheduled_date <= $currentTime
306 AND j.status = 'Scheduled'
307 AND j.end_date IS null ) )
308 AND ((j.job_type is NULL) OR (j.job_type <> 'child'))
309 ORDER BY j.scheduled_date,
316 // For each of the "Parent Jobs" we find, we split them into
317 // X Number of child jobs
318 while ($job->fetch()) {
319 // still use job level lock for each child job
320 $lockName = "civimail.job.{$job->id}";
322 $lock = new CRM_Core_Lock($lockName);
323 if (!$lock->isAcquired()) {
327 // Re-fetch the job status in case things
328 // changed between the first query and now
329 // to avoid race conditions
330 $job->status
= CRM_Core_DAO
::getFieldValue(
331 'CRM_Mailing_DAO_MailingJob',
337 if ($job->status
!= 'Scheduled') {
342 $job->split_job($offset);
344 // update the status of the parent job
345 $transaction = new CRM_Core_Transaction();
347 $saveJob = new CRM_Mailing_DAO_MailingJob();
348 $saveJob->id
= $job->id
;
349 $saveJob->start_date
= date('YmdHis');
350 $saveJob->status
= 'Running';
353 $transaction->commit();
355 // Release the job lock
360 // Split the parent job into n number of child job based on an offset
361 // If null or 0 , we create only one child job
362 public function split_job($offset = 200) {
363 $recipient_count = CRM_Mailing_BAO_Recipients
::mailingSize($this->mailing_id
);
365 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
368 $dao = new CRM_Core_DAO();
371 INSERT INTO civicrm_mailing_job
372 (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`)
373 VALUES (%1, %2, %3, %4, %5, %6, %7)
375 $params = array(1 => array($this->mailing_id
, 'Integer'),
376 2 => array($this->scheduled_date
, 'String'),
377 3 => array('Scheduled', 'String'),
378 4 => array('child', 'String'),
379 5 => array($this->id
, 'Integer'),
380 6 => array(0, 'Integer'),
381 7 => array($recipient_count, 'Integer'),
384 // create one child job if the mailing size is less than the offset
385 // probably use a CRM_Mailing_DAO_MailingJob( );
386 if (empty($offset) ||
387 $recipient_count <= $offset
389 CRM_Core_DAO
::executeQuery($sql, $params);
392 // Creating 'child jobs'
393 for ($i = 0; $i < $recipient_count; $i = $i +
$offset) {
395 $params[7][0] = $offset;
396 CRM_Core_DAO
::executeQuery($sql, $params);
401 public function queue($testParams = NULL) {
402 $mailing = new CRM_Mailing_BAO_Mailing();
403 $mailing->id
= $this->mailing_id
;
404 if (!empty($testParams)) {
405 $mailing->getTestRecipients($testParams);
408 // We are still getting all the recipients from the parent job
409 // so we don't mess with the include/exclude logic.
410 $recipients = CRM_Mailing_BAO_Recipients
::mailingQuery($this->mailing_id
, $this->job_offset
, $this->job_limit
);
412 // FIXME: this is not very smart, we should move this to one DB call
413 // INSERT INTO ... SELECT FROM ..
414 // the thing we need to figure out is how to generate the hash automatically
418 while ($recipients->fetch()) {
419 if ($recipients->phone_id
) {
420 $recipients->email_id
= "null";
423 $recipients->phone_id
= "null";
428 $recipients->email_id
,
429 $recipients->contact_id
,
430 $recipients->phone_id
,
433 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
434 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
440 if (!empty($params)) {
441 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
449 * @param object $mailer A Mail object to send the messages
454 public function deliver(&$mailer, $testParams = NULL) {
455 $mailing = new CRM_Mailing_BAO_Mailing();
456 $mailing->id
= $this->mailing_id
;
457 $mailing->find(TRUE);
460 $eq = new CRM_Mailing_Event_BAO_Queue();
461 $eqTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
462 $emailTable = CRM_Core_BAO_Email
::getTableName();
463 $phoneTable = CRM_Core_DAO_Phone
::getTableName();
464 $contactTable = CRM_Contact_BAO_Contact
::getTableName();
465 $edTable = CRM_Mailing_Event_BAO_Delivered
::getTableName();
466 $ebTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
468 $query = " SELECT $eqTable.id,
469 $emailTable.email as email,
474 INNER JOIN $emailTable
475 ON $eqTable.email_id = $emailTable.id
476 INNER JOIN $contactTable
477 ON $contactTable.id = $emailTable.contact_id
479 ON $eqTable.id = $edTable.event_queue_id
481 ON $eqTable.id = $ebTable.event_queue_id
482 WHERE $eqTable.job_id = " . $this->id
. "
483 AND $edTable.id IS null
484 AND $ebTable.id IS null
485 AND $contactTable.is_opt_out = 0";
487 if ($mailing->sms_provider_id
) {
490 $phoneTable.phone as phone,
495 INNER JOIN $phoneTable
496 ON $eqTable.phone_id = $phoneTable.id
497 INNER JOIN $contactTable
498 ON $contactTable.id = $phoneTable.contact_id
500 ON $eqTable.id = $edTable.event_queue_id
502 ON $eqTable.id = $ebTable.event_queue_id
503 WHERE $eqTable.job_id = " . $this->id
. "
504 AND $edTable.id IS null
505 AND $ebTable.id IS null
506 AND $contactTable.is_opt_out = 0";
510 static $config = NULL;
511 static $mailsProcessed = 0;
513 if ($config == NULL) {
514 $config = CRM_Core_Config
::singleton();
517 $job_date = CRM_Utils_Date
::isoToMysql($this->scheduled_date
);
520 if (!empty($testParams)) {
521 $mailing->subject
= ts('[CiviMail Draft]') . ' ' . $mailing->subject
;
524 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
526 // get and format attachments
527 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
529 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
530 CRM_Core_Smarty
::registerStringResource();
534 // This handles the edge case scenario where all the mails
535 // have been delivered in prior jobs
538 // make sure that there's no more than $config->mailerBatchLimit mails processed in a run
539 while ($eq->fetch()) {
540 // if ( ( $mailsProcessed % 100 ) == 0 ) {
541 // CRM_Utils_System::xMemory( "$mailsProcessed: " );
545 $config->mailerBatchLimit
> 0 &&
546 $mailsProcessed >= $config->mailerBatchLimit
548 if (!empty($fields)) {
549 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
559 'contact_id' => $eq->contact_id
,
560 'email' => $eq->email
,
561 'phone' => $eq->phone
,
563 if (count($fields) == self
::MAX_CONTACTS_TO_PROCESS
) {
564 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
575 if (!empty($fields)) {
576 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
581 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
582 static $smtpConnectionErrors = 0;
584 if (!is_object($mailer) ||
empty($fields)) {
585 CRM_Core_Error
::fatal();
588 // get the return properties
589 $returnProperties = $mailing->getReturnProperties();
590 $params = $targetParams = $deliveredParams = array();
593 foreach ($fields as $key => $field) {
594 $params[] = $field['contact_id'];
597 $details = CRM_Utils_Token
::getTokenDetails(
601 $mailing->getFlattenedTokens(),
606 $config = CRM_Core_Config
::singleton();
607 foreach ($fields as $key => $field) {
608 $contactID = $field['contact_id'];
609 if (!array_key_exists($contactID, $details[0])) {
610 $details[0][$contactID] = array();
613 /* Compose the mailing */
614 $recipient = $replyToEmail = NULL;
615 $replyValue = strcmp($mailing->replyto_email
, $mailing->from_email
);
617 $replyToEmail = $mailing->replyto_email
;
620 $message = &$mailing->compose(
621 $this->id
, $field['id'], $field['hash'],
622 $field['contact_id'], $field['email'],
623 $recipient, FALSE, $details[0][$contactID], $attachments,
624 FALSE, NULL, $replyToEmail
626 if (empty($message)) {
627 // lets keep the message in the queue
628 // most likely a permissions related issue with smarty templates
629 // or a bad contact id? CRM-9833
633 /* Send the mailing */
635 $body = &$message->get();
636 $headers = &$message->headers();
638 if ($mailing->sms_provider_id
) {
639 $provider = CRM_SMS_Provider
::singleton(array('mailing_id' => $mailing->id
));
640 $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
641 $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
644 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
645 $recipient = $headers['To'];
648 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
650 CRM_Core_Error
::ignoreException();
653 $result = $mailer->send($recipient, $headers, $body, $this->id
);
656 CRM_Core_Error
::setCallback();
659 // FIXME: for now we skipping bounce handling for sms
660 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id
) {
662 $message = $result->getMessage();
664 strpos($message, 'Failed to write to socket') !== FALSE ||
665 strpos($message, 'Failed to set sender') !== FALSE
667 // lets log this message and code
668 $code = $result->getCode();
669 CRM_Core_Error
::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code");
671 // these are socket write errors which most likely means smtp connection errors
673 $smtpConnectionErrors++
;
674 if ($smtpConnectionErrors <= 5) {
678 // seems like we have too many of them in a row, we should
679 // write stuff to disk and abort the cron job
687 CRM_Core_Error
::debug_log_message("Too many SMTP Socket Errors. Exiting");
688 CRM_Utils_System
::civiExit();
691 /* Register the bounce event */
694 'event_queue_id' => $field['id'],
695 'job_id' => $this->id
,
696 'hash' => $field['hash'],
698 $params = array_merge($params,
699 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
701 CRM_Mailing_Event_BAO_Bounce
::create($params);
704 /* Register the delivery event */
705 $deliveredParams[] = $field['id'];
706 $targetParams[] = $field['contact_id'];
709 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
718 // hack to stop mailing job at run time, CRM-4246.
719 // to avoid making too many DB calls for this rare case
720 // lets do it when we snapshot
721 $status = CRM_Core_DAO
::getFieldValue(
722 'CRM_Mailing_DAO_MailingJob',
729 if ($status != 'Running') {
737 // seems like a successful delivery or bounce, lets decrement error count
738 // only if we have smtp connection errors
739 if ($smtpConnectionErrors > 0) {
740 $smtpConnectionErrors--;
743 // If we have enabled the Throttle option, this is the time to enforce it.
744 if (isset($config->mailThrottleTime
) && $config->mailThrottleTime
> 0) {
745 usleep((int ) $config->mailThrottleTime
);
749 $result = $this->writeToDB(
762 * @param int $mailingId the id of the mailing to be canceled
765 public static function cancel($mailingId) {
768 FROM civicrm_mailing_job
769 WHERE mailing_id = %1
771 AND ( ( job_type IS NULL ) OR
772 job_type <> 'child' )
774 $params = array(1 => array($mailingId, 'Integer'));
775 $job = CRM_Core_DAO
::executeQuery($sql, $params);
777 in_array($job->status
, array('Scheduled', 'Running', 'Paused'))
780 $newJob = new CRM_Mailing_BAO_MailingJob();
781 $newJob->id
= $job->id
;
782 $newJob->end_date
= date('YmdHis');
783 $newJob->status
= 'Canceled';
786 // also cancel all child jobs
788 UPDATE civicrm_mailing_job
789 SET status = 'Canceled',
793 AND job_type = 'child'
794 AND status IN ( 'Scheduled', 'Running', 'Paused' )
796 $params = array(1 => array($job->id
, 'Integer'),
797 2 => array(date('YmdHis'), 'Timestamp'),
799 CRM_Core_DAO
::executeQuery($sql, $params);
801 CRM_Core_Session
::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
806 * Return a translated status enum string
808 * @param string $status The status enum
810 * @return string The translated version
814 public static function status($status) {
815 static $translation = NULL;
817 if (empty($translation)) {
818 $translation = array(
819 'Scheduled' => ts('Scheduled'),
820 'Running' => ts('Running'),
821 'Complete' => ts('Complete'),
822 'Paused' => ts('Paused'),
823 'Canceled' => ts('Canceled'),
826 return CRM_Utils_Array
::value($status, $translation, ts('Not scheduled'));
830 * Return a workflow clause for use in SQL queries,
831 * to only process jobs that are approved.
833 * @return string For use in a WHERE clause
837 public static function workflowClause() {
838 // add an additional check and only process
839 // jobs that are approved
840 if (CRM_Mailing_Info
::workflowEnabled()) {
841 $approveOptionID = CRM_Core_OptionGroup
::getValue('mail_approval_status',
845 if ($approveOptionID) {
846 return " AND m.approval_status_id = $approveOptionID ";
852 public function writeToDB(
858 static $activityTypeID = NULL;
859 static $writeActivity = NULL;
861 if (!empty($deliveredParams)) {
862 CRM_Mailing_Event_BAO_Delivered
::bulkCreate($deliveredParams);
863 $deliveredParams = array();
866 if ($writeActivity === NULL) {
867 $writeActivity = CRM_Core_BAO_Setting
::getItem(
868 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
869 'write_activity_record',
875 if (!$writeActivity) {
880 if (!empty($targetParams) && !empty($mailing->scheduled_id
)) {
881 if (!$activityTypeID) {
882 if ($mailing->sms_provider_id
) {
883 $mailing->subject
= $mailing->name
;
884 $activityTypeID = CRM_Core_OptionGroup
::getValue(
891 $activityTypeID = CRM_Core_OptionGroup
::getValue(
897 if (!$activityTypeID) {
898 CRM_Core_Error
::fatal();
903 'source_contact_id' => $mailing->scheduled_id
,
905 'target_contact_id' => array_unique($targetParams),
906 'activity_type_id' => $activityTypeID,
907 'source_record_id' => $this->mailing_id
,
908 'activity_date_time' => $job_date,
909 'subject' => $mailing->subject
,
911 'deleteActivityTarget' => FALSE,
912 'campaign_id' => $mailing->campaign_id
,
915 //check whether activity is already created for this mailing.
916 //if yes then create only target contact record.
919 FROM civicrm_activity
920 WHERE civicrm_activity.activity_type_id = %1
921 AND civicrm_activity.source_record_id = %2
924 $queryParams = array(
925 1 => array($activityTypeID, 'Integer'),
926 2 => array($this->mailing_id
, 'Integer'),
928 $activityID = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
931 $activity['id'] = $activityID;
934 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
935 static $targetRecordID = NULL;
936 if (!$targetRecordID) {
937 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
938 $targetRecordID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
941 // make sure we don't attempt to duplicate the target activity
942 foreach ($activity['target_contact_id'] as $key => $targetID) {
945 FROM civicrm_activity_contact
946 WHERE activity_id = $activityID
947 AND contact_id = $targetID
948 AND record_type_id = $targetRecordID
950 if (CRM_Core_DAO
::singleValueQuery($sql)) {
951 unset($activity['target_contact_id'][$key]);
957 if (is_a(CRM_Activity_BAO_Activity
::create($activity), 'CRM_Core_Error')) {
961 $targetParams = array();