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