Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Class contains functions for phone | |
38 | */ | |
39 | class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone { | |
40 | ||
41 | /* | |
42 | * Create phone object - note that the create function calls 'add' but | |
43 | * has more business logic | |
44 | * | |
6a0b768e TO |
45 | * @param array $params |
46 | * Input parameters. | |
6a488035 | 47 | */ |
b5c2afd0 | 48 | /** |
c490a46a | 49 | * @param array $params |
b5c2afd0 EM |
50 | * |
51 | * @return object | |
52 | * @throws API_Exception | |
53 | */ | |
00be9182 | 54 | public static function create($params) { |
aca2de91 CW |
55 | // Ensure mysql phone function exists |
56 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
57 | ||
6a488035 TO |
58 | if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || |
59 | // if id is set & is_primary isn't we can assume no change | |
60 | empty($params['id']) | |
61 | ) { | |
62 | CRM_Core_BAO_Block::handlePrimary($params, get_class()); | |
63 | } | |
64 | $phone = self::add($params); | |
65 | ||
66 | return $phone; | |
67 | } | |
68 | ||
69 | /** | |
100fef9d | 70 | * Takes an associative array and adds phone |
6a488035 | 71 | * |
6a0b768e TO |
72 | * @param array $params |
73 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 74 | * |
a6c01b45 CW |
75 | * @return object |
76 | * CRM_Core_BAO_Phone object on success, null otherwise | |
6a488035 TO |
77 | * @static |
78 | */ | |
00be9182 | 79 | public static function add(&$params) { |
aca2de91 CW |
80 | // Ensure mysql phone function exists |
81 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
82 | ||
6a488035 TO |
83 | $hook = empty($params['id']) ? 'create' : 'edit'; |
84 | CRM_Utils_Hook::pre($hook, 'Phone', CRM_Utils_Array::value('id', $params), $params); | |
85 | ||
86 | $phone = new CRM_Core_DAO_Phone(); | |
87 | $phone->copyValues($params); | |
88 | $phone->save(); | |
89 | ||
90 | CRM_Utils_Hook::post($hook, 'Phone', $phone->id, $phone); | |
91 | return $phone; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Given the list of params in the params array, fetch the object | |
96 | * and store the values in the values array | |
97 | * | |
98 | * @param array entityBlock input parameters to find object | |
99 | * | |
a6c01b45 CW |
100 | * @return array |
101 | * array of phone objects | |
6a488035 TO |
102 | * @static |
103 | */ | |
00be9182 | 104 | public static function &getValues($entityBlock) { |
6a488035 TO |
105 | $getValues = CRM_Core_BAO_Block::getValues('phone', $entityBlock); |
106 | return $getValues; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Get all the phone numbers for a specified contact_id, with the primary being first | |
111 | * | |
6a0b768e TO |
112 | * @param int $id |
113 | * The contact id. | |
6a488035 | 114 | * |
dd244018 EM |
115 | * @param bool $updateBlankLocInfo |
116 | * @param null $type | |
117 | * @param array $filters | |
118 | * | |
a6c01b45 CW |
119 | * @return array |
120 | * the array of phone ids which are potential numbers | |
6a488035 TO |
121 | * @static |
122 | */ | |
00be9182 | 123 | public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = array()) { |
6a488035 TO |
124 | if (!$id) { |
125 | return NULL; | |
126 | } | |
127 | ||
128 | $cond = NULL; | |
129 | if ($type) { | |
b4f964d9 | 130 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
6a488035 TO |
131 | if ($phoneTypeId) { |
132 | $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId"; | |
133 | } | |
134 | } | |
135 | ||
136 | if (!empty($filters) && is_array($filters)) { | |
137 | foreach ($filters as $key => $value) { | |
138 | $cond .= " AND " . $key . " = " . $value; | |
139 | } | |
140 | } | |
141 | ||
142 | $query = " | |
143 | SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary, | |
144 | civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId, | |
145 | civicrm_phone.phone_type_id as phoneTypeId | |
146 | FROM civicrm_contact | |
147 | LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id ) | |
148 | LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id ) | |
149 | WHERE civicrm_contact.id = %1 $cond | |
150 | ORDER BY civicrm_phone.is_primary DESC, phone_id ASC "; | |
151 | ||
152 | ||
153 | $params = array( | |
154 | 1 => array( | |
155 | $id, | |
156 | 'Integer', | |
157 | ), | |
158 | ); | |
159 | ||
160 | $numbers = $values = array(); | |
353ffa53 TO |
161 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
162 | $count = 1; | |
6a488035 TO |
163 | while ($dao->fetch()) { |
164 | $values = array( | |
165 | 'locationType' => $dao->locationType, | |
166 | 'is_primary' => $dao->is_primary, | |
167 | 'id' => $dao->phone_id, | |
168 | 'phone' => $dao->phone, | |
169 | 'locationTypeId' => $dao->locationTypeId, | |
170 | 'phoneTypeId' => $dao->phoneTypeId, | |
171 | ); | |
172 | ||
173 | if ($updateBlankLocInfo) { | |
174 | $numbers[$count++] = $values; | |
175 | } | |
176 | else { | |
177 | $numbers[$dao->phone_id] = $values; | |
178 | } | |
179 | } | |
180 | return $numbers; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Get all the phone numbers for a specified location_block id, with the primary phone being first | |
185 | * | |
6a0b768e TO |
186 | * @param array $entityElements |
187 | * The array containing entity_id and. | |
16b10e64 | 188 | * entity_table name |
6a488035 | 189 | * |
da6b46f4 EM |
190 | * @param null $type |
191 | * | |
a6c01b45 CW |
192 | * @return array |
193 | * the array of phone ids which are potential numbers | |
6a488035 TO |
194 | * @static |
195 | */ | |
00be9182 | 196 | public static function allEntityPhones($entityElements, $type = NULL) { |
6a488035 TO |
197 | if (empty($entityElements)) { |
198 | return NULL; | |
199 | } | |
200 | ||
201 | $cond = NULL; | |
202 | if ($type) { | |
b4f964d9 | 203 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
6a488035 TO |
204 | if ($phoneTypeId) { |
205 | $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId"; | |
206 | } | |
207 | } | |
208 | ||
209 | $entityId = $entityElements['entity_id']; | |
210 | $entityTable = $entityElements['entity_table']; | |
211 | ||
212 | $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary, | |
213 | ph.id as phone_id, ph.location_type_id as locationTypeId | |
214 | FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev | |
215 | WHERE ev.id = %1 | |
216 | AND loc.id = ev.loc_block_id | |
217 | AND ph.id IN (loc.phone_id, loc.phone_2_id) | |
218 | AND ltype.id = ph.location_type_id | |
219 | ORDER BY ph.is_primary DESC, phone_id ASC "; | |
220 | ||
221 | $params = array( | |
222 | 1 => array( | |
223 | $entityId, | |
224 | 'Integer', | |
225 | ), | |
226 | ); | |
227 | $numbers = array(); | |
228 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
229 | while ($dao->fetch()) { | |
230 | $numbers[$dao->phone_id] = array( | |
231 | 'locationType' => $dao->locationType, | |
232 | 'is_primary' => $dao->is_primary, | |
233 | 'id' => $dao->phone_id, | |
234 | 'phone' => $dao->phone, | |
235 | 'locationTypeId' => $dao->locationTypeId, | |
236 | ); | |
237 | } | |
238 | return $numbers; | |
239 | } | |
240 | ||
241 | /** | |
242 | * Set NULL to phone, mapping, uffield | |
243 | * | |
6a0b768e TO |
244 | * @param $optionId |
245 | * Value of option to be deleted. | |
6a488035 TO |
246 | * |
247 | * return void | |
248 | * @static | |
249 | */ | |
00be9182 | 250 | public static function setOptionToNull($optionId) { |
6a488035 TO |
251 | if (!$optionId) { |
252 | return; | |
253 | } | |
aca2de91 CW |
254 | // Ensure mysql phone function exists |
255 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
6a488035 TO |
256 | |
257 | $tables = array( | |
258 | 'civicrm_phone', | |
259 | 'civicrm_mapping_field', | |
260 | 'civicrm_uf_field', | |
261 | ); | |
262 | $params = array( | |
263 | 1 => array( | |
264 | $optionId, | |
265 | 'Integer', | |
266 | ), | |
267 | ); | |
268 | ||
269 | foreach ($tables as $tableName) { | |
270 | $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1"; | |
271 | CRM_Core_DAO::executeQuery($query, $params); | |
272 | } | |
273 | } | |
12445e1c CW |
274 | |
275 | /** | |
276 | * Call common delete function | |
277 | */ | |
00be9182 | 278 | public static function del($id) { |
aca2de91 CW |
279 | // Ensure mysql phone function exists |
280 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
a65e2e55 | 281 | return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id); |
12445e1c | 282 | } |
6a488035 | 283 | } |