don't crash for nonexistent uf_id
[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
adc7f09e 23 /**
2e2605fe 24 * Function to add activity contact.
adc7f09e 25 *
041ab3d1
TO
26 * @param array $params
27 * The values for this table: activity id, contact id, record type.
adc7f09e 28 *
4cdd873a 29 * @return CRM_Activity_DAO_ActivityContact
a6c01b45 30 * activity_contact object
4cdd873a 31 *
32 * @throws \CRM_Core_Exception
33 * @throws \PEAR_Exception
adc7f09e 34 */
4cdd873a 35 public static function create(array $params): CRM_Activity_DAO_ActivityContact {
8293f7c2 36 try {
4cdd873a 37 return self::writeRecord($params);
2517d079 38 }
8293f7c2 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.
4cdd873a 44 $activityContact = new CRM_Activity_DAO_ActivityContact();
45 $activityContact->copyValues($params);
8293f7c2 46 if ($activityContact->find(TRUE)) {
47 return $activityContact;
48 }
49 throw $e;
50 }
adc7f09e 51 }
f04255e4
DL
52
53 /**
fe482240 54 * Retrieve names of contact by activity_id.
f04255e4 55 *
c490a46a
CW
56 * @param int $activityID
57 * @param int $recordTypeID
6c8f6e67
EM
58 * @param bool $alsoIDs
59 *
f04255e4 60 * @return array
f04255e4 61 */
00be9182 62 public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
96f94695 63 $names = [];
64 $ids = [];
f04255e4
DL
65
66 if (empty($activityID)) {
96f94695 67 return $alsoIDs ? [$names, $ids] : $names;
f04255e4
DL
68 }
69
70 $query = "
71SELECT contact_a.id, contact_a.sort_name
72FROM civicrm_contact contact_a
73INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
74WHERE civicrm_activity_contact.activity_id = %1
a24b3694 75AND civicrm_activity_contact.record_type_id = %2
f04255e4
DL
76AND contact_a.is_deleted = 0
77";
96f94695 78 $params = [
79 1 => [$activityID, 'Integer'],
80 2 => [$recordTypeID, 'Integer'],
81 ];
f04255e4
DL
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
96f94695 89 return $alsoIDs ? [$names, $ids] : $names;
f04255e4
DL
90 }
91
034500d4 92 /**
fe482240 93 * Retrieve id of target contact by activity_id.
034500d4 94 *
100fef9d
CW
95 * @param int $activityID
96 * @param int $recordTypeID
77b97be7 97 *
034500d4 98 * @return mixed
034500d4 99 */
00be9182 100 public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
96f94695 101 $activityContact = [];
034500d4 102 if (!CRM_Utils_Rule::positiveInteger($activityID) ||
353ffa53
TO
103 !CRM_Utils_Rule::positiveInteger($recordTypeID)
104 ) {
034500d4 105 return $activityContact;
106 }
107
108 $sql = " SELECT contact_id
109FROM civicrm_activity_contact
110INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
111WHERE activity_id = %1
2517d079
DL
112AND record_type_id = %2
113AND civicrm_contact.is_deleted = 0
034500d4 114";
96f94695 115 $params = [
116 1 => [$activityID, 'Integer'],
117 2 => [$recordTypeID, 'Integer'],
118 ];
034500d4 119
120 $dao = CRM_Core_DAO::executeQuery($sql, $params);
121 while ($dao->fetch()) {
122 $activityContact[] = $dao->contact_id;
123 }
124 return $activityContact;
125 }
126
ffd93213
EM
127 /**
128 * Get the links associate array as defined by the links.ini file.
129 *
ffd93213
EM
130 * Experimental... -
131 * Should look a bit like
132 * [local_col_name] => "related_tablename:related_col_name"
133 *
7808aae6
SB
134 * @see DB_DataObject::getLinks()
135 * @see DB_DataObject::getLink()
ffd93213 136 *
2b37475d 137 * @return array|null
62d3ee27
SL
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).
ffd93213 141 */
00be9182 142 public function links() {
96f94695 143 $link = ['activity_id' => 'civicrm_activity:id'];
6e1bb60c
N
144 return $link;
145 }
96025800 146
adc7f09e 147}