/**
  * Pre processing.
  *
  * @return void
  */
 public function preProcess()
 {
     // Ensure user has permission to be here
     if (!CRM_Core_Permission::check('administer dedupe rules')) {
         CRM_Utils_System::permissionDenied();
         CRM_Utils_System::civiExit();
     }
     $this->_options = CRM_Core_SelectValues::getDedupeRuleTypes();
     $this->_rgid = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
     $this->_contactType = CRM_Utils_Request::retrieve('contact_type', 'String', $this, FALSE, 0);
     if ($this->_rgid) {
         $rgDao = new CRM_Dedupe_DAO_RuleGroup();
         $rgDao->id = $this->_rgid;
         $rgDao->find(TRUE);
         $this->_defaults['threshold'] = $rgDao->threshold;
         $this->_contactType = $rgDao->contact_type;
         $this->_defaults['used'] = CRM_Utils_Array::key($rgDao->used, $this->_options);
         $this->_defaults['title'] = $rgDao->title;
         $this->_defaults['name'] = $rgDao->name;
         $this->_defaults['is_reserved'] = $rgDao->is_reserved;
         $this->assign('isReserved', $rgDao->is_reserved);
         $this->assign('ruleName', $rgDao->name);
         $ruleDao = new CRM_Dedupe_DAO_Rule();
         $ruleDao->dedupe_rule_group_id = $this->_rgid;
         $ruleDao->find();
         $count = 0;
         while ($ruleDao->fetch()) {
             $this->_defaults["where_{$count}"] = "{$ruleDao->rule_table}.{$ruleDao->rule_field}";
             $this->_defaults["length_{$count}"] = $ruleDao->rule_length;
             $this->_defaults["weight_{$count}"] = $ruleDao->rule_weight;
             $count++;
         }
     }
     $supported = CRM_Dedupe_BAO_RuleGroup::supportedFields($this->_contactType);
     if (is_array($supported)) {
         foreach ($supported as $table => $fields) {
             foreach ($fields as $field => $title) {
                 $this->_fields["{$table}.{$field}"] = $title;
             }
         }
     }
     asort($this->_fields);
 }
 /**
  * Function to pre processing
  *
  * @return None
  * @access public
  */
 function preProcess()
 {
     // Ensure user has permission to be here
     require_once 'CRM/Core/Permission.php';
     if (!CRM_Core_Permission::check('administer dedupe rules')) {
         CRM_Utils_System::permissionDenied();
         CRM_Utils_System::civiExit();
     }
     $this->_rgid = CRM_Utils_Request::retrieve('id', 'Positive', $this, false, 0);
     $this->_contactType = CRM_Utils_Request::retrieve('contact_type', 'String', $this, false, 0);
     if ($this->_rgid) {
         $rgDao = new CRM_Dedupe_DAO_RuleGroup();
         $rgDao->id = $this->_rgid;
         $rgDao->find(true);
         $this->_defaults['threshold'] = $rgDao->threshold;
         $this->_contactType = $rgDao->contact_type;
         $this->_defaults['level'] = $rgDao->level;
         $this->_defaults['name'] = $rgDao->name;
         $this->_defaults['is_default'] = $rgDao->is_default;
         $ruleDao = new CRM_Dedupe_DAO_Rule();
         $ruleDao->dedupe_rule_group_id = $this->_rgid;
         $ruleDao->find();
         $count = 0;
         while ($ruleDao->fetch()) {
             $this->_defaults["where_{$count}"] = "{$ruleDao->rule_table}.{$ruleDao->rule_field}";
             $this->_defaults["length_{$count}"] = $ruleDao->rule_length;
             $this->_defaults["weight_{$count}"] = $ruleDao->rule_weight;
             $count++;
         }
     }
     $supported =& CRM_Dedupe_BAO_RuleGroup::supportedFields($this->_contactType);
     if (is_array($supported)) {
         foreach ($supported as $table => $fields) {
             foreach ($fields as $field => $title) {
                 $this->_fields["{$table}.{$field}"] = $title;
             }
         }
     }
     asort($this->_fields);
 }
 public function testDupesByParams()
 {
     // make dupe checks based on based on following contact sets:
     // FIRST - LAST - EMAIL
     // ---------------------------------
     // robin  - hood - robin@example.com
     // robin  - hood - hood@example.com
     // robin  - dale - robin@example.com
     // little - dale - dale@example.com
     // will   - dale - dale@example.com
     // will   - dale - will@example.com
     // will   - dale - will@example.com
     // contact data set
     // FIXME: move create params to separate function
     $params = array(array('first_name' => 'robin', 'last_name' => 'hood', 'email' => '*****@*****.**', 'contact_type' => 'Individual'), array('first_name' => 'robin', 'last_name' => 'hood', 'email' => '*****@*****.**', 'contact_type' => 'Individual'), array('first_name' => 'robin', 'last_name' => 'dale', 'email' => '*****@*****.**', 'contact_type' => 'Individual'), array('first_name' => 'little', 'last_name' => 'dale', 'email' => '*****@*****.**', 'contact_type' => 'Individual'), array('first_name' => 'will', 'last_name' => 'dale', 'email' => '*****@*****.**', 'contact_type' => 'Individual'), array('first_name' => 'will', 'last_name' => 'dale', 'email' => '*****@*****.**', 'contact_type' => 'Individual'), array('first_name' => 'will', 'last_name' => 'dale', 'email' => '*****@*****.**', 'contact_type' => 'Individual'));
     $count = 1;
     foreach ($params as $param) {
         $contact = $this->callAPISuccess('contact', 'create', $param);
         $params = array('contact_id' => $contact['id'], 'street_address' => 'Ambachtstraat 23', 'location_type_id' => 1);
         $this->callAPISuccess('address', 'create', $params);
         $contactIds[$count++] = $contact['id'];
     }
     // verify that all contacts have been created separately
     $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
     $dao = new CRM_Dedupe_DAO_RuleGroup();
     $dao->contact_type = 'Individual';
     $dao->used = 'General';
     $dao->is_default = 1;
     $dao->find(TRUE);
     $fields = array('first_name' => 'robin', 'last_name' => 'hood', 'email' => '*****@*****.**', 'street_address' => 'Ambachtstraat 23');
     CRM_Core_TemporaryErrorScope::useException();
     $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
     $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'General');
     // Check with default Individual-General rule
     $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
     // delete all created contacts
     foreach ($contactIds as $contactId) {
         $this->contactDelete($contactId);
     }
 }
