3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_ACL_Form_ACL
extends CRM_Admin_Form
{
20 * Set default values for the form.
22 public function setDefaultValues() {
23 $defaults = parent
::setDefaultValues();
25 if ($this->_action
& CRM_Core_Action
::ADD
) {
26 $defaults['object_type'] = 1;
29 $showHide = new CRM_Core_ShowHideBlocks();
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");
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");
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");
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");
71 $showHide->addHide("id-group-acl");
72 $showHide->addHide("id-profile-acl");
73 $showHide->addHide("id-custom-acl");
74 $showHide->addHide("id-event-acl");
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();
86 * Build the form object.
88 public function buildQuickForm() {
89 parent
::buildQuickForm();
91 $this->setPageTitle(ts('ACL'));
93 if ($this->_action
& CRM_Core_Action
::DELETE
) {
97 $attributes = CRM_Core_DAO
::getAttribute('CRM_ACL_DAO_ACL');
99 $this->add('text', 'name', ts('Description'), CRM_Core_DAO
::getAttribute('CRM_ACL_DAO_ACL', 'name'), TRUE);
101 $operations = ['' => ts('- select -')] + CRM_ACL_BAO_ACL
::operation();
109 '1' => ts('A group of contacts'),
110 '2' => ts('A profile'),
111 '3' => ts('A set of custom data fields'),
114 if (CRM_Core_Permission
::access('CiviEvent')) {
115 $objTypes['4'] = ts('Events');
118 $extra = ['onclick' => "showObjectSelect();"];
119 $this->addRadio('object_type',
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);
134 '-1' => ts('- select -'),
135 '0' => ts('All Groups'),
136 ] + CRM_Core_PseudoConstant
::group();
139 '-1' => ts('- select -'),
140 '0' => ts('All Custom Groups'),
141 ] + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id');
144 '-1' => ts('- select -'),
145 '0' => ts('All Profiles'),
146 ] + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_UFField', 'uf_group_id');
149 '-1' => ts('- select -'),
150 '0' => ts('All Events'),
151 ] + CRM_Event_PseudoConstant
::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
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);
158 $this->add('checkbox', 'is_active', ts('Enabled?'));
160 $this->addFormRule(['CRM_ACL_Form_ACL', 'formRule']);
164 * @param array $params
168 public static function formRule($params) {
169 $showHide = new CRM_Core_ShowHideBlocks();
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.');
176 $validOperations = ['View', 'Edit'];
177 $operationMessage = ts("Only 'View' and 'Edit' operations are valid for this type of data");
179 // Figure out which type of object we're permissioning on and make sure user has selected a value.
180 switch ($params['object_type']) {
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");
189 if (!in_array($params['operation'], $validOperations)) {
190 $errors['operation'] = $operationMessage;
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");
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");
212 if (!in_array($params['operation'], $validOperations)) {
213 $errors['operation'] = $operationMessage;
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");
225 if (!in_array($params['operation'], $validOperations)) {
226 $errors['operation'] = $operationMessage;
231 $showHide->addToTemplate();
233 return empty($errors) ?
TRUE : $errors;
237 * Process the form submission.
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();
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');
250 $params = $this->controller
->exportValues($this->_name
);
251 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
253 $params['entity_table'] = 'civicrm_acl_role';
255 // Figure out which type of object we're permissioning on and set object_table and object_id.
256 switch ($params['object_type']) {
258 $params['object_table'] = 'civicrm_saved_search';
259 $params['object_id'] = $params['group_id'];
263 $params['object_table'] = 'civicrm_uf_group';
264 $params['object_id'] = $params['uf_group_id'];
268 $params['object_table'] = 'civicrm_custom_group';
269 $params['object_id'] = $params['custom_group_id'];
273 $params['object_table'] = 'civicrm_event';
274 $params['object_id'] = $params['event_id'];
279 $params['id'] = $this->_id
;
282 CRM_ACL_BAO_ACL
::create($params);