Merge pull request #7965 from eileenmcnaughton/comments
[civicrm-core.git] / CRM / Contact / Form / Merge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 /**
35 * Class CRM_Contact_Form_Merge.
36 */
37 class CRM_Contact_Form_Merge extends CRM_Core_Form {
38 // the id of the contact that tere's a duplicate for; this one will
39 // possibly inherit some of $_oid's properties and remain in the system
40 var $_cid = NULL;
41
42 // the id of the other contact - the duplicate one that will get deleted
43 var $_oid = NULL;
44
45 var $_contactType = NULL;
46
47 // FIXME: QuickForm can't create advcheckboxes with value set to 0 or '0' :(
48 // see HTML_QuickForm_advcheckbox::setValues() - but patching that doesn't
49 // help, as QF doesn't put the 0-value elements in exportValues() anyway...
50 // to side-step this, we use the below UUID as a (re)placeholder
51 var $_qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
52
53 public function preProcess() {
54 if (!CRM_Core_Permission::check('merge duplicate contacts')) {
55 CRM_Core_Error::fatal(ts('You do not have access to this page'));
56 }
57
58 $rows = array();
59 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
60 $oid = CRM_Utils_Request::retrieve('oid', 'Positive', $this, TRUE);
61 $flip = CRM_Utils_Request::retrieve('flip', 'Positive', $this, FALSE);
62
63 $this->_rgid = $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE);
64 $this->_gid = $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE);
65 $this->_mergeId = CRM_Utils_Request::retrieve('mergeId', 'Positive', $this, FALSE);
66
67 // Sanity check
68 if ($cid == $oid) {
69 CRM_Core_Error::statusBounce(ts('Cannot merge a contact with itself.'));
70 }
71
72 if (!CRM_Dedupe_BAO_Rule::validateContacts($cid, $oid)) {
73 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'))));
74 }
75
76 //load cache mechanism
77 $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'contact_type');
78 $cacheKey = "merge $contactType";
79 $cacheKey .= $rgid ? "_{$rgid}" : '_0';
80 $cacheKey .= $gid ? "_{$gid}" : '_0';
81
82 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
83 pn.entity_id2 = de.contact_id2 )";
84 $where = "de.id IS NULL";
85
86 $pos = CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, $cid, $oid, $this->_mergeId, $join, $where, $flip);
87
88 // Block access if user does not have EDIT permissions for both contacts.
89 if (!(CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT) &&
90 CRM_Contact_BAO_Contact_Permission::allow($oid, CRM_Core_Permission::EDIT)
91 )
92 ) {
93 CRM_Utils_System::permissionDenied();
94 }
95
96 // get user info of main contact.
97 $config = CRM_Core_Config::singleton();
98 $config->doNotResetCache = 1;
99
100 $viewUser = CRM_Core_Permission::check('access user profiles');
101 $mainUfId = CRM_Core_BAO_UFMatch::getUFId($cid);
102 $mainUser = NULL;
103 if ($mainUfId) {
104 // d6 compatible
105 if ($config->userSystem->is_drupal == '1') {
106 $mainUser = user_load($mainUfId);
107 }
108 elseif ($config->userFramework == 'Joomla') {
109 $mainUser = JFactory::getUser($mainUfId);
110 }
111
112 $this->assign('mainUfId', $mainUfId);
113 $this->assign('mainUfName', $mainUser ? $mainUser->name : NULL);
114 }
115
116 $flipUrl = CRM_Utils_System::url('civicrm/contact/merge',
117 "reset=1&action=update&cid={$oid}&oid={$cid}&rgid={$rgid}&gid={$gid}"
118 );
119 if (!$flip) {
120 $flipUrl .= '&flip=1';
121 }
122 $this->assign('flip', $flipUrl);
123
124 $this->prev = $this->next = NULL;
125 foreach (array(
126 'prev',
127 'next',
128 ) as $position) {
129 if (!empty($pos[$position])) {
130 if ($pos[$position]['id1'] && $pos[$position]['id2']) {
131 $urlParam = "reset=1&cid={$pos[$position]['id1']}&oid={$pos[$position]['id2']}&mergeId={$pos[$position]['mergeId']}&action=update";
132
133 if ($rgid) {
134 $urlParam .= "&rgid={$rgid}";
135 }
136 if ($gid) {
137 $urlParam .= "&gid={$gid}";
138 }
139
140 $this->$position = CRM_Utils_System::url('civicrm/contact/merge', $urlParam);
141 $this->assign($position, $this->$position);
142 }
143 }
144 }
145
146 // get user info of other contact.
147 $otherUfId = CRM_Core_BAO_UFMatch::getUFId($oid);
148 $otherUser = NULL;
149
150 if ($otherUfId) {
151 // d6 compatible
152 if ($config->userSystem->is_drupal == '1') {
153 $otherUser = user_load($otherUfId);
154 }
155 elseif ($config->userFramework == 'Joomla') {
156 $otherUser = JFactory::getUser($otherUfId);
157 }
158
159 $this->assign('otherUfId', $otherUfId);
160 $this->assign('otherUfName', $otherUser ? $otherUser->name : NULL);
161 }
162
163 $cmsUser = ($mainUfId && $otherUfId) ? TRUE : FALSE;
164 $this->assign('user', $cmsUser);
165
166 $session = CRM_Core_Session::singleton();
167
168 // context fixed.
169 if ($rgid) {
170 $urlParam = "reset=1&action=browse&rgid={$rgid}";
171 if ($gid) {
172 $urlParam .= "&gid={$gid}";
173 }
174 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlParam));
175 }
176
177 // ensure that oid is not the current user, if so refuse to do the merge
178 if ($session->get('userID') == $oid) {
179 $display_name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $oid, 'display_name');
180 $message = ts('The contact record which is linked to the currently logged in user account - \'%1\' - cannot be deleted.',
181 array(1 => $display_name)
182 );
183 CRM_Core_Error::statusBounce($message);
184 }
185
186 $rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($cid, $oid);
187 $main = $this->_mainDetails = &$rowsElementsAndInfo['main_details'];
188 $other = $this->_otherDetails = &$rowsElementsAndInfo['other_details'];
189
190 if ($main['contact_id'] != $cid) {
191 CRM_Core_Error::fatal(ts('The main contact record does not exist'));
192 }
193
194 if ($other['contact_id'] != $oid) {
195 CRM_Core_Error::fatal(ts('The other contact record does not exist'));
196 }
197
198 $this->assign('contact_type', $main['contact_type']);
199 $this->assign('main_name', $main['display_name']);
200 $this->assign('other_name', $other['display_name']);
201 $this->assign('main_cid', $main['contact_id']);
202 $this->assign('other_cid', $other['contact_id']);
203 $this->assign('rgid', $rgid);
204
205 $this->_cid = $cid;
206 $this->_oid = $oid;
207 $this->_rgid = $rgid;
208 $this->_contactType = $main['contact_type'];
209 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
210
211 $this->assign('mainLocBlock', json_encode($rowsElementsAndInfo['main_loc_block']));
212 $this->assign('rows', $rowsElementsAndInfo['rows']);
213
214 // add elements
215 foreach ($rowsElementsAndInfo['elements'] as $element) {
216 $this->addElement($element[0],
217 $element[1],
218 array_key_exists('2', $element) ? $element[2] : NULL,
219 array_key_exists('3', $element) ? $element[3] : NULL,
220 array_key_exists('4', $element) ? $element[4] : NULL,
221 array_key_exists('5', $element) ? $element[5] : NULL
222 );
223 }
224
225 // add related table elements
226 foreach ($rowsElementsAndInfo['rel_table_elements'] as $relTableElement) {
227 $element = $this->addElement($relTableElement[0], $relTableElement[1]);
228 $element->setChecked(TRUE);
229 }
230
231 $this->assign('rel_tables', $rowsElementsAndInfo['rel_tables']);
232 $this->assign('userContextURL', $session->readUserContext());
233 }
234
235 /**
236 * This virtual function is used to set the default values of
237 * various form elements
238 *
239 * access public
240 *
241 * @return array
242 * reference to the array of default values
243 */
244 /**
245 * @return array
246 */
247 public function setDefaultValues() {
248 return array('deleteOther' => 1);
249 }
250
251 public function addRules() {
252 }
253
254 public function buildQuickForm() {
255 CRM_Utils_System::setTitle(ts('Merge %1 contacts', array(1 => $this->_contactType)));
256 $buttons = array();
257
258 $buttons[] = array(
259 'type' => 'next',
260 'name' => $this->next ? ts('Merge and go to Next Pair') : ts('Merge'),
261 'isDefault' => TRUE,
262 'icon' => $this->next ? 'circle-triangle-e' : 'check',
263 );
264
265 if ($this->next || $this->prev) {
266 $buttons[] = array(
267 'type' => 'submit',
268 'name' => ts('Merge and go to Listing'),
269 );
270 $buttons[] = array(
271 'type' => 'done',
272 'name' => ts('Merge and View Result'),
273 'icon' => 'fa-check-circle',
274 );
275 }
276
277 $buttons[] = array(
278 'type' => 'cancel',
279 'name' => ts('Cancel'),
280 );
281
282 $this->addButtons($buttons);
283 $this->addFormRule(array('CRM_Contact_Form_Merge', 'formRule'), $this);
284 }
285
286 /**
287 * @param $fields
288 * @param $files
289 * @param $self
290 *
291 * @return array
292 */
293 public static function formRule($fields, $files, $self) {
294 $errors = array();
295 $link = CRM_Utils_System::href(ts('Flip between the original and duplicate contacts.'),
296 'civicrm/contact/merge',
297 'reset=1&action=update&cid=' . $self->_oid . '&oid=' . $self->_cid . '&rgid=' . $self->_rgid . '&flip=1'
298 );
299 if (CRM_Contact_BAO_Contact::checkDomainContact($self->_oid)) {
300 $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));
301 }
302 return $errors;
303 }
304
305 public function postProcess() {
306 $formValues = $this->exportValues();
307
308 // reset all selected contact ids from session
309 // when we came from search context, CRM-3526
310 $session = CRM_Core_Session::singleton();
311 if ($session->get('selectedSearchContactIds')) {
312 $session->resetScope('selectedSearchContactIds');
313 }
314
315 $formValues['main_details'] = $this->_mainDetails;
316 $formValues['other_details'] = $this->_otherDetails;
317
318 CRM_Dedupe_Merger::moveAllBelongings($this->_cid, $this->_oid, $formValues);
319
320 $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_cid, 'display_name');
321 $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>';
322 CRM_Core_Session::setStatus($message, ts('Contacts Merged'), 'success');
323
324 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_cid}");
325 if (!empty($formValues['_qf_Merge_submit'])) {
326 $listParamsURL = "reset=1&action=update&rgid={$this->_rgid}";
327 if ($this->_gid) {
328 $listParamsURL .= "&gid={$this->_gid}";
329 }
330 $lisitingURL = CRM_Utils_System::url('civicrm/contact/dedupefind',
331 $listParamsURL
332 );
333 CRM_Utils_System::redirect($lisitingURL);
334 }
335 if (!empty($formValues['_qf_Merge_done'])) {
336 CRM_Utils_System::redirect($url);
337 }
338
339 if ($this->next && $this->_mergeId) {
340 $cacheKey = "merge {$this->_contactType}";
341 $cacheKey .= $this->_rgid ? "_{$this->_rgid}" : '_0';
342 $cacheKey .= $this->_gid ? "_{$this->_gid}" : '_0';
343
344 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
345 pn.entity_id2 = de.contact_id2 )";
346 $where = "de.id IS NULL";
347
348 $pos = CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, NULL, NULL, $this->_mergeId, $join, $where);
349
350 if (!empty($pos) &&
351 $pos['next']['id1'] &&
352 $pos['next']['id2']
353 ) {
354
355 $urlParam = "reset=1&cid={$pos['next']['id1']}&oid={$pos['next']['id2']}&mergeId={$pos['next']['mergeId']}&action=update";
356 if ($this->_rgid) {
357 $urlParam .= "&rgid={$this->_rgid}";
358 }
359 if ($this->_gid) {
360 $urlParam .= "&gid={$this->_gid}";
361 }
362
363 $url = CRM_Utils_System::url('civicrm/contact/merge', $urlParam);
364 }
365 }
366
367 CRM_Utils_System::redirect($url);
368 }
369
370 }