Merge pull request #21213 from colemanw/entityBatchRef
[civicrm-core.git] / CRM / Activity / BAO / ActivityContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class is for activity assignment functions.
20 */
21 class CRM_Activity_BAO_ActivityContact extends CRM_Activity_DAO_ActivityContact {
22
23 /**
24 * Function to add activity contact.
25 *
26 * @param array $params
27 * The values for this table: activity id, contact id, record type.
28 *
29 * @return CRM_Activity_DAO_ActivityContact
30 * activity_contact object
31 *
32 * @throws \CRM_Core_Exception
33 * @throws \PEAR_Exception
34 */
35 public static function create(array $params): CRM_Activity_DAO_ActivityContact {
36 try {
37 return self::writeRecord($params);
38 }
39 catch (PEAR_Exception $e) {
40 // This check used to be done first, creating an extra query before each insert.
41 // However, in none of the core tests was this ever called with values that already
42 // existed, meaning that this line would never or almost never be hit.
43 // hence it's better to save the select query here.
44 $activityContact = new CRM_Activity_DAO_ActivityContact();
45 $activityContact->copyValues($params);
46 if ($activityContact->find(TRUE)) {
47 return $activityContact;
48 }
49 throw $e;
50 }
51 }
52
53 /**
54 * Retrieve names of contact by activity_id.
55 *
56 * @param int $activityID
57 * @param int $recordTypeID
58 * @param bool $alsoIDs
59 *
60 * @return array
61 */
62 public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
63 $names = [];
64 $ids = [];
65
66 if (empty($activityID)) {
67 return $alsoIDs ? [$names, $ids] : $names;
68 }
69
70 $query = "
71 SELECT contact_a.id, contact_a.sort_name
72 FROM civicrm_contact contact_a
73 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
74 WHERE civicrm_activity_contact.activity_id = %1
75 AND civicrm_activity_contact.record_type_id = %2
76 AND contact_a.is_deleted = 0
77 ";
78 $params = [
79 1 => [$activityID, 'Integer'],
80 2 => [$recordTypeID, 'Integer'],
81 ];
82
83 $dao = CRM_Core_DAO::executeQuery($query, $params);
84 while ($dao->fetch()) {
85 $names[$dao->id] = $dao->sort_name;
86 $ids[] = $dao->id;
87 }
88
89 return $alsoIDs ? [$names, $ids] : $names;
90 }
91
92 /**
93 * Retrieve id of target contact by activity_id.
94 *
95 * @param int $activityID
96 * @param int $recordTypeID
97 *
98 * @return mixed
99 */
100 public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
101 $activityContact = [];
102 if (!CRM_Utils_Rule::positiveInteger($activityID) ||
103 !CRM_Utils_Rule::positiveInteger($recordTypeID)
104 ) {
105 return $activityContact;
106 }
107
108 $sql = " SELECT contact_id
109 FROM civicrm_activity_contact
110 INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
111 WHERE activity_id = %1
112 AND record_type_id = %2
113 AND civicrm_contact.is_deleted = 0
114 ";
115 $params = [
116 1 => [$activityID, 'Integer'],
117 2 => [$recordTypeID, 'Integer'],
118 ];
119
120 $dao = CRM_Core_DAO::executeQuery($sql, $params);
121 while ($dao->fetch()) {
122 $activityContact[] = $dao->contact_id;
123 }
124 return $activityContact;
125 }
126
127 /**
128 * Get the links associate array as defined by the links.ini file.
129 *
130 * Experimental... -
131 * Should look a bit like
132 * [local_col_name] => "related_tablename:related_col_name"
133 *
134 * @see DB_DataObject::getLinks()
135 * @see DB_DataObject::getLink()
136 *
137 * @return array|null
138 * array if there are links defined for this table.
139 * empty array - if there is a links.ini file, but no links on this table
140 * null - if no links.ini exists for this database (hence try auto_links).
141 */
142 public function links() {
143 $link = ['activity_id' => 'civicrm_activity:id'];
144 return $link;
145 }
146
147 }