Merge pull request #23210 from eileenmcnaughton/cancel
[civicrm-core.git] / CRM / Core / BAO / Phone.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
8eedd10a 19 * Class contains functions for phone.
6a488035
TO
20 */
21class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone {
1d3cbc3c 22 use CRM_Contact_AccessTrait;
6a488035 23
d424ffde 24 /**
6a488035
TO
25 * Create phone object - note that the create function calls 'add' but
26 * has more business logic
27 *
c490a46a 28 * @param array $params
b5c2afd0 29 *
3bec4854 30 * @return \CRM_Core_DAO_Phone
31 *
b5c2afd0 32 * @throws API_Exception
3bec4854 33 * @throws \CRM_Core_Exception
b5c2afd0 34 */
00be9182 35 public static function create($params) {
3bec4854 36 CRM_Core_BAO_Block::handlePrimary($params, get_class());
717c2903 37 return self::writeRecord($params);
6a488035
TO
38 }
39
40 /**
fe482240 41 * Takes an associative array and adds phone.
6a488035 42 *
717c2903 43 * @deprecated use create.
44 *
6a0b768e
TO
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
6a488035 47 *
a6c01b45
CW
48 * @return object
49 * CRM_Core_BAO_Phone object on success, null otherwise
717c2903 50 *
51 * @throws \API_Exception
52 * @throws \CRM_Core_Exception
6a488035 53 */
717c2903 54 public static function add($params) {
55 CRM_Core_Error::deprecatedFunctionWarning('Use the v4 api');
56 return self::create($params);
6a488035
TO
57 }
58
59 /**
60 * Given the list of params in the params array, fetch the object
61 * and store the values in the values array
62 *
6c552737 63 * @param array $entityBlock
6a488035 64 *
a6c01b45
CW
65 * @return array
66 * array of phone objects
3bec4854 67 * @throws \CRM_Core_Exception
6a488035 68 */
00be9182 69 public static function &getValues($entityBlock) {
6a488035
TO
70 $getValues = CRM_Core_BAO_Block::getValues('phone', $entityBlock);
71 return $getValues;
72 }
73
74 /**
75 * Get all the phone numbers for a specified contact_id, with the primary being first
76 *
6a0b768e
TO
77 * @param int $id
78 * The contact id.
dd244018 79 * @param bool $updateBlankLocInfo
3fd42bb5 80 * @param string|null $type
dd244018
EM
81 * @param array $filters
82 *
a6c01b45
CW
83 * @return array
84 * the array of phone ids which are potential numbers
6a488035 85 */
be2fb01f 86 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) {
6a488035
TO
87 if (!$id) {
88 return NULL;
89 }
90
91 $cond = NULL;
92 if ($type) {
7a59889d 93 $phoneTypeId = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Phone', 'phone_type_id', $type);
6a488035
TO
94 if ($phoneTypeId) {
95 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
96 }
97 }
98
99 if (!empty($filters) && is_array($filters)) {
100 foreach ($filters as $key => $value) {
101 $cond .= " AND " . $key . " = " . $value;
102 }
103 }
104
105 $query = "
106 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
107 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
108 civicrm_phone.phone_type_id as phoneTypeId
109 FROM civicrm_contact
110LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
111LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
112WHERE civicrm_contact.id = %1 $cond
113ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
114
be2fb01f
CW
115 $params = [
116 1 => [
6a488035
TO
117 $id,
118 'Integer',
be2fb01f
CW
119 ],
120 ];
6a488035 121
be2fb01f 122 $numbers = $values = [];
353ffa53
TO
123 $dao = CRM_Core_DAO::executeQuery($query, $params);
124 $count = 1;
6a488035 125 while ($dao->fetch()) {
be2fb01f 126 $values = [
6a488035
TO
127 'locationType' => $dao->locationType,
128 'is_primary' => $dao->is_primary,
129 'id' => $dao->phone_id,
130 'phone' => $dao->phone,
131 'locationTypeId' => $dao->locationTypeId,
132 'phoneTypeId' => $dao->phoneTypeId,
be2fb01f 133 ];
6a488035
TO
134
135 if ($updateBlankLocInfo) {
136 $numbers[$count++] = $values;
137 }
138 else {
139 $numbers[$dao->phone_id] = $values;
140 }
141 }
142 return $numbers;
143 }
144
145 /**
f47539f6 146 * Get all the phone numbers for a specified location_block id, with the primary phone being first.
147 *
148 * This is called from CRM_Core_BAO_Block as a calculated function.
6a488035 149 *
6a0b768e 150 * @param array $entityElements
3fd42bb5
BT
151 * The array containing entity_id and entity_table name
152 * @param string|null $type
da6b46f4 153 *
a6c01b45
CW
154 * @return array
155 * the array of phone ids which are potential numbers
6a488035 156 */
00be9182 157 public static function allEntityPhones($entityElements, $type = NULL) {
6a488035
TO
158 if (empty($entityElements)) {
159 return NULL;
160 }
161
162 $cond = NULL;
163 if ($type) {
b4f964d9 164 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
6a488035
TO
165 if ($phoneTypeId) {
166 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
167 }
168 }
169
170 $entityId = $entityElements['entity_id'];
171 $entityTable = $entityElements['entity_table'];
172
173 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
174 ph.id as phone_id, ph.location_type_id as locationTypeId
175FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
176WHERE ev.id = %1
177AND loc.id = ev.loc_block_id
178AND ph.id IN (loc.phone_id, loc.phone_2_id)
179AND ltype.id = ph.location_type_id
180ORDER BY ph.is_primary DESC, phone_id ASC ";
181
be2fb01f
CW
182 $params = [
183 1 => [
6a488035
TO
184 $entityId,
185 'Integer',
be2fb01f
CW
186 ],
187 ];
188 $numbers = [];
6a488035
TO
189 $dao = CRM_Core_DAO::executeQuery($sql, $params);
190 while ($dao->fetch()) {
be2fb01f 191 $numbers[$dao->phone_id] = [
6a488035
TO
192 'locationType' => $dao->locationType,
193 'is_primary' => $dao->is_primary,
194 'id' => $dao->phone_id,
195 'phone' => $dao->phone,
196 'locationTypeId' => $dao->locationTypeId,
be2fb01f 197 ];
6a488035
TO
198 }
199 return $numbers;
200 }
201
202 /**
203 * Set NULL to phone, mapping, uffield
204 *
a2f24340 205 * @param int $optionId
6a0b768e 206 * Value of option to be deleted.
6a488035 207 */
00be9182 208 public static function setOptionToNull($optionId) {
6a488035
TO
209 if (!$optionId) {
210 return;
211 }
212
be2fb01f 213 $tables = [
6a488035
TO
214 'civicrm_phone',
215 'civicrm_mapping_field',
216 'civicrm_uf_field',
be2fb01f
CW
217 ];
218 $params = [
219 1 => [
6a488035
TO
220 $optionId,
221 'Integer',
be2fb01f
CW
222 ],
223 ];
6a488035
TO
224
225 foreach ($tables as $tableName) {
226 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
227 CRM_Core_DAO::executeQuery($query, $params);
228 }
229 }
12445e1c
CW
230
231 /**
fe482240 232 * Call common delete function.
ea3ddccf 233 *
7a592535 234 * @see \CRM_Contact_BAO_Contact::on_hook_civicrm_post
ea3ddccf 235 *
7a592535
CW
236 * @param int $id
237 * @deprecated
ea3ddccf 238 * @return bool
12445e1c 239 */
00be9182 240 public static function del($id) {
7a592535 241 return (bool) self::deleteRecord(['id' => $id]);
12445e1c 242 }
96025800 243
6a488035 244}