Merge pull request #22448 from mattwire/recurid
[civicrm-core.git] / CRM / ACL / Form / ACL.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_ACL_Form_ACL extends CRM_Admin_Form {
18
19 /**
20 * @var bool
21 */
22 public $submitOnce = TRUE;
23
24 /**
25 * Set default values for the form.
26 */
27 public function setDefaultValues() {
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 /**
91 * Build the form object.
92 */
93 public function buildQuickForm() {
94 parent::buildQuickForm();
95
96 $this->setPageTitle(ts('ACL'));
97
98 if ($this->_action & CRM_Core_Action::DELETE) {
99 return;
100 }
101
102 $this->add('text', 'name', ts('Description'), CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL', 'name'), TRUE);
103
104 $this->add('select',
105 'operation',
106 ts('Operation'),
107 CRM_ACL_BAO_ACL::operation(),
108 TRUE,
109 ['placeholder' => TRUE]
110 );
111
112 $objTypes = [
113 '1' => ts('A group of contacts'),
114 '2' => ts('A profile'),
115 '3' => ts('A set of custom data fields'),
116 ];
117
118 if (CRM_Core_Permission::access('CiviEvent')) {
119 $objTypes['4'] = ts('Events');
120 }
121
122 $extra = ['onclick' => "showObjectSelect();"];
123 $this->addRadio('object_type',
124 ts('Type of Data'),
125 $objTypes,
126 $extra,
127 '&nbsp;', TRUE
128 );
129
130 $label = ts('Role');
131 $role = [
132 '-1' => ts('- select role -'),
133 '0' => ts('Everyone'),
134 ] + CRM_Core_OptionGroup::values('acl_role');
135 $this->add('select', 'entity_id', $label, $role, TRUE);
136
137 $group = [
138 '-1' => ts('- select group -'),
139 '0' => ts('All Groups'),
140 ] + CRM_Core_PseudoConstant::group();
141
142 $customGroup = [
143 '-1' => ts('- select set of custom fields -'),
144 '0' => ts('All Custom Groups'),
145 ] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
146
147 $ufGroup = [
148 '-1' => ts('- select profile -'),
149 '0' => ts('All Profiles'),
150 ] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
151
152 $event = [
153 '-1' => ts('- select event -'),
154 '0' => ts('All Events'),
155 ] + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
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
164 $this->addFormRule(['CRM_ACL_Form_ACL', 'formRule']);
165 }
166
167 /**
168 * @param array $params
169 *
170 * @return bool
171 */
172 public static function formRule($params) {
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
180 $validOperations = ['View', 'Edit'];
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 /**
241 * Process the form submission.
242 */
243 public function postProcess() {
244 // note this also resets any ACL cache
245 Civi::cache('fields')->flush();
246 // reset ACL and system caches.
247 CRM_Core_BAO_Cache::resetCaches();
248
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 }
289
290 }