Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / CRM / Core / BAO / Phone.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 /**
48 * @param array $params
49 *
50 * @return object
51 * @throws API_Exception
52 */
53 static function create($params) {
54 // Ensure mysql phone function exists
55 CRM_Core_DAO::checkSqlFunctionsExist();
56
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) {
78 // Ensure mysql phone function exists
79 CRM_Core_DAO::checkSqlFunctionsExist();
80
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 *
112 * @param bool $updateBlankLocInfo
113 * @param null $type
114 * @param array $filters
115 *
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 if (!$id) {
122 return NULL;
123 }
124
125 $cond = NULL;
126 if ($type) {
127 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
128 if ($phoneTypeId) {
129 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
130 }
131 }
132
133 if (!empty($filters) && is_array($filters)) {
134 foreach ($filters as $key => $value) {
135 $cond .= " AND " . $key . " = " . $value;
136 }
137 }
138
139 $query = "
140 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
141 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
142 civicrm_phone.phone_type_id as phoneTypeId
143 FROM civicrm_contact
144 LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
145 LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
146 WHERE civicrm_contact.id = %1 $cond
147 ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
148
149
150 $params = array(
151 1 => array(
152 $id,
153 'Integer',
154 ),
155 );
156
157 $numbers = $values = array();
158 $dao = CRM_Core_DAO::executeQuery($query, $params);
159 $count = 1;
160 while ($dao->fetch()) {
161 $values = array(
162 'locationType' => $dao->locationType,
163 'is_primary' => $dao->is_primary,
164 'id' => $dao->phone_id,
165 'phone' => $dao->phone,
166 'locationTypeId' => $dao->locationTypeId,
167 'phoneTypeId' => $dao->phoneTypeId,
168 );
169
170 if ($updateBlankLocInfo) {
171 $numbers[$count++] = $values;
172 }
173 else {
174 $numbers[$dao->phone_id] = $values;
175 }
176 }
177 return $numbers;
178 }
179
180 /**
181 * Get all the phone numbers for a specified location_block id, with the primary phone being first
182 *
183 * @param array $entityElements the array containing entity_id and
184 * entity_table name
185 *
186 * @param null $type
187 *
188 * @return array the array of phone ids which are potential numbers
189 * @access public
190 * @static
191 */
192 static function allEntityPhones($entityElements, $type = NULL) {
193 if (empty($entityElements)) {
194 return NULL;
195 }
196
197 $cond = NULL;
198 if ($type) {
199 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
200 if ($phoneTypeId) {
201 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
202 }
203 }
204
205 $entityId = $entityElements['entity_id'];
206 $entityTable = $entityElements['entity_table'];
207
208 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
209 ph.id as phone_id, ph.location_type_id as locationTypeId
210 FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
211 WHERE ev.id = %1
212 AND loc.id = ev.loc_block_id
213 AND ph.id IN (loc.phone_id, loc.phone_2_id)
214 AND ltype.id = ph.location_type_id
215 ORDER BY ph.is_primary DESC, phone_id ASC ";
216
217 $params = array(
218 1 => array(
219 $entityId,
220 'Integer',
221 ),
222 );
223 $numbers = array();
224 $dao = CRM_Core_DAO::executeQuery($sql, $params);
225 while ($dao->fetch()) {
226 $numbers[$dao->phone_id] = array(
227 'locationType' => $dao->locationType,
228 'is_primary' => $dao->is_primary,
229 'id' => $dao->phone_id,
230 'phone' => $dao->phone,
231 'locationTypeId' => $dao->locationTypeId,
232 );
233 }
234 return $numbers;
235 }
236
237 /**
238 * Set NULL to phone, mapping, uffield
239 *
240 * @param $optionId value of option to be deleted
241 *
242 * return void
243 * @static
244 */
245 static function setOptionToNull($optionId) {
246 if (!$optionId) {
247 return;
248 }
249 // Ensure mysql phone function exists
250 CRM_Core_DAO::checkSqlFunctionsExist();
251
252 $tables = array(
253 'civicrm_phone',
254 'civicrm_mapping_field',
255 'civicrm_uf_field',
256 );
257 $params = array(
258 1 => array(
259 $optionId,
260 'Integer',
261 ),
262 );
263
264 foreach ($tables as $tableName) {
265 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
266 CRM_Core_DAO::executeQuery($query, $params);
267 }
268 }
269
270 /**
271 * Call common delete function
272 */
273 static function del($id) {
274 // Ensure mysql phone function exists
275 CRM_Core_DAO::checkSqlFunctionsExist();
276 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id);
277 }
278 }
279