Merge pull request #18510 from eileenmcnaughton/preprocess
[civicrm-core.git] / CRM / Core / BAO / Phone.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
8eedd10a 19 * Class contains functions for phone.
6a488035
TO
20 */
21class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone {
22
d424ffde 23 /**
6a488035
TO
24 * Create phone object - note that the create function calls 'add' but
25 * has more business logic
26 *
c490a46a 27 * @param array $params
b5c2afd0
EM
28 *
29 * @return object
30 * @throws API_Exception
31 */
00be9182 32 public static function create($params) {
aca2de91
CW
33 // Ensure mysql phone function exists
34 CRM_Core_DAO::checkSqlFunctionsExist();
35
6a488035
TO
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 /**
fe482240 48 * Takes an associative array and adds phone.
6a488035 49 *
6a0b768e
TO
50 * @param array $params
51 * (reference ) an assoc array of name/value pairs.
6a488035 52 *
a6c01b45
CW
53 * @return object
54 * CRM_Core_BAO_Phone object on success, null otherwise
6a488035 55 */
00be9182 56 public static function add(&$params) {
aca2de91
CW
57 // Ensure mysql phone function exists
58 CRM_Core_DAO::checkSqlFunctionsExist();
59
0419bf7b 60 return self::writeRecord($params);
6a488035
TO
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 *
6c552737 67 * @param array $entityBlock
6a488035 68 *
a6c01b45
CW
69 * @return array
70 * array of phone objects
6a488035 71 */
00be9182 72 public static function &getValues($entityBlock) {
6a488035
TO
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 *
6a0b768e
TO
80 * @param int $id
81 * The contact id.
6a488035 82 *
dd244018
EM
83 * @param bool $updateBlankLocInfo
84 * @param null $type
85 * @param array $filters
86 *
a6c01b45
CW
87 * @return array
88 * the array of phone ids which are potential numbers
6a488035 89 */
be2fb01f 90 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) {
6a488035
TO
91 if (!$id) {
92 return NULL;
93 }
94
95 $cond = NULL;
96 if ($type) {
b4f964d9 97 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
6a488035
TO
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
114LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
115LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
116WHERE civicrm_contact.id = %1 $cond
117ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
118
be2fb01f
CW
119 $params = [
120 1 => [
6a488035
TO
121 $id,
122 'Integer',
be2fb01f
CW
123 ],
124 ];
6a488035 125
be2fb01f 126 $numbers = $values = [];
353ffa53
TO
127 $dao = CRM_Core_DAO::executeQuery($query, $params);
128 $count = 1;
6a488035 129 while ($dao->fetch()) {
be2fb01f 130 $values = [
6a488035
TO
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,
be2fb01f 137 ];
6a488035
TO
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 /**
f47539f6 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.
6a488035 153 *
6a0b768e
TO
154 * @param array $entityElements
155 * The array containing entity_id and.
16b10e64 156 * entity_table name
6a488035 157 *
da6b46f4
EM
158 * @param null $type
159 *
a6c01b45
CW
160 * @return array
161 * the array of phone ids which are potential numbers
6a488035 162 */
00be9182 163 public static function allEntityPhones($entityElements, $type = NULL) {
6a488035
TO
164 if (empty($entityElements)) {
165 return NULL;
166 }
167
168 $cond = NULL;
169 if ($type) {
b4f964d9 170 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
6a488035
TO
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
181FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
182WHERE ev.id = %1
183AND loc.id = ev.loc_block_id
184AND ph.id IN (loc.phone_id, loc.phone_2_id)
185AND ltype.id = ph.location_type_id
186ORDER BY ph.is_primary DESC, phone_id ASC ";
187
be2fb01f
CW
188 $params = [
189 1 => [
6a488035
TO
190 $entityId,
191 'Integer',
be2fb01f
CW
192 ],
193 ];
194 $numbers = [];
6a488035
TO
195 $dao = CRM_Core_DAO::executeQuery($sql, $params);
196 while ($dao->fetch()) {
be2fb01f 197 $numbers[$dao->phone_id] = [
6a488035
TO
198 'locationType' => $dao->locationType,
199 'is_primary' => $dao->is_primary,
200 'id' => $dao->phone_id,
201 'phone' => $dao->phone,
202 'locationTypeId' => $dao->locationTypeId,
be2fb01f 203 ];
6a488035
TO
204 }
205 return $numbers;
206 }
207
208 /**
209 * Set NULL to phone, mapping, uffield
210 *
6a0b768e
TO
211 * @param $optionId
212 * Value of option to be deleted.
6a488035 213 */
00be9182 214 public static function setOptionToNull($optionId) {
6a488035
TO
215 if (!$optionId) {
216 return;
217 }
aca2de91
CW
218 // Ensure mysql phone function exists
219 CRM_Core_DAO::checkSqlFunctionsExist();
6a488035 220
be2fb01f 221 $tables = [
6a488035
TO
222 'civicrm_phone',
223 'civicrm_mapping_field',
224 'civicrm_uf_field',
be2fb01f
CW
225 ];
226 $params = [
227 1 => [
6a488035
TO
228 $optionId,
229 'Integer',
be2fb01f
CW
230 ],
231 ];
6a488035
TO
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 }
12445e1c
CW
238
239 /**
fe482240 240 * Call common delete function.
ea3ddccf 241 *
242 * @param int $id
243 *
244 * @return bool
12445e1c 245 */
00be9182 246 public static function del($id) {
aca2de91
CW
247 // Ensure mysql phone function exists
248 CRM_Core_DAO::checkSqlFunctionsExist();
a65e2e55 249 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id);
12445e1c 250 }
96025800 251
6a488035 252}