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