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