Merge pull request #18324 from mattwire/payproc_configoptional
[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 * Class constructor.
25 */
26 public function __construct() {
27 parent::__construct();
28 }
29
30 /**
31 * Function to add activity contact.
32 *
33 * @param array $params
34 * The values for this table: activity id, contact id, record type.
35 *
36 * @return object
37 * activity_contact object
38 */
39 public static function create($params) {
40 $errorScope = CRM_Core_TemporaryErrorScope::useException();
41 $activityContact = new CRM_Activity_DAO_ActivityContact();
42 $activityContact->copyValues($params);
43 try {
44 return $activityContact->save();
45 }
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 }
56 }
57
58 /**
59 * Retrieve names of contact by activity_id.
60 *
61 * @param int $activityID
62 * @param int $recordTypeID
63 * @param bool $alsoIDs
64 *
65 * @return array
66 */
67 public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
68 $names = [];
69 $ids = [];
70
71 if (empty($activityID)) {
72 return $alsoIDs ? [$names, $ids] : $names;
73 }
74
75 $query = "
76 SELECT contact_a.id, contact_a.sort_name
77 FROM civicrm_contact contact_a
78 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
79 WHERE civicrm_activity_contact.activity_id = %1
80 AND civicrm_activity_contact.record_type_id = %2
81 AND contact_a.is_deleted = 0
82 ";
83 $params = [
84 1 => [$activityID, 'Integer'],
85 2 => [$recordTypeID, 'Integer'],
86 ];
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
94 return $alsoIDs ? [$names, $ids] : $names;
95 }
96
97 /**
98 * Retrieve id of target contact by activity_id.
99 *
100 * @param int $activityID
101 * @param int $recordTypeID
102 *
103 * @return mixed
104 */
105 public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
106 $activityContact = [];
107 if (!CRM_Utils_Rule::positiveInteger($activityID) ||
108 !CRM_Utils_Rule::positiveInteger($recordTypeID)
109 ) {
110 return $activityContact;
111 }
112
113 $sql = " SELECT contact_id
114 FROM civicrm_activity_contact
115 INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
116 WHERE activity_id = %1
117 AND record_type_id = %2
118 AND civicrm_contact.is_deleted = 0
119 ";
120 $params = [
121 1 => [$activityID, 'Integer'],
122 2 => [$recordTypeID, 'Integer'],
123 ];
124
125 $dao = CRM_Core_DAO::executeQuery($sql, $params);
126 while ($dao->fetch()) {
127 $activityContact[] = $dao->contact_id;
128 }
129 return $activityContact;
130 }
131
132 /**
133 * Get the links associate array as defined by the links.ini file.
134 *
135 * Experimental... -
136 * Should look a bit like
137 * [local_col_name] => "related_tablename:related_col_name"
138 *
139 * @see DB_DataObject::getLinks()
140 * @see DB_DataObject::getLink()
141 *
142 * @return array|null
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).
146 */
147 public function links() {
148 $link = ['activity_id' => 'civicrm_activity:id'];
149 return $link;
150 }
151
152 }