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
{
22 public $submitOnce = TRUE;
25 * Set default values for the form.
27 public function setDefaultValues() {
28 $defaults = parent
::setDefaultValues();
30 if ($this->_action
& CRM_Core_Action
::ADD
) {
31 $defaults['object_type'] = 1;
34 $showHide = new CRM_Core_ShowHideBlocks();
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");
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");
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");
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");
76 $showHide->addHide("id-group-acl");
77 $showHide->addHide("id-profile-acl");
78 $showHide->addHide("id-custom-acl");
79 $showHide->addHide("id-event-acl");
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();
91 * Build the form object.
93 public function buildQuickForm() {
94 parent
::buildQuickForm();
96 $this->setPageTitle(ts('ACL'));
98 if ($this->_action
& CRM_Core_Action
::DELETE
) {
102 $this->add('text', 'name', ts('Description'), CRM_Core_DAO
::getAttribute('CRM_ACL_DAO_ACL', 'name'), TRUE);
107 CRM_ACL_BAO_ACL
::operation(),
109 ['placeholder' => TRUE]
113 '1' => ts('A group of contacts'),
114 '2' => ts('A profile'),
115 '3' => ts('A set of custom data fields'),
118 if (CRM_Core_Permission
::access('CiviEvent')) {
119 $objTypes['4'] = ts('Events');
122 $extra = ['onclick' => "showObjectSelect();"];
123 $this->addRadio('object_type',
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);
138 '-1' => ts('- select group -'),
139 '0' => ts('All Groups'),
140 ] + CRM_Core_PseudoConstant
::group();
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');
148 '-1' => ts('- select profile -'),
149 '0' => ts('All Profiles'),
150 ] + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_UFField', 'uf_group_id');
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 )");
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);
162 $this->add('checkbox', 'is_active', ts('Enabled?'));
164 $this->addFormRule(['CRM_ACL_Form_ACL', 'formRule']);
168 * @param array $params
172 public static function formRule($params) {
173 $showHide = new CRM_Core_ShowHideBlocks();
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.');
180 $validOperations = ['View', 'Edit'];
181 $operationMessage = ts("Only 'View' and 'Edit' operations are valid for this type of data");
183 // Figure out which type of object we're permissioning on and make sure user has selected a value.
184 switch ($params['object_type']) {
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");
193 if (!in_array($params['operation'], $validOperations)) {
194 $errors['operation'] = $operationMessage;
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");
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");
216 if (!in_array($params['operation'], $validOperations)) {
217 $errors['operation'] = $operationMessage;
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");
229 if (!in_array($params['operation'], $validOperations)) {
230 $errors['operation'] = $operationMessage;
235 $showHide->addToTemplate();
237 return empty($errors) ?
TRUE : $errors;
241 * Process the form submission.
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();
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');
254 $params = $this->controller
->exportValues($this->_name
);
255 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
257 $params['entity_table'] = 'civicrm_acl_role';
259 // Figure out which type of object we're permissioning on and set object_table and object_id.
260 switch ($params['object_type']) {
262 $params['object_table'] = 'civicrm_saved_search';
263 $params['object_id'] = $params['group_id'];
267 $params['object_table'] = 'civicrm_uf_group';
268 $params['object_id'] = $params['uf_group_id'];
272 $params['object_table'] = 'civicrm_custom_group';
273 $params['object_id'] = $params['custom_group_id'];
277 $params['object_table'] = 'civicrm_event';
278 $params['object_id'] = $params['event_id'];
283 $params['id'] = $this->_id
;
286 CRM_ACL_BAO_ACL
::create($params);