[MediaWiki-commits] [Gerrit] wikimedia...civicrm[master]: Add ability to find duplicates for selected contacts.

2017-09-01 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/373155 )

Change subject: Add ability to find duplicates for selected contacts.
..


Add ability to find duplicates for selected contacts.

Notes:

- This is not in my mind a final implementation. I wish to merge to core as 
part of
extracting the code to an extension & version iteration & hence some choices 
may not
be permanent
- In the extension context the addition of the task may be by hook rather than
adding to the array. I could not see any reason for the weird CONST numbering 
scheme in core
for the task list & ignored it.
- I wanted to confirm whether it is necessary to extend the task class
which seems unwieldly to me & so have done this just extending CRM_Core_Form.
-It occurs to me there are 2 separate actions we are interested in
  1) select a bunch of contacts & find all the matches for them (based on either
default rule or selecting a rule).
  2) dedupe the contacts selected against each other.

The patch here adds an action to contact search results (Find duplicates for 
these contacts)
that uses the default rule to find all matches for selected contacts. This is 
the simpler
option of those listed. Implementing 2 will require some more work on the 
cacheKey mechanism
since we are trying to by-pass the rules.

Normally I would try to focus on what I can get into core first but in this case
I'm looking for a user win & to explore the code more to plan the next step
upstream, which will involve refactoring.

Bug: T151270

Change-Id: I2da53510e879c6260be8cee4a455d4a0a2d54693
---
M CRM/Contact/Form/Merge.php
A CRM/Contact/Form/Task/FindDuplicates.php
M CRM/Contact/Page/AJAX.php
M CRM/Contact/Page/DedupeFind.php
M CRM/Contact/Page/DedupeMerge.php
M CRM/Contact/Task.php
M templates/CRM/Contact/Page/DedupeFind.tpl
7 files changed, 138 insertions(+), 17 deletions(-)

Approvals:
  jenkins-bot: Verified
  Ejegg: Looks good to me, approved



