Commit | Line | Data |
---|---|---|
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 | */ |
21 | class 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 | ||
6a488035 TO |
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 | * | |
6c552737 | 75 | * @param array $entityBlock |
6a488035 | 76 | * |
a6c01b45 CW |
77 | * @return array |
78 | * array of phone objects | |
6a488035 | 79 | */ |
00be9182 | 80 | public static function &getValues($entityBlock) { |
6a488035 TO |
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 | * | |
6a0b768e TO |
88 | * @param int $id |
89 | * The contact id. | |
6a488035 | 90 | * |
dd244018 EM |
91 | * @param bool $updateBlankLocInfo |
92 | * @param null $type | |
93 | * @param array $filters | |
94 | * | |
a6c01b45 CW |
95 | * @return array |
96 | * the array of phone ids which are potential numbers | |
6a488035 | 97 | */ |
be2fb01f | 98 | public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) { |
6a488035 TO |
99 | if (!$id) { |
100 | return NULL; | |
101 | } | |
102 | ||
103 | $cond = NULL; | |
104 | if ($type) { | |
b4f964d9 | 105 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
6a488035 TO |
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 | ||
be2fb01f CW |
127 | $params = [ |
128 | 1 => [ | |
6a488035 TO |
129 | $id, |
130 | 'Integer', | |
be2fb01f CW |
131 | ], |
132 | ]; | |
6a488035 | 133 | |
be2fb01f | 134 | $numbers = $values = []; |
353ffa53 TO |
135 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
136 | $count = 1; | |
6a488035 | 137 | while ($dao->fetch()) { |
be2fb01f | 138 | $values = [ |
6a488035 TO |
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, | |
be2fb01f | 145 | ]; |
6a488035 TO |
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 | /** | |
f47539f6 | 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. | |
6a488035 | 161 | * |
6a0b768e TO |
162 | * @param array $entityElements |
163 | * The array containing entity_id and. | |
16b10e64 | 164 | * entity_table name |
6a488035 | 165 | * |
da6b46f4 EM |
166 | * @param null $type |
167 | * | |
a6c01b45 CW |
168 | * @return array |
169 | * the array of phone ids which are potential numbers | |
6a488035 | 170 | */ |
00be9182 | 171 | public static function allEntityPhones($entityElements, $type = NULL) { |
6a488035 TO |
172 | if (empty($entityElements)) { |
173 | return NULL; | |
174 | } | |
175 | ||
176 | $cond = NULL; | |
177 | if ($type) { | |
b4f964d9 | 178 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
6a488035 TO |
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 | ||
be2fb01f CW |
196 | $params = [ |
197 | 1 => [ | |
6a488035 TO |
198 | $entityId, |
199 | 'Integer', | |
be2fb01f CW |
200 | ], |
201 | ]; | |
202 | $numbers = []; | |
6a488035 TO |
203 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
204 | while ($dao->fetch()) { | |
be2fb01f | 205 | $numbers[$dao->phone_id] = [ |
6a488035 TO |
206 | 'locationType' => $dao->locationType, |
207 | 'is_primary' => $dao->is_primary, | |
208 | 'id' => $dao->phone_id, | |
209 | 'phone' => $dao->phone, | |
210 | 'locationTypeId' => $dao->locationTypeId, | |
be2fb01f | 211 | ]; |
6a488035 TO |
212 | } |
213 | return $numbers; | |
214 | } | |
215 | ||
216 | /** | |
217 | * Set NULL to phone, mapping, uffield | |
218 | * | |
6a0b768e TO |
219 | * @param $optionId |
220 | * Value of option to be deleted. | |
6a488035 | 221 | */ |
00be9182 | 222 | public static function setOptionToNull($optionId) { |
6a488035 TO |
223 | if (!$optionId) { |
224 | return; | |
225 | } | |
aca2de91 CW |
226 | // Ensure mysql phone function exists |
227 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
6a488035 | 228 | |
be2fb01f | 229 | $tables = [ |
6a488035 TO |
230 | 'civicrm_phone', |
231 | 'civicrm_mapping_field', | |
232 | 'civicrm_uf_field', | |
be2fb01f CW |
233 | ]; |
234 | $params = [ | |
235 | 1 => [ | |
6a488035 TO |
236 | $optionId, |
237 | 'Integer', | |
be2fb01f CW |
238 | ], |
239 | ]; | |
6a488035 TO |
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 | } | |
12445e1c CW |
246 | |
247 | /** | |
fe482240 | 248 | * Call common delete function. |
ea3ddccf | 249 | * |
250 | * @param int $id | |
251 | * | |
252 | * @return bool | |
12445e1c | 253 | */ |
00be9182 | 254 | public static function del($id) { |
aca2de91 CW |
255 | // Ensure mysql phone function exists |
256 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
a65e2e55 | 257 | return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id); |
12445e1c | 258 | } |
96025800 | 259 | |
6a488035 | 260 | } |