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