Merge pull request #17253 from mattwire/utf8convertblocksize
[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 return self::writeRecord($params);
61 }
62
63 /**
64 * Given the list of params in the params array, fetch the object
65 * and store the values in the values array
66 *
67 * @param array $entityBlock
68 *
69 * @return array
70 * array of phone objects
71 */
72 public static function &getValues($entityBlock) {
73 $getValues = CRM_Core_BAO_Block::getValues('phone', $entityBlock);
74 return $getValues;
75 }
76
77 /**
78 * Get all the phone numbers for a specified contact_id, with the primary being first
79 *
80 * @param int $id
81 * The contact id.
82 *
83 * @param bool $updateBlankLocInfo
84 * @param null $type
85 * @param array $filters
86 *
87 * @return array
88 * the array of phone ids which are potential numbers
89 */
90 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) {
91 if (!$id) {
92 return NULL;
93 }
94
95 $cond = NULL;
96 if ($type) {
97 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
98 if ($phoneTypeId) {
99 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
100 }
101 }
102
103 if (!empty($filters) && is_array($filters)) {
104 foreach ($filters as $key => $value) {
105 $cond .= " AND " . $key . " = " . $value;
106 }
107 }
108
109 $query = "
110 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
111 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
112 civicrm_phone.phone_type_id as phoneTypeId
113 FROM civicrm_contact
114 LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
115 LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
116 WHERE civicrm_contact.id = %1 $cond
117 ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
118
119 $params = [
120 1 => [
121 $id,
122 'Integer',
123 ],
124 ];
125
126 $numbers = $values = [];
127 $dao = CRM_Core_DAO::executeQuery($query, $params);
128 $count = 1;
129 while ($dao->fetch()) {
130 $values = [
131 'locationType' => $dao->locationType,
132 'is_primary' => $dao->is_primary,
133 'id' => $dao->phone_id,
134 'phone' => $dao->phone,
135 'locationTypeId' => $dao->locationTypeId,
136 'phoneTypeId' => $dao->phoneTypeId,
137 ];
138
139 if ($updateBlankLocInfo) {
140 $numbers[$count++] = $values;
141 }
142 else {
143 $numbers[$dao->phone_id] = $values;
144 }
145 }
146 return $numbers;
147 }
148
149 /**
150 * Get all the phone numbers for a specified location_block id, with the primary phone being first.
151 *
152 * This is called from CRM_Core_BAO_Block as a calculated function.
153 *
154 * @param array $entityElements
155 * The array containing entity_id and.
156 * entity_table name
157 *
158 * @param null $type
159 *
160 * @return array
161 * the array of phone ids which are potential numbers
162 */
163 public static function allEntityPhones($entityElements, $type = NULL) {
164 if (empty($entityElements)) {
165 return NULL;
166 }
167
168 $cond = NULL;
169 if ($type) {
170 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
171 if ($phoneTypeId) {
172 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
173 }
174 }
175
176 $entityId = $entityElements['entity_id'];
177 $entityTable = $entityElements['entity_table'];
178
179 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
180 ph.id as phone_id, ph.location_type_id as locationTypeId
181 FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
182 WHERE ev.id = %1
183 AND loc.id = ev.loc_block_id
184 AND ph.id IN (loc.phone_id, loc.phone_2_id)
185 AND ltype.id = ph.location_type_id
186 ORDER BY ph.is_primary DESC, phone_id ASC ";
187
188 $params = [
189 1 => [
190 $entityId,
191 'Integer',
192 ],
193 ];
194 $numbers = [];
195 $dao = CRM_Core_DAO::executeQuery($sql, $params);
196 while ($dao->fetch()) {
197 $numbers[$dao->phone_id] = [
198 'locationType' => $dao->locationType,
199 'is_primary' => $dao->is_primary,
200 'id' => $dao->phone_id,
201 'phone' => $dao->phone,
202 'locationTypeId' => $dao->locationTypeId,
203 ];
204 }
205 return $numbers;
206 }
207
208 /**
209 * Set NULL to phone, mapping, uffield
210 *
211 * @param $optionId
212 * Value of option to be deleted.
213 */
214 public static function setOptionToNull($optionId) {
215 if (!$optionId) {
216 return;
217 }
218 // Ensure mysql phone function exists
219 CRM_Core_DAO::checkSqlFunctionsExist();
220
221 $tables = [
222 'civicrm_phone',
223 'civicrm_mapping_field',
224 'civicrm_uf_field',
225 ];
226 $params = [
227 1 => [
228 $optionId,
229 'Integer',
230 ],
231 ];
232
233 foreach ($tables as $tableName) {
234 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
235 CRM_Core_DAO::executeQuery($query, $params);
236 }
237 }
238
239 /**
240 * Call common delete function.
241 *
242 * @param int $id
243 *
244 * @return bool
245 */
246 public static function del($id) {
247 // Ensure mysql phone function exists
248 CRM_Core_DAO::checkSqlFunctionsExist();
249 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id);
250 }
251
252 }