3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 * Class contains functions for phone.
37 class CRM_Core_BAO_Phone
extends CRM_Core_DAO_Phone
{
40 * Create phone object - note that the create function calls 'add' but
41 * has more business logic
43 * @param array $params
46 * @throws API_Exception
48 public static function create($params) {
49 // Ensure mysql phone function exists
50 CRM_Core_DAO
::checkSqlFunctionsExist();
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
56 CRM_Core_BAO_Block
::handlePrimary($params, get_class());
58 $phone = self
::add($params);
64 * Takes an associative array and adds phone.
66 * @param array $params
67 * (reference ) an assoc array of name/value pairs.
70 * CRM_Core_BAO_Phone object on success, null otherwise
72 public static function add(&$params) {
73 // Ensure mysql phone function exists
74 CRM_Core_DAO
::checkSqlFunctionsExist();
76 $hook = empty($params['id']) ?
'create' : 'edit';
77 CRM_Utils_Hook
::pre($hook, 'Phone', CRM_Utils_Array
::value('id', $params), $params);
79 $phone = new CRM_Core_DAO_Phone();
80 $phone->copyValues($params);
83 CRM_Utils_Hook
::post($hook, 'Phone', $phone->id
, $phone);
88 * Given the list of params in the params array, fetch the object
89 * and store the values in the values array
91 * @param array $entityBlock
94 * array of phone objects
96 public static function &getValues($entityBlock) {
97 $getValues = CRM_Core_BAO_Block
::getValues('phone', $entityBlock);
102 * Get all the phone numbers for a specified contact_id, with the primary being first
107 * @param bool $updateBlankLocInfo
109 * @param array $filters
112 * the array of phone ids which are potential numbers
114 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = array()) {
121 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'));
123 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
127 if (!empty($filters) && is_array($filters)) {
128 foreach ($filters as $key => $value) {
129 $cond .= " AND " . $key . " = " . $value;
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
138 LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
139 LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
140 WHERE civicrm_contact.id = %1 $cond
141 ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
150 $numbers = $values = array();
151 $dao = CRM_Core_DAO
::executeQuery($query, $params);
153 while ($dao->fetch()) {
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
,
163 if ($updateBlankLocInfo) {
164 $numbers[$count++
] = $values;
167 $numbers[$dao->phone_id
] = $values;
174 * Get all the phone numbers for a specified location_block id, with the primary phone being first
176 * @param array $entityElements
177 * The array containing entity_id and.
183 * the array of phone ids which are potential numbers
185 public static function allEntityPhones($entityElements, $type = NULL) {
186 if (empty($entityElements)) {
192 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'));
194 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
198 $entityId = $entityElements['entity_id'];
199 $entityTable = $entityElements['entity_table'];
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
203 FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
205 AND loc.id = ev.loc_block_id
206 AND ph.id IN (loc.phone_id, loc.phone_2_id)
207 AND ltype.id = ph.location_type_id
208 ORDER BY ph.is_primary DESC, phone_id ASC ";
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
,
231 * Set NULL to phone, mapping, uffield
234 * Value of option to be deleted.
236 public static function setOptionToNull($optionId) {
240 // Ensure mysql phone function exists
241 CRM_Core_DAO
::checkSqlFunctionsExist();
245 'civicrm_mapping_field',
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);
262 * Call common delete function.
268 public static function del($id) {
269 // Ensure mysql phone function exists
270 CRM_Core_DAO
::checkSqlFunctionsExist();
271 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Phone', $id);