Exemple #4
0
 /**
  * Browse all rule groups.
  */
 public function browse()
 {
     // get all rule groups
     $ruleGroups = array();
     $dao = new CRM_Dedupe_DAO_RuleGroup();
     $dao->orderBy('contact_type,used ASC');
     $dao->find();
     $dedupeRuleTypes = CRM_Core_SelectValues::getDedupeRuleTypes();
     while ($dao->fetch()) {
         $ruleGroups[$dao->contact_type][$dao->id] = array();
         CRM_Core_DAO::storeValues($dao, $ruleGroups[$dao->contact_type][$dao->id]);
         // form all action links
         $action = array_sum(array_keys($this->links()));
         $links = self::links();
         /* if ($dao->is_default) {
            unset($links[CRM_Core_Action::MAP]);
            unset($links[CRM_Core_Action::DELETE]);
            }*/
         if ($dao->is_reserved) {
             unset($links[CRM_Core_Action::DELETE]);
         }
         $ruleGroups[$dao->contact_type][$dao->id]['action'] = CRM_Core_Action::formLink($links, $action, array('id' => $dao->id), ts('more'), FALSE, 'dedupeRule.manage.action', 'DedupeRule', $dao->id);
         $ruleGroups[$dao->contact_type][$dao->id]['used_display'] = $dedupeRuleTypes[$ruleGroups[$dao->contact_type][$dao->id]['used']];
     }
     $this->assign('brows', $ruleGroups);
 }
 /**
  * Get an array of rule group id to rule group name
  * for all th groups for that contactType. If contactType
  * not specified, do it for all
  *
  * @param string $contactType
  *   Individual, Household or Organization.
  *
  *
  * @return array
  *   id => "nice name" of rule group
  */
 public static function getByType($contactType = NULL)
 {
     $dao = new CRM_Dedupe_DAO_RuleGroup();
     if ($contactType) {
         $dao->contact_type = $contactType;
     }
     $dao->find();
     $result = array();
     while ($dao->fetch()) {
         $title = !empty($dao->title) ? $dao->title : (!empty($dao->name) ? $dao->name : $dao->contact_type);
         $name = "{$title} - {$dao->used}";
         $result[$dao->id] = $name;
     }
     return $result;
 }
