3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Class contains functions for phone
39 class CRM_Core_BAO_Phone
extends CRM_Core_DAO_Phone
{
42 * Create phone object - note that the create function calls 'add' but
43 * has more business logic
45 * @param array $params input parameters
51 * @throws API_Exception
53 static function create($params) {
54 // Ensure mysql phone function exists
55 CRM_Core_DAO
::checkSqlFunctionsExist();
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
61 CRM_Core_BAO_Block
::handlePrimary($params, get_class());
63 $phone = self
::add($params);
69 * takes an associative array and adds phone
71 * @param array $params (reference ) an assoc array of name/value pairs
73 * @return object CRM_Core_BAO_Phone object on success, null otherwise
77 static function add(&$params) {
78 // Ensure mysql phone function exists
79 CRM_Core_DAO
::checkSqlFunctionsExist();
81 $hook = empty($params['id']) ?
'create' : 'edit';
82 CRM_Utils_Hook
::pre($hook, 'Phone', CRM_Utils_Array
::value('id', $params), $params);
84 $phone = new CRM_Core_DAO_Phone();
85 $phone->copyValues($params);
88 CRM_Utils_Hook
::post($hook, 'Phone', $phone->id
, $phone);
93 * Given the list of params in the params array, fetch the object
94 * and store the values in the values array
96 * @param array entityBlock input parameters to find object
98 * @return array array of phone objects
102 static function &getValues($entityBlock) {
103 $getValues = CRM_Core_BAO_Block
::getValues('phone', $entityBlock);
108 * Get all the phone numbers for a specified contact_id, with the primary being first
110 * @param int $id the contact id
112 * @param bool $updateBlankLocInfo
114 * @param array $filters
116 * @return array the array of phone ids which are potential numbers
120 static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = array(
128 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'));
130 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
134 if (!empty($filters) && is_array($filters)) {
135 foreach ($filters as $key => $value) {
136 $cond .= " AND " . $key . " = " . $value;
141 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
142 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
143 civicrm_phone.phone_type_id as phoneTypeId
145 LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
146 LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
147 WHERE civicrm_contact.id = %1 $cond
148 ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
158 $numbers = $values = array();
159 $dao = CRM_Core_DAO
::executeQuery($query, $params);
161 while ($dao->fetch()) {
163 'locationType' => $dao->locationType
,
164 'is_primary' => $dao->is_primary
,
165 'id' => $dao->phone_id
,
166 'phone' => $dao->phone
,
167 'locationTypeId' => $dao->locationTypeId
,
168 'phoneTypeId' => $dao->phoneTypeId
,
171 if ($updateBlankLocInfo) {
172 $numbers[$count++
] = $values;
175 $numbers[$dao->phone_id
] = $values;
182 * Get all the phone numbers for a specified location_block id, with the primary phone being first
184 * @param array $entityElements the array containing entity_id and
189 * @return array the array of phone ids which are potential numbers
193 static function allEntityPhones($entityElements, $type = NULL) {
194 if (empty($entityElements)) {
200 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'));
202 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
206 $entityId = $entityElements['entity_id'];
207 $entityTable = $entityElements['entity_table'];
209 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
210 ph.id as phone_id, ph.location_type_id as locationTypeId
211 FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
213 AND loc.id = ev.loc_block_id
214 AND ph.id IN (loc.phone_id, loc.phone_2_id)
215 AND ltype.id = ph.location_type_id
216 ORDER BY ph.is_primary DESC, phone_id ASC ";
225 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
226 while ($dao->fetch()) {
227 $numbers[$dao->phone_id
] = array(
228 'locationType' => $dao->locationType
,
229 'is_primary' => $dao->is_primary
,
230 'id' => $dao->phone_id
,
231 'phone' => $dao->phone
,
232 'locationTypeId' => $dao->locationTypeId
,
239 * Set NULL to phone, mapping, uffield
241 * @param $optionId value of option to be deleted
246 static function setOptionToNull($optionId) {
250 // Ensure mysql phone function exists
251 CRM_Core_DAO
::checkSqlFunctionsExist();
255 'civicrm_mapping_field',
265 foreach ($tables as $tableName) {
266 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
267 CRM_Core_DAO
::executeQuery($query, $params);
272 * Call common delete function
274 static function del($id) {
275 // Ensure mysql phone function exists
276 CRM_Core_DAO
::checkSqlFunctionsExist();
277 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Phone', $id);