You are here

crm_core_match.module in CRM Core 7

Manages matching engines for identifying duplicate contacts in CRM Core. Allows CRM Core to install, enable and disable matching engines.

A matching engine is used to do the following:

  • identify duplicate contact records
  • inject data into contact records being saved
  • carry out other scripted operations not easily handled in the normal

bootstrapping process.

This module does not define any matching engines itself, it only provides the framework through which they can be applied to new contacts being created in the database.

@TODO: add hooks for altering the submission process

File

modules/crm_core_match/crm_core_match.module
View source
<?php

/**
 * @file
 * Manages matching engines for identifying duplicate contacts in CRM Core.
 * Allows CRM Core to install, enable and disable matching engines.
 *
 * A matching engine is used to do the following:
 *
 * - identify duplicate contact records
 * - inject data into contact records being saved
 * - carry out other scripted operations not easily handled in the normal
 * bootstrapping process.
 *
 * This module does not define any matching engines itself, it only provides the
 * framework through which they can be applied to new contacts being created in
 * the database.
 *
 * @TODO: add hooks for altering the submission process
 */

/**
 * Implements hook_crm_core_contact_match().
 */
function crm_core_match_crm_core_contact_match($values) {

  // Get a list of all matching engines for a contact.
  $engines =& drupal_static(__FUNCTION__);
  if (!is_array($engines)) {
    $engines = crm_core_match_get_engines();
  }

  // Go through each and look for matches.
  $matches = array();
  if (count($engines) > 0) {
    foreach ($engines as $engine) {
      $engine
        ->execute($values['contact'], $matches);
    }
  }
  $values['matches'] = array_merge($values['matches'], $matches);
}

/**
 * Implements hook_hook_info().
 */
function crm_core_match_hook_info() {
  $hooks = array(
    'crm_core_match_engine_register' => array(
      'group' => 'crm_core_match',
    ),
  );
  return $hooks;
}

/**
 * Implements hook_menu().
 */
function crm_core_match_menu() {
  $items['admin/config/crm-core/match'] = array(
    'title' => 'Matching Engines',
    'description' => 'Configure the default rules for matching duplicate contacts in CRM Core.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'crm_core_match_admin_config_engines_form',
    ),
    'access arguments' => array(
      'administer matching engines',
    ),
    'file' => 'crm_core_match.admin.inc',
  );
  $items['admin/config/crm-core/match/engines'] = array(
    'title' => 'Engines',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/crm-core/match/engines/%/%'] = array(
    'title' => 'Engine status toggle callback',
    'type' => MENU_CALLBACK,
    'page callback' => 'crm_core_match_engine_status_toggle',
    'page arguments' => array(
      5,
      6,
    ),
    'access arguments' => array(
      'administer matching engines',
    ),
  );
  $items['crm-core/crm-core-match/check-rules/%crm_core_contact'] = array(
    'page callback' => 'crm_core_match_testing_page',
    'page arguments' => array(
      3,
    ),
    'title callback' => 'crm_core_match_testing_page_title',
    'title arguments' => array(
      3,
    ),
    'access arguments' => array(
      'view match information',
    ),
    'type' => MENU_SUGGESTED_ITEM,
    'file' => 'crm_core_match.test.inc',
  );
  $items['crm-core/crm-core-match/info'] = array(
    'page callback' => 'crm_core_match_info_page',
    'title' => 'CRM Core Match info',
    'access arguments' => array(
      'view matching engine rules configuration',
    ),
    'type' => MENU_SUGGESTED_ITEM,
    'file' => 'crm_core_match.test.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function crm_core_match_theme() {
  return array(
    'crm_core_match_admin_config_engines_form' => array(
      'render element' => 'form',
      'file' => 'crm_core_match.admin.inc',
    ),
  );
}

/**
 * Returns a list of all matching engines registered with CRM Core Match.
 *
 * @return array
 *   A list of matching engines for CRM Core Match.
 */
function crm_core_match_get_engines() {
  $cache =& drupal_static(__FUNCTION__);
  if (empty($cache)) {
    $engines = array();
    $stored_engines = db_select('crm_core_match_engines')
      ->fields('crm_core_match_engines')
      ->execute()
      ->fetchAllAssoc('machine_name');
    foreach (module_implements('crm_core_match_engine_register') as $module) {
      $function = $module . '_crm_core_match_engine_register';
      $engine = $function();
      if (isset($stored_engines[$engine
        ->getMachineName()])) {
        $engine
          ->setStatus($stored_engines[$engine
          ->getMachineName()]->status);
        $engine
          ->setWeight($stored_engines[$engine
          ->getMachineName()]->weight);
        $engine
          ->setID($stored_engines[$engine
          ->getMachineName()]->eid);
      }
      else {
        $engine
          ->setStatus(0);
        $engine
          ->setWeight(10);
        $engine
          ->setID(0);
      }
      $engines[] = $engine;
    }
    uasort($engines, '_crm_core_match_engine_weight_cmp');
    $cache = $engines;
  }
  return $cache;
}

/**
 * Page callback to toggle matching engine status.
 */
function crm_core_match_engine_status_toggle($engine_machine_name, $to_status) {
  $status = NULL;
  switch ($to_status) {
    case 'enable':
      $status = 1;
      break;
    case 'disable':
      $status = 0;
      break;
  }
  if (isset($status)) {
    db_merge('crm_core_match_engines')
      ->condition('machine_name', $engine_machine_name)
      ->fields(array(
      'status' => $status,
      'machine_name' => $engine_machine_name,
    ))
      ->execute();
    drupal_set_message(t('Status changed successfully.'));
  }
  $destination = drupal_get_destination();
  drupal_goto($destination['destination']);
}

/**
 * Helper function for engines weight comparison.
 */
function _crm_core_match_engine_weight_cmp($a, $b) {
  if ($a
    ->getWeight() == $b
    ->getWeight()) {
    return 0;
  }
  return $a
    ->getWeight() < $b
    ->getWeight() ? -1 : 1;
}

/**
 * Implements hook_permission().
 */
function crm_core_match_permission() {
  return array(
    'administer matching engines' => array(
      'title' => t('Administer matching engines'),
    ),
    'view matching engine rules configuration' => array(
      'title' => t('View matching engine rules configuration'),
    ),
    'view match information' => array(
      'title' => t('View match information'),
    ),
  );
}

Functions

Namesort descending Description
crm_core_match_crm_core_contact_match Implements hook_crm_core_contact_match().
crm_core_match_engine_status_toggle Page callback to toggle matching engine status.
crm_core_match_get_engines Returns a list of all matching engines registered with CRM Core Match.
crm_core_match_hook_info Implements hook_hook_info().
crm_core_match_menu Implements hook_menu().
crm_core_match_permission Implements hook_permission().
crm_core_match_theme Implements hook_theme().
_crm_core_match_engine_weight_cmp Helper function for engines weight comparison.