[REF] Remove duplicate checks for an array key existing
[civicrm-core.git] / CRM / Contact / Page / DedupeRules.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contact_Page_DedupeRules extends CRM_Core_Page_Basic {
18
19 /**
fe482240 20 * The action links that we need to display for the browse screen.
6a488035
TO
21 *
22 * @var array
6a488035 23 */
69078420 24 public static $_links = NULL;
6a488035
TO
25
26 /**
fe482240 27 * Get BAO Name.
6a488035 28 *
a6c01b45
CW
29 * @return string
30 * Classname of BAO.
6a488035 31 */
00be9182 32 public function getBAOName() {
6a488035
TO
33 return 'CRM_Dedupe_BAO_RuleGroup';
34 }
35
36 /**
fe482240 37 * Get action Links.
6a488035 38 *
a6c01b45
CW
39 * @return array
40 * (reference) of action links
6a488035 41 */
00be9182 42 public function &links() {
6a488035
TO
43 if (!(self::$_links)) {
44 $deleteExtra = ts('Are you sure you want to delete this Rule?');
45
46 // helper variable for nicer formatting
be2fb01f 47 $links = [];
6a488035
TO
48
49 if (CRM_Core_Permission::check('merge duplicate contacts')) {
be2fb01f 50 $links[CRM_Core_Action::VIEW] = [
6a488035
TO
51 'name' => ts('Use Rule'),
52 'url' => 'civicrm/contact/dedupefind',
53 'qs' => 'reset=1&rgid=%%id%%&action=preview',
54 'title' => ts('Use DedupeRule'),
be2fb01f 55 ];
6a488035
TO
56 }
57 if (CRM_Core_Permission::check('administer dedupe rules')) {
be2fb01f 58 $links[CRM_Core_Action::UPDATE] = [
6a488035
TO
59 'name' => ts('Edit Rule'),
60 'url' => 'civicrm/contact/deduperules',
61 'qs' => 'action=update&id=%%id%%',
62 'title' => ts('Edit DedupeRule'),
be2fb01f
CW
63 ];
64 $links[CRM_Core_Action::DELETE] = [
6a488035
TO
65 'name' => ts('Delete'),
66 'url' => 'civicrm/contact/deduperules',
67 'qs' => 'action=delete&id=%%id%%',
68 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
69 'title' => ts('Delete DedupeRule'),
be2fb01f 70 ];
6a488035
TO
71 }
72
73 self::$_links = $links;
74 }
75 return self::$_links;
76 }
77
78 /**
fe482240 79 * Run the page.
6a488035
TO
80 *
81 * This method is called after the page is created. It checks for the type
82 * of action and executes that action. Finally it calls the parent's run
83 * method.
6a488035 84 */
00be9182 85 public function run() {
5d6d0104 86 $id = $this->getIdAndAction();
6a488035 87
edc80cda 88 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE);
6a488035
TO
89 if ($context == 'nonDupe') {
90 CRM_Core_Session::setStatus(ts('Selected contacts have been marked as not duplicates'), ts('Changes Saved'), 'success');
91 }
92
93 // assign permissions vars to template
94 $this->assign('hasperm_administer_dedupe_rules', CRM_Core_Permission::check('administer dedupe rules'));
95 $this->assign('hasperm_merge_duplicate_contacts', CRM_Core_Permission::check('merge duplicate contacts'));
96
97 // which action to take?
5d6d0104
AH
98 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
99 $this->edit($this->_action, $id);
6a488035 100 }
5d6d0104 101 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
102 $this->delete($id);
103 }
104
105 // browse the rules
106 $this->browse();
107
5d6d0104
AH
108 // This replaces parent run, but do parent's parent run
109 return CRM_Core_Page::run();
6a488035
TO
110 }
111
112 /**
fe482240 113 * Browse all rule groups.
6a488035 114 */
00be9182 115 public function browse() {
6a488035 116 // get all rule groups
be2fb01f 117 $ruleGroups = [];
6a488035 118 $dao = new CRM_Dedupe_DAO_RuleGroup();
52e87955 119 $dao->orderBy('contact_type ASC, used ASC, title ASC');
6a488035
TO
120 $dao->find();
121
77d0b1f8 122 $dedupeRuleTypes = CRM_Core_SelectValues::getDedupeRuleTypes();
6a488035 123 while ($dao->fetch()) {
be2fb01f 124 $ruleGroups[$dao->contact_type][$dao->id] = [];
6a488035
TO
125 CRM_Core_DAO::storeValues($dao, $ruleGroups[$dao->contact_type][$dao->id]);
126
127 // form all action links
128 $action = array_sum(array_keys($this->links()));
129 $links = self::links();
130 /* if ($dao->is_default) {
e70a7fc0
TO
131 unset($links[CRM_Core_Action::MAP]);
132 unset($links[CRM_Core_Action::DELETE]);
133 }*/
6a488035
TO
134
135 if ($dao->is_reserved) {
136 unset($links[CRM_Core_Action::DELETE]);
137 }
138
87dab4a4
AH
139 $ruleGroups[$dao->contact_type][$dao->id]['action'] = CRM_Core_Action::formLink(
140 $links,
141 $action,
be2fb01f 142 ['id' => $dao->id],
87dab4a4
AH
143 ts('more'),
144 FALSE,
145 'dedupeRule.manage.action',
146 'DedupeRule',
147 $dao->id
148 );
6a488035 149
77d0b1f8 150 $ruleGroups[$dao->contact_type][$dao->id]['used_display'] = $dedupeRuleTypes[$ruleGroups[$dao->contact_type][$dao->id]['used']];
151 }
6a488035
TO
152 $this->assign('brows', $ruleGroups);
153 }
154
155 /**
fe482240 156 * Get name of edit form.
6a488035 157 *
a6c01b45
CW
158 * @return string
159 * classname of edit form
6a488035 160 */
00be9182 161 public function editForm() {
6a488035
TO
162 return 'CRM_Contact_Form_DedupeRules';
163 }
164
165 /**
fe482240 166 * Get edit form name.
6a488035 167 *
a6c01b45
CW
168 * @return string
169 * name of this page
6a488035 170 */
00be9182 171 public function editName() {
6a488035
TO
172 return 'DedupeRules';
173 }
174
175 /**
fe482240 176 * Get user context.
6a488035 177 *
da6b46f4
EM
178 * @param null $mode
179 *
a6c01b45
CW
180 * @return string
181 * user context
6a488035 182 */
00be9182 183 public function userContext($mode = NULL) {
6a488035
TO
184 return 'civicrm/contact/deduperules';
185 }
186
4319322b 187 /**
100fef9d 188 * @param int $id
4319322b 189 */
00be9182 190 public function delete($id) {
6a488035
TO
191 $ruleDao = new CRM_Dedupe_DAO_Rule();
192 $ruleDao->dedupe_rule_group_id = $id;
193 $ruleDao->delete();
194
195 $rgDao = new CRM_Dedupe_DAO_RuleGroup();
196 $rgDao->id = $id;
cbd404b9 197 if ($rgDao->find(TRUE)) {
198 $rgDao->delete();
be2fb01f 199 CRM_Core_Session::setStatus(ts("The rule '%1' has been deleted.", [1 => $rgDao->title]), ts('Rule Deleted'), 'success');
cbd404b9 200 CRM_Utils_System::redirect(CRM_Utils_System::url($this->userContext(), 'reset=1'));
201 }
6a488035 202 }
96025800 203
6a488035 204}