Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value
[civicrm-core.git] / CRM / Contact / Page / DedupeRules.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 class CRM_Contact_Page_DedupeRules extends CRM_Core_Page_Basic {
18
19 /**
20 * The action links that we need to display for the browse screen.
21 *
22 * @var array
23 */
24 public static $_links = NULL;
25
26 /**
27 * Get BAO Name.
28 *
29 * @return string
30 * Classname of BAO.
31 */
32 public function getBAOName() {
33 return 'CRM_Dedupe_BAO_RuleGroup';
34 }
35
36 /**
37 * Get action Links.
38 *
39 * @return array
40 * (reference) of action links
41 */
42 public function &links() {
43 if (!(self::$_links)) {
44 $deleteExtra = ts('Are you sure you want to delete this Rule?');
45
46 // helper variable for nicer formatting
47 $links = [];
48
49 if (CRM_Core_Permission::check('merge duplicate contacts')) {
50 $links[CRM_Core_Action::VIEW] = [
51 'name' => ts('Use Rule'),
52 'url' => 'civicrm/contact/dedupefind',
53 'qs' => 'reset=1&rgid=%%id%%&action=preview',
54 'title' => ts('Use DedupeRule'),
55 ];
56 }
57 if (CRM_Core_Permission::check('administer dedupe rules')) {
58 $links[CRM_Core_Action::UPDATE] = [
59 'name' => ts('Edit Rule'),
60 'url' => 'civicrm/contact/deduperules',
61 'qs' => 'action=update&id=%%id%%',
62 'title' => ts('Edit DedupeRule'),
63 ];
64 $links[CRM_Core_Action::DELETE] = [
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'),
70 ];
71 }
72
73 self::$_links = $links;
74 }
75 return self::$_links;
76 }
77
78 /**
79 * Run the page.
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.
84 */
85 public function run() {
86 $id = $this->getIdAndAction();
87
88 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE);
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?
98 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
99 $this->edit($this->_action, $id);
100 }
101 if ($this->_action & CRM_Core_Action::DELETE) {
102 $this->delete($id);
103 }
104
105 // browse the rules
106 $this->browse();
107
108 // This replaces parent run, but do parent's parent run
109 return CRM_Core_Page::run();
110 }
111
112 /**
113 * Browse all rule groups.
114 */
115 public function browse() {
116 // get all rule groups
117 $ruleGroups = [];
118 $dao = new CRM_Dedupe_DAO_RuleGroup();
119 $dao->orderBy('contact_type ASC, used ASC, title ASC');
120 $dao->find();
121
122 $dedupeRuleTypes = CRM_Core_SelectValues::getDedupeRuleTypes();
123 while ($dao->fetch()) {
124 $ruleGroups[$dao->contact_type][$dao->id] = [];
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) {
131 unset($links[CRM_Core_Action::MAP]);
132 unset($links[CRM_Core_Action::DELETE]);
133 }*/
134
135 if ($dao->is_reserved) {
136 unset($links[CRM_Core_Action::DELETE]);
137 }
138
139 $ruleGroups[$dao->contact_type][$dao->id]['action'] = CRM_Core_Action::formLink(
140 $links,
141 $action,
142 ['id' => $dao->id],
143 ts('more'),
144 FALSE,
145 'dedupeRule.manage.action',
146 'DedupeRule',
147 $dao->id
148 );
149
150 $ruleGroups[$dao->contact_type][$dao->id]['used_display'] = $dedupeRuleTypes[$ruleGroups[$dao->contact_type][$dao->id]['used']];
151 }
152 $this->assign('brows', $ruleGroups);
153 }
154
155 /**
156 * Get name of edit form.
157 *
158 * @return string
159 * classname of edit form
160 */
161 public function editForm() {
162 return 'CRM_Contact_Form_DedupeRules';
163 }
164
165 /**
166 * Get edit form name.
167 *
168 * @return string
169 * name of this page
170 */
171 public function editName() {
172 return 'DedupeRules';
173 }
174
175 /**
176 * Get user context.
177 *
178 * @param null $mode
179 *
180 * @return string
181 * user context
182 */
183 public function userContext($mode = NULL) {
184 return 'civicrm/contact/deduperules';
185 }
186
187 /**
188 * @param int $id
189 */
190 public function delete($id) {
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;
197 if ($rgDao->find(TRUE)) {
198 $rgDao->delete();
199 CRM_Core_Session::setStatus(ts("The rule '%1' has been deleted.", [1 => $rgDao->title]), ts('Rule Deleted'), 'success');
200 CRM_Utils_System::redirect(CRM_Utils_System::url($this->userContext(), 'reset=1'));
201 }
202 }
203
204 }