3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class contains functions for phone.
21 class CRM_Core_BAO_Phone
extends CRM_Core_DAO_Phone
{
22 use CRM_Contact_AccessTrait
;
25 * Create phone object - note that the create function calls 'add' but
26 * has more business logic
28 * @param array $params
30 * @return \CRM_Core_DAO_Phone
32 * @throws API_Exception
33 * @throws \CRM_Core_Exception
35 public static function create($params) {
36 CRM_Core_BAO_Block
::handlePrimary($params, get_class());
37 return self
::writeRecord($params);
41 * Takes an associative array and adds phone.
43 * @deprecated use create.
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
49 * CRM_Core_BAO_Phone object on success, null otherwise
51 * @throws \API_Exception
52 * @throws \CRM_Core_Exception
54 public static function add($params) {
55 CRM_Core_Error
::deprecatedFunctionWarning('Use the v4 api');
56 return self
::create($params);
60 * Given the list of params in the params array, fetch the object
61 * and store the values in the values array
63 * @param array $entityBlock
66 * array of phone objects
67 * @throws \CRM_Core_Exception
69 public static function &getValues($entityBlock) {
70 $getValues = CRM_Core_BAO_Block
::getValues('phone', $entityBlock);
75 * Get all the phone numbers for a specified contact_id, with the primary being first
80 * @param bool $updateBlankLocInfo
82 * @param array $filters
85 * the array of phone ids which are potential numbers
87 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) {
94 $phoneTypeId = CRM_Core_PseudoConstant
::getKey('CRM_Core_DAO_Phone', 'phone_type_id', $type);
96 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
100 if (!empty($filters) && is_array($filters)) {
101 foreach ($filters as $key => $value) {
102 $cond .= " AND " . $key . " = " . $value;
107 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
108 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
109 civicrm_phone.phone_type_id as phoneTypeId
111 LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
112 LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
113 WHERE civicrm_contact.id = %1 $cond
114 ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
123 $numbers = $values = [];
124 $dao = CRM_Core_DAO
::executeQuery($query, $params);
126 while ($dao->fetch()) {
128 'locationType' => $dao->locationType
,
129 'is_primary' => $dao->is_primary
,
130 'id' => $dao->phone_id
,
131 'phone' => $dao->phone
,
132 'locationTypeId' => $dao->locationTypeId
,
133 'phoneTypeId' => $dao->phoneTypeId
,
136 if ($updateBlankLocInfo) {
137 $numbers[$count++
] = $values;
140 $numbers[$dao->phone_id
] = $values;
147 * Get all the phone numbers for a specified location_block id, with the primary phone being first.
149 * This is called from CRM_Core_BAO_Block as a calculated function.
151 * @param array $entityElements
152 * The array containing entity_id and.
158 * the array of phone ids which are potential numbers
160 public static function allEntityPhones($entityElements, $type = NULL) {
161 if (empty($entityElements)) {
167 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'));
169 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
173 $entityId = $entityElements['entity_id'];
174 $entityTable = $entityElements['entity_table'];
176 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
177 ph.id as phone_id, ph.location_type_id as locationTypeId
178 FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
180 AND loc.id = ev.loc_block_id
181 AND ph.id IN (loc.phone_id, loc.phone_2_id)
182 AND ltype.id = ph.location_type_id
183 ORDER BY ph.is_primary DESC, phone_id ASC ";
192 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
193 while ($dao->fetch()) {
194 $numbers[$dao->phone_id
] = [
195 'locationType' => $dao->locationType
,
196 'is_primary' => $dao->is_primary
,
197 'id' => $dao->phone_id
,
198 'phone' => $dao->phone
,
199 'locationTypeId' => $dao->locationTypeId
,
206 * Set NULL to phone, mapping, uffield
209 * Value of option to be deleted.
211 public static function setOptionToNull($optionId) {
218 'civicrm_mapping_field',
228 foreach ($tables as $tableName) {
229 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
230 CRM_Core_DAO
::executeQuery($query, $params);
235 * Call common delete function.
241 public static function del($id) {
242 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Phone', $id);