Merge pull request #18569 from eileenmcnaughton/synt
[civicrm-core.git] / CRM / Activity / BAO / ActivityContact.php
CommitLineData
adc7f09e
DL
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
adc7f09e 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
adc7f09e 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
adc7f09e
DL
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
adc7f09e
DL
16 */
17
18/**
3819f101 19 * This class is for activity assignment functions.
adc7f09e
DL
20 */
21class CRM_Activity_BAO_ActivityContact extends CRM_Activity_DAO_ActivityContact {
22
23 /**
fe482240 24 * Class constructor.
adc7f09e 25 */
00be9182 26 public function __construct() {
adc7f09e
DL
27 parent::__construct();
28 }
29
30 /**
2e2605fe 31 * Function to add activity contact.
adc7f09e 32 *
041ab3d1
TO
33 * @param array $params
34 * The values for this table: activity id, contact id, record type.
adc7f09e 35 *
a6c01b45
CW
36 * @return object
37 * activity_contact object
adc7f09e 38 */
991cd69b 39 public static function create($params) {
8293f7c2 40 $errorScope = CRM_Core_TemporaryErrorScope::useException();
adc7f09e 41 $activityContact = new CRM_Activity_DAO_ActivityContact();
adc7f09e 42 $activityContact->copyValues($params);
8293f7c2 43 try {
2517d079
DL
44 return $activityContact->save();
45 }
8293f7c2 46 catch (PEAR_Exception $e) {
47 // This check used to be done first, creating an extra query before each insert.
48 // However, in none of the core tests was this ever called with values that already
49 // existed, meaning that this line would never or almost never be hit.
50 // hence it's better to save the select query here.
51 if ($activityContact->find(TRUE)) {
52 return $activityContact;
53 }
54 throw $e;
55 }
adc7f09e 56 }
f04255e4
DL
57
58 /**
fe482240 59 * Retrieve names of contact by activity_id.
f04255e4 60 *
c490a46a
CW
61 * @param int $activityID
62 * @param int $recordTypeID
6c8f6e67
EM
63 * @param bool $alsoIDs
64 *
f04255e4 65 * @return array
f04255e4 66 */
00be9182 67 public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
96f94695 68 $names = [];
69 $ids = [];
f04255e4
DL
70
71 if (empty($activityID)) {
96f94695 72 return $alsoIDs ? [$names, $ids] : $names;
f04255e4
DL
73 }
74
75 $query = "
76SELECT contact_a.id, contact_a.sort_name
77FROM civicrm_contact contact_a
78INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
79WHERE civicrm_activity_contact.activity_id = %1
a24b3694 80AND civicrm_activity_contact.record_type_id = %2
f04255e4
DL
81AND contact_a.is_deleted = 0
82";
96f94695 83 $params = [
84 1 => [$activityID, 'Integer'],
85 2 => [$recordTypeID, 'Integer'],
86 ];
f04255e4
DL
87
88 $dao = CRM_Core_DAO::executeQuery($query, $params);
89 while ($dao->fetch()) {
90 $names[$dao->id] = $dao->sort_name;
91 $ids[] = $dao->id;
92 }
93
96f94695 94 return $alsoIDs ? [$names, $ids] : $names;
f04255e4
DL
95 }
96
034500d4 97 /**
fe482240 98 * Retrieve id of target contact by activity_id.
034500d4 99 *
100fef9d
CW
100 * @param int $activityID
101 * @param int $recordTypeID
77b97be7 102 *
034500d4 103 * @return mixed
034500d4 104 */
00be9182 105 public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
96f94695 106 $activityContact = [];
034500d4 107 if (!CRM_Utils_Rule::positiveInteger($activityID) ||
353ffa53
TO
108 !CRM_Utils_Rule::positiveInteger($recordTypeID)
109 ) {
034500d4 110 return $activityContact;
111 }
112
113 $sql = " SELECT contact_id
114FROM civicrm_activity_contact
115INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
116WHERE activity_id = %1
2517d079
DL
117AND record_type_id = %2
118AND civicrm_contact.is_deleted = 0
034500d4 119";
96f94695 120 $params = [
121 1 => [$activityID, 'Integer'],
122 2 => [$recordTypeID, 'Integer'],
123 ];
034500d4 124
125 $dao = CRM_Core_DAO::executeQuery($sql, $params);
126 while ($dao->fetch()) {
127 $activityContact[] = $dao->contact_id;
128 }
129 return $activityContact;
130 }
131
ffd93213
EM
132 /**
133 * Get the links associate array as defined by the links.ini file.
134 *
ffd93213
EM
135 * Experimental... -
136 * Should look a bit like
137 * [local_col_name] => "related_tablename:related_col_name"
138 *
7808aae6
SB
139 * @see DB_DataObject::getLinks()
140 * @see DB_DataObject::getLink()
ffd93213 141 *
2b37475d 142 * @return array|null
62d3ee27
SL
143 * array if there are links defined for this table.
144 * empty array - if there is a links.ini file, but no links on this table
145 * null - if no links.ini exists for this database (hence try auto_links).
ffd93213 146 */
00be9182 147 public function links() {
96f94695 148 $link = ['activity_id' => 'civicrm_activity:id'];
6e1bb60c
N
149 return $link;
150 }
96025800 151
adc7f09e 152}