3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class is for activity assignment functions.
21 class CRM_Activity_BAO_ActivityContact
extends CRM_Activity_DAO_ActivityContact
{
24 * Function to add activity contact.
26 * @param array $params
27 * The values for this table: activity id, contact id, record type.
29 * @return CRM_Activity_DAO_ActivityContact
30 * activity_contact object
32 * @throws \CRM_Core_Exception
33 * @throws \PEAR_Exception
35 public static function create(array $params): CRM_Activity_DAO_ActivityContact
{
37 return self
::writeRecord($params);
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;
54 * Retrieve names of contact by activity_id.
56 * @param int $activityID
57 * @param int $recordTypeID
58 * @param bool $alsoIDs
62 public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
66 if (empty($activityID)) {
67 return $alsoIDs ?
[$names, $ids] : $names;
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
79 1 => [$activityID, 'Integer'],
80 2 => [$recordTypeID, 'Integer'],
83 $dao = CRM_Core_DAO
::executeQuery($query, $params);
84 while ($dao->fetch()) {
85 $names[$dao->id
] = $dao->sort_name
;
89 return $alsoIDs ?
[$names, $ids] : $names;
93 * Retrieve id of target contact by activity_id.
95 * @param int $activityID
96 * @param int $recordTypeID
100 public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
101 $activityContact = [];
102 if (!CRM_Utils_Rule
::positiveInteger($activityID) ||
103 !CRM_Utils_Rule
::positiveInteger($recordTypeID)
105 return $activityContact;
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
116 1 => [$activityID, 'Integer'],
117 2 => [$recordTypeID, 'Integer'],
120 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
121 while ($dao->fetch()) {
122 $activityContact[] = $dao->contact_id
;
124 return $activityContact;
128 * Get the links associate array as defined by the links.ini file.
131 * Should look a bit like
132 * [local_col_name] => "related_tablename:related_col_name"
134 * @see DB_DataObject::getLinks()
135 * @see DB_DataObject::getLink()
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).
142 public function links() {
143 $link = ['activity_id' => 'civicrm_activity:id'];