Merge pull request #22448 from mattwire/recurid
[civicrm-core.git] / CRM / ACL / Form / ACL.php
CommitLineData
6a488035 1<?php
6a488035
TO
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_ACL_Form_ACL extends CRM_Admin_Form {
18
88aae6d4
A
19 /**
20 * @var bool
21 */
22 public $submitOnce = TRUE;
23
6a488035 24 /**
c490a46a 25 * Set default values for the form.
6a488035 26 */
00be9182 27 public function setDefaultValues() {
6a488035
TO
28 $defaults = parent::setDefaultValues();
29
30 if ($this->_action & CRM_Core_Action::ADD) {
31 $defaults['object_type'] = 1;
32 }
33
34 $showHide = new CRM_Core_ShowHideBlocks();
35
36 if (isset($defaults['object_table'])) {
37 switch ($defaults['object_table']) {
38 case 'civicrm_saved_search':
39 $defaults['group_id'] = $defaults['object_id'];
40 $defaults['object_type'] = 1;
41 $showHide->addShow("id-group-acl");
42 $showHide->addHide("id-profile-acl");
43 $showHide->addHide("id-custom-acl");
44 $showHide->addHide("id-event-acl");
45 break;
46
47 case 'civicrm_uf_group':
48 $defaults['uf_group_id'] = $defaults['object_id'];
49 $defaults['object_type'] = 2;
50 $showHide->addHide("id-group-acl");
51 $showHide->addShow("id-profile-acl");
52 $showHide->addHide("id-custom-acl");
53 $showHide->addHide("id-event-acl");
54 break;
55
56 case 'civicrm_custom_group':
57 $defaults['custom_group_id'] = $defaults['object_id'];
58 $defaults['object_type'] = 3;
59 $showHide->addHide("id-group-acl");
60 $showHide->addHide("id-profile-acl");
61 $showHide->addShow("id-custom-acl");
62 $showHide->addHide("id-event-acl");
63 break;
64
65 case 'civicrm_event':
66 $defaults['event_id'] = $defaults['object_id'];
67 $defaults['object_type'] = 4;
68 $showHide->addHide("id-group-acl");
69 $showHide->addHide("id-profile-acl");
70 $showHide->addHide("id-custom-acl");
71 $showHide->addShow("id-event-acl");
72 break;
73 }
74 }
75 else {
76 $showHide->addHide("id-group-acl");
77 $showHide->addHide("id-profile-acl");
78 $showHide->addHide("id-custom-acl");
79 $showHide->addHide("id-event-acl");
80 }
81
82 // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
83 if (!($this->_action & CRM_Core_Action::DELETE)) {
84 $showHide->addToTemplate();
85 }
86
87 return $defaults;
88 }
89
90 /**
d2e5d2ce 91 * Build the form object.
6a488035
TO
92 */
93 public function buildQuickForm() {
94 parent::buildQuickForm();
95
e2046b33
CW
96 $this->setPageTitle(ts('ACL'));
97
6a488035
TO
98 if ($this->_action & CRM_Core_Action::DELETE) {
99 return;
100 }
101
6a488035
TO
102 $this->add('text', 'name', ts('Description'), CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL', 'name'), TRUE);
103
6a488035
TO
104 $this->add('select',
105 'operation',
106 ts('Operation'),
a2eb6152
AH
107 CRM_ACL_BAO_ACL::operation(),
108 TRUE,
109 ['placeholder' => TRUE]
6a488035
TO
110 );
111
cf0d1c08 112 $objTypes = [
c490a46a 113 '1' => ts('A group of contacts'),
6a488035
TO
114 '2' => ts('A profile'),
115 '3' => ts('A set of custom data fields'),
cf0d1c08 116 ];
6a488035
TO
117
118 if (CRM_Core_Permission::access('CiviEvent')) {
119 $objTypes['4'] = ts('Events');
120 }
121
cf0d1c08 122 $extra = ['onclick' => "showObjectSelect();"];
6a488035
TO
123 $this->addRadio('object_type',
124 ts('Type of Data'),
125 $objTypes,
126 $extra,
127 '&nbsp;', TRUE
128 );
129
6a488035 130 $label = ts('Role');
cf0d1c08 131 $role = [
0d48f1cc
TO
132 '-1' => ts('- select role -'),
133 '0' => ts('Everyone'),
134 ] + CRM_Core_OptionGroup::values('acl_role');
6a488035
TO
135 $this->add('select', 'entity_id', $label, $role, TRUE);
136
cf0d1c08 137 $group = [
a2eb6152 138 '-1' => ts('- select group -'),
0d48f1cc
TO
139 '0' => ts('All Groups'),
140 ] + CRM_Core_PseudoConstant::group();
6a488035 141
cf0d1c08 142 $customGroup = [
a2eb6152 143 '-1' => ts('- select set of custom fields -'),
0d48f1cc
TO
144 '0' => ts('All Custom Groups'),
145 ] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
6a488035 146
cf0d1c08 147 $ufGroup = [
a2eb6152 148 '-1' => ts('- select profile -'),
0d48f1cc
TO
149 '0' => ts('All Profiles'),
150 ] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035 151
cf0d1c08 152 $event = [
a2eb6152 153 '-1' => ts('- select event -'),
0d48f1cc
TO
154 '0' => ts('All Events'),
155 ] + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
6a488035
TO
156
157 $this->add('select', 'group_id', ts('Group'), $group);
158 $this->add('select', 'custom_group_id', ts('Custom Data'), $customGroup);
159 $this->add('select', 'uf_group_id', ts('Profile'), $ufGroup);
160 $this->add('select', 'event_id', ts('Event'), $event);
161
162 $this->add('checkbox', 'is_active', ts('Enabled?'));
163
cf0d1c08 164 $this->addFormRule(['CRM_ACL_Form_ACL', 'formRule']);
6a488035
TO
165 }
166
28518c90 167 /**
c490a46a 168 * @param array $params
28518c90
EM
169 *
170 * @return bool
171 */
00be9182 172 public static function formRule($params) {
6a488035
TO
173 $showHide = new CRM_Core_ShowHideBlocks();
174
175 // Make sure role is not -1
176 if ($params['entity_id'] == -1) {
177 $errors['entity_id'] = ts('Please assign this permission to a Role.');
178 }
179
cf0d1c08 180 $validOperations = ['View', 'Edit'];
6a488035
TO
181 $operationMessage = ts("Only 'View' and 'Edit' operations are valid for this type of data");
182
183 // Figure out which type of object we're permissioning on and make sure user has selected a value.
184 switch ($params['object_type']) {
185 case 1:
186 if ($params['group_id'] == -1) {
187 $errors['group_id'] = ts('Please select a Group (or ALL Groups).');
188 $showHide->addShow("id-group-acl");
189 $showHide->addHide("id-profile-acl");
190 $showHide->addHide("id-custom-acl");
191 $showHide->addHide("id-event-acl");
192 }
193 if (!in_array($params['operation'], $validOperations)) {
194 $errors['operation'] = $operationMessage;
195 }
196 break;
197
198 case 2:
199 if ($params['uf_group_id'] == -1) {
200 $errors['uf_group_id'] = ts('Please select a Profile (or ALL Profiles).');
201 $showHide->addShow("id-profile-acl");
202 $showHide->addHide("id-group-acl");
203 $showHide->addHide("id-custom-acl");
204 $showHide->addHide("id-event-acl");
205 }
206 break;
207
208 case 3:
209 if ($params['custom_group_id'] == -1) {
210 $errors['custom_group_id'] = ts('Please select a set of Custom Data (or ALL Custom Data).');
211 $showHide->addShow("id-custom-acl");
212 $showHide->addHide("id-group-acl");
213 $showHide->addHide("id-profile-acl");
214 $showHide->addHide("id-event-acl");
215 }
216 if (!in_array($params['operation'], $validOperations)) {
217 $errors['operation'] = $operationMessage;
218 }
219 break;
220
221 case 4:
222 if ($params['event_id'] == -1) {
223 $errors['event_id'] = ts('Please select an Event (or ALL Events).');
224 $showHide->addShow("id-event-acl");
225 $showHide->addHide("id-custom-acl");
226 $showHide->addHide("id-group-acl");
227 $showHide->addHide("id-profile-acl");
228 }
229 if (!in_array($params['operation'], $validOperations)) {
230 $errors['operation'] = $operationMessage;
231 }
232 break;
233 }
234
235 $showHide->addToTemplate();
236
237 return empty($errors) ? TRUE : $errors;
238 }
239
240 /**
d2e5d2ce 241 * Process the form submission.
6a488035
TO
242 */
243 public function postProcess() {
244 // note this also resets any ACL cache
9cdf85c1 245 Civi::cache('fields')->flush();
5e601882
SL
246 // reset ACL and system caches.
247 CRM_Core_BAO_Cache::resetCaches();
6a488035 248
6a488035
TO
249 if ($this->_action & CRM_Core_Action::DELETE) {
250 CRM_ACL_BAO_ACL::del($this->_id);
251 CRM_Core_Session::setStatus(ts('Selected ACL has been deleted.'), ts('Record Deleted'), 'success');
252 }
253 else {
254 $params = $this->controller->exportValues($this->_name);
255 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
256 $params['deny'] = 0;
257 $params['entity_table'] = 'civicrm_acl_role';
258
259 // Figure out which type of object we're permissioning on and set object_table and object_id.
260 switch ($params['object_type']) {
261 case 1:
262 $params['object_table'] = 'civicrm_saved_search';
263 $params['object_id'] = $params['group_id'];
264 break;
265
266 case 2:
267 $params['object_table'] = 'civicrm_uf_group';
268 $params['object_id'] = $params['uf_group_id'];
269 break;
270
271 case 3:
272 $params['object_table'] = 'civicrm_custom_group';
273 $params['object_id'] = $params['custom_group_id'];
274 break;
275
276 case 4:
277 $params['object_table'] = 'civicrm_event';
278 $params['object_id'] = $params['event_id'];
279 break;
280 }
281
282 if ($this->_id) {
283 $params['id'] = $this->_id;
284 }
285
286 CRM_ACL_BAO_ACL::create($params);
287 }
288 }
e2046b33 289
6a488035 290}