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
{
24 * Create phone object - note that the create function calls 'add' but
25 * has more business logic
27 * @param array $params
30 * @throws API_Exception
32 public static function create($params) {
33 // Ensure mysql phone function exists
34 CRM_Core_DAO
::checkSqlFunctionsExist();
36 if (is_numeric(CRM_Utils_Array
::value('is_primary', $params)) ||
37 // if id is set & is_primary isn't we can assume no change
40 CRM_Core_BAO_Block
::handlePrimary($params, get_class());
42 $phone = self
::add($params);
48 * Takes an associative array and adds phone.
50 * @param array $params
51 * (reference ) an assoc array of name/value pairs.
54 * CRM_Core_BAO_Phone object on success, null otherwise
56 public static function add(&$params) {
57 // Ensure mysql phone function exists
58 CRM_Core_DAO
::checkSqlFunctionsExist();
60 $hook = empty($params['id']) ?
'create' : 'edit';
61 CRM_Utils_Hook
::pre($hook, 'Phone', CRM_Utils_Array
::value('id', $params), $params);
63 $phone = new CRM_Core_DAO_Phone();
64 $phone->copyValues($params);
67 CRM_Utils_Hook
::post($hook, 'Phone', $phone->id
, $phone);
72 * Given the list of params in the params array, fetch the object
73 * and store the values in the values array
75 * @param array $entityBlock
78 * array of phone objects
80 public static function &getValues($entityBlock) {
81 $getValues = CRM_Core_BAO_Block
::getValues('phone', $entityBlock);
86 * Get all the phone numbers for a specified contact_id, with the primary being first
91 * @param bool $updateBlankLocInfo
93 * @param array $filters
96 * the array of phone ids which are potential numbers
98 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) {
105 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'));
107 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
111 if (!empty($filters) && is_array($filters)) {
112 foreach ($filters as $key => $value) {
113 $cond .= " AND " . $key . " = " . $value;
118 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
119 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
120 civicrm_phone.phone_type_id as phoneTypeId
122 LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
123 LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
124 WHERE civicrm_contact.id = %1 $cond
125 ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
134 $numbers = $values = [];
135 $dao = CRM_Core_DAO
::executeQuery($query, $params);
137 while ($dao->fetch()) {
139 'locationType' => $dao->locationType
,
140 'is_primary' => $dao->is_primary
,
141 'id' => $dao->phone_id
,
142 'phone' => $dao->phone
,
143 'locationTypeId' => $dao->locationTypeId
,
144 'phoneTypeId' => $dao->phoneTypeId
,
147 if ($updateBlankLocInfo) {
148 $numbers[$count++
] = $values;
151 $numbers[$dao->phone_id
] = $values;
158 * Get all the phone numbers for a specified location_block id, with the primary phone being first.
160 * This is called from CRM_Core_BAO_Block as a calculated function.
162 * @param array $entityElements
163 * The array containing entity_id and.
169 * the array of phone ids which are potential numbers
171 public static function allEntityPhones($entityElements, $type = NULL) {
172 if (empty($entityElements)) {
178 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'));
180 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
184 $entityId = $entityElements['entity_id'];
185 $entityTable = $entityElements['entity_table'];
187 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
188 ph.id as phone_id, ph.location_type_id as locationTypeId
189 FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
191 AND loc.id = ev.loc_block_id
192 AND ph.id IN (loc.phone_id, loc.phone_2_id)
193 AND ltype.id = ph.location_type_id
194 ORDER BY ph.is_primary DESC, phone_id ASC ";
203 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
204 while ($dao->fetch()) {
205 $numbers[$dao->phone_id
] = [
206 'locationType' => $dao->locationType
,
207 'is_primary' => $dao->is_primary
,
208 'id' => $dao->phone_id
,
209 'phone' => $dao->phone
,
210 'locationTypeId' => $dao->locationTypeId
,
217 * Set NULL to phone, mapping, uffield
220 * Value of option to be deleted.
222 public static function setOptionToNull($optionId) {
226 // Ensure mysql phone function exists
227 CRM_Core_DAO
::checkSqlFunctionsExist();
231 'civicrm_mapping_field',
241 foreach ($tables as $tableName) {
242 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
243 CRM_Core_DAO
::executeQuery($query, $params);
248 * Call common delete function.
254 public static function del($id) {
255 // Ensure mysql phone function exists
256 CRM_Core_DAO
::checkSqlFunctionsExist();
257 return CRM_Contact_BAO_Contact
::deleteObjectWithPrimary('Phone', $id);