Exemple #6
0
 public function testBatchMergeAllDuplicates()
 {
     $this->createDupeContacts();
     // verify that all contacts have been created separately
     $this->assertEquals(count($this->_contactIds), 9, 'Check for number of contacts.');
     $dao = new CRM_Dedupe_DAO_RuleGroup();
     $dao->contact_type = 'Individual';
     $dao->name = 'IndividualSupervised';
     $dao->is_default = 1;
     $dao->find(TRUE);
     $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $this->_groupId);
     // -------------------------------------------------------------------------
     // Name and Email (reserved) Matches ( 3 pairs )
     // --------------------------------------------------------------------------
     // robin  - hood - robin@example.com
     // robin  - hood - robin@example.com
     // little - dale - dale@example.com
     // little - dale - dale@example.com
     // will   - dale - will@example.com
     // will   - dale - will@example.com
     // so 3 pairs for - first + last + mail
     $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
     // Run dedupe finder as the browser would
     $_SERVER['REQUEST_METHOD'] = 'GET';
     //avoid invalid key error
     $object = new CRM_Contact_Page_DedupeFind();
     $object->set('gid', $this->_groupId);
     $object->set('rgid', $dao->id);
     $object->set('action', CRM_Core_Action::UPDATE);
     @$object->run();
     // Retrieve pairs from prev next cache table
     $select = array('pn.is_selected' => 'is_selected');
     $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
     $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
     $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
     // batch merge all dupes
     $result = CRM_Dedupe_Merger::batchMerge($dao->id, $this->_groupId, 'safe', TRUE, 5, 2);
     $this->assertEquals(count($result['merged']), 3, 'Check number of merged pairs.');
     // retrieve pairs from prev next cache table
     $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
     $this->assertEquals(count($pnDupePairs), 0, 'Check number of remaining dupe pairs in prev next cache.');
     $this->deleteDupeContacts();
 }
 /**
  * Browse all rule groups
  *
  * @return void
  * @access public
  */
 function browse()
 {
     // get all rule groups
     $ruleGroups = array();
     $dao = new CRM_Dedupe_DAO_RuleGroup();
     $dao->orderBy('contact_type,level,is_default DESC');
     $dao->find();
     while ($dao->fetch()) {
         $ruleGroups[$dao->id] = array();
         CRM_Core_DAO::storeValues($dao, $ruleGroups[$dao->id]);
         // form all action links
         $action = array_sum(array_keys($this->links()));
         $links = self::links();
         if ($dao->is_default) {
             unset($links[CRM_Core_Action::MAP]);
             unset($links[CRM_Core_Action::DELETE]);
         }
         if ($dao->is_reserved) {
             unset($links[CRM_Core_Action::DELETE]);
         }
         $ruleGroups[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action, array('id' => $dao->id));
         CRM_Dedupe_DAO_RuleGroup::addDisplayEnums($ruleGroups[$dao->id]);
     }
     $this->assign('rows', $ruleGroups);
 }
 /**
  * Get an array of rule group id to rule group name
  * for all th groups for that contactType. If contactType
  * not specified, do it for all
  *
  * @param string $contactType Individual, Household or Organization
  *
  * @static
  *
  * @return array id => "nice name" of rule group
  */
 static function getByType($contactType = NULL)
 {
     $dao = new CRM_Dedupe_DAO_RuleGroup();
     if ($contactType) {
         $dao->contact_type = $contactType;
     }
     $dao->find();
     $result = array();
     while ($dao->fetch()) {
         if (!empty($dao->name)) {
             $name = "{$dao->name} - {$dao->level}";
         } else {
             $name = "{$dao->contact_type} - {$dao->level}";
         }
         $result[$dao->id] = $name;
     }
     return $result;
 }
Exemple #9
0
 /**
  * build all the data structures needed to build the form
  *
  * @return void
  * @access public
  */
 function preProcess()
 {
     parent::preProcess();
     $statusMsg = null;
     $contactIds = array();
     if (is_array($this->_contactIds)) {
         $contactIds = array_unique($this->_contactIds);
     }
     if (count($contactIds) < 2) {
         $statusMsg = ts('Minimum two contact records are required to perform merge operation.');
     }
     // do check for same contact type.
     $contactTypes = array();
     if (!$statusMsg) {
         $sql = "SELECT contact_type FROM civicrm_contact WHERE id IN (" . implode(',', $contactIds) . ")";
         $contact = CRM_Core_DAO::executeQuery($sql);
         while ($contact->fetch()) {
             $contactTypes[$contact->contact_type] = true;
             if (count($contactTypes) > 1) {
                 break;
             }
         }
         if (count($contactTypes) > 1) {
             $statusMsg = ts('Please select same contact type records.');
         }
     }
     if ($statusMsg) {
         CRM_Core_Error::statusBounce($statusMsg);
     }
     $url = null;
     // redirect to merge form directly.
     if (count($contactIds) == 2) {
         $cid = $contactIds[0];
         $oid = $contactIds[1];
         //don't allow to delete logged in user.
         $session = CRM_Core_Session::singleton();
         if ($oid == $session->get('userID')) {
             $oid = $cid;
             $cid = $session->get('userID');
         }
         $url = CRM_Utils_System::url('civicrm/contact/merge', "reset=1&cid={$cid}&oid={$oid}");
     } else {
         $level = 'Fuzzy';
         $cType = key($contactTypes);
         require_once 'CRM/Dedupe/DAO/RuleGroup.php';
         $rgBao = new CRM_Dedupe_DAO_RuleGroup();
         $rgBao->level = $level;
         $rgBao->is_default = 1;
         $rgBao->contact_type = $cType;
         if (!$rgBao->find(true)) {
             CRM_Core_Error::statusBounce("You can not merge contact records because {$level} rule for {$cType} does not exist.");
         }
         $ruleGroupID = $rgBao->id;
         $session = CRM_Core_Session::singleton();
         $session->set('selectedSearchContactIds', $contactIds);
         // create a hidden group and poceed to merge
         $url = CRM_Utils_System::url('civicrm/contact/dedupefind', "reset=1&action=update&rgid={$ruleGroupID}&context=search");
     }
     // redirect to merge page.
     if ($url) {
         CRM_Utils_System::redirect($url);
     }
 }