Commit | Line | Data |
---|---|---|
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 | /** | |
f452d72c | 13 | * Add static functions to include some common functionality used across location sub object BAO classes. |
6a488035 TO |
14 | * |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
17 | */ |
18 | class CRM_Core_BAO_Block { | |
19 | ||
20 | /** | |
fe482240 | 21 | * Fields that are required for a valid block. |
518fa0ee | 22 | * @var array |
6a488035 | 23 | */ |
518fa0ee | 24 | public static $requiredBlockFields = [ |
be2fb01f CW |
25 | 'email' => ['email'], |
26 | 'phone' => ['phone'], | |
27 | 'im' => ['name'], | |
28 | 'openid' => ['openid'], | |
29 | ]; | |
6a488035 TO |
30 | |
31 | /** | |
32 | * Given the list of params in the params array, fetch the object | |
33 | * and store the values in the values array | |
34 | * | |
6a0b768e TO |
35 | * @param string $blockName |
36 | * Name of the above object. | |
37 | * @param array $params | |
38 | * Input parameters to find object. | |
77b97be7 | 39 | * |
a6c01b45 | 40 | * @return array |
16b10e64 | 41 | * Array of $block objects. |
ac15829d | 42 | * @throws CRM_Core_Exception |
6a488035 | 43 | */ |
00be9182 | 44 | public static function &getValues($blockName, $params) { |
6a488035 TO |
45 | if (empty($params)) { |
46 | return NULL; | |
47 | } | |
e52506b0 | 48 | $BAOString = 'CRM_Core_BAO_' . $blockName; |
481a74f4 | 49 | $block = new $BAOString(); |
6a488035 | 50 | |
be2fb01f | 51 | $blocks = []; |
6a488035 TO |
52 | if (!isset($params['entity_table'])) { |
53 | $block->contact_id = $params['contact_id']; | |
54 | if (!$block->contact_id) { | |
ac15829d | 55 | throw new CRM_Core_Exception('Invalid Contact ID parameter passed'); |
6a488035 TO |
56 | } |
57 | $blocks = self::retrieveBlock($block, $blockName); | |
58 | } | |
59 | else { | |
60 | $blockIds = self::getBlockIds($blockName, NULL, $params); | |
61 | ||
62 | if (empty($blockIds)) { | |
63 | return $blocks; | |
64 | } | |
65 | ||
66 | $count = 1; | |
67 | foreach ($blockIds as $blockId) { | |
481a74f4 | 68 | $block = new $BAOString(); |
353ffa53 TO |
69 | $block->id = $blockId['id']; |
70 | $getBlocks = self::retrieveBlock($block, $blockName); | |
6a488035 TO |
71 | $blocks[$count++] = array_pop($getBlocks); |
72 | } | |
73 | } | |
74 | ||
75 | return $blocks; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Given the list of params in the params array, fetch the object | |
80 | * and store the values in the values array | |
81 | * | |
6a0b768e TO |
82 | * @param Object $block |
83 | * Typically a Phone|Email|IM|OpenID object. | |
84 | * @param string $blockName | |
85 | * Name of the above object. | |
77b97be7 | 86 | * |
a6c01b45 | 87 | * @return array |
16b10e64 | 88 | * Array of $block objects. |
6a488035 | 89 | */ |
00be9182 | 90 | public static function retrieveBlock(&$block, $blockName) { |
6a488035 TO |
91 | // we first get the primary location due to the order by clause |
92 | $block->orderBy('is_primary desc, id'); | |
93 | $block->find(); | |
94 | ||
95 | $count = 1; | |
be2fb01f | 96 | $blocks = []; |
6a488035 TO |
97 | while ($block->fetch()) { |
98 | CRM_Core_DAO::storeValues($block, $blocks[$count]); | |
99 | //unset is_primary after first block. Due to some bug in earlier version | |
100 | //there might be more than one primary blocks, hence unset is_primary other than first | |
101 | if ($count > 1) { | |
102 | unset($blocks[$count]['is_primary']); | |
103 | } | |
104 | $count++; | |
105 | } | |
106 | ||
107 | return $blocks; | |
108 | } | |
109 | ||
110 | /** | |
fe482240 | 111 | * Check if the current block object has any valid data. |
6a488035 | 112 | * |
6a0b768e TO |
113 | * @param array $blockFields |
114 | * Array of fields that are of interest for this object. | |
115 | * @param array $params | |
116 | * Associated array of submitted fields. | |
6a488035 | 117 | * |
317fceb4 | 118 | * @return bool |
a6c01b45 | 119 | * true if the block has data, otherwise false |
6a488035 | 120 | */ |
00be9182 | 121 | public static function dataExists($blockFields, &$params) { |
6a488035 TO |
122 | foreach ($blockFields as $field) { |
123 | if (CRM_Utils_System::isNull(CRM_Utils_Array::value($field, $params))) { | |
124 | return FALSE; | |
125 | } | |
126 | } | |
127 | return TRUE; | |
128 | } | |
129 | ||
130 | /** | |
fe482240 | 131 | * Check if the current block exits. |
6a488035 | 132 | * |
6a0b768e | 133 | * @param string $blockName |
9ce8301b | 134 | * Block name. |
6a0b768e | 135 | * @param array $params |
9ce8301b | 136 | * Array of submitted fields. |
6a488035 | 137 | * |
317fceb4 | 138 | * @return bool |
9ce8301b | 139 | * true if the block is in the params and is an array |
6a488035 | 140 | */ |
9ce8301b | 141 | public static function blockExists($blockName, $params) { |
142 | return !empty($params[$blockName]) && is_array($params[$blockName]); | |
6a488035 TO |
143 | } |
144 | ||
145 | /** | |
fe482240 | 146 | * Get all block ids for a contact. |
6a488035 | 147 | * |
6a0b768e TO |
148 | * @param string $blockName |
149 | * Block name. | |
150 | * @param int $contactId | |
151 | * Contact id. | |
fd31fa4c EM |
152 | * |
153 | * @param null $entityElements | |
154 | * @param bool $updateBlankLocInfo | |
6a488035 | 155 | * |
a6c01b45 CW |
156 | * @return array |
157 | * formatted array of block ids | |
6a488035 | 158 | * |
6a488035 | 159 | */ |
00be9182 | 160 | public static function getBlockIds($blockName, $contactId = NULL, $entityElements = NULL, $updateBlankLocInfo = FALSE) { |
be2fb01f | 161 | $allBlocks = []; |
e4f5a5f9 | 162 | |
6a488035 TO |
163 | $name = ucfirst($blockName); |
164 | if ($blockName == 'im') { | |
165 | $name = 'IM'; | |
166 | } | |
167 | elseif ($blockName == 'openid') { | |
168 | $name = 'OpenID'; | |
169 | } | |
170 | ||
e4f5a5f9 | 171 | $baoString = 'CRM_Core_BAO_' . $name; |
6a488035 | 172 | if ($contactId) { |
e4f5a5f9 | 173 | //@todo a cleverer way to do this would be to use the same fn name on each |
174 | // BAO rather than constructing the fn | |
175 | // it would also be easier to grep for | |
176 | // e.g $bao = new $baoString; | |
177 | // $bao->getAllBlocks() | |
178 | $baoFunction = 'all' . $name . 's'; | |
481a74f4 | 179 | $allBlocks = $baoString::$baoFunction($contactId, $updateBlankLocInfo); |
6a488035 TO |
180 | } |
181 | elseif (!empty($entityElements) && $blockName != 'openid') { | |
e4f5a5f9 | 182 | $baoFunction = 'allEntity' . $name . 's'; |
481a74f4 | 183 | $allBlocks = $baoString::$baoFunction($entityElements); |
6a488035 TO |
184 | } |
185 | ||
186 | return $allBlocks; | |
187 | } | |
188 | ||
189 | /** | |
fe482240 | 190 | * Takes an associative array and creates a block. |
6a488035 | 191 | * |
6a0b768e TO |
192 | * @param string $blockName |
193 | * Block name. | |
194 | * @param array $params | |
349e79ad | 195 | * Array of name/value pairs. |
da6b46f4 | 196 | * |
349e79ad | 197 | * @return array|null |
198 | * Array of created location entities or NULL if none to create. | |
6a488035 | 199 | */ |
172dfebe | 200 | public static function create($blockName, $params) { |
6a488035 TO |
201 | if (!self::blockExists($blockName, $params)) { |
202 | return NULL; | |
203 | } | |
204 | ||
60fd90d8 | 205 | $name = ucfirst($blockName); |
60fd90d8 | 206 | $isPrimary = $isBilling = TRUE; |
be2fb01f | 207 | $entityElements = $blocks = []; |
a3b489b9 | 208 | $resetPrimaryId = NULL; |
209 | $primaryId = FALSE; | |
6a488035 | 210 | |
172dfebe | 211 | $contactId = $params['contact_id']; |
6a488035 | 212 | |
60fd90d8 | 213 | $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE); |
a3b489b9 | 214 | $isIdSet = CRM_Utils_Array::value('isIdSet', $params[$blockName], FALSE); |
7f660ef3 | 215 | |
85c882c5 | 216 | //get existing block ids. |
217 | $blockIds = self::getBlockIds($blockName, $contactId, $entityElements); | |
a3b489b9 | 218 | foreach ($params[$blockName] as $count => $value) { |
9c1bc317 | 219 | $blockId = $value['id'] ?? NULL; |
a3b489b9 | 220 | if ($blockId) { |
221 | if (is_array($blockIds) && array_key_exists($blockId, $blockIds)) { | |
222 | unset($blockIds[$blockId]); | |
223 | } | |
224 | else { | |
225 | unset($value['id']); | |
60fd90d8 | 226 | } |
a3b489b9 | 227 | } |
60fd90d8 | 228 | } |
4f31fe03 | 229 | $baoString = 'CRM_Core_BAO_' . $name; |
60fd90d8 DG |
230 | foreach ($params[$blockName] as $count => $value) { |
231 | if (!is_array($value)) { | |
232 | continue; | |
6a488035 | 233 | } |
a3b489b9 | 234 | // if in some cases (eg. email used in Online Conribution Page, Profiles, etc.) id is not set |
85c882c5 | 235 | // lets try to add using the previous method to avoid any false creation of existing data. |
236 | foreach ($blockIds as $blockId => $blockValue) { | |
a3b489b9 | 237 | if (empty($value['id']) && $blockValue['locationTypeId'] == CRM_Utils_Array::value('location_type_id', $value) && !$isIdSet) { |
238 | $valueId = FALSE; | |
239 | if ($blockName == 'phone') { | |
9c1bc317 | 240 | $phoneTypeBlockValue = $blockValue['phoneTypeId'] ?? NULL; |
a3b489b9 | 241 | if ($phoneTypeBlockValue == CRM_Utils_Array::value('phone_type_id', $value)) { |
242 | $valueId = TRUE; | |
243 | } | |
244 | } | |
245 | elseif ($blockName == 'im') { | |
9c1bc317 | 246 | $providerBlockValue = $blockValue['providerId'] ?? NULL; |
a3b489b9 | 247 | if (!empty($value['provider_id']) && $providerBlockValue == $value['provider_id']) { |
248 | $valueId = TRUE; | |
d0a860ed | 249 | } |
60fd90d8 DG |
250 | } |
251 | else { | |
a3b489b9 | 252 | $valueId = TRUE; |
253 | } | |
254 | if ($valueId) { | |
255 | $value['id'] = $blockValue['id']; | |
256 | if (!$primaryId && !empty($blockValue['is_primary'])) { | |
257 | $value['is_primary'] = $blockValue['is_primary']; | |
6a488035 | 258 | } |
a3b489b9 | 259 | break; |
6a488035 TO |
260 | } |
261 | } | |
262 | } | |
85c882c5 | 263 | $dataExists = self::dataExists(self::$requiredBlockFields[$blockName], $value); |
60fd90d8 DG |
264 | // Note there could be cases when block info already exist ($value[id] is set) for a contact/entity |
265 | // BUT info is not present at this time, and therefore we should be really careful when deleting the block. | |
266 | // $updateBlankLocInfo will help take appropriate decision. CRM-5969 | |
85c882c5 | 267 | if (!empty($value['id']) && !$dataExists && $updateBlankLocInfo) { |
60fd90d8 | 268 | //delete the existing record |
4f31fe03 | 269 | $baoString::del($value['id']); |
60fd90d8 DG |
270 | continue; |
271 | } | |
85c882c5 | 272 | elseif (!$dataExists) { |
60fd90d8 | 273 | continue; |
6a488035 | 274 | } |
be2fb01f | 275 | $contactFields = [ |
85c882c5 | 276 | 'contact_id' => $contactId, |
6b409353 | 277 | 'location_type_id' => $value['location_type_id'] ?? NULL, |
be2fb01f | 278 | ]; |
6a488035 | 279 | |
a3b489b9 | 280 | $contactFields['is_billing'] = 0; |
60fd90d8 DG |
281 | if ($isBilling && !empty($value['is_billing'])) { |
282 | $contactFields['is_billing'] = $value['is_billing']; | |
283 | $isBilling = FALSE; | |
284 | } | |
6a488035 | 285 | |
60fd90d8 | 286 | $blockFields = array_merge($value, $contactFields); |
9f8f650e | 287 | $blocks[] = $baoString::create($blockFields); |
60fd90d8 DG |
288 | } |
289 | ||
6a488035 TO |
290 | return $blocks; |
291 | } | |
292 | ||
293 | /** | |
fe482240 | 294 | * Delete block. |
4f31fe03 | 295 | * @deprecated - just call the BAO / api directly. |
6a488035 | 296 | * |
6a0b768e TO |
297 | * @param string $blockName |
298 | * Block name. | |
299 | * @param int $params | |
300 | * Associates array. | |
6a488035 | 301 | */ |
00be9182 | 302 | public static function blockDelete($blockName, $params) { |
6a488035 TO |
303 | $name = ucfirst($blockName); |
304 | if ($blockName == 'im') { | |
305 | $name = 'IM'; | |
306 | } | |
307 | elseif ($blockName == 'openid') { | |
308 | $name = 'OpenID'; | |
309 | } | |
310 | ||
5e559a23 | 311 | $baoString = 'CRM_Core_BAO_' . $name; |
312 | $baoString::del($params['id']); | |
6a488035 TO |
313 | } |
314 | ||
315 | /** | |
316 | * Handling for is_primary. | |
317 | * $params is_primary could be | |
318 | * # 1 - find other entries with is_primary = 1 & reset them to 0 | |
319 | * # 0 - make sure at least one entry is set to 1 | |
320 | * - if no other entry is 1 change to 1 | |
321 | * - if one other entry exists change that to 1 | |
322 | * - if more than one other entry exists change first one to 1 | |
77b97be7 | 323 | * @fixme - perhaps should choose by location_type |
6a488035 TO |
324 | * # empty - same as 0 as once we have checked first step |
325 | * we know if it should be 1 or 0 | |
326 | * | |
327 | * if $params['id'] is set $params['contact_id'] may need to be retrieved | |
328 | * | |
77b97be7 EM |
329 | * @param array $params |
330 | * @param $class | |
331 | * | |
332 | * @throws API_Exception | |
6a488035 TO |
333 | */ |
334 | public static function handlePrimary(&$params, $class) { | |
3bec4854 | 335 | if (isset($params['id']) && CRM_Utils_System::isNull($params['is_primary'] ?? NULL)) { |
336 | // if id is set & is_primary isn't we can assume no change) | |
337 | return; | |
338 | } | |
37a77d9c TO |
339 | $table = CRM_Core_DAO_AllCoreTables::getTableForClass($class); |
340 | if (!$table) { | |
341 | throw new API_Exception("Failed to locate table for class [$class]"); | |
342 | } | |
d195b60c | 343 | |
50fa40e8 | 344 | // contact_id in params might be empty or the string 'null' so cast to integer |
2975f0aa | 345 | $contactId = (int) ($params['contact_id'] ?? 0); |
50fa40e8 CW |
346 | // If id is set & we haven't been passed a contact_id, retrieve it |
347 | if (!empty($params['id']) && !isset($params['contact_id'])) { | |
6a488035 TO |
348 | $entity = new $class(); |
349 | $entity->id = $params['id']; | |
350 | $entity->find(TRUE); | |
da5e820b | 351 | $contactId = $params['contact_id'] = $entity->contact_id; |
6a488035 | 352 | } |
50fa40e8 CW |
353 | // If entity is not associated with contact, concept of is_primary not relevant |
354 | if (!$contactId) { | |
6a488035 TO |
355 | return; |
356 | } | |
357 | ||
358 | // if params is_primary then set all others to not be primary & exit out | |
57f8e7f0 | 359 | // if is_primary = 1 |
a7488080 | 360 | if (!empty($params['is_primary'])) { |
6a488035 | 361 | $sql = "UPDATE $table SET is_primary = 0 WHERE contact_id = %1"; |
be2fb01f | 362 | $sqlParams = [1 => [$contactId, 'Integer']]; |
b44e3f84 | 363 | // we don't want to create unnecessary entries in the log_ tables so exclude the one we are working on |
9b873358 | 364 | if (!empty($params['id'])) { |
6a488035 | 365 | $sql .= " AND id <> %2"; |
be2fb01f | 366 | $sqlParams[2] = [$params['id'], 'Integer']; |
6a488035 TO |
367 | } |
368 | CRM_Core_DAO::executeQuery($sql, $sqlParams); | |
369 | return; | |
370 | } | |
371 | ||
372 | //Check what other emails exist for the contact | |
373 | $existingEntities = new $class(); | |
374 | $existingEntities->contact_id = $contactId; | |
375 | $existingEntities->orderBy('is_primary DESC'); | |
376 | if (!$existingEntities->find(TRUE) || (!empty($params['id']) && $existingEntities->id == $params['id'])) { | |
377 | // ie. if no others is set to be primary then this has to be primary set to 1 so change | |
378 | $params['is_primary'] = 1; | |
379 | return; | |
380 | } | |
381 | else { | |
382 | /* | |
383 | * If the only existing email is the one we are editing then we must set | |
384 | * is_primary to 1 | |
0e480632 | 385 | * @see https://issues.civicrm.org/jira/browse/CRM-10451 |
6a488035 | 386 | */ |
481a74f4 | 387 | if ($existingEntities->N == 1 && $existingEntities->id == CRM_Utils_Array::value('id', $params)) { |
6a488035 TO |
388 | $params['is_primary'] = 1; |
389 | return; | |
390 | } | |
391 | ||
392 | if ($existingEntities->is_primary == 1) { | |
393 | return; | |
394 | } | |
395 | // so at this point we are only dealing with ones explicity setting is_primary to 0 | |
396 | // since we have reverse sorted by email we can either set the first one to | |
397 | // primary or return if is already is | |
398 | $existingEntities->is_primary = 1; | |
399 | $existingEntities->save(); | |
da5e820b CW |
400 | if ($class === 'CRM_Core_BAO_Email') { |
401 | CRM_Core_BAO_Email::updateContactName($contactId, $existingEntities->email); | |
402 | } | |
6a488035 TO |
403 | } |
404 | } | |
405 | ||
406 | /** | |
fe482240 | 407 | * Sort location array so primary element is first. |
2a6da8d7 | 408 | * |
c490a46a | 409 | * @param array $locations |
6a488035 | 410 | */ |
9b873358 | 411 | public static function sortPrimaryFirst(&$locations) { |
6a488035 TO |
412 | uasort($locations, 'self::primaryComparison'); |
413 | } | |
414 | ||
2aa397bc TO |
415 | /** |
416 | * compare 2 locations to see which should go first based on is_primary | |
417 | * (sort function for sortPrimaryFirst) | |
418 | * @param array $location1 | |
419 | * @param array $location2 | |
317fceb4 | 420 | * @return int |
2aa397bc | 421 | */ |
9b873358 | 422 | public static function primaryComparison($location1, $location2) { |
9c1bc317 CW |
423 | $l1 = $location1['is_primary'] ?? NULL; |
424 | $l2 = $location2['is_primary'] ?? NULL; | |
6a488035 TO |
425 | if ($l1 == $l2) { |
426 | return 0; | |
427 | } | |
428 | return ($l1 < $l2) ? -1 : 1; | |
429 | } | |
96025800 | 430 | |
6a488035 | 431 | } |