Add test cover for subscription history create
[civicrm-core.git] / CRM / Dedupe / BAO / DedupeException.php
CommitLineData
bb4187d7
MD
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 */
21class CRM_Dedupe_BAO_DedupeException extends CRM_Dedupe_DAO_DedupeException {
22
23 /**
24 * Create a dedupe exception record.
25 *
26 * @param array $params
27 *
61194d45 28 * @return \CRM_Dedupe_DAO_DedupeException
bb4187d7
MD
29 */
30 public static function create($params) {
31 $hook = empty($params['id']) ? 'create' : 'edit';
61194d45
CW
32 CRM_Utils_Hook::pre($hook, 'DedupeException', $params['id'] ?? NULL, $params);
33 // Also call hook with deprecated entity name
34 CRM_Utils_Hook::pre($hook, 'Exception', $params['id'] ?? NULL, $params);
bb4187d7
MD
35 $contact1 = $params['contact_id1'] ?? NULL;
36 $contact2 = $params['contact_id2'] ?? NULL;
61194d45 37 $dao = new CRM_Dedupe_BAO_DedupeException();
bb4187d7
MD
38 $dao->copyValues($params);
39 if ($contact1 && $contact2) {
40 CRM_Core_DAO::singleValueQuery("
41 DELETE FROM civicrm_prevnext_cache
42 WHERE (entity_id1 = %1 AND entity_id2 = %2)
43 OR (entity_id1 = %2 AND entity_id2 = %2)",
44 [1 => [$contact1, 'Integer'], 2 => [$contact2, 'Integer']]
45 );
46 if ($contact2 < $contact1) {
47 // These are expected to be saved lowest first.
48 $dao->contact_id1 = $contact2;
49 $dao->contact_id2 = $contact1;
50 }
51 }
52 $dao->save();
61194d45
CW
53 CRM_Utils_Hook::post($hook, 'DedupeException', $dao->id, $dao);
54 // Also call hook with deprecated entity name
bb4187d7
MD
55 CRM_Utils_Hook::post($hook, 'Exception', $dao->id, $dao);
56 return $dao;
57 }
58
59}