Skip to content

Commit

Permalink
dev/core#667 Fix bug where entity_tags are not deleted by 'soft FK'
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Mar 20, 2020
1 parent 6247b60 commit 4c40c02
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,8 @@ public static function deleteContact($id, $restore = FALSE, $skipUndelete = FALS
$transaction = new CRM_Core_Transaction();

if ($skipUndelete) {
CRM_Utils_Hook::pre('delete', $contactType, $id, CRM_Core_DAO::$_nullArray);
$hookParams = ['check_permissions' => $checkPermissions];
CRM_Utils_Hook::pre('delete', $contactType, $id, $hookParams);

//delete billing address if exists.
CRM_Contribute_BAO_Contribution::deleteAddress(NULL, $id);
Expand Down
36 changes: 36 additions & 0 deletions CRM/Core/BAO/EntityTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,40 @@ public static function buildOptions($fieldName, $context = NULL, $props = []) {
return $options;
}

/**
* This function deletes entity tags when a related entity is called.
*
* It is registered as a listener in \Civi\Core\Container::createEventDispatcher
*
* Ideally we will later nuance it so it's only called on delete....
*
* @param \Civi\Core\DAO\Event\PreDelete $event
*/
public static function preDeleteOtherEntity($event) {
if (
$event->object instanceof CRM_Core_DAO_EntityTag
// Activity can call the pre hook for delete with no ID - this seems to be isolated to activity....
// @todo - what is the correct way to standardise activity delete?
|| ($event->object instanceof CRM_Activity_DAO_Activity && !$event->object->id)

) {
return;
}
// This is probably fairly mild in terms of helping performance - a case could be made to check if tags
// exist before deleting (further down) as delete is a locking action.
$entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($event->object));
if (!isset(Civi::$statics[__CLASS__]['tagged_entities'][$entity])) {
$tableName = CRM_Core_DAO_AllCoreTables::getTableForEntityName($entity);
$used_for = CRM_Core_OptionGroup::values('tag_used_for');
Civi::$statics[__CLASS__]['tagged_entities'][$entity] = !empty($used_for[$tableName]) ? $tableName : FALSE;
}

if (Civi::$statics[__CLASS__]['tagged_entities'][$entity]) {
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_entity_tag WHERE entity_table = %1 AND entity_id = %2',
[1 => [Civi::$statics[__CLASS__]['tagged_entities'][$entity], 'String'], 2 => [$event->object->id, 'Integer']]
);
}

}

}
11 changes: 11 additions & 0 deletions CRM/Core/DAO/AllCoreTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ public static function getTableForClass($className) {
self::tables());
}

/**
* Convert the entity name into a table name.
*
* @param string $entityBriefName
*
* @return FALSE|string
*/
public static function getTableForEntityName($entityBriefName) {
return self::getTableForClass(self::getFullName($entityBriefName));
}

/**
* Reinitialise cache.
*
Expand Down
1 change: 1 addition & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public function createEventDispatcher($container) {
$dispatcher->addListener(SystemInstallEvent::EVENT_NAME, ['\Civi\Core\LocalizationInitializer', 'initialize']);
$dispatcher->addListener('hook_civicrm_post', ['\CRM_Core_Transaction', 'addPostCommit'], -1000);
$dispatcher->addListener('hook_civicrm_pre', ['\Civi\Core\Event\PreEvent', 'dispatchSubevent'], 100);
$dispatcher->addListener('civi.dao.preDelete', ['\CRM_Core_BAO_EntityTag', 'preDeleteOtherEntity']);
$dispatcher->addListener('hook_civicrm_post', ['\Civi\Core\Event\PostEvent', 'dispatchSubevent'], 100);
$dispatcher->addListener('hook_civicrm_post::Activity', ['\Civi\CCase\Events', 'fireCaseChange']);
$dispatcher->addListener('hook_civicrm_post::Case', ['\Civi\CCase\Events', 'fireCaseChange']);
Expand Down

0 comments on commit 4c40c02

Please sign in to comment.