Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | * | |
45 | * @param array $params input parameters | |
46 | */ | |
b5c2afd0 EM |
47 | /** |
48 | * @param $params | |
49 | * | |
50 | * @return object | |
51 | * @throws API_Exception | |
52 | */ | |
6a488035 | 53 | static function create($params) { |
aca2de91 CW |
54 | // Ensure mysql phone function exists |
55 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
56 | ||
6a488035 TO |
57 | if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || |
58 | // if id is set & is_primary isn't we can assume no change | |
59 | empty($params['id']) | |
60 | ) { | |
61 | CRM_Core_BAO_Block::handlePrimary($params, get_class()); | |
62 | } | |
63 | $phone = self::add($params); | |
64 | ||
65 | return $phone; | |
66 | } | |
67 | ||
68 | /** | |
69 | * takes an associative array and adds phone | |
70 | * | |
71 | * @param array $params (reference ) an assoc array of name/value pairs | |
72 | * | |
73 | * @return object CRM_Core_BAO_Phone object on success, null otherwise | |
74 | * @access public | |
75 | * @static | |
76 | */ | |
77 | static function add(&$params) { | |
aca2de91 CW |
78 | // Ensure mysql phone function exists |
79 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
80 | ||
6a488035 TO |
81 | $hook = empty($params['id']) ? 'create' : 'edit'; |
82 | CRM_Utils_Hook::pre($hook, 'Phone', CRM_Utils_Array::value('id', $params), $params); | |
83 | ||
84 | $phone = new CRM_Core_DAO_Phone(); | |
85 | $phone->copyValues($params); | |
86 | $phone->save(); | |
87 | ||
88 | CRM_Utils_Hook::post($hook, 'Phone', $phone->id, $phone); | |
89 | return $phone; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Given the list of params in the params array, fetch the object | |
94 | * and store the values in the values array | |
95 | * | |
96 | * @param array entityBlock input parameters to find object | |
97 | * | |
98 | * @return array array of phone objects | |
99 | * @access public | |
100 | * @static | |
101 | */ | |
102 | static function &getValues($entityBlock) { | |
103 | $getValues = CRM_Core_BAO_Block::getValues('phone', $entityBlock); | |
104 | return $getValues; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Get all the phone numbers for a specified contact_id, with the primary being first | |
109 | * | |
110 | * @param int $id the contact id | |
111 | * | |
dd244018 EM |
112 | * @param bool $updateBlankLocInfo |
113 | * @param null $type | |
114 | * @param array $filters | |
115 | * | |
6a488035 TO |
116 | * @return array the array of phone ids which are potential numbers |
117 | * @access public | |
118 | * @static | |
119 | */ | |
120 | static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = array( | |
121 | )) { | |
122 | if (!$id) { | |
123 | return NULL; | |
124 | } | |
125 | ||
126 | $cond = NULL; | |
127 | if ($type) { | |
b4f964d9 | 128 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
6a488035 TO |
129 | if ($phoneTypeId) { |
130 | $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId"; | |
131 | } | |
132 | } | |
133 | ||
134 | if (!empty($filters) && is_array($filters)) { | |
135 | foreach ($filters as $key => $value) { | |
136 | $cond .= " AND " . $key . " = " . $value; | |
137 | } | |
138 | } | |
139 | ||
140 | $query = " | |
141 | SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary, | |
142 | civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId, | |
143 | civicrm_phone.phone_type_id as phoneTypeId | |
144 | FROM civicrm_contact | |
145 | LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id ) | |
146 | LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id ) | |
147 | WHERE civicrm_contact.id = %1 $cond | |
148 | ORDER BY civicrm_phone.is_primary DESC, phone_id ASC "; | |
149 | ||
150 | ||
151 | $params = array( | |
152 | 1 => array( | |
153 | $id, | |
154 | 'Integer', | |
155 | ), | |
156 | ); | |
157 | ||
158 | $numbers = $values = array(); | |
159 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
160 | $count = 1; | |
161 | while ($dao->fetch()) { | |
162 | $values = array( | |
163 | 'locationType' => $dao->locationType, | |
164 | 'is_primary' => $dao->is_primary, | |
165 | 'id' => $dao->phone_id, | |
166 | 'phone' => $dao->phone, | |
167 | 'locationTypeId' => $dao->locationTypeId, | |
168 | 'phoneTypeId' => $dao->phoneTypeId, | |
169 | ); | |
170 | ||
171 | if ($updateBlankLocInfo) { | |
172 | $numbers[$count++] = $values; | |
173 | } | |
174 | else { | |
175 | $numbers[$dao->phone_id] = $values; | |
176 | } | |
177 | } | |
178 | return $numbers; | |
179 | } | |
180 | ||
181 | /** | |
182 | * Get all the phone numbers for a specified location_block id, with the primary phone being first | |
183 | * | |
184 | * @param array $entityElements the array containing entity_id and | |
185 | * entity_table name | |
186 | * | |
da6b46f4 EM |
187 | * @param null $type |
188 | * | |
6a488035 TO |
189 | * @return array the array of phone ids which are potential numbers |
190 | * @access public | |
191 | * @static | |
192 | */ | |
193 | static function allEntityPhones($entityElements, $type = NULL) { | |
194 | if (empty($entityElements)) { | |
195 | return NULL; | |
196 | } | |
197 | ||
198 | $cond = NULL; | |
199 | if ($type) { | |
b4f964d9 | 200 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
6a488035 TO |
201 | if ($phoneTypeId) { |
202 | $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId"; | |
203 | } | |
204 | } | |
205 | ||
206 | $entityId = $entityElements['entity_id']; | |
207 | $entityTable = $entityElements['entity_table']; | |
208 | ||
209 | $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary, | |
210 | ph.id as phone_id, ph.location_type_id as locationTypeId | |
211 | FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev | |
212 | WHERE ev.id = %1 | |
213 | AND loc.id = ev.loc_block_id | |
214 | AND ph.id IN (loc.phone_id, loc.phone_2_id) | |
215 | AND ltype.id = ph.location_type_id | |
216 | ORDER BY ph.is_primary DESC, phone_id ASC "; | |
217 | ||
218 | $params = array( | |
219 | 1 => array( | |
220 | $entityId, | |
221 | 'Integer', | |
222 | ), | |
223 | ); | |
224 | $numbers = array(); | |
225 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
226 | while ($dao->fetch()) { | |
227 | $numbers[$dao->phone_id] = array( | |
228 | 'locationType' => $dao->locationType, | |
229 | 'is_primary' => $dao->is_primary, | |
230 | 'id' => $dao->phone_id, | |
231 | 'phone' => $dao->phone, | |
232 | 'locationTypeId' => $dao->locationTypeId, | |
233 | ); | |
234 | } | |
235 | return $numbers; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Set NULL to phone, mapping, uffield | |
240 | * | |
241 | * @param $optionId value of option to be deleted | |
242 | * | |
243 | * return void | |
244 | * @static | |
245 | */ | |
246 | static function setOptionToNull($optionId) { | |
247 | if (!$optionId) { | |
248 | return; | |
249 | } | |
aca2de91 CW |
250 | // Ensure mysql phone function exists |
251 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
6a488035 TO |
252 | |
253 | $tables = array( | |
254 | 'civicrm_phone', | |
255 | 'civicrm_mapping_field', | |
256 | 'civicrm_uf_field', | |
257 | ); | |
258 | $params = array( | |
259 | 1 => array( | |
260 | $optionId, | |
261 | 'Integer', | |
262 | ), | |
263 | ); | |
264 | ||
265 | foreach ($tables as $tableName) { | |
266 | $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1"; | |
267 | CRM_Core_DAO::executeQuery($query, $params); | |
268 | } | |
269 | } | |
12445e1c CW |
270 | |
271 | /** | |
272 | * Call common delete function | |
273 | */ | |
274 | static function del($id) { | |
aca2de91 CW |
275 | // Ensure mysql phone function exists |
276 | CRM_Core_DAO::checkSqlFunctionsExist(); | |
a65e2e55 | 277 | return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id); |
12445e1c | 278 | } |
6a488035 TO |
279 | } |
280 |