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