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