Skip to content

Commit

Permalink
ManagedEntities - Always delete managed record when deleting an entity
Browse files Browse the repository at this point in the history
This uses hooks to ensure managed records are always cleared out when an entity is deleted.
Fixes OptionValue::delete which was previously not calling hooks.
  • Loading branch information
colemanw committed Nov 6, 2021
1 parent 1ce6ee5 commit ad95c79
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
46 changes: 46 additions & 0 deletions CRM/Core/BAO/Managed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

/**
* This class contains functions for managed entities.
*/
class CRM_Core_BAO_Managed extends CRM_Core_DAO_Managed implements Civi\Test\HookInterface {

/**
* Callback for hook_civicrm_post().
* @param \Civi\Core\Event\PostEvent $event
*/
public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) {
// When an entity is deleted, delete the corresponding Managed record
if ($event->action === 'delete' && $event->id && $event->entity !== 'Managed') {
$entityName = [$event->entity];
// Legacy support for strangely-named v3 entities
if ($event->entity === 'OptionValue') {
$entityName[] = 'CustomSearch';
$entityName[] = 'ActivityType';
}
if (in_array($event->entity, ['Individual', 'Household', 'Organization'], TRUE)) {
$entityName[] = 'Contact';
}
\Civi\Api4\Managed::delete(FALSE)
->addWhere('entity_type', 'IN', $entityName)
->addWhere('entity_id', '=', $event->id)
->execute();
}
}

}
3 changes: 3 additions & 0 deletions CRM/Core/BAO/OptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,12 @@ public static function del($optionValueId) {
if (!$optionValue->find()) {
return FALSE;
}
$hookParams = ['id' => $optionValueId];
CRM_Utils_Hook::pre('delete', 'OptionValue', $optionValueId, $hookParams);
if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) {
CRM_Core_PseudoConstant::flush();
$optionValue->delete();
CRM_Utils_Hook::post('delete', 'OptionValue', $optionValueId, $optionValue);
return TRUE;
}
return FALSE;
Expand Down
5 changes: 1 addition & 4 deletions CRM/Core/ManagedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ protected function removeStaleEntity($dao) {
'id' => $dao->entity_id,
];
$check = civicrm_api3($dao->entity_type, 'get', $params);
if ((bool) $check['count']) {
if ($check['count']) {
$result = civicrm_api($dao->entity_type, 'delete', $params);
if ($result['is_error']) {
if (isset($dao->name)) {
Expand All @@ -454,9 +454,6 @@ protected function removeStaleEntity($dao) {
$this->onApiError($dao->entity_type, 'delete', $params, $result);
}
}
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', [
1 => [$dao->id, 'Integer'],
]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Core/ManagedEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ public function testRemoveDeclaration_CleanupNever(): void {

/**
* Set up an active module with one managed-entity using the
* policy "cleanup=>never". When the managed-entity goes away,
* ensure that the policy is followed (ie the entity is not
* policy "cleanup=>unused". When the managed-entity goes away,
* ensure that the policy is followed (ie the entity is conditionally
* deleted).
*
* @throws \CRM_Core_Exception
Expand Down

0 comments on commit ad95c79

Please sign in to comment.