CRM-17335 - Remove unnecessary use of CRM_Core_DAO::$_nullObject
[civicrm-core.git] / CRM / Contact / Page / DedupeRules.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
32 */
33 class CRM_Contact_Page_DedupeRules extends CRM_Core_Page_Basic {
34
35 /**
36 * The action links that we need to display for the browse screen.
37 *
38 * @var array
39 */
40 static $_links = NULL;
41
42 /**
43 * Get BAO Name.
44 *
45 * @return string
46 * Classname of BAO.
47 */
48 public function getBAOName() {
49 return 'CRM_Dedupe_BAO_RuleGroup';
50 }
51
52 /**
53 * Get action Links.
54 *
55 * @return array
56 * (reference) of action links
57 */
58 public function &links() {
59 if (!(self::$_links)) {
60 $deleteExtra = ts('Are you sure you want to delete this Rule?');
61
62 // helper variable for nicer formatting
63 $links = array();
64
65 if (CRM_Core_Permission::check('merge duplicate contacts')) {
66 $links[CRM_Core_Action::VIEW] = array(
67 'name' => ts('Use Rule'),
68 'url' => 'civicrm/contact/dedupefind',
69 'qs' => 'reset=1&rgid=%%id%%&action=preview',
70 'title' => ts('Use DedupeRule'),
71 );
72 }
73 if (CRM_Core_Permission::check('administer dedupe rules')) {
74 $links[CRM_Core_Action::UPDATE] = array(
75 'name' => ts('Edit Rule'),
76 'url' => 'civicrm/contact/deduperules',
77 'qs' => 'action=update&id=%%id%%',
78 'title' => ts('Edit DedupeRule'),
79 );
80 $links[CRM_Core_Action::DELETE] = array(
81 'name' => ts('Delete'),
82 'url' => 'civicrm/contact/deduperules',
83 'qs' => 'action=delete&id=%%id%%',
84 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
85 'title' => ts('Delete DedupeRule'),
86 );
87 }
88
89 self::$_links = $links;
90 }
91 return self::$_links;
92 }
93
94 /**
95 * Run the page.
96 *
97 * This method is called after the page is created. It checks for the type
98 * of action and executes that action. Finally it calls the parent's run
99 * method.
100 */
101 public function run() {
102 // get the requested action, default to 'browse'
103 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
104
105 // assign vars to templates
106 $this->assign('action', $action);
107 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
108
109 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE);
110 if ($context == 'nonDupe') {
111 CRM_Core_Session::setStatus(ts('Selected contacts have been marked as not duplicates'), ts('Changes Saved'), 'success');
112 }
113
114 // assign permissions vars to template
115 $this->assign('hasperm_administer_dedupe_rules', CRM_Core_Permission::check('administer dedupe rules'));
116 $this->assign('hasperm_merge_duplicate_contacts', CRM_Core_Permission::check('merge duplicate contacts'));
117
118 // which action to take?
119 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
120 $this->edit($action, $id);
121 }
122 if ($action & CRM_Core_Action::DELETE) {
123 $this->delete($id);
124 }
125
126 // browse the rules
127 $this->browse();
128
129 // parent run
130 return parent::run();
131 }
132
133 /**
134 * Browse all rule groups.
135 */
136 public function browse() {
137 // get all rule groups
138 $ruleGroups = array();
139 $dao = new CRM_Dedupe_DAO_RuleGroup();
140 $dao->orderBy('contact_type,used ASC');
141 $dao->find();
142
143 $dedupeRuleTypes = CRM_Core_SelectValues::getDedupeRuleTypes();
144 while ($dao->fetch()) {
145 $ruleGroups[$dao->contact_type][$dao->id] = array();
146 CRM_Core_DAO::storeValues($dao, $ruleGroups[$dao->contact_type][$dao->id]);
147
148 // form all action links
149 $action = array_sum(array_keys($this->links()));
150 $links = self::links();
151 /* if ($dao->is_default) {
152 unset($links[CRM_Core_Action::MAP]);
153 unset($links[CRM_Core_Action::DELETE]);
154 }*/
155
156 if ($dao->is_reserved) {
157 unset($links[CRM_Core_Action::DELETE]);
158 }
159
160 $ruleGroups[$dao->contact_type][$dao->id]['action'] = CRM_Core_Action::formLink(
161 $links,
162 $action,
163 array('id' => $dao->id),
164 ts('more'),
165 FALSE,
166 'dedupeRule.manage.action',
167 'DedupeRule',
168 $dao->id
169 );
170
171 $ruleGroups[$dao->contact_type][$dao->id]['used_display'] = $dedupeRuleTypes[$ruleGroups[$dao->contact_type][$dao->id]['used']];
172 }
173 $this->assign('brows', $ruleGroups);
174 }
175
176 /**
177 * Get name of edit form.
178 *
179 * @return string
180 * classname of edit form
181 */
182 public function editForm() {
183 return 'CRM_Contact_Form_DedupeRules';
184 }
185
186 /**
187 * Get edit form name.
188 *
189 * @return string
190 * name of this page
191 */
192 public function editName() {
193 return 'DedupeRules';
194 }
195
196 /**
197 * Get user context.
198 *
199 * @param null $mode
200 *
201 * @return string
202 * user context
203 */
204 public function userContext($mode = NULL) {
205 return 'civicrm/contact/deduperules';
206 }
207
208 /**
209 * @param int $id
210 */
211 public function delete($id) {
212 $ruleDao = new CRM_Dedupe_DAO_Rule();
213 $ruleDao->dedupe_rule_group_id = $id;
214 $ruleDao->delete();
215
216 $rgDao = new CRM_Dedupe_DAO_RuleGroup();
217 $rgDao->id = $id;
218 $rgDao->delete();
219 }
220
221 }