3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Class CRM_Contact_Form_Merge.
37 class CRM_Contact_Form_Merge
extends CRM_Core_Form
{
38 // The id of the contact that there's a duplicate for; this one will
39 // possibly inherit some of $_oid's properties and remain in the system.
42 // The id of the other contact - the duplicate one that will get deleted.
45 var $_contactType = NULL;
50 public $criteria = array();
53 * Query limit to be retained in the urls.
60 * String for quickform bug handling.
62 * FIXME: QuickForm can't create advcheckboxes with value set to 0 or '0' :(
63 * see HTML_QuickForm_advcheckbox::setValues() - but patching that doesn't
64 * help, as QF doesn't put the 0-value elements in exportValues() anyway...
65 * to side-step this, we use the below UUID as a (re)placeholder
69 var $_qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
71 public function preProcess() {
74 $this->_cid
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
75 $this->_oid
= CRM_Utils_Request
::retrieve('oid', 'Positive', $this, TRUE);
76 $flip = CRM_Utils_Request
::retrieve('flip', 'Positive', $this, FALSE);
78 $this->_rgid
= CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE);
79 $this->_gid
= $gid = CRM_Utils_Request
::retrieve('gid', 'Positive', $this, FALSE);
80 $this->_mergeId
= CRM_Utils_Request
::retrieve('mergeId', 'Positive', $this, FALSE);
81 $this->limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this, FALSE);
82 $this->criteria
= CRM_Utils_Request
::retrieve('criteria', 'Json', $this, FALSE, '{}');
84 $urlParams = ['reset' => 1, 'rgid' => $this->_rgid
, 'gid' => $this->_gid
, 'limit' => $this->limit
, 'criteria' => $this->criteria
];
86 $this->bounceIfInvalid($this->_cid
, $this->_oid
);
88 $this->_contactType
= civicrm_api3('Contact', 'getvalue', array(
90 'return' => 'contact_type',
93 $browseUrl = CRM_Utils_System
::url('civicrm/contact/dedupefind', array_merge($urlParams, ['action' => 'browse']));
96 // Unset browse URL as we have come from the search screen.
98 $this->_rgid
= civicrm_api3('RuleGroup', 'getvalue', array(
99 'contact_type' => $this->_contactType
,
100 'used' => 'Supervised',
104 $this->assign('browseUrl', $browseUrl);
106 CRM_Core_Session
::singleton()->pushUserContext($browseUrl);
109 $cacheKey = CRM_Dedupe_Merger
::getMergeCacheKeyString($this->_rgid
, $gid, json_decode($this->criteria
, TRUE));
111 $join = CRM_Dedupe_Merger
::getJoinOnDedupeTable();
112 $where = "de.id IS NULL";
114 $pos = CRM_Core_BAO_PrevNextCache
::getPositions($cacheKey, $this->_cid
, $this->_oid
, $this->_mergeId
, $join, $where, $flip);
116 // get user info of main contact.
117 $config = CRM_Core_Config
::singleton();
118 CRM_Core_Config
::setPermitCacheFlushMode(FALSE);
120 $mainUfId = CRM_Core_BAO_UFMatch
::getUFId($this->_cid
);
123 $mainUser = $config->userSystem
->getUser($this->_cid
);
124 $this->assign('mainUfId', $mainUfId);
125 $this->assign('mainUfName', $mainUser ?
$mainUser['name'] : NULL);
127 $flipParams = array_merge($urlParams, ['action' => 'update', 'cid' => $this->_oid
, 'oid' => $this->_cid
]);
129 $flipParams['flip'] = '1';
131 $flipUrl = CRM_Utils_System
::url('civicrm/contact/merge',
134 $this->assign('flip', $flipUrl);
136 $this->prev
= $this->next
= NULL;
141 if (!empty($pos[$position])) {
142 if ($pos[$position]['id1'] && $pos[$position]['id2']) {
143 $rowParams = array_merge($urlParams, [
144 'action' => 'update',
145 'cid' => $pos[$position]['id1'],
146 'oid' => $pos[$position]['id2'],
147 'mergeId' => $pos[$position]['mergeId'],
149 $this->$position = CRM_Utils_System
::url('civicrm/contact/merge', $rowParams);
150 $this->assign($position, $this->$position);
155 // get user info of other contact.
156 $otherUfId = CRM_Core_BAO_UFMatch
::getUFId($this->_oid
);
160 $otherUser = $config->userSystem
->getUser($this->_oid
);
161 $this->assign('otherUfId', $otherUfId);
162 $this->assign('otherUfName', $otherUser ?
$otherUser['name'] : NULL);
165 $cmsUser = ($mainUfId && $otherUfId) ?
TRUE : FALSE;
166 $this->assign('user', $cmsUser);
168 $rowsElementsAndInfo = CRM_Dedupe_Merger
::getRowsElementsAndInfo($this->_cid
, $this->_oid
);
169 $main = $this->_mainDetails
= $rowsElementsAndInfo['main_details'];
170 $other = $this->_otherDetails
= $rowsElementsAndInfo['other_details'];
172 $this->assign('contact_type', $main['contact_type']);
173 $this->assign('main_name', $main['display_name']);
174 $this->assign('other_name', $other['display_name']);
175 $this->assign('main_cid', $main['contact_id']);
176 $this->assign('other_cid', $other['contact_id']);
177 $this->assign('rgid', $this->_rgid
);
179 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
181 $this->assign('mainLocBlock', json_encode($rowsElementsAndInfo['main_details']['location_blocks']));
182 $this->assign('locationBlockInfo', json_encode(CRM_Dedupe_Merger
::getLocationBlockInfo()));
183 $this->assign('rows', $rowsElementsAndInfo['rows']);
186 foreach ($rowsElementsAndInfo['elements'] as $element) {
187 // We could push this down to the getRowsElementsAndInfo function but it's
188 // already so overloaded - let's start moving towards doing form-things
190 if (substr($element[1], 0, 13) === 'move_location') {
191 $element[4] = array_merge(
192 (array) CRM_Utils_Array
::value(4, $element, array()),
194 'data-location' => substr($element[1], 14),
195 'data-is_location' => TRUE,
198 if (substr($element[1], 0, 15) === 'location_blocks') {
199 // @todo We could add some data elements here to make jquery manipulation more straight-forward
200 // @todo consider enabling if it is an add & defaulting to true.
201 $element[4] = array_merge((array) CRM_Utils_Array
::value(4, $element, array()), array('disabled' => TRUE));
203 $this->addElement($element[0],
205 array_key_exists('2', $element) ?
$element[2] : NULL,
206 array_key_exists('3', $element) ?
$element[3] : NULL,
207 array_key_exists('4', $element) ?
$element[4] : NULL,
208 array_key_exists('5', $element) ?
$element[5] : NULL
212 // add related table elements
213 foreach ($rowsElementsAndInfo['rel_table_elements'] as $relTableElement) {
214 $element = $this->addElement($relTableElement[0], $relTableElement[1]);
215 $element->setChecked(TRUE);
218 $this->assign('rel_tables', $rowsElementsAndInfo['rel_tables']);
219 $this->assign('userContextURL', CRM_Core_Session
::singleton()
220 ->readUserContext());
222 catch (CRM_Core_Exception
$e) {
223 CRM_Core_Error
::statusBounce(ts($e->getMessage()));
227 public function addRules() {
230 public function buildQuickForm() {
231 $this->unsavedChangesWarn
= FALSE;
232 CRM_Utils_System
::setTitle(ts('Merge %1 contacts', array(1 => $this->_contactType
)));
237 'name' => $this->next ?
ts('Merge and go to Next Pair') : ts('Merge'),
239 'icon' => $this->next ?
'fa-play-circle' : 'check',
242 if ($this->next ||
$this->prev
) {
245 'name' => ts('Merge and go to Listing'),
249 'name' => ts('Merge and View Result'),
250 'icon' => 'fa-check-circle',
256 'name' => ts('Cancel'),
259 $this->addButtons($buttons);
260 $this->addFormRule(array('CRM_Contact_Form_Merge', 'formRule'), $this);
270 public static function formRule($fields, $files, $self) {
272 $link = CRM_Utils_System
::href(ts('Flip between the original and duplicate contacts.'),
273 'civicrm/contact/merge',
274 'reset=1&action=update&cid=' . $self->_oid
. '&oid=' . $self->_cid
. '&rgid=' . $self->_rgid
. '&flip=1'
276 if (CRM_Contact_BAO_Contact
::checkDomainContact($self->_oid
)) {
277 $errors['_qf_default'] = ts("The Default Organization contact cannot be merged into another contact record. It is associated with the CiviCRM installation for this domain and contains information used for system functions. If you want to merge these records, you can: %1", array(1 => $link));
282 public function postProcess() {
283 $formValues = $this->exportValues();
285 $formValues['main_details'] = $this->_mainDetails
;
286 $formValues['other_details'] = $this->_otherDetails
;
287 $migrationData = array('migration_info' => $formValues);
288 CRM_Utils_Hook
::merge('form', $migrationData, $this->_cid
, $this->_oid
);
289 CRM_Dedupe_Merger
::moveAllBelongings($this->_cid
, $this->_oid
, $migrationData['migration_info']);
291 $name = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_cid
, 'display_name');
292 $message = '<ul><li>' . ts('%1 has been updated.', array(1 => $name)) . '</li><li>' . ts('Contact ID %1 has been deleted.', array(1 => $this->_oid
)) . '</li></ul>';
293 CRM_Core_Session
::setStatus($message, ts('Contacts Merged'), 'success');
295 $urlParams = ['reset' => 1, 'cid' => $this->_cid
, 'rgid' => $this->_rgid
, 'gid' => $this->_gid
, 'limit' => $this->limit
, 'criteria' => $this->criteria
];
296 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', ['reset' => 1, 'cid' => $this->_cid
]);
298 if (!empty($formValues['_qf_Merge_submit'])) {
299 $urlParams['action'] = "update";
300 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind',
304 if (!empty($formValues['_qf_Merge_done'])) {
305 CRM_Utils_System
::redirect($contactViewUrl);
308 if ($this->next
&& $this->_mergeId
) {
309 $cacheKey = CRM_Dedupe_Merger
::getMergeCacheKeyString($this->_rgid
, $this->_gid
, json_decode($this->criteria
, TRUE));
311 $join = CRM_Dedupe_Merger
::getJoinOnDedupeTable();
312 $where = "de.id IS NULL";
314 $pos = CRM_Core_BAO_PrevNextCache
::getPositions($cacheKey, NULL, NULL, $this->_mergeId
, $join, $where);
317 $pos['next']['id1'] &&
321 $urlParams['cid'] = $pos['next']['id1'];
322 $urlParams['oid'] = $pos['next']['id2'];
323 $urlParams['mergeId'] = $pos['next']['mergeId'];
324 $urlParams['action'] = 'update';
325 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/merge', $urlParams));
329 // Perhaps never reached.
330 CRM_Utils_System
::redirect($contactViewUrl);
334 * Bounce if the merge action is invalid.
336 * We don't allow the merge if it is nonsensical, marked as a duplicate
337 * or outside the user's permission.
340 * Contact ID to retain
342 * Contact ID to delete.
344 public function bounceIfInvalid($cid, $oid) {
346 CRM_Core_Error
::statusBounce(ts('Cannot merge a contact with itself.'));
349 if (!CRM_Dedupe_BAO_Rule
::validateContacts($cid, $oid)) {
350 CRM_Core_Error
::statusBounce(ts('The selected pair of contacts are marked as non duplicates. If these records should be merged, you can remove this exception on the <a href="%1">Dedupe Exceptions</a> page.', array(1 => CRM_Utils_System
::url('civicrm/dedupe/exception', 'reset=1'))));
353 if (!(CRM_Contact_BAO_Contact_Permission
::allow($cid, CRM_Core_Permission
::EDIT
) &&
354 CRM_Contact_BAO_Contact_Permission
::allow($oid, CRM_Core_Permission
::EDIT
)
357 CRM_Utils_System
::permissionDenied();
359 // ensure that oid is not the current user, if so refuse to do the merge
360 if (CRM_Core_Session
::singleton()->getLoggedInContactID() == $oid) {
361 $message = ts('The contact record which is linked to the currently logged in user account - \'%1\' - cannot be deleted.',
362 array(1 => CRM_Core_Session
::singleton()->getLoggedInContactDisplayName())
364 CRM_Core_Error
::statusBounce($message);