Merge pull request #23895 from colemanw/searchKitManaged
[civicrm-core.git] / CRM / Core / BAO / Phone.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 * Class contains functions for phone.
20 */
21 class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone {
22 use CRM_Contact_AccessTrait;
23
24 /**
25 * Create phone object - note that the create function calls 'add' but
26 * has more business logic
27 *
28 * @param array $params
29 *
30 * @return \CRM_Core_DAO_Phone
31 *
32 * @throws API_Exception
33 * @throws \CRM_Core_Exception
34 */
35 public static function create($params) {
36 CRM_Core_BAO_Block::handlePrimary($params, get_class());
37 return self::writeRecord($params);
38 }
39
40 /**
41 * Takes an associative array and adds phone.
42 *
43 * @deprecated use create.
44 *
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
47 *
48 * @return object
49 * CRM_Core_BAO_Phone object on success, null otherwise
50 *
51 * @throws \API_Exception
52 * @throws \CRM_Core_Exception
53 */
54 public static function add($params) {
55 CRM_Core_Error::deprecatedFunctionWarning('Use the v4 api');
56 return self::create($params);
57 }
58
59 /**
60 * Given the list of params in the params array, fetch the object
61 * and store the values in the values array
62 *
63 * @param array $entityBlock
64 *
65 * @return array
66 * array of phone objects
67 * @throws \CRM_Core_Exception
68 */
69 public static function &getValues($entityBlock) {
70 $getValues = CRM_Core_BAO_Block::getValues('phone', $entityBlock);
71 return $getValues;
72 }
73
74 /**
75 * Get all the phone numbers for a specified contact_id, with the primary being first
76 *
77 * @param int $id
78 * The contact id.
79 * @param bool $updateBlankLocInfo
80 * @param string|null $type
81 * @param array $filters
82 *
83 * @return array
84 * the array of phone ids which are potential numbers
85 */
86 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) {
87 if (!$id) {
88 return NULL;
89 }
90
91 $cond = NULL;
92 if ($type) {
93 $phoneTypeId = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Phone', 'phone_type_id', $type);
94 if ($phoneTypeId) {
95 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
96 }
97 }
98
99 if (!empty($filters) && is_array($filters)) {
100 foreach ($filters as $key => $value) {
101 $cond .= " AND " . $key . " = " . $value;
102 }
103 }
104
105 $query = "
106 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
107 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
108 civicrm_phone.phone_type_id as phoneTypeId
109 FROM civicrm_contact
110 LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
111 LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
112 WHERE civicrm_contact.id = %1 $cond
113 ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
114
115 $params = [
116 1 => [
117 $id,
118 'Integer',
119 ],
120 ];
121
122 $numbers = $values = [];
123 $dao = CRM_Core_DAO::executeQuery($query, $params);
124 $count = 1;
125 while ($dao->fetch()) {
126 $values = [
127 'locationType' => $dao->locationType,
128 'is_primary' => $dao->is_primary,
129 'id' => $dao->phone_id,
130 'phone' => $dao->phone,
131 'locationTypeId' => $dao->locationTypeId,
132 'phoneTypeId' => $dao->phoneTypeId,
133 ];
134
135 if ($updateBlankLocInfo) {
136 $numbers[$count++] = $values;
137 }
138 else {
139 $numbers[$dao->phone_id] = $values;
140 }
141 }
142 return $numbers;
143 }
144
145 /**
146 * Get all the phone numbers for a specified location_block id, with the primary phone being first.
147 *
148 * This is called from CRM_Core_BAO_Block as a calculated function.
149 *
150 * @param array $entityElements
151 * The array containing entity_id and entity_table name
152 * @param string|null $type
153 *
154 * @return array
155 * the array of phone ids which are potential numbers
156 */
157 public static function allEntityPhones($entityElements, $type = NULL) {
158 if (empty($entityElements)) {
159 return NULL;
160 }
161
162 $cond = NULL;
163 if ($type) {
164 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
165 if ($phoneTypeId) {
166 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
167 }
168 }
169
170 $entityId = $entityElements['entity_id'];
171 $entityTable = $entityElements['entity_table'];
172
173 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
174 ph.id as phone_id, ph.location_type_id as locationTypeId
175 FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
176 WHERE ev.id = %1
177 AND loc.id = ev.loc_block_id
178 AND ph.id IN (loc.phone_id, loc.phone_2_id)
179 AND ltype.id = ph.location_type_id
180 ORDER BY ph.is_primary DESC, phone_id ASC ";
181
182 $params = [
183 1 => [
184 $entityId,
185 'Integer',
186 ],
187 ];
188 $numbers = [];
189 $dao = CRM_Core_DAO::executeQuery($sql, $params);
190 while ($dao->fetch()) {
191 $numbers[$dao->phone_id] = [
192 'locationType' => $dao->locationType,
193 'is_primary' => $dao->is_primary,
194 'id' => $dao->phone_id,
195 'phone' => $dao->phone,
196 'locationTypeId' => $dao->locationTypeId,
197 ];
198 }
199 return $numbers;
200 }
201
202 /**
203 * Set NULL to phone, mapping, uffield
204 *
205 * @param int $optionId
206 * Value of option to be deleted.
207 */
208 public static function setOptionToNull($optionId) {
209 if (!$optionId) {
210 return;
211 }
212
213 $tables = [
214 'civicrm_phone',
215 'civicrm_mapping_field',
216 'civicrm_uf_field',
217 ];
218 $params = [
219 1 => [
220 $optionId,
221 'Integer',
222 ],
223 ];
224
225 foreach ($tables as $tableName) {
226 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
227 CRM_Core_DAO::executeQuery($query, $params);
228 }
229 }
230
231 /**
232 * Call common delete function.
233 *
234 * @see \CRM_Contact_BAO_Contact::on_hook_civicrm_post
235 *
236 * @param int $id
237 * @deprecated
238 * @return bool
239 */
240 public static function del($id) {
241 return (bool) self::deleteRecord(['id' => $id]);
242 }
243
244 }