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