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