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