diff --git a/CRM/Contact/Form/Merge.php b/CRM/Contact/Form/Merge.php
index 6381852..1be1904 100644
--- a/CRM/Contact/Form/Merge.php
+++ b/CRM/Contact/Form/Merge.php
@@ -45,6 +45,11 @@
   var $_contactType = NULL;
 
   /**
+   * @var array
+   */
+  public $criteria = array();
+
+  /**
* Query limit to be retained in the urls.
*
* @var int
@@ -74,7 +79,9 @@
   $this->_gid = $gid = CRM_Utils_Request::retrieve('gid', 'Positive', 
$this, FALSE);
   $this->_mergeId = CRM_Utils_Request::retrieve('mergeId', 'Positive', 
$this, FALSE);
   $this->limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this, 
FALSE);
-  $urlParams = "reset=1={$this->_rgid}={$this->_gid}=" . 
$this->limit;
+  $this->criteria = CRM_Utils_Request::retrieve('criteria', 'String', 
$this, FALSE, '{}');
+
+  $urlParams = "reset=1={$this->_rgid}={$this->_gid}=" . 
$this->limit . '=' . $this->criteria;
 
   $this->bounceIfInvalid($this->_cid, $this->_oid);
 
@@ -83,7 +90,7 @@
 'return' => 'contact_type',
   ));
 
-  $browseUrl = CRM_Utils_System::url('civicrm/contact/dedupefind', 
$urlParams . '=browse');
+  $browseUrl = CRM_Utils_System::url('civicrm/contact/dedupefind', 
$urlParams . '=browse', FALSE, NULL, FALSE);
 
   if (!$this->_rgid) {
 // Unset browse URL as we have come from the search screen.
@@ -99,7 +106,7 @@
 CRM_Core_Session::singleton()->pushUserContext($browseUrl);
   }
 
-  $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($this->_rgid, 
$gid);
+  $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($this->_rgid, 
$gid, json_decode($this->criteria, TRUE));
 
   $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
   $where = "de.id IS NULL";
@@ -126,7 +133,7 @@
   }
 
   $flipUrl = CRM_Utils_System::url('civicrm/contact/merge',
-
"reset=1=update={$this->_oid}={$this->_cid}={$this->_rgid}={$gid}"
+
"reset=1=update={$this->_oid}={$this->_cid}={$this->_rgid}={$gid}={$this->criteria}"
   );
   if (!$flip) {
 $flipUrl .= '=1';
@@ -294,7 +301,7 @@
 CRM_Core_Session::setStatus($message, ts('Contacts Merged'), 'success');
 
 $url = CRM_Utils_System::url('civicrm/contact/view', 
"reset=1={$this->_cid}");
-$urlParams = 
"reset=1={$this->_gid}={$this->_rgid}={$this->limit}";
+$urlParams = "reset=1={$this->_rgid}={$this->_gid}=" . 
$this->limit . '=' . $this->criteria;
 
 if (!empty($formValues['_qf_Merge_submit'])) {
   $urlParams .= "=update";
@@ -308,7 +315,7 @@
 }
 
 if ($this->next && $this->_mergeId) {
-  $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($this->_rgid, 
$this->_gid);
+  $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($this->_rgid, 
$this->_gid, json_decode($this->criteria, TRUE));
 
   $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
   $where = "de.id IS NULL";
@@ -321,7 +328,7 @@
   ) {
 
 $urlParams 

[MediaWiki-commits] [Gerrit] wikimedia...civicrm[master]: Add ability to find duplicates for selected contacts.

2017-08-22 Thread Eileen (Code Review)
Eileen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/373155 )

Change subject: Add ability to find duplicates for selected contacts.
..

Add ability to find duplicates for selected contacts.

Notes:

- This is not in my mind a final implementation. I wish to merge to core as 
part of
extracting the code to an extension & version iteration & hence some choices 
may not
be permanent
- In the extension context the addition of the task may be by hook rather than
adding to the array. I could not see any reason for the weird CONST numbering 
scheme in core
for the task list & ignored it.
- I wanted to confirm whether it is necessary to extend the task class
which seems unwieldly to me & so have done this just extending CRM_Core_Form.
-It occurs to me there are 2 separate actions we are interested in
  1) select a bunch of contacts & find all the matches for them (based on either
default rule or selecting a rule).
  2) dedupe the contacts selected against each other.

The patch here adds an action to contact search results (Find duplicates for 
these contacts)
that uses the default rule to find all matches for selected contacts. This is 
the simpler
option of those listed. Implementing 2 will require some more work on the 
cacheKey mechanism
since we are trying to by-pass the rules.

Normally I would try to focus on what I can get into core first but in this case
I'm looking for a user win & to explore the code more to plan the next step
upstream, which will involve refactoring.

Bug: T151270

Change-Id: I2da53510e879c6260be8cee4a455d4a0a2d54693
---
A CRM/Contact/Form/Task/FindDuplicates.php
M CRM/Contact/Form/Task/Merge.php
M CRM/Contact/Page/AJAX.php
M CRM/Contact/Page/DedupeFind.php
M CRM/Contact/Task.php
5 files changed, 87 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm 
refs/changes/55/373155/1

diff --git a/CRM/Contact/Form/Task/FindDuplicates.php 
b/CRM/Contact/Form/Task/FindDuplicates.php
new file mode 100644
index 000..a3c3c37
--- /dev/null
+++ b/CRM/Contact/Form/Task/FindDuplicates.php
@@ -0,0 +1,73 @@
+http://civicrm.org/licensing|
+ ++
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2017
+ */
+
+/**
+ * This class provides the functionality to find potential matches.
+ */
+class CRM_Contact_Form_Task_FindDuplicates extends CRM_Core_Form {
+
+  /**
+   * Selected contact ids.
+   *
+   * @var array
+   */
+  public $_contactIds = array();
+
+  /**
+   * Build all the data structures needed to build the form.
+   */
+  public function preProcess() {
+ CRM_Contact_Form_Task::preProcessCommon($this);
+ $contactType = CRM_Core_DAO::singleValueQuery(
+   'SELECT GROUP_CONCAT(DISTINCT contact_type) FROM civicrm_contact WHERE 
id IN (' . implode($this->_contactIds). ')'
+ );
+
+ try {
+   $rule_group_id = civicrm_api3('RuleGroup', 'getvalue', array(
+ 'contact_type' => $contactType,
+ 'used' => 'Unsupervised',
+ 'return' => 'id',
+ 'options' => array('limit' => 1),
+   ));
+ }
+ catch (CiviCRM_API3_Exception $e) {
+   CRM_Core_Error::statusBounce(ts('It was not possible to identify a 
default rule that was applicable to all selected contacts. You must choose only 
one contact type. You chose %1', array($contactType)));
+ }
+
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', 
array(
+  'reset' => 1,
+  'action' => 'update',
+  'rgid' => $rule_group_id,
+  'criteria' => json_encode(array('contact_id' => array('IN' => 
$this->_contactIds))),
+)));
+  }
+}
\ No newline at end of file
diff --git a/CRM/Contact/Form/Task/Merge.php b/CRM/Contact/Form/Task/Merge.php
index d0f0abd..9bbdf4e 100644
--- a/CRM/Contact/Form/Task/Merge.php
+++ b/CRM/Contact/Form/Task/Merge.php
@@ -33,7 +33,6 @@
 
 /**
  * This class provides the functionality to Merge contacts.
- *
  */
 class CRM_Contact_Form_Task_Merge extends CRM_Contact_Form_Task {
 
diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php
index cfdff39..8185937 100644
--- a/CRM/Contact/Page/AJAX.php
+++ b/CRM/Contact/Page/AJAX.php
@@ -649,13 +649,15 @@
 
 $gid = CRM_Utils_Request::retrieve('gid', 'Positive');
 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive');
+$null = NULL;
+$criteria = json_decode(CRM_Utils_Request::retrieve('criteria', 'String', 
$null, FALSE, '{}'), TRUE);
 $selected= isset($_REQUEST['selected']) ? 
CRM_Utils_Type::escape($_REQUEST['selected'], 'Integer') : 0;
 if ($rowCount < 0) {
   $rowCount = 0;
 }
 
 $whereClause = $orderByClause = '';
-$cacheKeyString   = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
+$cacheKeyString   = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, 
$criteria);