Merge pull request #17866 from colemanw/customFix
[civicrm-core.git] / CRM / Dedupe / BAO / Exception.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Manages dedupe exceptions - ie pairs marked as non-duplicates.
20 */
21 class CRM_Dedupe_BAO_Exception extends CRM_Dedupe_DAO_Exception {
22
23 /**
24 * Create a dedupe exception record.
25 *
26 * @param array $params
27 *
28 * @return \CRM_Dedupe_BAO_Exception
29 */
30 public static function create($params) {
31 $hook = empty($params['id']) ? 'create' : 'edit';
32 CRM_Utils_Hook::pre($hook, 'Exception', CRM_Utils_Array::value('id', $params), $params);
33 $contact1 = $params['contact_id1'] ?? NULL;
34 $contact2 = $params['contact_id2'] ?? NULL;
35 $dao = new CRM_Dedupe_BAO_Exception();
36 $dao->copyValues($params);
37 if ($contact1 && $contact2) {
38 CRM_Core_DAO::singleValueQuery("
39 DELETE FROM civicrm_prevnext_cache
40 WHERE (entity_id1 = %1 AND entity_id2 = %2)
41 OR (entity_id1 = %2 AND entity_id2 = %2)",
42 [1 => [$contact1, 'Integer'], 2 => [$contact2, 'Integer']]
43 );
44 if ($contact2 < $contact1) {
45 // These are expected to be saved lowest first.
46 $dao->contact_id1 = $contact2;
47 $dao->contact_id2 = $contact1;
48 }
49 }
50 $dao->save();
51
52 CRM_Utils_Hook::post($hook, 'Exception', $dao->id, $dao);
53 return $dao;
54 }
55
56 }