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
460 * @param null $testParams
465 public function deliver(&$mailer, $testParams = NULL) {
466 $mailing = new CRM_Mailing_BAO_Mailing();
467 $mailing->id
= $this->mailing_id
;
468 $mailing->find(TRUE);
471 $eq = new CRM_Mailing_Event_BAO_Queue();
472 $eqTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
473 $emailTable = CRM_Core_BAO_Email
::getTableName();
474 $phoneTable = CRM_Core_DAO_Phone
::getTableName();
475 $contactTable = CRM_Contact_BAO_Contact
::getTableName();
476 $edTable = CRM_Mailing_Event_BAO_Delivered
::getTableName();
477 $ebTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
479 $query = " SELECT $eqTable.id,
480 $emailTable.email as email,
485 INNER JOIN $emailTable
486 ON $eqTable.email_id = $emailTable.id
487 INNER JOIN $contactTable
488 ON $contactTable.id = $emailTable.contact_id
490 ON $eqTable.id = $edTable.event_queue_id
492 ON $eqTable.id = $ebTable.event_queue_id
493 WHERE $eqTable.job_id = " . $this->id
. "
494 AND $edTable.id IS null
495 AND $ebTable.id IS null
496 AND $contactTable.is_opt_out = 0";
498 if ($mailing->sms_provider_id
) {
501 $phoneTable.phone as phone,
506 INNER JOIN $phoneTable
507 ON $eqTable.phone_id = $phoneTable.id
508 INNER JOIN $contactTable
509 ON $contactTable.id = $phoneTable.contact_id
511 ON $eqTable.id = $edTable.event_queue_id
513 ON $eqTable.id = $ebTable.event_queue_id
514 WHERE $eqTable.job_id = " . $this->id
. "
515 AND $edTable.id IS null
516 AND $ebTable.id IS null
517 AND $contactTable.is_opt_out = 0";
521 static $config = NULL;
522 static $mailsProcessed = 0;
524 if ($config == NULL) {
525 $config = CRM_Core_Config
::singleton();
528 $job_date = CRM_Utils_Date
::isoToMysql($this->scheduled_date
);
531 if (!empty($testParams)) {
532 $mailing->subject
= ts('[CiviMail Draft]') . ' ' . $mailing->subject
;
535 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
537 // get and format attachments
538 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
540 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
541 CRM_Core_Smarty
::registerStringResource();
545 // This handles the edge case scenario where all the mails
546 // have been delivered in prior jobs
549 // make sure that there's no more than $config->mailerBatchLimit mails processed in a run
550 while ($eq->fetch()) {
551 // if ( ( $mailsProcessed % 100 ) == 0 ) {
552 // CRM_Utils_System::xMemory( "$mailsProcessed: " );
556 $config->mailerBatchLimit
> 0 &&
557 $mailsProcessed >= $config->mailerBatchLimit
559 if (!empty($fields)) {
560 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
570 'contact_id' => $eq->contact_id
,
571 'email' => $eq->email
,
572 'phone' => $eq->phone
,
574 if (count($fields) == self
::MAX_CONTACTS_TO_PROCESS
) {
575 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
586 if (!empty($fields)) {
587 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
592 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
593 static $smtpConnectionErrors = 0;
595 if (!is_object($mailer) ||
empty($fields)) {
596 CRM_Core_Error
::fatal();
599 // get the return properties
600 $returnProperties = $mailing->getReturnProperties();
601 $params = $targetParams = $deliveredParams = array();
604 foreach ($fields as $key => $field) {
605 $params[] = $field['contact_id'];
608 $details = CRM_Utils_Token
::getTokenDetails(
612 $mailing->getFlattenedTokens(),
617 $config = CRM_Core_Config
::singleton();
618 foreach ($fields as $key => $field) {
619 $contactID = $field['contact_id'];
620 if (!array_key_exists($contactID, $details[0])) {
621 $details[0][$contactID] = array();
624 /* Compose the mailing */
625 $recipient = $replyToEmail = NULL;
626 $replyValue = strcmp($mailing->replyto_email
, $mailing->from_email
);
628 $replyToEmail = $mailing->replyto_email
;
631 $message = &$mailing->compose(
632 $this->id
, $field['id'], $field['hash'],
633 $field['contact_id'], $field['email'],
634 $recipient, FALSE, $details[0][$contactID], $attachments,
635 FALSE, NULL, $replyToEmail
637 if (empty($message)) {
638 // lets keep the message in the queue
639 // most likely a permissions related issue with smarty templates
640 // or a bad contact id? CRM-9833
644 /* Send the mailing */
646 $body = &$message->get();
647 $headers = &$message->headers();
649 if ($mailing->sms_provider_id
) {
650 $provider = CRM_SMS_Provider
::singleton(array('mailing_id' => $mailing->id
));
651 $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
652 $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
655 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
656 $recipient = $headers['To'];
659 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
661 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
664 $result = $mailer->send($recipient, $headers, $body, $this->id
);
670 // FIXME: for now we skipping bounce handling for sms
671 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id
) {
673 $message = $result->getMessage();
675 'Failed to write to socket'
677 // lets log this message and code
678 $code = $result->getCode();
679 CRM_Core_Error
::debug_log_message("SMTP Socket Error. Message: $message, Code: $code");
681 // these are socket write errors which most likely means smtp connection errors
683 $smtpConnectionErrors++
;
684 if ($smtpConnectionErrors <= 5) {
688 // seems like we have too many of them in a row, we should
689 // write stuff to disk and abort the cron job
697 CRM_Core_Error
::debug_log_message("Too many SMTP Socket Errors. Exiting");
698 CRM_Utils_System
::civiExit();
701 /* Register the bounce event */
704 'event_queue_id' => $field['id'],
705 'job_id' => $this->id
,
706 'hash' => $field['hash'],
708 $params = array_merge($params,
709 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
711 CRM_Mailing_Event_BAO_Bounce
::create($params);
714 /* Register the delivery event */
715 $deliveredParams[] = $field['id'];
716 $targetParams[] = $field['contact_id'];
719 if ($count % CRM_Core_DAO
::BULK_MAIL_INSERT_COUNT
== 0) {
728 // hack to stop mailing job at run time, CRM-4246.
729 // to avoid making too many DB calls for this rare case
730 // lets do it when we snapshot
731 $status = CRM_Core_DAO
::getFieldValue(
732 'CRM_Mailing_DAO_MailingJob',
739 if ($status != 'Running') {
747 // seems like a successful delivery or bounce, lets decrement error count
748 // only if we have smtp connection errors
749 if ($smtpConnectionErrors > 0) {
750 $smtpConnectionErrors--;
753 // If we have enabled the Throttle option, this is the time to enforce it.
754 if (isset($config->mailThrottleTime
) && $config->mailThrottleTime
> 0) {
755 usleep((int ) $config->mailThrottleTime
);
759 $result = $this->writeToDB(
772 * @param int $mailingId the id of the mailing to be canceled
775 public static function cancel($mailingId) {
778 FROM civicrm_mailing_job
779 WHERE mailing_id = %1
781 AND ( ( job_type IS NULL ) OR
782 job_type <> 'child' )
784 $params = array(1 => array($mailingId, 'Integer'));
785 $job = CRM_Core_DAO
::executeQuery($sql, $params);
787 in_array($job->status
, array('Scheduled', 'Running', 'Paused'))
790 $newJob = new CRM_Mailing_BAO_MailingJob();
791 $newJob->id
= $job->id
;
792 $newJob->end_date
= date('YmdHis');
793 $newJob->status
= 'Canceled';
796 // also cancel all child jobs
798 UPDATE civicrm_mailing_job
799 SET status = 'Canceled',
803 AND job_type = 'child'
804 AND status IN ( 'Scheduled', 'Running', 'Paused' )
806 $params = array(1 => array($job->id
, 'Integer'),
807 2 => array(date('YmdHis'), 'Timestamp'),
809 CRM_Core_DAO
::executeQuery($sql, $params);
811 CRM_Core_Session
::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
816 * Return a translated status enum string
818 * @param string $status The status enum
820 * @return string The translated version
824 public static function status($status) {
825 static $translation = NULL;
827 if (empty($translation)) {
828 $translation = array(
829 'Scheduled' => ts('Scheduled'),
830 'Running' => ts('Running'),
831 'Complete' => ts('Complete'),
832 'Paused' => ts('Paused'),
833 'Canceled' => ts('Canceled'),
836 return CRM_Utils_Array
::value($status, $translation, ts('Not scheduled'));
840 * Return a workflow clause for use in SQL queries,
841 * to only process jobs that are approved.
843 * @return string For use in a WHERE clause
847 public static function workflowClause() {
848 // add an additional check and only process
849 // jobs that are approved
850 if (CRM_Mailing_Info
::workflowEnabled()) {
851 $approveOptionID = CRM_Core_OptionGroup
::getValue('mail_approval_status',
855 if ($approveOptionID) {
856 return " AND m.approval_status_id = $approveOptionID ";
862 public function writeToDB(
868 static $activityTypeID = NULL;
869 static $writeActivity = NULL;
871 if (!empty($deliveredParams)) {
872 CRM_Mailing_Event_BAO_Delivered
::bulkCreate($deliveredParams);
873 $deliveredParams = array();
876 if ($writeActivity === NULL) {
877 $writeActivity = CRM_Core_BAO_Setting
::getItem(
878 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
879 'write_activity_record',
885 if (!$writeActivity) {
890 if (!empty($targetParams) && !empty($mailing->scheduled_id
)) {
891 if (!$activityTypeID) {
892 if ($mailing->sms_provider_id
) {
893 $mailing->subject
= $mailing->name
;
894 $activityTypeID = CRM_Core_OptionGroup
::getValue(
901 $activityTypeID = CRM_Core_OptionGroup
::getValue(
907 if (!$activityTypeID) {
908 CRM_Core_Error
::fatal();
913 'source_contact_id' => $mailing->scheduled_id
,
915 'target_contact_id' => array_unique($targetParams),
916 'activity_type_id' => $activityTypeID,
917 'source_record_id' => $this->mailing_id
,
918 'activity_date_time' => $job_date,
919 'subject' => $mailing->subject
,
921 'deleteActivityTarget' => FALSE,
922 'campaign_id' => $mailing->campaign_id
,
925 //check whether activity is already created for this mailing.
926 //if yes then create only target contact record.
929 FROM civicrm_activity
930 WHERE civicrm_activity.activity_type_id = %1
931 AND civicrm_activity.source_record_id = %2
934 $queryParams = array(
935 1 => array($activityTypeID, 'Integer'),
936 2 => array($this->mailing_id
, 'Integer'),
938 $activityID = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
941 $activity['id'] = $activityID;
944 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
945 static $targetRecordID = NULL;
946 if (!$targetRecordID) {
947 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
948 $targetRecordID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
951 // make sure we don't attempt to duplicate the target activity
952 foreach ($activity['target_contact_id'] as $key => $targetID) {
955 FROM civicrm_activity_contact
956 WHERE activity_id = $activityID
957 AND contact_id = $targetID
958 AND record_type_id = $targetRecordID
960 if (CRM_Core_DAO
::singleValueQuery($sql)) {
961 unset($activity['target_contact_id'][$key]);
967 if (is_a(CRM_Activity_BAO_Activity
::create($activity), 'CRM_Core_Error')) {
971 $targetParams = array();