Skip to content

Commit

Permalink
[#13] added X-Dedupe resolvers (merged 'origin/dev_14' [sic])
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Jan 20, 2022
2 parents a234aaa + ce5cb40 commit 78dbf4b
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
28 changes: 28 additions & 0 deletions CRM/Moregreetings/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@
*/
class CRM_Moregreetings_Renderer {

/** @var array list of contact ids that should be excluded from updating */
protected static $excluded_contact_ids = [];

/**
* Re-calculate the more-greetings for one contact
*/
public static function updateMoreGreetings($contact_id, $contact = NULL) {
// check exclusion list
if (in_array($contact_id, self::$excluded_contact_ids)) {
return;
}

// load the templates
$templates = CRM_Core_BAO_Setting::getItem('moregreetings', 'moregreetings_templates');
if (!is_array($templates)) {
Expand Down Expand Up @@ -168,4 +176,24 @@ public static function getUsedContactFields($templates) {

return implode(',', array_keys($fields_used));
}

/**
* Add a list of contact IDs to the exclusion list
*
* @param array $excluded_contact_ids
* list of contact IDs to be excluded from rendering
*/
public static function addExcludedContactIDs($excluded_contact_ids) {
self::$excluded_contact_ids = array_merge(self::$excluded_contact_ids, $excluded_contact_ids);
}

/**
* Clear the list of contact_ids to be excluded from rendering
*
* @return array
* previously set list of contact IDs
*/
public static function clearExcludedContactIDs() {
self::$excluded_contact_ids = [];
}
}
76 changes: 76 additions & 0 deletions CRM/Xdedupe/Resolver/MoreGreetings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/*-------------------------------------------------------+
| SYSTOPIA - MORE GREETINGS EXTENSION |
| Copyright (C) 2019 SYSTOPIA |
| Author: B. Endres (endres@systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

use Civi\API\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use \Civi\Core\Event\GenericHookEvent;

/**
* Implements a resolver to resolve conflicts in the MoreGreetings fields
*/
class CRM_Xdedupe_Resolver_MoreGreetings extends CRM_Xdedupe_Resolver {

/**
* get the name of the finder
* @return string name
*/
public function getName() {
return ts("More Greetings", array('domain' => 'de.systopia.moregreetings'));
}

/**
* get an explanation what the finder does
* @return string name
*/
public function getHelp() {
return ts("Will make sure differing MoreGreetings values will not get in the way, by dropping them before the merge and recalculating them after.", array('domain' => 'de.systopia.moregreetings'));
}

/**
* Resolve the problem by simply deleting the records for all contacts
*
* @param $main_contact_id int the main contact ID
* @param $other_contact_ids array other contact IDs
* @return boolean TRUE, if there was a conflict to be resolved
* @throws Exception if the conflict couldn't be resolved
*/
public function resolve($main_contact_id, $other_contact_ids) {
$all_contact_ids = array_merge($other_contact_ids, [$main_contact_id]);
if (!empty($all_contact_ids)) {
$all_contact_ids_list = implode(',', $all_contact_ids);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_value_moregreetings WHERE entity_id IN ({$all_contact_ids_list})");

// suppress new generation
CRM_Moregreetings_Renderer::addExcludedContactIDs($all_contact_ids);
}

return TRUE;
}

/**
* simply re-calculate the main contact
*
* @param $main_contact_id int the main contact ID
* @param $other_contact_ids array other contact IDs
* @throws Exception if the conflict couldn't be resolved
*/
public function postProcess($main_contact_id, $other_contact_ids) {
CRM_Moregreetings_Renderer::clearExcludedContactIDs();
CRM_Moregreetings_Renderer::updateMoreGreetings($main_contact_id);
// todo: other contacts, too? no, right!?
}

}
42 changes: 42 additions & 0 deletions CRM/Xdedupe/Resolver/MoreGreetingsSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/*-------------------------------------------------------+
| SYSTOPIA - MORE GREETINGS EXTENSION |
| Copyright (C) 2019 SYSTOPIA |
| Author: B. Endres (endres@systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/


use Civi\API\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use \Civi\Core\Event\GenericHookEvent;

/**
* Subscribe to XDedupe events, so we can register our resolver
*/
class CRM_Xdedupe_Resolver_MoreGreetingsSubscriber implements EventSubscriberInterface {

/**
* Subscribe to the list events, so we can plug the built-in ones
*/
public static function getSubscribedEvents() {
return [
'civi.xdedupe.resolvers' => ['addBuiltinResolvers', Events::W_MIDDLE],
];
}

/**
* Return the list of built-in resolvers
*/
public function addBuiltinResolvers(GenericHookEvent $xdedupe_list) {
$xdedupe_list->list[] = 'CRM_Xdedupe_Resolver_MoreGreetings';
}
}
3 changes: 3 additions & 0 deletions moregreetings.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function moregreetings_civicrm_enable() {
*/
function moregreetings_civicrm_config(&$config) {
_moregreetings_civix_civicrm_config($config);

require_once 'CRM/Xdedupe/Resolver/MoreGreetingsSubscriber.php';
\Civi::dispatcher()->addSubscriber(new CRM_Xdedupe_Resolver_MoreGreetingsSubscriber());
}

/**
Expand Down

0 comments on commit 78dbf4b

Please sign in to comment.