CRM-21773 Refactor related table static variable caches to support unit testing
[civicrm-core.git] / CRM / Dedupe / Merger.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
32 */
33 class CRM_Dedupe_Merger {
34
35 /**
36 * FIXME: consider creating a common structure with cidRefs() and eidRefs()
37 * FIXME: the sub-pages references by the URLs should
38 * be loaded dynamically on the merge form instead
39 * @return array
40 */
41 public static function relTables() {
42
43 if (!isset(Civi::$statics[__CLASS__]['relTables'])) {
44
45 // Setting these merely prevents enotices - but it may be more appropriate not to add the user table below
46 // if the url can't be retrieved. A more standardised way to retrieve them is.
47 // CRM_Core_Config::singleton()->userSystem->getUserRecordUrl() - however that function takes a contact_id &
48 // we may need a different function when it is not known.
49 $title = $userRecordUrl = '';
50
51 $config = CRM_Core_Config::singleton();
52 if ($config->userSystem->is_drupal) {
53 $userRecordUrl = CRM_Utils_System::url('user/%ufid');
54 $title = ts('%1 User: %2; user id: %3', array(1 => $config->userFramework, 2 => '$ufname', 3 => '$ufid'));
55 }
56 elseif ($config->userFramework == 'Joomla') {
57 $userRecordUrl = $config->userSystem->getVersion() > 1.5 ? $config->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . '%ufid' : $config->userFrameworkBaseURL . "index2.php?option=com_users&view=user&task=edit&id[]=" . '%ufid';
58 $title = ts('%1 User: %2; user id: %3', array(1 => $config->userFramework, 2 => '$ufname', 3 => '$ufid'));
59 }
60
61 $relTables = array(
62 'rel_table_contributions' => array(
63 'title' => ts('Contributions'),
64 'tables' => array('civicrm_contribution', 'civicrm_contribution_recur', 'civicrm_contribution_soft'),
65 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=contribute'),
66 ),
67 'rel_table_contribution_page' => array(
68 'title' => ts('Contribution Pages'),
69 'tables' => array('civicrm_contribution_page'),
70 'url' => CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1&cid=$cid'),
71 ),
72 'rel_table_memberships' => array(
73 'title' => ts('Memberships'),
74 'tables' => array('civicrm_membership', 'civicrm_membership_log', 'civicrm_membership_type'),
75 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=member'),
76 ),
77 'rel_table_participants' => array(
78 'title' => ts('Participants'),
79 'tables' => array('civicrm_participant'),
80 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=participant'),
81 ),
82 'rel_table_events' => array(
83 'title' => ts('Events'),
84 'tables' => array('civicrm_event'),
85 'url' => CRM_Utils_System::url('civicrm/event/manage', 'reset=1&cid=$cid'),
86 ),
87 'rel_table_activities' => array(
88 'title' => ts('Activities'),
89 'tables' => array('civicrm_activity', 'civicrm_activity_contact'),
90 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=activity'),
91 ),
92 'rel_table_relationships' => array(
93 'title' => ts('Relationships'),
94 'tables' => array('civicrm_relationship'),
95 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=rel'),
96 ),
97 'rel_table_custom_groups' => array(
98 'title' => ts('Custom Groups'),
99 'tables' => array('civicrm_custom_group'),
100 'url' => CRM_Utils_System::url('civicrm/admin/custom/group', 'reset=1'),
101 ),
102 'rel_table_uf_groups' => array(
103 'title' => ts('Profiles'),
104 'tables' => array('civicrm_uf_group'),
105 'url' => CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'),
106 ),
107 'rel_table_groups' => array(
108 'title' => ts('Groups'),
109 'tables' => array('civicrm_group_contact'),
110 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=group'),
111 ),
112 'rel_table_notes' => array(
113 'title' => ts('Notes'),
114 'tables' => array('civicrm_note'),
115 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=note'),
116 ),
117 'rel_table_tags' => array(
118 'title' => ts('Tags'),
119 'tables' => array('civicrm_entity_tag'),
120 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=tag'),
121 ),
122 'rel_table_mailings' => array(
123 'title' => ts('Mailings'),
124 'tables' => array('civicrm_mailing', 'civicrm_mailing_event_queue', 'civicrm_mailing_event_subscribe'),
125 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=mailing'),
126 ),
127 'rel_table_cases' => array(
128 'title' => ts('Cases'),
129 'tables' => array('civicrm_case_contact'),
130 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=case'),
131 ),
132 'rel_table_grants' => array(
133 'title' => ts('Grants'),
134 'tables' => array('civicrm_grant'),
135 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=grant'),
136 ),
137 'rel_table_pcp' => array(
138 'title' => ts('PCPs'),
139 'tables' => array('civicrm_pcp'),
140 'url' => CRM_Utils_System::url('civicrm/contribute/pcp/manage', 'reset=1'),
141 ),
142 'rel_table_pledges' => array(
143 'title' => ts('Pledges'),
144 'tables' => array('civicrm_pledge', 'civicrm_pledge_payment'),
145 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=pledge'),
146 ),
147 'rel_table_users' => array(
148 'title' => $title,
149 'tables' => array('civicrm_uf_match'),
150 'url' => $userRecordUrl,
151 ),
152 );
153
154 $relTables += self::getMultiValueCustomSets('relTables');
155
156 // Allow hook_civicrm_merge() to adjust $relTables
157 CRM_Utils_Hook::merge('relTables', $relTables);
158
159 // Cache the results in a static variable
160 Civi::$statics[__CLASS__]['relTables'] = $relTables;
161 }
162
163 return Civi::$statics[__CLASS__]['relTables'];
164 }
165
166 /**
167 * Returns the related tables groups for which a contact has any info entered.
168 *
169 * @param int $cid
170 *
171 * @return array
172 */
173 public static function getActiveRelTables($cid) {
174 $cid = (int) $cid;
175 $groups = array();
176
177 $relTables = self::relTables();
178 $cidRefs = self::cidRefs();
179 $eidRefs = self::eidRefs();
180 foreach ($relTables as $group => $params) {
181 $sqls = array();
182 foreach ($params['tables'] as $table) {
183 if (isset($cidRefs[$table])) {
184 foreach ($cidRefs[$table] as $field) {
185 $sqls[] = "SELECT COUNT(*) AS count FROM $table WHERE $field = $cid";
186 }
187 }
188 if (isset($eidRefs[$table])) {
189 foreach ($eidRefs[$table] as $entityTable => $entityId) {
190 $sqls[] = "SELECT COUNT(*) AS count FROM $table WHERE $entityId = $cid AND $entityTable = 'civicrm_contact'";
191 }
192 }
193 foreach ($sqls as $sql) {
194 if (CRM_Core_DAO::singleValueQuery($sql) > 0) {
195 $groups[] = $group;
196 }
197 }
198 }
199 }
200 return array_unique($groups);
201 }
202
203 /**
204 * Get array tables and fields that reference civicrm_contact.id.
205 *
206 * This includes core tables, custom group tables, tables added by the merge
207 * hook and (somewhat randomly) the entity_tag table.
208 *
209 * Refer to CRM-17454 for information on the danger of querying the information
210 * schema to derive this.
211 *
212 * This function calls the merge hook but the entityTypes hook is the recommended
213 * way to add tables to this result.
214 */
215 public static function cidRefs() {
216 if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
217 return \Civi::$statics[__CLASS__]['contact_references'];
218 }
219 $contactReferences = array();
220 $coreReferences = CRM_Core_DAO::getReferencesToTable('civicrm_contact');
221 foreach ($coreReferences as $coreReference) {
222 if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
223 $contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
224 }
225 }
226 self::addCustomTablesExtendingContactsToCidRefs($contactReferences);
227
228 // FixME for time being adding below line statically as no Foreign key constraint defined for table 'civicrm_entity_tag'
229 $contactReferences['civicrm_entity_tag'][] = 'entity_id';
230
231 // Allow hook_civicrm_merge() to adjust $cidRefs.
232 // Note that if entities are registered using the entityTypes hook there
233 // is no need to use this hook.
234 CRM_Utils_Hook::merge('cidRefs', $contactReferences);
235 \Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
236 return \Civi::$statics[__CLASS__]['contact_references'];
237 }
238
239 /**
240 * Return tables and their fields referencing civicrm_contact.contact_id with entity_id
241 */
242 public static function eidRefs() {
243 static $eidRefs;
244 if (!$eidRefs) {
245 // FIXME: this should be generated dynamically from the schema
246 // tables that reference contacts with entity_{id,table}
247 $eidRefs = array(
248 'civicrm_acl' => array('entity_table' => 'entity_id'),
249 'civicrm_acl_entity_role' => array('entity_table' => 'entity_id'),
250 'civicrm_entity_file' => array('entity_table' => 'entity_id'),
251 'civicrm_log' => array('entity_table' => 'entity_id'),
252 'civicrm_mailing_group' => array('entity_table' => 'entity_id'),
253 'civicrm_note' => array('entity_table' => 'entity_id'),
254 );
255
256 // Allow hook_civicrm_merge() to adjust $eidRefs
257 CRM_Utils_Hook::merge('eidRefs', $eidRefs);
258 }
259 return $eidRefs;
260 }
261
262 /**
263 * Return tables using locations.
264 */
265 public static function locTables() {
266 static $locTables;
267 if (!$locTables) {
268 $locTables = array('civicrm_email', 'civicrm_address', 'civicrm_phone');
269
270 // Allow hook_civicrm_merge() to adjust $locTables
271 CRM_Utils_Hook::merge('locTables', $locTables);
272 }
273 return $locTables;
274 }
275
276 /**
277 * We treat multi-valued custom sets as "related tables" similar to activities, contributions, etc.
278 * @param string $request
279 * 'relTables' or 'cidRefs'.
280 * @return array
281 * @see CRM-13836
282 */
283 public static function getMultiValueCustomSets($request) {
284
285 if (!isset(Civi::$statics[__CLASS__]['multiValueCustomSets'])) {
286 $data = array(
287 'relTables' => array(),
288 'cidRefs' => array(),
289 );
290 $result = civicrm_api3('custom_group', 'get', array(
291 'is_multiple' => 1,
292 'extends' => array('IN' => array('Individual', 'Organization', 'Household', 'Contact')),
293 'return' => array('id', 'title', 'table_name', 'style'),
294 ));
295 foreach ($result['values'] as $custom) {
296 $data['cidRefs'][$custom['table_name']] = array('entity_id');
297 $urlSuffix = $custom['style'] == 'Tab' ? '&selectedChild=custom_' . $custom['id'] : '';
298 $data['relTables']['rel_table_custom_' . $custom['id']] = array(
299 'title' => $custom['title'],
300 'tables' => array($custom['table_name']),
301 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid' . $urlSuffix),
302 );
303 }
304
305 // Store the result in a static variable cache
306 Civi::$statics[__CLASS__]['multiValueCustomSets'] = $data;
307 }
308
309 return Civi::$statics[__CLASS__]['multiValueCustomSets'][$request];
310 }
311
312 /**
313 * Tables which require custom processing should declare functions to call here.
314 * Doing so will override normal processing.
315 */
316 public static function cpTables() {
317 static $tables;
318 if (!$tables) {
319 $tables = array(
320 'civicrm_case_contact' => array('CRM_Case_BAO_Case' => 'mergeContacts'),
321 'civicrm_group_contact' => array('CRM_Contact_BAO_GroupContact' => 'mergeGroupContact'),
322 // Empty array == do nothing - this table is handled by mergeGroupContact
323 'civicrm_subscription_history' => array(),
324 'civicrm_relationship' => array('CRM_Contact_BAO_Relationship' => 'mergeRelationships'),
325 );
326 }
327 return $tables;
328 }
329
330 /**
331 * Return payment related table.
332 */
333 public static function paymentTables() {
334 static $tables;
335 if (!$tables) {
336 $tables = array('civicrm_pledge', 'civicrm_membership', 'civicrm_participant');
337 }
338 return $tables;
339 }
340
341 /**
342 * Return payment update Query.
343 *
344 * @param string $tableName
345 * @param int $mainContactId
346 * @param int $otherContactId
347 *
348 * @return array
349 */
350 public static function paymentSql($tableName, $mainContactId, $otherContactId) {
351 $sqls = array();
352 if (!$tableName || !$mainContactId || !$otherContactId) {
353 return $sqls;
354 }
355
356 $paymentTables = self::paymentTables();
357 if (!in_array($tableName, $paymentTables)) {
358 return $sqls;
359 }
360
361 switch ($tableName) {
362 case 'civicrm_pledge':
363 $sqls[] = "
364 UPDATE IGNORE civicrm_contribution contribution
365 INNER JOIN civicrm_pledge_payment payment ON ( payment.contribution_id = contribution.id )
366 INNER JOIN civicrm_pledge pledge ON ( pledge.id = payment.pledge_id )
367 SET contribution.contact_id = $mainContactId
368 WHERE pledge.contact_id = $otherContactId";
369 break;
370
371 case 'civicrm_membership':
372 $sqls[] = "
373 UPDATE IGNORE civicrm_contribution contribution
374 INNER JOIN civicrm_membership_payment payment ON ( payment.contribution_id = contribution.id )
375 INNER JOIN civicrm_membership membership ON ( membership.id = payment.membership_id )
376 SET contribution.contact_id = $mainContactId
377 WHERE membership.contact_id = $otherContactId";
378 break;
379
380 case 'civicrm_participant':
381 $sqls[] = "
382 UPDATE IGNORE civicrm_contribution contribution
383 INNER JOIN civicrm_participant_payment payment ON ( payment.contribution_id = contribution.id )
384 INNER JOIN civicrm_participant participant ON ( participant.id = payment.participant_id )
385 SET contribution.contact_id = $mainContactId
386 WHERE participant.contact_id = $otherContactId";
387 break;
388 }
389
390 return $sqls;
391 }
392
393 /**
394 * @param int $mainId
395 * @param int $otherId
396 * @param string $tableName
397 * @param array $tableOperations
398 * @param string $mode
399 *
400 * @return array
401 */
402 public static function operationSql($mainId, $otherId, $tableName, $tableOperations = array(), $mode = 'add') {
403 $sqls = array();
404 if (!$tableName || !$mainId || !$otherId) {
405 return $sqls;
406 }
407
408 switch ($tableName) {
409 case 'civicrm_membership':
410 if (array_key_exists($tableName, $tableOperations) && $tableOperations[$tableName]['add']) {
411 break;
412 }
413 if ($mode == 'add') {
414 $sqls[] = "
415 DELETE membership1.* FROM civicrm_membership membership1
416 INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = membership2.membership_type_id
417 AND membership1.contact_id = {$mainId}
418 AND membership2.contact_id = {$otherId} ";
419 }
420 if ($mode == 'payment') {
421 $sqls[] = "
422 DELETE contribution.* FROM civicrm_contribution contribution
423 INNER JOIN civicrm_membership_payment payment ON payment.contribution_id = contribution.id
424 INNER JOIN civicrm_membership membership1 ON membership1.id = payment.membership_id
425 AND membership1.contact_id = {$mainId}
426 INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = membership2.membership_type_id
427 AND membership2.contact_id = {$otherId}";
428 }
429 break;
430
431 case 'civicrm_uf_match':
432 // normal queries won't work for uf_match since that will lead to violation of unique constraint,
433 // failing to meet intended result. Therefore we introduce this additional query:
434 $sqls[] = "DELETE FROM civicrm_uf_match WHERE contact_id = {$mainId}";
435 break;
436 }
437
438 return $sqls;
439 }
440
441 /**
442 * Based on the provided two contact_ids and a set of tables, move the
443 * belongings of the other contact to the main one.
444 *
445 * @param int $mainId
446 * @param int $otherId
447 * @param bool $tables
448 * @param array $tableOperations
449 * @param array $customTableToCopyFrom
450 */
451 public static function moveContactBelongings($mainId, $otherId, $tables = FALSE, $tableOperations = array(), $customTableToCopyFrom = NULL) {
452 $cidRefs = self::cidRefs();
453 $eidRefs = self::eidRefs();
454 $cpTables = self::cpTables();
455 $paymentTables = self::paymentTables();
456
457 // getting all custom tables
458 $customTables = array();
459 if ($customTableToCopyFrom !== NULL) {
460 self::addCustomTablesExtendingContactsToCidRefs($customTables);
461 $customTables = array_keys($customTables);
462 }
463
464 $affected = array_merge(array_keys($cidRefs), array_keys($eidRefs));
465
466 // if there aren't any specific tables, don't affect the ones handled by relTables()
467 // also don't affect tables in locTables() CRM-15658
468 $relTables = self::relTables();
469 $handled = self::locTables();
470
471 foreach ($relTables as $params) {
472 $handled = array_merge($handled, $params['tables']);
473 }
474 $affected = array_diff($affected, $handled);
475 $affected = array_unique(array_merge($affected, $tables));
476
477 $mainId = (int) $mainId;
478 $otherId = (int) $otherId;
479 $multi_value_tables = array_keys(CRM_Dedupe_Merger::getMultiValueCustomSets('cidRefs'));
480
481 $sqls = array();
482 foreach ($affected as $table) {
483 // skipping non selected single-value custom table's value migration
484 if (!in_array($table, $multi_value_tables)) {
485 if ($customTableToCopyFrom !== NULL && in_array($table, $customTables) && !in_array($table, $customTableToCopyFrom)) {
486 continue;
487 }
488 }
489
490 // Call custom processing function for objects that require it
491 if (isset($cpTables[$table])) {
492 foreach ($cpTables[$table] as $className => $fnName) {
493 $className::$fnName($mainId, $otherId, $sqls);
494 }
495 // Skip normal processing
496 continue;
497 }
498
499 // use UPDATE IGNORE + DELETE query pair to skip on situations when
500 // there's a UNIQUE restriction on ($field, some_other_field) pair
501 if (isset($cidRefs[$table])) {
502 foreach ($cidRefs[$table] as $field) {
503 // carry related contributions CRM-5359
504 if (in_array($table, $paymentTables)) {
505 $paymentSqls = self::paymentSql($table, $mainId, $otherId);
506 $sqls = array_merge($sqls, $paymentSqls);
507
508 if (!empty($tables) && !in_array('civicrm_contribution', $tables)) {
509 $payOprSqls = self::operationSql($mainId, $otherId, $table, $tableOperations, 'payment');
510 $sqls = array_merge($sqls, $payOprSqls);
511 }
512 }
513
514 $preOperationSqls = self::operationSql($mainId, $otherId, $table, $tableOperations);
515 $sqls = array_merge($sqls, $preOperationSqls);
516
517 if ($customTableToCopyFrom !== NULL && in_array($table, $customTableToCopyFrom) && !self::customRecordExists($mainId, $table, $field)) {
518 $sqls[] = "INSERT INTO $table ($field) VALUES ($mainId)";
519 }
520 $sqls[] = "UPDATE IGNORE $table SET $field = $mainId WHERE $field = $otherId";
521 $sqls[] = "DELETE FROM $table WHERE $field = $otherId";
522 }
523 }
524
525 if (isset($eidRefs[$table])) {
526 foreach ($eidRefs[$table] as $entityTable => $entityId) {
527 $sqls[] = "UPDATE IGNORE $table SET $entityId = $mainId WHERE $entityId = $otherId AND $entityTable = 'civicrm_contact'";
528 $sqls[] = "DELETE FROM $table WHERE $entityId = $otherId AND $entityTable = 'civicrm_contact'";
529 }
530 }
531 }
532
533 // Allow hook_civicrm_merge() to add SQL statements for the merge operation.
534 CRM_Utils_Hook::merge('sqls', $sqls, $mainId, $otherId, $tables);
535
536 // call the SQL queries in one transaction
537 $transaction = new CRM_Core_Transaction();
538 foreach ($sqls as $sql) {
539 CRM_Core_DAO::executeQuery($sql, array(), TRUE, NULL, TRUE);
540 }
541 CRM_Dedupe_Merger::addMembershipToRealtedContacts($mainId);
542 $transaction->commit();
543 }
544
545 /**
546 * Given a contact ID, will check if a record exists in given table.
547 *
548 * @param $contactID
549 * @param $table
550 * @param $idField
551 * Field where the contact's ID is stored in the table
552 *
553 * @return bool
554 * True if a record is found for the given contact ID, false otherwise
555 */
556 private static function customRecordExists($contactID, $table, $idField) {
557 $sql = "
558 SELECT COUNT(*) AS count
559 FROM $table
560 WHERE $idField = $contactID
561 ";
562 $dbResult = CRM_Core_DAO::executeQuery($sql);
563 $dbResult->fetch();
564
565 if ($dbResult->count > 0) {
566 return TRUE;
567 }
568
569 return FALSE;
570 }
571
572 /**
573 * Load all non-empty fields for the contacts
574 *
575 * @param array $main
576 * Contact details.
577 * @param array $other
578 * Contact details.
579 *
580 * @return array
581 */
582 public static function retrieveFields($main, $other) {
583 $result = array(
584 'contact' => array(),
585 'custom' => array(),
586 );
587 foreach (self::getContactFields() as $validField) {
588 // CRM-17556 Get all non-empty fields, to make comparison easier
589 if (!empty($main[$validField]) || !empty($other[$validField])) {
590 $result['contact'][] = $validField;
591 }
592 }
593
594 $mainEvs = CRM_Core_BAO_CustomValueTable::getEntityValues($main['id']);
595 $otherEvs = CRM_Core_BAO_CustomValueTable::getEntityValues($other['id']);
596 $keys = array_unique(array_merge(array_keys($mainEvs), array_keys($otherEvs)));
597 foreach ($keys as $key) {
598 // Exclude multi-value fields CRM-13836
599 if (strpos($key, '_')) {
600 continue;
601 }
602 $key1 = CRM_Utils_Array::value($key, $mainEvs);
603 $key2 = CRM_Utils_Array::value($key, $otherEvs);
604 // CRM-17556 Get all non-empty fields, to make comparison easier
605 if (!empty($key1) || !empty($key2)) {
606 $result['custom'][] = $key;
607 }
608 }
609 return $result;
610 }
611
612 /**
613 * Batch merge a set of contacts based on rule-group and group.
614 *
615 * @param int $rgid
616 * Rule group id.
617 * @param int $gid
618 * Group id.
619 * @param string $mode
620 * Helps decide how to behave when there are conflicts.
621 * A 'safe' value skips the merge if there are any un-resolved conflicts, wheras 'aggressive'
622 * mode does a force merge.
623 * @param int $batchLimit number of merges to carry out in one batch.
624 * @param int $isSelected if records with is_selected column needs to be processed.
625 *
626 * @param array $criteria
627 * Criteria to use in the filter.
628 *
629 * @param bool $checkPermissions
630 * Respect logged in user permissions.
631 *
632 * @return array|bool
633 */
634 public static function batchMerge($rgid, $gid = NULL, $mode = 'safe', $batchLimit = 1, $isSelected = 2, $criteria = array(), $checkPermissions = TRUE) {
635 $redirectForPerformance = ($batchLimit > 1) ? TRUE : FALSE;
636 $reloadCacheIfEmpty = (!$redirectForPerformance && $isSelected == 2);
637 $dupePairs = self::getDuplicatePairs($rgid, $gid, $reloadCacheIfEmpty, $batchLimit, $isSelected, '', ($mode == 'aggressive'), $criteria, $checkPermissions);
638
639 $cacheParams = array(
640 'cache_key_string' => self::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions),
641 // @todo stop passing these parameters in & instead calculate them in the merge function based
642 // on the 'real' params like $isRespectExclusions $batchLimit and $isSelected.
643 'join' => self::getJoinOnDedupeTable(),
644 'where' => self::getWhereString($batchLimit, $isSelected),
645 );
646 return CRM_Dedupe_Merger::merge($dupePairs, $cacheParams, $mode, $redirectForPerformance, $checkPermissions);
647 }
648
649 /**
650 * Get the string to join the prevnext cache to the dedupe table.
651 *
652 * @return string
653 * The join string to join prevnext cache on the dedupe table.
654 */
655 public static function getJoinOnDedupeTable() {
656 return "
657 LEFT JOIN civicrm_dedupe_exception de
658 ON (
659 pn.entity_id1 = de.contact_id1
660 AND pn.entity_id2 = de.contact_id2 )
661 ";
662 }
663
664 /**
665 * Get where string for dedupe join.
666 *
667 * @param int $batchLimit
668 * @param bool $isSelected
669 *
670 * @return string
671 */
672 protected static function getWhereString($batchLimit, $isSelected) {
673 $where = "de.id IS NULL";
674 if ($isSelected === 0 || $isSelected === 1) {
675 $where .= " AND pn.is_selected = {$isSelected}";
676 }
677 // else consider all dupe pairs
678 // @todo Adding limit to Where??!!
679 if ($batchLimit) {
680 $where .= " LIMIT {$batchLimit}";
681 }
682 return $where;
683 }
684
685 /**
686 * Update the statistics for the merge set.
687 *
688 * @param string $cacheKeyString
689 * @param array $result
690 */
691 public static function updateMergeStats($cacheKeyString, $result = array()) {
692 // gather latest stats
693 $merged = count($result['merged']);
694 $skipped = count($result['skipped']);
695
696 if ($merged <= 0 && $skipped <= 0) {
697 return;
698 }
699
700 // get previous stats
701 $previousStats = CRM_Core_BAO_PrevNextCache::retrieve("{$cacheKeyString}_stats");
702 if (!empty($previousStats)) {
703 if ($previousStats[0]['merged']) {
704 $merged = $merged + $previousStats[0]['merged'];
705 }
706 if ($previousStats[0]['skipped']) {
707 $skipped = $skipped + $previousStats[0]['skipped'];
708 }
709 }
710
711 // delete old stats
712 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
713
714 // store the updated stats
715 $data = array(
716 'merged' => $merged,
717 'skipped' => $skipped,
718 );
719 $data = CRM_Core_DAO::escapeString(serialize($data));
720
721 $values = array();
722 $values[] = " ( 'civicrm_contact', 0, 0, '{$cacheKeyString}_stats', '$data' ) ";
723 CRM_Core_BAO_PrevNextCache::setItem($values);
724 }
725
726 /**
727 * Delete information about merges for the given string.
728 *
729 * @param $cacheKeyString
730 */
731 public static function resetMergeStats($cacheKeyString) {
732 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, "{$cacheKeyString}_stats");
733 }
734
735 /**
736 * Get merge outcome statistics.
737 *
738 * @param string $cacheKeyString
739 *
740 * @return array
741 * Array of how many were merged and how many were skipped.
742 */
743 public static function getMergeStats($cacheKeyString) {
744 $stats = CRM_Core_BAO_PrevNextCache::retrieve("{$cacheKeyString}_stats");
745 if (!empty($stats)) {
746 $stats = $stats[0];
747 }
748 return $stats;
749 }
750
751 /**
752 * Get merge statistics message.
753 *
754 * @param array $stats
755 *
756 * @return string
757 */
758 public static function getMergeStatsMsg($stats) {
759 $msg = '';
760 if (!empty($stats['merged'])) {
761 $msg = '<p>' . ts('One contact merged.', array('count' => $stats['merged'], 'plural' => '%count contacts merged.')) . '</p>';
762 }
763 if (!empty($stats['skipped'])) {
764 $msg .= '<p>' . ts('One contact was skipped.', array('count' => $stats['skipped'], 'plural' => '%count contacts were skipped.')) . '</p>';
765 }
766 return $msg;
767 }
768
769 /**
770 * Merge given set of contacts. Performs core operation.
771 *
772 * @param array $dupePairs
773 * Set of pair of contacts for whom merge is to be done.
774 * @param array $cacheParams
775 * Prev-next-cache params based on which next pair of contacts are computed.
776 * Generally used with batch-merge.
777 * @param string $mode
778 * Helps decide how to behave when there are conflicts.
779 * A 'safe' value skips the merge if there are any un-resolved conflicts.
780 * Does a force merge otherwise (aggressive mode).
781 *
782 * @param bool $redirectForPerformance
783 * Redirect to a url for batch processing.
784 *
785 * @param bool $checkPermissions
786 * Respect logged in user permissions.
787 *
788 * @return array|bool
789 */
790 public static function merge($dupePairs = array(), $cacheParams = array(), $mode = 'safe',
791 $redirectForPerformance = FALSE, $checkPermissions = TRUE
792 ) {
793 $cacheKeyString = CRM_Utils_Array::value('cache_key_string', $cacheParams);
794 $resultStats = array('merged' => array(), 'skipped' => array());
795
796 // we don't want dupe caching to get reset after every-merge, and therefore set the
797 CRM_Core_Config::setPermitCacheFlushMode(FALSE);
798 $deletedContacts = array();
799
800 while (!empty($dupePairs)) {
801 foreach ($dupePairs as $index => $dupes) {
802 if (in_array($dupes['dstID'], $deletedContacts) || in_array($dupes['srcID'], $deletedContacts)) {
803 unset($dupePairs[$index]);
804 continue;
805 }
806 CRM_Utils_Hook::merge('flip', $dupes, $dupes['dstID'], $dupes['srcID']);
807 $mainId = $dupes['dstID'];
808 $otherId = $dupes['srcID'];
809
810 if (!$mainId || !$otherId) {
811 // return error
812 return FALSE;
813 }
814 // Generate var $migrationInfo. The variable structure is exactly same as
815 // $formValues submitted during a UI merge for a pair of contacts.
816 $rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($mainId, $otherId, $checkPermissions);
817 // add additional details that we might need to resolve conflicts
818 $rowsElementsAndInfo['migration_info']['main_details'] = &$rowsElementsAndInfo['main_details'];
819 $rowsElementsAndInfo['migration_info']['other_details'] = &$rowsElementsAndInfo['other_details'];
820 $rowsElementsAndInfo['migration_info']['rows'] = &$rowsElementsAndInfo['rows'];
821
822 self::dedupePair($rowsElementsAndInfo['migration_info'], $resultStats, $deletedContacts, $mode, $checkPermissions, $mainId, $otherId, $cacheKeyString);
823 }
824
825 if ($cacheKeyString && !$redirectForPerformance) {
826 // retrieve next pair of dupes
827 // @todo call getDuplicatePairs.
828 $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString,
829 $cacheParams['join'],
830 $cacheParams['where'],
831 0,
832 0,
833 array(),
834 '',
835 FALSE
836 );
837 }
838 else {
839 // do not proceed. Terminate the loop
840 unset($dupePairs);
841 }
842 }
843
844 CRM_Dedupe_Merger::updateMergeStats($cacheKeyString, $resultStats);
845 return $resultStats;
846 }
847
848 /**
849 * A function which uses various rules / algorithms for choosing which contact to bias to
850 * when there's a conflict (to handle "gotchas"). Plus the safest route to merge.
851 *
852 * @param int $mainId
853 * Main contact with whom merge has to happen.
854 * @param int $otherId
855 * Duplicate contact which would be deleted after merge operation.
856 * @param array $migrationInfo
857 * Array of information about which elements to merge.
858 * @param string $mode
859 * Helps decide how to behave when there are conflicts.
860 * - A 'safe' value skips the merge if there are any un-resolved conflicts.
861 * - Does a force merge otherwise (aggressive mode).
862 *
863 * @param array $conflicts
864 *
865 * @return bool
866 */
867 public static function skipMerge($mainId, $otherId, &$migrationInfo, $mode = 'safe', &$conflicts = array()) {
868
869 $originalMigrationInfo = $migrationInfo;
870 foreach ($migrationInfo as $key => $val) {
871 if ($val === "null") {
872 // Rule: Never overwrite with an empty value (in any mode)
873 unset($migrationInfo[$key]);
874 continue;
875 }
876 elseif ((in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) or
877 substr($key, 0, 12) == 'move_custom_'
878 ) and $val != NULL
879 ) {
880 // Rule: If both main-contact, and other-contact have a field with a
881 // different value, then let $mode decide if to merge it or not
882 if (
883 (!empty($migrationInfo['rows'][$key]['main'])
884 // For custom fields a 0 (e.g in an int field) could be a true conflict. This
885 // is probably true for other fields too - e.g. 'do_not_email' but
886 // leaving that investigation as a @todo - until tests can be written.
887 // Note the handling of this has test coverage - although the data-typing
888 // of '0' feels flakey we have insurance.
889 || ($migrationInfo['rows'][$key]['main'] === '0' && substr($key, 0, 12) == 'move_custom_')
890 )
891 && $migrationInfo['rows'][$key]['main'] != $migrationInfo['rows'][$key]['other']
892 ) {
893
894 // note it down & lets wait for response from the hook.
895 // For no response $mode will decide if to skip this merge
896 $conflicts[$key] = NULL;
897 }
898 }
899 elseif (substr($key, 0, 14) == 'move_location_' and $val != NULL) {
900 $locField = explode('_', $key);
901 $fieldName = $locField[2];
902 $fieldCount = $locField[3];
903
904 // Rule: Catch address conflicts (same address type on both contacts)
905 if (
906 isset($migrationInfo['main_details']['location_blocks'][$fieldName]) &&
907 !empty($migrationInfo['main_details']['location_blocks'][$fieldName])
908 ) {
909
910 // Load the address we're inspecting from the 'other' contact
911 $addressRecord = $migrationInfo['other_details']['location_blocks'][$fieldName][$fieldCount];
912 $addressRecordLocTypeId = CRM_Utils_Array::value('location_type_id', $addressRecord);
913
914 // If it exists on the 'main' contact already, skip it. Otherwise
915 // if the location type exists already, log a conflict.
916 foreach ($migrationInfo['main_details']['location_blocks'][$fieldName] as $mainAddressKey => $mainAddressRecord) {
917 if (self::locationIsSame($addressRecord, $mainAddressRecord)) {
918 unset($migrationInfo[$key]);
919 break;
920 }
921 elseif ($addressRecordLocTypeId == $mainAddressRecord['location_type_id']) {
922 $conflicts[$key] = NULL;
923 break;
924 }
925 }
926 }
927
928 // For other locations, don't merge/add if the values are the same
929 elseif (CRM_Utils_Array::value('main', $migrationInfo['rows'][$key]) == $migrationInfo['rows'][$key]['other']) {
930 unset($migrationInfo[$key]);
931 }
932 }
933 }
934
935 // A hook to implement other algorithms for choosing which contact to bias to when
936 // there's a conflict (to handle "gotchas"). fields_in_conflict could be modified here
937 // merge happens with new values filled in here. For a particular field / row not to be merged
938 // field should be unset from fields_in_conflict.
939 $migrationData = array(
940 'old_migration_info' => $originalMigrationInfo,
941 'mode' => $mode,
942 'fields_in_conflict' => $conflicts,
943 'merge_mode' => $mode,
944 'migration_info' => $migrationInfo,
945 );
946 CRM_Utils_Hook::merge('batch', $migrationData, $mainId, $otherId);
947 $conflicts = $migrationData['fields_in_conflict'];
948 // allow hook to override / manipulate migrationInfo as well
949 $migrationInfo = $migrationData['migration_info'];
950
951 if (!empty($conflicts)) {
952 foreach ($conflicts as $key => $val) {
953 if ($val === NULL and $mode == 'safe') {
954 // un-resolved conflicts still present. Lets skip this merge after saving the conflict / reason.
955 return TRUE;
956 }
957 else {
958 // copy over the resolved values
959 $migrationInfo[$key] = $val;
960 }
961 }
962 // if there are conflicts and mode is aggressive, allow hooks to decide if to skip merges
963 if (array_key_exists('skip_merge', $migrationData)) {
964 return (bool) $migrationData['skip_merge'];
965 }
966 }
967 return FALSE;
968 }
969
970 /**
971 * Compare 2 addresses to see if they are the same.
972 *
973 * @param array $mainAddress
974 * @param array $comparisonAddress
975 *
976 * @return bool
977 */
978 static public function locationIsSame($mainAddress, $comparisonAddress) {
979 $keysToIgnore = array('id', 'is_primary', 'is_billing', 'manual_geo_code', 'contact_id', 'reset_date', 'hold_date');
980 foreach ($comparisonAddress as $field => $value) {
981 if (in_array($field, $keysToIgnore)) {
982 continue;
983 }
984 if ((!empty($value) || $value === '0') && isset($mainAddress[$field]) && $mainAddress[$field] != $value) {
985 return FALSE;
986 }
987 }
988 return TRUE;
989 }
990
991 /**
992 * A function to build an array of information about location blocks that is
993 * required when merging location fields
994 *
995 * @return array
996 */
997 public static function getLocationBlockInfo() {
998 $locationBlocks = array(
999 'address' => array(
1000 'label' => 'Address',
1001 'displayField' => 'display',
1002 'sortString' => 'location_type_id',
1003 'hasLocation' => TRUE,
1004 'hasType' => FALSE,
1005 ),
1006 'email' => array(
1007 'label' => 'Email',
1008 'displayField' => 'email',
1009 'sortString' => 'location_type_id',
1010 'hasLocation' => TRUE,
1011 'hasType' => FALSE,
1012 ),
1013 'im' => array(
1014 'label' => 'IM',
1015 'displayField' => 'name',
1016 'sortString' => 'location_type_id,provider_id',
1017 'hasLocation' => TRUE,
1018 'hasType' => 'provider_id',
1019 ),
1020 'phone' => array(
1021 'label' => 'Phone',
1022 'displayField' => 'phone',
1023 'sortString' => 'location_type_id,phone_type_id',
1024 'hasLocation' => TRUE,
1025 'hasType' => 'phone_type_id',
1026 ),
1027 'website' => array(
1028 'label' => 'Website',
1029 'displayField' => 'url',
1030 'sortString' => 'website_type_id',
1031 'hasLocation' => FALSE,
1032 'hasType' => 'website_type_id',
1033 ),
1034 );
1035 return $locationBlocks;
1036 }
1037
1038 /**
1039 * A function to build an array of information required by merge function and the merge UI.
1040 *
1041 * @param int $mainId
1042 * Main contact with whom merge has to happen.
1043 * @param int $otherId
1044 * Duplicate contact which would be deleted after merge operation.
1045 * @param bool $checkPermissions
1046 * Should the logged in user's permissions be ignore. Setting this to false is
1047 * highly risky as it could cause data to be lost due to conflicts not showing up.
1048 * OTOH there is a risk a merger might view custom data they do not have permission to.
1049 * Hence for now only making this really explicit and making it reflect perms in
1050 * an api call.
1051 *
1052 * @todo review permissions issue!
1053 *
1054 * @return array|bool|int
1055 *
1056 * rows => An array of arrays, each is row of merge information for the table
1057 * Format: move_fieldname, eg: move_contact_type
1058 * main => Value associated with the main contact
1059 * other => Value associated with the other contact
1060 * title => The title of the field to display in the merge table
1061 *
1062 * elements => An array of form elements for the merge UI
1063 *
1064 * rel_table_elements => An array of form elements for the merge UI for
1065 * entities related to the contact (eg: checkbox to move 'mailings')
1066 *
1067 * rel_tables => Stores the tables that have related entities for the contact
1068 * for example mailings, groups
1069 *
1070 * main_details => An array of core contact field values, eg: first_name, etc.
1071 * location_blocks => An array of location block data for the main contact
1072 * stored as the 'result' of an API call.
1073 * eg: main_details['location_blocks']['address'][0]['id']
1074 * eg: main_details['location_blocks']['email'][1]['id']
1075 *
1076 * other_details => As above, but for the 'other' contact
1077 *
1078 * migration_info => Stores the 'default' merge actions for each field which
1079 * is used when programatically merging contacts. It contains instructions
1080 * to move all fields from the 'other' contact to the 'main' contact, as
1081 * though the form had been submitted with those options.
1082 *
1083 * @throws \CRM_Core_Exception
1084 * @throws \CiviCRM_API3_Exception
1085 * @throws \Exception
1086 */
1087 public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissions = TRUE) {
1088 $qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
1089 $fields = self::getMergeFieldsMetadata();
1090
1091 $main = self::getMergeContactDetails($mainId);
1092 $other = self::getMergeContactDetails($otherId);
1093 $specialValues['main'] = self::getSpecialValues($main);
1094 $specialValues['other'] = self::getSpecialValues($other);
1095
1096 $compareFields = self::retrieveFields($main, $other);
1097
1098 $rows = $elements = $relTableElements = $migrationInfo = array();
1099
1100 foreach ($compareFields['contact'] as $field) {
1101 if ($field == 'contact_sub_type') {
1102 // CRM-15681 don't display sub-types in UI
1103 continue;
1104 }
1105 foreach (array('main', 'other') as $moniker) {
1106 $contact = &$$moniker;
1107 $value = CRM_Utils_Array::value($field, $contact);
1108 if (isset($specialValues[$moniker][$field]) && is_string($specialValues[$moniker][$field])) {
1109 $value = CRM_Core_DAO::VALUE_SEPARATOR . trim($specialValues[$moniker][$field], CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR;
1110 }
1111 $label = isset($specialValues[$moniker]["{$field}_display"]) ? $specialValues[$moniker]["{$field}_display"] : $value;
1112 if (!empty($fields[$field]['type']) && $fields[$field]['type'] == CRM_Utils_Type::T_DATE) {
1113 if ($value) {
1114 $value = str_replace('-', '', $value);
1115 $label = CRM_Utils_Date::customFormat($label);
1116 }
1117 else {
1118 $value = "null";
1119 }
1120 }
1121 elseif (!empty($fields[$field]['type']) && $fields[$field]['type'] == CRM_Utils_Type::T_BOOLEAN) {
1122 if ($label === '0') {
1123 $label = ts('[ ]');
1124 }
1125 if ($label === '1') {
1126 $label = ts('[x]');
1127 }
1128 }
1129 elseif ($field == 'prefix_id') {
1130 $label = CRM_Utils_Array::value('individual_prefix', $contact);
1131 }
1132 elseif ($field == 'suffix_id') {
1133 $label = CRM_Utils_Array::value('individual_suffix', $contact);
1134 }
1135 elseif ($field == 'gender_id' && !empty($value)) {
1136 $genderOptions = civicrm_api3('contact', 'getoptions', array('field' => 'gender_id'));
1137 $label = $genderOptions['values'][$value];
1138 }
1139 elseif ($field == 'current_employer_id' && !empty($value)) {
1140 $label = "$value (" . CRM_Contact_BAO_Contact::displayName($value) . ")";
1141 }
1142 $rows["move_$field"][$moniker] = $label;
1143 if ($moniker == 'other') {
1144 //CRM-14334
1145 if ($value === NULL || $value == '') {
1146 $value = 'null';
1147 }
1148 if ($value === 0 or $value === '0') {
1149 $value = $qfZeroBug;
1150 }
1151 if (is_array($value) && empty($value[1])) {
1152 $value[1] = NULL;
1153 }
1154
1155 // Display a checkbox to migrate, only if the values are different
1156 if ($value != $main[$field]) {
1157 $elements[] = array('advcheckbox', "move_$field", NULL, NULL, NULL, $value);
1158 }
1159
1160 $migrationInfo["move_$field"] = $value;
1161 }
1162 }
1163 $rows["move_$field"]['title'] = $fields[$field]['title'];
1164 }
1165
1166 // Handle location blocks.
1167 // @todo OpenID not in API yet, so is not supported here.
1168
1169 // Set up useful information about the location blocks
1170 $locationBlocks = self::getLocationBlockInfo();
1171
1172 $locations = array(
1173 'main' => array(),
1174 'other' => array(),
1175 );
1176
1177 // @todo This could probably be defined and used earlier
1178 $mergeTargets = array(
1179 'main' => $mainId,
1180 'other' => $otherId,
1181 );
1182
1183 foreach ($locationBlocks as $blockName => $blockInfo) {
1184
1185 // Collect existing fields from both 'main' and 'other' contacts first
1186 // This allows us to match up location/types when building the table rows
1187 foreach ($mergeTargets as $moniker => $cid) {
1188 $searchParams = array(
1189 'contact_id' => $cid,
1190 // CRM-17556 Order by field-specific criteria
1191 'options' => array(
1192 'sort' => $blockInfo['sortString'],
1193 ),
1194 );
1195 $values = civicrm_api3($blockName, 'get', $searchParams);
1196 if ($values['count']) {
1197 $cnt = 0;
1198 foreach ($values['values'] as $value) {
1199 $locations[$moniker][$blockName][$cnt] = $value;
1200 // Fix address display
1201 if ($blockName == 'address') {
1202 CRM_Core_BAO_Address::fixAddress($value);
1203 $locations[$moniker][$blockName][$cnt]['display'] = CRM_Utils_Address::format($value);
1204 }
1205
1206 $cnt++;
1207 }
1208 }
1209 }
1210
1211 // Now, build the table rows appropriately, based off the information on
1212 // the 'other' contact
1213 if (!empty($locations['other']) && !empty($locations['other'][$blockName])) {
1214 foreach ($locations['other'][$blockName] as $count => $value) {
1215
1216 $displayValue = $value[$blockInfo['displayField']];
1217
1218 // Add this value to the table rows
1219 $rows["move_location_{$blockName}_{$count}"]['other'] = $displayValue;
1220
1221 // CRM-17556 Only display 'main' contact value if it's the same location + type
1222 // Look it up from main values...
1223
1224 $lookupLocation = FALSE;
1225 if ($blockInfo['hasLocation']) {
1226 $lookupLocation = $value['location_type_id'];
1227 }
1228
1229 $lookupType = FALSE;
1230 if ($blockInfo['hasType']) {
1231 $lookupType = CRM_Utils_Array::value($blockInfo['hasType'], $value);
1232 }
1233
1234 // Hold ID of main contact's matching block
1235 $mainContactBlockId = 0;
1236
1237 if (!empty($locations['main'][$blockName])) {
1238 foreach ($locations['main'][$blockName] as $mainValueCheck) {
1239 // No location/type, or matching location and type
1240 if (
1241 (empty($lookupLocation) || $lookupLocation == $mainValueCheck['location_type_id'])
1242 && (empty($lookupType) || $lookupType == $mainValueCheck[$blockInfo['hasType']])
1243 ) {
1244 // Set this value as the default against the 'other' contact value
1245 $rows["move_location_{$blockName}_{$count}"]['main'] = $mainValueCheck[$blockInfo['displayField']];
1246 $rows["move_location_{$blockName}_{$count}"]['main_is_primary'] = $mainValueCheck['is_primary'];
1247 $mainContactBlockId = $mainValueCheck['id'];
1248 break;
1249 }
1250 }
1251 }
1252
1253 // Add checkbox to migrate data from 'other' to 'main'
1254 $elements[] = array('advcheckbox', "move_location_{$blockName}_{$count}");
1255
1256 // Add checkbox to set the 'other' location as primary
1257 $elements[] = array('advcheckbox', "location_blocks[$blockName][$count][set_other_primary]", NULL, ts('Set as primary'));
1258
1259 // Flag up this field to skipMerge function (@todo: do we need to?)
1260 $migrationInfo["move_location_{$blockName}_{$count}"] = 1;
1261
1262 // Add a hidden field to store the ID of the target main contact block
1263 $elements[] = array('hidden', "location_blocks[$blockName][$count][mainContactBlockId]", $mainContactBlockId);
1264
1265 // Setup variables
1266 $thisTypeId = FALSE;
1267 $thisLocId = FALSE;
1268
1269 // Provide a select drop-down for the location's location type
1270 // eg: Home, Work...
1271
1272 if ($blockInfo['hasLocation']) {
1273
1274 // Load the location options for this entity
1275 $locationOptions = civicrm_api3($blockName, 'getoptions', array('field' => 'location_type_id'));
1276
1277 $thisLocId = $value['location_type_id'];
1278
1279 // Put this field's location type at the top of the list
1280 $tmpIdList = $locationOptions['values'];
1281 $defaultLocId = array($thisLocId => $tmpIdList[$thisLocId]);
1282 unset($tmpIdList[$thisLocId]);
1283
1284 // Add the element
1285 $elements[] = array(
1286 'select',
1287 "location_blocks[$blockName][$count][locTypeId]",
1288 NULL,
1289 $defaultLocId + $tmpIdList,
1290 );
1291
1292 // Add the relevant information to the $migrationInfo
1293 // Keep location-type-id same as that of other-contact
1294 // @todo Check this logic out
1295 $migrationInfo['location_blocks'][$blockName][$count]['locTypeId'] = $thisLocId;
1296 if ($blockName != 'address') {
1297 $elements[] = array('advcheckbox', "location_blocks[{$blockName}][$count][operation]", NULL, ts('Add new'));
1298 // always use add operation
1299 $migrationInfo['location_blocks'][$blockName][$count]['operation'] = 1;
1300 }
1301
1302 }
1303
1304 // Provide a select drop-down for the location's type/provider
1305 // eg websites: Google+, Facebook...
1306
1307 if ($blockInfo['hasType']) {
1308
1309 // Load the type options for this entity
1310 $typeOptions = civicrm_api3($blockName, 'getoptions', array('field' => $blockInfo['hasType']));
1311
1312 $thisTypeId = CRM_Utils_Array::value($blockInfo['hasType'], $value);
1313
1314 // Put this field's location type at the top of the list
1315 $tmpIdList = $typeOptions['values'];
1316 $defaultTypeId = array($thisTypeId => CRM_Utils_Array::value($thisTypeId, $tmpIdList));
1317 unset($tmpIdList[$thisTypeId]);
1318
1319 // Add the element
1320 $elements[] = array(
1321 'select',
1322 "location_blocks[$blockName][$count][typeTypeId]",
1323 NULL,
1324 $defaultTypeId + $tmpIdList,
1325 );
1326
1327 // Add the information to the migrationInfo
1328 $migrationInfo['location_blocks'][$blockName][$count]['typeTypeId'] = $thisTypeId;
1329
1330 }
1331
1332 // Set the label for this row
1333 $rowTitle = $blockInfo['label'] . ' ' . ($count + 1);
1334 if (!empty($thisLocId)) {
1335 $rowTitle .= ' (' . $locationOptions['values'][$thisLocId] . ')';
1336 }
1337 if (!empty($thisTypeId)) {
1338 $rowTitle .= ' (' . $typeOptions['values'][$thisTypeId] . ')';
1339 }
1340 $rows["move_location_{$blockName}_$count"]['title'] = $rowTitle;
1341
1342 } // End loop through 'other' locations of this type
1343
1344 } // End if 'other' location for this type exists
1345
1346 } // End loop through each location block entity
1347
1348 // add the related tables and unset the ones that don't sport any of the duplicate contact's info
1349 $config = CRM_Core_Config::singleton();
1350 $mainUfId = CRM_Core_BAO_UFMatch::getUFId($mainId);
1351 $mainUser = NULL;
1352 if ($mainUfId) {
1353 // d6 compatible
1354 if ($config->userSystem->is_drupal == '1' && function_exists($mainUser)) {
1355 $mainUser = user_load($mainUfId);
1356 }
1357 elseif ($config->userFramework == 'Joomla') {
1358 $mainUser = JFactory::getUser($mainUfId);
1359 }
1360 }
1361 $otherUfId = CRM_Core_BAO_UFMatch::getUFId($otherId);
1362 $otherUser = NULL;
1363 if ($otherUfId) {
1364 // d6 compatible
1365 if ($config->userSystem->is_drupal == '1' && function_exists($mainUser)) {
1366 $otherUser = user_load($otherUfId);
1367 }
1368 elseif ($config->userFramework == 'Joomla') {
1369 $otherUser = JFactory::getUser($otherUfId);
1370 }
1371 }
1372
1373 $relTables = CRM_Dedupe_Merger::relTables();
1374 $activeRelTables = CRM_Dedupe_Merger::getActiveRelTables($otherId);
1375 $activeMainRelTables = CRM_Dedupe_Merger::getActiveRelTables($mainId);
1376 foreach ($relTables as $name => $null) {
1377 if (!in_array($name, $activeRelTables) &&
1378 !(($name == 'rel_table_users') && in_array($name, $activeMainRelTables))
1379 ) {
1380 unset($relTables[$name]);
1381 continue;
1382 }
1383
1384 $relTableElements[] = array('checkbox', "move_$name");
1385 $migrationInfo["move_$name"] = 1;
1386
1387 $relTables[$name]['main_url'] = str_replace('$cid', $mainId, $relTables[$name]['url']);
1388 $relTables[$name]['other_url'] = str_replace('$cid', $otherId, $relTables[$name]['url']);
1389 if ($name == 'rel_table_users') {
1390 $relTables[$name]['main_url'] = str_replace('%ufid', $mainUfId, $relTables[$name]['url']);
1391 $relTables[$name]['other_url'] = str_replace('%ufid', $otherUfId, $relTables[$name]['url']);
1392 $find = array('$ufid', '$ufname');
1393 if ($mainUser) {
1394 $replace = array($mainUfId, $mainUser->name);
1395 $relTables[$name]['main_title'] = str_replace($find, $replace, $relTables[$name]['title']);
1396 }
1397 if ($otherUser) {
1398 $replace = array($otherUfId, $otherUser->name);
1399 $relTables[$name]['other_title'] = str_replace($find, $replace, $relTables[$name]['title']);
1400 }
1401 }
1402 if ($name == 'rel_table_memberships') {
1403 $elements[] = array('checkbox', "operation[move_{$name}][add]", NULL, ts('add new'));
1404 $migrationInfo["operation"]["move_{$name}"]['add'] = 1;
1405 }
1406 }
1407 foreach ($relTables as $name => $null) {
1408 $relTables["move_$name"] = $relTables[$name];
1409 unset($relTables[$name]);
1410 }
1411
1412 // handle custom fields
1413 $mainTree = CRM_Core_BAO_CustomGroup::getTree($main['contact_type'], NULL, $mainId, -1,
1414 CRM_Utils_Array::value('contact_sub_type', $main), NULL, TRUE, NULL, TRUE, $checkPermissions
1415 );
1416 $otherTree = CRM_Core_BAO_CustomGroup::getTree($main['contact_type'], NULL, $otherId, -1,
1417 CRM_Utils_Array::value('contact_sub_type', $other), NULL, TRUE, NULL, TRUE, $checkPermissions
1418 );
1419 CRM_Core_DAO::freeResult();
1420
1421 foreach ($otherTree as $gid => $group) {
1422 $foundField = FALSE;
1423 if (!isset($group['fields'])) {
1424 continue;
1425 }
1426
1427 foreach ($group['fields'] as $fid => $field) {
1428 if (in_array($fid, $compareFields['custom'])) {
1429 if (!$foundField) {
1430 $rows["custom_group_$gid"]['title'] = $group['title'];
1431 $foundField = TRUE;
1432 }
1433 if (!empty($mainTree[$gid]['fields'][$fid]['customValue'])) {
1434 foreach ($mainTree[$gid]['fields'][$fid]['customValue'] as $valueId => $values) {
1435 $rows["move_custom_$fid"]['main'] = CRM_Core_BAO_CustomField::displayValue($values['data'], $fid);
1436 }
1437 }
1438 $value = "null";
1439 if (!empty($otherTree[$gid]['fields'][$fid]['customValue'])) {
1440 foreach ($otherTree[$gid]['fields'][$fid]['customValue'] as $valueId => $values) {
1441 $rows["move_custom_$fid"]['other'] = CRM_Core_BAO_CustomField::displayValue($values['data'], $fid);
1442 if ($values['data'] === 0 || $values['data'] === '0') {
1443 $values['data'] = $qfZeroBug;
1444 }
1445 $value = ($values['data']) ? $values['data'] : $value;
1446 }
1447 }
1448 $rows["move_custom_$fid"]['title'] = $field['label'];
1449
1450 $elements[] = array('advcheckbox', "move_custom_$fid", NULL, NULL, NULL, $value);
1451 $migrationInfo["move_custom_$fid"] = $value;
1452 }
1453 }
1454 }
1455
1456 $result = array(
1457 'rows' => $rows,
1458 'elements' => $elements,
1459 'rel_table_elements' => $relTableElements,
1460 'rel_tables' => $relTables,
1461 'main_details' => $main,
1462 'other_details' => $other,
1463 'migration_info' => $migrationInfo,
1464 );
1465
1466 $result['main_details']['location_blocks'] = $locations['main'];
1467 $result['other_details']['location_blocks'] = $locations['other'];
1468
1469 return $result;
1470 }
1471
1472 /**
1473 * Based on the provided two contact_ids and a set of tables, move the belongings of the
1474 * other contact to the main one - be it Location / CustomFields or Contact .. related info.
1475 * A superset of moveContactBelongings() function.
1476 *
1477 * @param int $mainId
1478 * Main contact with whom merge has to happen.
1479 * @param int $otherId
1480 * Duplicate contact which would be deleted after merge operation.
1481 *
1482 * @param $migrationInfo
1483 *
1484 * @param bool $checkPermissions
1485 * Respect logged in user permissions.
1486 *
1487 * @return bool
1488 */
1489 public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $checkPermissions = TRUE) {
1490 if (empty($migrationInfo)) {
1491 return FALSE;
1492 }
1493
1494 $qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
1495 $relTables = CRM_Dedupe_Merger::relTables();
1496 $submittedCustomFields = $moveTables = $locationMigrationInfo = $tableOperations = array();
1497
1498 foreach ($migrationInfo as $key => $value) {
1499 if ($value == $qfZeroBug) {
1500 $value = '0';
1501 }
1502
1503 if (substr($key, 0, 12) == 'move_custom_' && $value != NULL) {
1504 $submitted[substr($key, 5)] = $value;
1505 $submittedCustomFields[] = substr($key, 12);
1506 }
1507 elseif (in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) && $value != NULL) {
1508 $submitted[substr($key, 5)] = $value;
1509 }
1510 // Set up initial information for handling migration of location blocks
1511 elseif (substr($key, 0, 14) == 'move_location_' and $value != NULL) {
1512 $locationMigrationInfo[$key] = $value;
1513 }
1514 elseif (substr($key, 0, 15) == 'move_rel_table_' and $value == '1') {
1515 $moveTables = array_merge($moveTables, $relTables[substr($key, 5)]['tables']);
1516 if (array_key_exists('operation', $migrationInfo)) {
1517 foreach ($relTables[substr($key, 5)]['tables'] as $table) {
1518 if (array_key_exists($key, $migrationInfo['operation'])) {
1519 $tableOperations[$table] = $migrationInfo['operation'][$key];
1520 }
1521 }
1522 }
1523 }
1524 }
1525 self::mergeLocations($mainId, $otherId, $locationMigrationInfo, $migrationInfo);
1526
1527 // **** Do contact related migrations
1528 $customTablesToCopyValues = self::getAffectedCustomTables($submittedCustomFields);
1529 CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId, $moveTables, $tableOperations, $customTablesToCopyValues);
1530 unset($moveTables, $tableOperations);
1531
1532 // FIXME: fix gender, prefix and postfix, so they're edible by createProfileContact()
1533 $names['gender'] = array('newName' => 'gender_id', 'groupName' => 'gender');
1534 $names['individual_prefix'] = array('newName' => 'prefix_id', 'groupName' => 'individual_prefix');
1535 $names['individual_suffix'] = array('newName' => 'suffix_id', 'groupName' => 'individual_suffix');
1536 $names['communication_style'] = array('newName' => 'communication_style_id', 'groupName' => 'communication_style');
1537 $names['addressee'] = array('newName' => 'addressee_id', 'groupName' => 'addressee');
1538 $names['email_greeting'] = array('newName' => 'email_greeting_id', 'groupName' => 'email_greeting');
1539 $names['postal_greeting'] = array('newName' => 'postal_greeting_id', 'groupName' => 'postal_greeting');
1540 CRM_Core_OptionGroup::lookupValues($submitted, $names, TRUE);
1541
1542 // fix custom fields so they're edible by createProfileContact()
1543 static $treeCache = array();
1544 if (!array_key_exists($migrationInfo['main_details']['contact_type'], $treeCache)) {
1545 $treeCache[$migrationInfo['main_details']['contact_type']] = CRM_Core_BAO_CustomGroup::getTree($migrationInfo['main_details']['contact_type'], NULL, NULL, -1);
1546 }
1547
1548 $cFields = array();
1549 foreach ($treeCache[$migrationInfo['main_details']['contact_type']] as $key => $group) {
1550 if (!isset($group['fields'])) {
1551 continue;
1552 }
1553 foreach ($group['fields'] as $fid => $field) {
1554 $cFields[$fid]['attributes'] = $field;
1555 }
1556 }
1557
1558 if (!isset($submitted)) {
1559 $submitted = array();
1560 }
1561 foreach ($submitted as $key => $value) {
1562 if (substr($key, 0, 7) == 'custom_') {
1563 $fid = (int) substr($key, 7);
1564 if (empty($cFields[$fid])) {
1565 continue;
1566 }
1567 $htmlType = $cFields[$fid]['attributes']['html_type'];
1568 switch ($htmlType) {
1569 case 'File':
1570 $customFiles[] = $fid;
1571 unset($submitted["custom_$fid"]);
1572 break;
1573
1574 case 'Select Country':
1575 case 'Select State/Province':
1576 $submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
1577 break;
1578
1579 case 'Select Date':
1580 if ($cFields[$fid]['attributes']['is_view']) {
1581 $submitted[$key] = date('YmdHis', strtotime($submitted[$key]));
1582 }
1583 break;
1584
1585 case 'CheckBox':
1586 case 'AdvMulti-Select':
1587 case 'Multi-Select':
1588 case 'Multi-Select Country':
1589 case 'Multi-Select State/Province':
1590 // Merge values from both contacts for multivalue fields, CRM-4385
1591 // get the existing custom values from db.
1592 $customParams = array('entityID' => $mainId, $key => TRUE);
1593 $customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
1594 if (!empty($customfieldValues[$key])) {
1595 $existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
1596 if (is_array($existingValue) && !empty($existingValue)) {
1597 $mergeValue = $submittedCustomFields = array();
1598 if ($value == 'null') {
1599 // CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
1600 $submitted[$key] = $value;
1601 }
1602 else {
1603 if ($value) {
1604 $submittedCustomFields = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
1605 }
1606
1607 // CRM-19653: overwrite or add the existing custom field value with dupicate contact's
1608 // custom field value stored at $submittedCustomValue.
1609 foreach ($submittedCustomFields as $k => $v) {
1610 if ($v != '' && !in_array($v, $mergeValue)) {
1611 $mergeValue[] = $v;
1612 }
1613 }
1614
1615 //keep state and country as array format.
1616 //for checkbox and m-select format w/ VALUE_SEPARATOR
1617 if (in_array($htmlType, array(
1618 'CheckBox',
1619 'Multi-Select',
1620 'AdvMulti-Select',
1621 ))) {
1622 $submitted[$key] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
1623 $mergeValue
1624 ) . CRM_Core_DAO::VALUE_SEPARATOR;
1625 }
1626 else {
1627 $submitted[$key] = $mergeValue;
1628 }
1629 }
1630 }
1631 }
1632 elseif (in_array($htmlType, array(
1633 'Multi-Select Country',
1634 'Multi-Select State/Province',
1635 ))) {
1636 //we require submitted values should be in array format
1637 if ($value) {
1638 $mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
1639 //hack to remove null values from array.
1640 $mergeValue = array();
1641 foreach ($mergeValueArray as $k => $v) {
1642 if ($v != '') {
1643 $mergeValue[] = $v;
1644 }
1645 }
1646 $submitted[$key] = $mergeValue;
1647 }
1648 }
1649 break;
1650
1651 default:
1652 break;
1653 }
1654 }
1655 }
1656
1657 // **** Do file custom fields related migrations
1658 // FIXME: move this someplace else (one of the BAOs) after discussing
1659 // where to, and whether CRM_Core_BAO_File::deleteFileReferences() shouldn't actually,
1660 // like, delete a file...
1661
1662 if (!isset($customFiles)) {
1663 $customFiles = array();
1664 }
1665 foreach ($customFiles as $customId) {
1666 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($customId);
1667
1668 // get the contact_id -> file_id mapping
1669 $fileIds = array();
1670 $sql = "SELECT entity_id, {$columnName} AS file_id FROM {$tableName} WHERE entity_id IN ({$mainId}, {$otherId})";
1671 $dao = CRM_Core_DAO::executeQuery($sql);
1672 while ($dao->fetch()) {
1673 $fileIds[$dao->entity_id] = $dao->file_id;
1674 if ($dao->entity_id == $mainId) {
1675 CRM_Core_BAO_File::deleteFileReferences($fileIds[$mainId], $mainId, $customId);
1676 }
1677 }
1678 $dao->free();
1679
1680 // move the other contact's file to main contact
1681 //NYSS need to INSERT or UPDATE depending on whether main contact has an existing record
1682 if (CRM_Core_DAO::singleValueQuery("SELECT id FROM {$tableName} WHERE entity_id = {$mainId}")) {
1683 $sql = "UPDATE {$tableName} SET {$columnName} = {$fileIds[$otherId]} WHERE entity_id = {$mainId}";
1684 }
1685 else {
1686 $sql = "INSERT INTO {$tableName} ( entity_id, {$columnName} ) VALUES ( {$mainId}, {$fileIds[$otherId]} )";
1687 }
1688 CRM_Core_DAO::executeQuery($sql);
1689
1690 if (CRM_Core_DAO::singleValueQuery("
1691 SELECT id
1692 FROM civicrm_entity_file
1693 WHERE entity_table = '{$tableName}' AND file_id = {$fileIds[$otherId]}")
1694 ) {
1695 $sql = "
1696 UPDATE civicrm_entity_file
1697 SET entity_id = {$mainId}
1698 WHERE entity_table = '{$tableName}' AND file_id = {$fileIds[$otherId]}";
1699 }
1700 else {
1701 $sql = "
1702 INSERT INTO civicrm_entity_file ( entity_table, entity_id, file_id )
1703 VALUES ( '{$tableName}', {$mainId}, {$fileIds[$otherId]} )";
1704 }
1705 CRM_Core_DAO::executeQuery($sql);
1706 }
1707
1708 // move view only custom fields CRM-5362
1709 $viewOnlyCustomFields = array();
1710 foreach ($submitted as $key => $value) {
1711 $fid = (int) substr($key, 7);
1712 if (array_key_exists($fid, $cFields) && !empty($cFields[$fid]['attributes']['is_view'])) {
1713 $viewOnlyCustomFields[$key] = $value;
1714 }
1715 }
1716
1717 // special case to set values for view only, CRM-5362
1718 if (!empty($viewOnlyCustomFields)) {
1719 $viewOnlyCustomFields['entityID'] = $mainId;
1720 CRM_Core_BAO_CustomValueTable::setValues($viewOnlyCustomFields);
1721 }
1722
1723 if (!$checkPermissions || (CRM_Core_Permission::check('merge duplicate contacts') &&
1724 CRM_Core_Permission::check('delete contacts'))
1725 ) {
1726 // if ext id is submitted then set it null for contact to be deleted
1727 if (!empty($submitted['external_identifier'])) {
1728 $query = "UPDATE civicrm_contact SET external_identifier = null WHERE id = {$otherId}";
1729 CRM_Core_DAO::executeQuery($query);
1730 }
1731 civicrm_api3('contact', 'delete', array('id' => $otherId));
1732 }
1733
1734 // CRM-15681 merge sub_types
1735 if ($other_sub_types = CRM_Utils_Array::value('contact_sub_type', $migrationInfo['other_details'])) {
1736 if ($main_sub_types = CRM_Utils_Array::value('contact_sub_type', $migrationInfo['main_details'])) {
1737 $submitted['contact_sub_type'] = array_unique(array_merge($main_sub_types, $other_sub_types));
1738 }
1739 else {
1740 $submitted['contact_sub_type'] = $other_sub_types;
1741 }
1742 }
1743
1744 // **** Update contact related info for the main contact
1745 if (!empty($submitted)) {
1746 $submitted['contact_id'] = $mainId;
1747
1748 //update current employer field
1749 if ($currentEmloyerId = CRM_Utils_Array::value('current_employer_id', $submitted)) {
1750 if (!CRM_Utils_System::isNull($currentEmloyerId)) {
1751 $submitted['current_employer'] = $submitted['current_employer_id'];
1752 }
1753 else {
1754 $submitted['current_employer'] = '';
1755 }
1756 unset($submitted['current_employer_id']);
1757 }
1758
1759 //CRM-14312 include prefix/suffix from mainId if not overridden for proper construction of display/sort name
1760 if (!isset($submitted['prefix_id']) && !empty($migrationInfo['main_details']['prefix_id'])) {
1761 $submitted['prefix_id'] = $migrationInfo['main_details']['prefix_id'];
1762 }
1763 if (!isset($submitted['suffix_id']) && !empty($migrationInfo['main_details']['suffix_id'])) {
1764 $submitted['suffix_id'] = $migrationInfo['main_details']['suffix_id'];
1765 }
1766
1767 CRM_Contact_BAO_Contact::createProfileContact($submitted, CRM_Core_DAO::$_nullArray, $mainId);
1768 }
1769
1770 CRM_Utils_Hook::post('merge', 'Contact', $mainId);
1771 self::createMergeActivities($mainId, $otherId);
1772
1773 return TRUE;
1774 }
1775
1776 /**
1777 * Builds an Array of Custom tables for given custom field ID's.
1778 *
1779 * @param $customFieldIDs
1780 *
1781 * @return array
1782 * Array of custom table names
1783 */
1784 private static function getAffectedCustomTables($customFieldIDs) {
1785 $customTableToCopyValues = array();
1786
1787 foreach ($customFieldIDs as $fieldID) {
1788 if (!empty($fieldID)) {
1789 $customField = civicrm_api3('custom_field', 'getsingle', array(
1790 'id' => $fieldID,
1791 'is_active' => TRUE,
1792 ));
1793 if (!civicrm_error($customField) && !empty($customField['custom_group_id'])) {
1794 $customGroup = civicrm_api3('custom_group', 'getsingle', array(
1795 'id' => $customField['custom_group_id'],
1796 'is_active' => TRUE,
1797 ));
1798
1799 if (!civicrm_error($customGroup) && !empty($customGroup['table_name'])) {
1800 $customTableToCopyValues[] = $customGroup['table_name'];
1801 }
1802 }
1803 }
1804 }
1805
1806 return $customTableToCopyValues;
1807 }
1808
1809 /**
1810 * Get fields in the contact table suitable for merging.
1811 *
1812 * @return array
1813 * Array of field names to be potentially merged.
1814 */
1815 public static function getContactFields() {
1816 $contactFields = CRM_Contact_DAO_Contact::fields();
1817 $invalidFields = array(
1818 'api_key',
1819 'created_date',
1820 'display_name',
1821 'hash',
1822 'id',
1823 'modified_date',
1824 'primary_contact_id',
1825 'sort_name',
1826 'user_unique_id',
1827 );
1828 foreach ($contactFields as $field => $value) {
1829 if (in_array($field, $invalidFields)) {
1830 unset($contactFields[$field]);
1831 }
1832 }
1833 return array_keys($contactFields);
1834 }
1835
1836 /**
1837 * Added for CRM-12695
1838 * Based on the contactID provided
1839 * add/update membership(s) to related contacts
1840 *
1841 * @param int $contactID
1842 */
1843 public static function addMembershipToRealtedContacts($contactID) {
1844 $dao = new CRM_Member_DAO_Membership();
1845 $dao->contact_id = $contactID;
1846 $dao->is_test = 0;
1847 $dao->find();
1848
1849 //checks membership of contact itself
1850 while ($dao->fetch()) {
1851 $relationshipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $dao->membership_type_id, 'relationship_type_id', 'id');
1852 if ($relationshipTypeId) {
1853 $membershipParams = array(
1854 'id' => $dao->id,
1855 'contact_id' => $dao->contact_id,
1856 'membership_type_id' => $dao->membership_type_id,
1857 'join_date' => CRM_Utils_Date::isoToMysql($dao->join_date),
1858 'start_date' => CRM_Utils_Date::isoToMysql($dao->start_date),
1859 'end_date' => CRM_Utils_Date::isoToMysql($dao->end_date),
1860 'source' => $dao->source,
1861 'status_id' => $dao->status_id,
1862 );
1863 // create/update membership(s) for related contact(s)
1864 CRM_Member_BAO_Membership::createRelatedMemberships($membershipParams, $dao);
1865 } // end of if relationshipTypeId
1866 }
1867 }
1868
1869 /**
1870 * Add custom tables that extend contacts to the list of contact references.
1871 *
1872 * CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
1873 * function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
1874 * - the down side is it is not cached.
1875 *
1876 * Further changes should be include tests in the CRM_Core_MergerTest class
1877 * to ensure that disabled, subtype, multiple etc groups are still captured.
1878 *
1879 * @param array $cidRefs
1880 */
1881 public static function addCustomTablesExtendingContactsToCidRefs(&$cidRefs) {
1882 $customValueTables = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
1883 $customValueTables->find();
1884 while ($customValueTables->fetch()) {
1885 $cidRefs[$customValueTables->table_name] = array('entity_id');
1886 }
1887 }
1888
1889 /**
1890 * Create activities tracking the merge on affected contacts.
1891 *
1892 * @param int $mainId
1893 * @param int $otherId
1894 *
1895 * @throws \CiviCRM_API3_Exception
1896 */
1897 public static function createMergeActivities($mainId, $otherId) {
1898 $params = array(
1899 1 => $otherId,
1900 2 => $mainId,
1901 );
1902 $activity = civicrm_api3('activity', 'create', array(
1903 'source_contact_id' => CRM_Core_Session::getLoggedInContactID() ? CRM_Core_Session::getLoggedInContactID() :
1904 $mainId,
1905 'subject' => ts('Contact ID %1 has been merged and deleted.', $params),
1906 'target_contact_id' => $mainId,
1907 'activity_type_id' => 'Contact Merged',
1908 'status_id' => 'Completed',
1909 ));
1910 if (civicrm_api3('Setting', 'getvalue', array('name' => 'contact_undelete', 'group' => 'CiviCRM Preferences'))) {
1911 civicrm_api3('activity', 'create', array(
1912 'source_contact_id' => CRM_Core_Session::getLoggedInContactID() ? CRM_Core_Session::getLoggedInContactID() :
1913 $otherId,
1914 'subject' => ts('Contact ID %1 has been merged into Contact ID %2 and deleted.', $params),
1915 'target_contact_id' => $otherId,
1916 'activity_type_id' => 'Contact Deleted by Merge',
1917 'parent_id' => $activity['id'],
1918 'status_id' => 'Completed',
1919 ));
1920 }
1921 }
1922
1923 /**
1924 * Get Duplicate Pairs based on a rule for a group.
1925 *
1926 * @param int $rule_group_id
1927 * @param int $group_id
1928 * @param bool $reloadCacheIfEmpty
1929 * @param int $batchLimit
1930 * @param bool $isSelected
1931 * Limit to selected pairs.
1932 * @param array|string $orderByClause
1933 * @param bool $includeConflicts
1934 * @param array $criteria
1935 * Additional criteria to narrow down the merge group.
1936 *
1937 * @param bool $checkPermissions
1938 * Respect logged in user permissions.
1939 *
1940 * @param int $searchLimit
1941 * Limit to searching for matches against this many contacts.
1942 *
1943 * @return array
1944 * Array of matches meeting the criteria.
1945 */
1946 public static function getDuplicatePairs($rule_group_id, $group_id, $reloadCacheIfEmpty, $batchLimit, $isSelected, $orderByClause = '', $includeConflicts = TRUE, $criteria = array(), $checkPermissions = TRUE, $searchLimit = 0) {
1947 $where = self::getWhereString($batchLimit, $isSelected);
1948 $cacheKeyString = self::getMergeCacheKeyString($rule_group_id, $group_id, $criteria, $checkPermissions);
1949 $join = self::getJoinOnDedupeTable();
1950 $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where, 0, 0, array(), $orderByClause, $includeConflicts);
1951 if (empty($dupePairs) && $reloadCacheIfEmpty) {
1952 // If we haven't found any dupes, probably cache is empty.
1953 // Try filling cache and give another try. We don't need to specify include conflicts here are there will not be any
1954 // until we have done some processing.
1955 CRM_Core_BAO_PrevNextCache::refillCache($rule_group_id, $group_id, $cacheKeyString, $criteria, $checkPermissions, $searchLimit);
1956 $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where, 0, 0, array(), $orderByClause, $includeConflicts);
1957 return $dupePairs;
1958 }
1959 return $dupePairs;
1960 }
1961
1962 /**
1963 * Get the cache key string for the merge action.
1964 *
1965 * @param int $rule_group_id
1966 * @param int $group_id
1967 * @param array $criteria
1968 * Additional criteria to narrow down the merge group.
1969 * Currently we are only supporting the key 'contact' within it.
1970 *
1971 * @param bool $checkPermissions
1972 * Respect the users permissions.
1973 *
1974 * @return string
1975 */
1976 public static function getMergeCacheKeyString($rule_group_id, $group_id, $criteria = array(), $checkPermissions = TRUE) {
1977 $contactType = CRM_Dedupe_BAO_RuleGroup::getContactTypeForRuleGroup($rule_group_id);
1978 $cacheKeyString = "merge {$contactType}";
1979 $cacheKeyString .= $rule_group_id ? "_{$rule_group_id}" : '_0';
1980 $cacheKeyString .= $group_id ? "_{$group_id}" : '_0';
1981 $cacheKeyString .= !empty($criteria) ? md5(serialize($criteria)) : '_0';
1982 if ($checkPermissions) {
1983 $contactID = CRM_Core_Session::getLoggedInContactID();
1984 if (!$contactID) {
1985 // Distinguish between no permission check & no logged in user.
1986 $contactID = 'null';
1987 }
1988 $cacheKeyString .= '_' . $contactID;
1989 }
1990 else {
1991 $cacheKeyString .= '_0';
1992 }
1993 return $cacheKeyString;
1994 }
1995
1996 /**
1997 * @param array $contact
1998 * @return array
1999 * $specialValues
2000 */
2001 public static function getSpecialValues($contact) {
2002 $preferred_communication_method = CRM_Utils_Array::value('preferred_communication_method', $contact);
2003 $value = empty($preferred_communication_method) ? array() : $preferred_communication_method;
2004 $specialValues = array(
2005 'preferred_communication_method' => $value,
2006 'communication_style_id' => $value,
2007 );
2008
2009 if (!empty($contact['preferred_communication_method'])) {
2010 // api 3 returns pref_comm_method as an array, which breaks the lookup; so we reconstruct
2011 $prefCommList = is_array($specialValues['preferred_communication_method']) ? implode(CRM_Core_DAO::VALUE_SEPARATOR, $specialValues['preferred_communication_method']) : $specialValues['preferred_communication_method'];
2012 $specialValues['preferred_communication_method'] = CRM_Core_DAO::VALUE_SEPARATOR . $prefCommList . CRM_Core_DAO::VALUE_SEPARATOR;
2013 }
2014 $names = array(
2015 'preferred_communication_method' => array(
2016 'newName' => 'preferred_communication_method_display',
2017 'groupName' => 'preferred_communication_method',
2018 ),
2019 );
2020 CRM_Core_OptionGroup::lookupValues($specialValues, $names);
2021
2022 if (!empty($contact['communication_style'])) {
2023 $specialValues['communication_style_id_display'] = $contact['communication_style'];
2024 }
2025 return $specialValues;
2026 }
2027
2028 /**
2029 * Get the metadata for the merge fields.
2030 *
2031 * This is basically the contact metadata, augmented with fields to
2032 * represent email greeting, postal greeting & addressee.
2033 *
2034 * @return array
2035 */
2036 public static function getMergeFieldsMetadata() {
2037 if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['merge_fields_metadata'])) {
2038 return \Civi::$statics[__CLASS__]['merge_fields_metadata'];
2039 }
2040 $fields = CRM_Contact_DAO_Contact::fields();
2041 static $optionValueFields = array();
2042 if (empty($optionValueFields)) {
2043 $optionValueFields = CRM_Core_OptionValue::getFields();
2044 }
2045 foreach ($optionValueFields as $field => $params) {
2046 $fields[$field]['title'] = $params['title'];
2047 }
2048 \Civi::$statics[__CLASS__]['merge_fields_metadata'] = $fields;
2049 return \Civi::$statics[__CLASS__]['merge_fields_metadata'];
2050 }
2051
2052 /**
2053 * Get the details of the contact to be merged.
2054 *
2055 * @param int $contact_id
2056 *
2057 * @return array
2058 *
2059 * @throws CRM_Core_Exception
2060 */
2061 public static function getMergeContactDetails($contact_id) {
2062 $params = array(
2063 'contact_id' => $contact_id,
2064 'version' => 3,
2065 'return' => array_merge(array('display_name'), self::getContactFields()),
2066 );
2067 $result = civicrm_api('contact', 'get', $params);
2068
2069 // CRM-18480: Cancel the process if the contact is already deleted
2070 if (isset($result['values'][$contact_id]['contact_is_deleted']) && !empty($result['values'][$contact_id]['contact_is_deleted'])) {
2071 throw new CRM_Core_Exception(ts('Cannot merge because one contact (ID %1) has been deleted.', array(
2072 1 => $contact_id,
2073 )));
2074 }
2075
2076 return $result['values'][$contact_id];
2077 }
2078
2079 /**
2080 * Merge location.
2081 *
2082 * Based on the data in the $locationMigrationInfo merge the locations for 2 contacts.
2083 *
2084 * The data is in the format received from the merge form (which is a fairly confusing format).
2085 *
2086 * It is converted into an array of DAOs which is passed to the alterLocationMergeData hook
2087 * before saving or deleting the DAOs. A new hook is added to allow these to be altered after they have
2088 * been calculated and before saving because
2089 * - the existing format & hook combo is so confusing it is hard for developers to change & inherently fragile
2090 * - passing to a hook right before save means calculations only have to be done once
2091 * - the existing pattern of passing dissimilar data to the same (merge) hook with a different 'type' is just
2092 * ugly.
2093 *
2094 * The use of the new hook is tested, including the fact it is called before contributions are merged, as this
2095 * is likely to be significant data in merge hooks.
2096 *
2097 * @param int $mainId
2098 * @param int $otherId
2099 * @param array $locationMigrationInfo
2100 * Portion of the migration_info that holds location migration information.
2101 *
2102 * @param array $migrationInfo
2103 * Migration info for the merge. This is passed to the hook as informational only.
2104 */
2105 public static function mergeLocations($mainId, $otherId, $locationMigrationInfo, $migrationInfo) {
2106 foreach ($locationMigrationInfo as $key => $value) {
2107 $locField = explode('_', $key);
2108 $fieldName = $locField[2];
2109 $fieldCount = $locField[3];
2110
2111 // Set up the operation type (add/overwrite)
2112 // Ignore operation for websites
2113 // @todo Tidy this up
2114 $operation = 0;
2115 if ($fieldName != 'website') {
2116 $operation = CRM_Utils_Array::value('operation', $migrationInfo['location_blocks'][$fieldName][$fieldCount]);
2117 }
2118 // default operation is overwrite.
2119 if (!$operation) {
2120 $operation = 2;
2121 }
2122 $locBlocks[$fieldName][$fieldCount]['operation'] = $operation;
2123 }
2124 $blocksDAO = array();
2125
2126 // @todo Handle OpenID (not currently in API).
2127 if (!empty($locBlocks)) {
2128 $locationBlocks = self::getLocationBlockInfo();
2129
2130 $primaryBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mainId, array('is_primary' => 1));
2131 $billingBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mainId, array('is_billing' => 1));
2132
2133 foreach ($locBlocks as $name => $block) {
2134 $blocksDAO[$name] = array('delete' => array(), 'update' => array());
2135 if (!is_array($block) || CRM_Utils_System::isNull($block)) {
2136 continue;
2137 }
2138 $daoName = 'CRM_Core_DAO_' . $locationBlocks[$name]['label'];
2139 $changePrimary = FALSE;
2140 $primaryDAOId = (array_key_exists($name, $primaryBlockIds)) ? array_pop($primaryBlockIds[$name]) : NULL;
2141 $billingDAOId = (array_key_exists($name, $billingBlockIds)) ? array_pop($billingBlockIds[$name]) : NULL;
2142
2143 foreach ($block as $blkCount => $values) {
2144 $otherBlockId = CRM_Utils_Array::value('id', $migrationInfo['other_details']['location_blocks'][$name][$blkCount]);
2145 $mainBlockId = CRM_Utils_Array::value('mainContactBlockId', $migrationInfo['location_blocks'][$name][$blkCount], 0);
2146 if (!$otherBlockId) {
2147 continue;
2148 }
2149
2150 // For the block which belongs to other-contact, link the location block to main-contact
2151 $otherBlockDAO = new $daoName();
2152 $otherBlockDAO->contact_id = $mainId;
2153
2154 // Get the ID of this block on the 'other' contact, otherwise skip
2155 $otherBlockDAO->id = $otherBlockId;
2156
2157 // Add/update location and type information from the form, if applicable
2158 if ($locationBlocks[$name]['hasLocation']) {
2159 $locTypeId = CRM_Utils_Array::value('locTypeId', $migrationInfo['location_blocks'][$name][$blkCount]);
2160 $otherBlockDAO->location_type_id = $locTypeId;
2161 }
2162 if ($locationBlocks[$name]['hasType']) {
2163 $typeTypeId = CRM_Utils_Array::value('typeTypeId', $migrationInfo['location_blocks'][$name][$blkCount]);
2164 $otherBlockDAO->{$locationBlocks[$name]['hasType']} = $typeTypeId;
2165 }
2166
2167 // If we're deliberately setting this as primary then add the flag
2168 // and remove it from the current primary location (if there is one).
2169 // But only once for each entity.
2170 $set_primary = CRM_Utils_Array::value('set_other_primary', $migrationInfo['location_blocks'][$name][$blkCount]);
2171 if (!$changePrimary && $set_primary == "1") {
2172 $otherBlockDAO->is_primary = 1;
2173 if ($primaryDAOId) {
2174 $removePrimaryDAO = new $daoName();
2175 $removePrimaryDAO->id = $primaryDAOId;
2176 $removePrimaryDAO->is_primary = 0;
2177 $blocksDAO[$name]['update'][$primaryDAOId] = $removePrimaryDAO;
2178 }
2179 $changePrimary = TRUE;
2180 }
2181 // Otherwise, if main contact already has primary, set it to 0.
2182 elseif ($primaryDAOId) {
2183 $otherBlockDAO->is_primary = 0;
2184 }
2185
2186 // If the main contact already has a billing location, set this to 0.
2187 if ($billingDAOId) {
2188 $otherBlockDAO->is_billing = 0;
2189 }
2190
2191 $operation = CRM_Utils_Array::value('operation', $values, 2);
2192 // overwrite - need to delete block which belongs to main-contact.
2193 if (!empty($mainBlockId) && ($operation == 2)) {
2194 $deleteDAO = new $daoName();
2195 $deleteDAO->id = $mainBlockId;
2196 $deleteDAO->find(TRUE);
2197
2198 // if we about to delete a primary / billing block, set the flags for new block
2199 // that we going to assign to main-contact
2200 if ($primaryDAOId && ($primaryDAOId == $deleteDAO->id)) {
2201 $otherBlockDAO->is_primary = 1;
2202 }
2203 if ($billingDAOId && ($billingDAOId == $deleteDAO->id)) {
2204 $otherBlockDAO->is_billing = 1;
2205 }
2206 $blocksDAO[$name]['delete'][$deleteDAO->id] = $deleteDAO;
2207 }
2208 $blocksDAO[$name]['update'][$otherBlockDAO->id] = $otherBlockDAO;
2209 }
2210 }
2211 }
2212
2213 CRM_Utils_Hook::alterLocationMergeData($blocksDAO, $mainId, $otherId, $migrationInfo);
2214 foreach ($blocksDAO as $blockDAOs) {
2215 if (!empty($blockDAOs['update'])) {
2216 foreach ($blockDAOs['update'] as $blockDAO) {
2217 $blockDAO->save();
2218 }
2219 }
2220 if (!empty($blockDAOs['delete'])) {
2221 foreach ($blockDAOs['delete'] as $blockDAO) {
2222 $blockDAO->delete();
2223 }
2224 }
2225 }
2226 }
2227
2228 /**
2229 * Dedupe a pair of contacts.
2230 *
2231 * @param array $migrationInfo
2232 * @param array $resultStats
2233 * @param array $deletedContacts
2234 * @param string $mode
2235 * @param bool $checkPermissions
2236 * @param int $mainId
2237 * @param int $otherId
2238 * @param string $cacheKeyString
2239 */
2240 protected static function dedupePair(&$migrationInfo, &$resultStats, &$deletedContacts, $mode, $checkPermissions, $mainId, $otherId, $cacheKeyString) {
2241
2242 // go ahead with merge if there is no conflict
2243 $conflicts = array();
2244 if (!CRM_Dedupe_Merger::skipMerge($mainId, $otherId, $migrationInfo, $mode, $conflicts)) {
2245 CRM_Dedupe_Merger::moveAllBelongings($mainId, $otherId, $migrationInfo, $checkPermissions);
2246 $resultStats['merged'][] = array(
2247 'main_id' => $mainId,
2248 'other_id' => $otherId,
2249 );
2250 $deletedContacts[] = $otherId;
2251 }
2252 else {
2253 $resultStats['skipped'][] = array(
2254 'main_id' => $mainId,
2255 'other_id' => $otherId,
2256 );
2257 }
2258
2259 // store any conflicts
2260 if (!empty($conflicts)) {
2261 foreach ($conflicts as $key => $dnc) {
2262 $conflicts[$key] = "{$migrationInfo['rows'][$key]['title']}: '{$migrationInfo['rows'][$key]['main']}' vs. '{$migrationInfo['rows'][$key]['other']}'";
2263 }
2264 CRM_Core_BAO_PrevNextCache::markConflict($mainId, $otherId, $cacheKeyString, $conflicts);
2265 }
2266 else {
2267 // delete entry from PrevNextCache table so we don't consider the pair next time
2268 // pair may have been flipped, so make sure we delete using both orders
2269 CRM_Core_BAO_PrevNextCache::deletePair($mainId, $otherId, $cacheKeyString, TRUE);
2270 }
2271
2272 CRM_Core_DAO::freeResult();
2273 }
2274
2275 }