Implement _checkAccess for Contact BAO and related entities (email, phone, etc.)
[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) {
aca2de91
CW
36 // Ensure mysql phone function exists
37 CRM_Core_DAO::checkSqlFunctionsExist();
3bec4854 38 CRM_Core_BAO_Block::handlePrimary($params, get_class());
717c2903 39 return self::writeRecord($params);
6a488035
TO
40 }
41
42 /**
fe482240 43 * Takes an associative array and adds phone.
6a488035 44 *
717c2903 45 * @deprecated use create.
46 *
6a0b768e
TO
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
6a488035 49 *
a6c01b45
CW
50 * @return object
51 * CRM_Core_BAO_Phone object on success, null otherwise
717c2903 52 *
53 * @throws \API_Exception
54 * @throws \CRM_Core_Exception
6a488035 55 */
717c2903 56 public static function add($params) {
57 CRM_Core_Error::deprecatedFunctionWarning('Use the v4 api');
58 return self::create($params);
6a488035
TO
59 }
60
61 /**
62 * Given the list of params in the params array, fetch the object
63 * and store the values in the values array
64 *
6c552737 65 * @param array $entityBlock
6a488035 66 *
a6c01b45
CW
67 * @return array
68 * array of phone objects
3bec4854 69 * @throws \CRM_Core_Exception
6a488035 70 */
00be9182 71 public static function &getValues($entityBlock) {
6a488035
TO
72 $getValues = CRM_Core_BAO_Block::getValues('phone', $entityBlock);
73 return $getValues;
74 }
75
76 /**
77 * Get all the phone numbers for a specified contact_id, with the primary being first
78 *
6a0b768e
TO
79 * @param int $id
80 * The contact id.
6a488035 81 *
dd244018
EM
82 * @param bool $updateBlankLocInfo
83 * @param null $type
84 * @param array $filters
85 *
a6c01b45
CW
86 * @return array
87 * the array of phone ids which are potential numbers
6a488035 88 */
be2fb01f 89 public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = []) {
6a488035
TO
90 if (!$id) {
91 return NULL;
92 }
93
94 $cond = NULL;
95 if ($type) {
7a59889d 96 $phoneTypeId = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Phone', 'phone_type_id', $type);
6a488035
TO
97 if ($phoneTypeId) {
98 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
99 }
100 }
101
102 if (!empty($filters) && is_array($filters)) {
103 foreach ($filters as $key => $value) {
104 $cond .= " AND " . $key . " = " . $value;
105 }
106 }
107
108 $query = "
109 SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary,
110 civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId,
111 civicrm_phone.phone_type_id as phoneTypeId
112 FROM civicrm_contact
113LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id )
114LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id )
115WHERE civicrm_contact.id = %1 $cond
116ORDER BY civicrm_phone.is_primary DESC, phone_id ASC ";
117
be2fb01f
CW
118 $params = [
119 1 => [
6a488035
TO
120 $id,
121 'Integer',
be2fb01f
CW
122 ],
123 ];
6a488035 124
be2fb01f 125 $numbers = $values = [];
353ffa53
TO
126 $dao = CRM_Core_DAO::executeQuery($query, $params);
127 $count = 1;
6a488035 128 while ($dao->fetch()) {
be2fb01f 129 $values = [
6a488035
TO
130 'locationType' => $dao->locationType,
131 'is_primary' => $dao->is_primary,
132 'id' => $dao->phone_id,
133 'phone' => $dao->phone,
134 'locationTypeId' => $dao->locationTypeId,
135 'phoneTypeId' => $dao->phoneTypeId,
be2fb01f 136 ];
6a488035
TO
137
138 if ($updateBlankLocInfo) {
139 $numbers[$count++] = $values;
140 }
141 else {
142 $numbers[$dao->phone_id] = $values;
143 }
144 }
145 return $numbers;
146 }
147
148 /**
f47539f6 149 * Get all the phone numbers for a specified location_block id, with the primary phone being first.
150 *
151 * This is called from CRM_Core_BAO_Block as a calculated function.
6a488035 152 *
6a0b768e
TO
153 * @param array $entityElements
154 * The array containing entity_id and.
16b10e64 155 * entity_table name
6a488035 156 *
da6b46f4
EM
157 * @param null $type
158 *
a6c01b45
CW
159 * @return array
160 * the array of phone ids which are potential numbers
6a488035 161 */
00be9182 162 public static function allEntityPhones($entityElements, $type = NULL) {
6a488035
TO
163 if (empty($entityElements)) {
164 return NULL;
165 }
166
167 $cond = NULL;
168 if ($type) {
b4f964d9 169 $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'));
6a488035
TO
170 if ($phoneTypeId) {
171 $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId";
172 }
173 }
174
175 $entityId = $entityElements['entity_id'];
176 $entityTable = $entityElements['entity_table'];
177
178 $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary,
179 ph.id as phone_id, ph.location_type_id as locationTypeId
180FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev
181WHERE ev.id = %1
182AND loc.id = ev.loc_block_id
183AND ph.id IN (loc.phone_id, loc.phone_2_id)
184AND ltype.id = ph.location_type_id
185ORDER BY ph.is_primary DESC, phone_id ASC ";
186
be2fb01f
CW
187 $params = [
188 1 => [
6a488035
TO
189 $entityId,
190 'Integer',
be2fb01f
CW
191 ],
192 ];
193 $numbers = [];
6a488035
TO
194 $dao = CRM_Core_DAO::executeQuery($sql, $params);
195 while ($dao->fetch()) {
be2fb01f 196 $numbers[$dao->phone_id] = [
6a488035
TO
197 'locationType' => $dao->locationType,
198 'is_primary' => $dao->is_primary,
199 'id' => $dao->phone_id,
200 'phone' => $dao->phone,
201 'locationTypeId' => $dao->locationTypeId,
be2fb01f 202 ];
6a488035
TO
203 }
204 return $numbers;
205 }
206
207 /**
208 * Set NULL to phone, mapping, uffield
209 *
6a0b768e
TO
210 * @param $optionId
211 * Value of option to be deleted.
6a488035 212 */
00be9182 213 public static function setOptionToNull($optionId) {
6a488035
TO
214 if (!$optionId) {
215 return;
216 }
aca2de91
CW
217 // Ensure mysql phone function exists
218 CRM_Core_DAO::checkSqlFunctionsExist();
6a488035 219
be2fb01f 220 $tables = [
6a488035
TO
221 'civicrm_phone',
222 'civicrm_mapping_field',
223 'civicrm_uf_field',
be2fb01f
CW
224 ];
225 $params = [
226 1 => [
6a488035
TO
227 $optionId,
228 'Integer',
be2fb01f
CW
229 ],
230 ];
6a488035
TO
231
232 foreach ($tables as $tableName) {
233 $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1";
234 CRM_Core_DAO::executeQuery($query, $params);
235 }
236 }
12445e1c
CW
237
238 /**
fe482240 239 * Call common delete function.
ea3ddccf 240 *
241 * @param int $id
242 *
243 * @return bool
12445e1c 244 */
00be9182 245 public static function del($id) {
aca2de91
CW
246 // Ensure mysql phone function exists
247 CRM_Core_DAO::checkSqlFunctionsExist();
a65e2e55 248 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id);
12445e1c 249 }
96025800 250
6a488035 251}