Merge pull request #12583 from omarabuhussein/dev/core#288
[civicrm-core.git] / CRM / ACL / Form / ACL.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_ACL_Form_ACL extends CRM_Admin_Form {
34
35 /**
36 * Set default values for the form.
37 */
38 public function setDefaultValues() {
39 $defaults = parent::setDefaultValues();
40
41 if ($this->_action & CRM_Core_Action::ADD) {
42 $defaults['object_type'] = 1;
43 }
44
45 $showHide = new CRM_Core_ShowHideBlocks();
46
47 if (isset($defaults['object_table'])) {
48 switch ($defaults['object_table']) {
49 case 'civicrm_saved_search':
50 $defaults['group_id'] = $defaults['object_id'];
51 $defaults['object_type'] = 1;
52 $showHide->addShow("id-group-acl");
53 $showHide->addHide("id-profile-acl");
54 $showHide->addHide("id-custom-acl");
55 $showHide->addHide("id-event-acl");
56 break;
57
58 case 'civicrm_uf_group':
59 $defaults['uf_group_id'] = $defaults['object_id'];
60 $defaults['object_type'] = 2;
61 $showHide->addHide("id-group-acl");
62 $showHide->addShow("id-profile-acl");
63 $showHide->addHide("id-custom-acl");
64 $showHide->addHide("id-event-acl");
65 break;
66
67 case 'civicrm_custom_group':
68 $defaults['custom_group_id'] = $defaults['object_id'];
69 $defaults['object_type'] = 3;
70 $showHide->addHide("id-group-acl");
71 $showHide->addHide("id-profile-acl");
72 $showHide->addShow("id-custom-acl");
73 $showHide->addHide("id-event-acl");
74 break;
75
76 case 'civicrm_event':
77 $defaults['event_id'] = $defaults['object_id'];
78 $defaults['object_type'] = 4;
79 $showHide->addHide("id-group-acl");
80 $showHide->addHide("id-profile-acl");
81 $showHide->addHide("id-custom-acl");
82 $showHide->addShow("id-event-acl");
83 break;
84 }
85 }
86 else {
87 $showHide->addHide("id-group-acl");
88 $showHide->addHide("id-profile-acl");
89 $showHide->addHide("id-custom-acl");
90 $showHide->addHide("id-event-acl");
91 }
92
93 // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
94 if (!($this->_action & CRM_Core_Action::DELETE)) {
95 $showHide->addToTemplate();
96 }
97
98 return $defaults;
99 }
100
101 /**
102 * Build the form object.
103 */
104 public function buildQuickForm() {
105 parent::buildQuickForm();
106
107 $this->setPageTitle(ts('ACL'));
108
109 if ($this->_action & CRM_Core_Action::DELETE) {
110 return;
111 }
112
113 $attributes = CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL');
114
115 $this->add('text', 'name', ts('Description'), CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL', 'name'), TRUE);
116
117 $operations = array('' => ts('- select -')) + CRM_ACL_BAO_ACL::operation();
118 $this->add('select',
119 'operation',
120 ts('Operation'),
121 $operations, TRUE
122 );
123
124 $objTypes = array(
125 '1' => ts('A group of contacts'),
126 '2' => ts('A profile'),
127 '3' => ts('A set of custom data fields'),
128 );
129
130 if (CRM_Core_Permission::access('CiviEvent')) {
131 $objTypes['4'] = ts('Events');
132 }
133
134 $extra = array('onclick' => "showObjectSelect();");
135 $this->addRadio('object_type',
136 ts('Type of Data'),
137 $objTypes,
138 $extra,
139 '&nbsp;', TRUE
140 );
141
142 $label = ts('Role');
143 $role = array(
144 '-1' => ts('- select role -'),
145 '0' => ts('Everyone'),
146 ) + CRM_Core_OptionGroup::values('acl_role');
147 $this->add('select', 'entity_id', $label, $role, TRUE);
148
149 $group = array(
150 '-1' => ts('- select -'),
151 '0' => ts('All Groups'),
152 ) + CRM_Core_PseudoConstant::group();
153
154 $customGroup = array(
155 '-1' => ts('- select -'),
156 '0' => ts('All Custom Groups'),
157 ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
158
159 $ufGroup = array(
160 '-1' => ts('- select -'),
161 '0' => ts('All Profiles'),
162 ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
163
164 $event = array(
165 '-1' => ts('- select -'),
166 '0' => ts('All Events'),
167 ) + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
168
169 $this->add('select', 'group_id', ts('Group'), $group);
170 $this->add('select', 'custom_group_id', ts('Custom Data'), $customGroup);
171 $this->add('select', 'uf_group_id', ts('Profile'), $ufGroup);
172 $this->add('select', 'event_id', ts('Event'), $event);
173
174 $this->add('checkbox', 'is_active', ts('Enabled?'));
175
176 $this->addFormRule(array('CRM_ACL_Form_ACL', 'formRule'));
177 }
178
179 /**
180 * @param array $params
181 *
182 * @return bool
183 */
184 public static function formRule($params) {
185 $showHide = new CRM_Core_ShowHideBlocks();
186
187 // Make sure role is not -1
188 if ($params['entity_id'] == -1) {
189 $errors['entity_id'] = ts('Please assign this permission to a Role.');
190 }
191
192 $validOperations = array('View', 'Edit');
193 $operationMessage = ts("Only 'View' and 'Edit' operations are valid for this type of data");
194
195 // Figure out which type of object we're permissioning on and make sure user has selected a value.
196 switch ($params['object_type']) {
197 case 1:
198 if ($params['group_id'] == -1) {
199 $errors['group_id'] = ts('Please select a Group (or ALL Groups).');
200 $showHide->addShow("id-group-acl");
201 $showHide->addHide("id-profile-acl");
202 $showHide->addHide("id-custom-acl");
203 $showHide->addHide("id-event-acl");
204 }
205 if (!in_array($params['operation'], $validOperations)) {
206 $errors['operation'] = $operationMessage;
207 }
208 break;
209
210 case 2:
211 if ($params['uf_group_id'] == -1) {
212 $errors['uf_group_id'] = ts('Please select a Profile (or ALL Profiles).');
213 $showHide->addShow("id-profile-acl");
214 $showHide->addHide("id-group-acl");
215 $showHide->addHide("id-custom-acl");
216 $showHide->addHide("id-event-acl");
217 }
218 break;
219
220 case 3:
221 if ($params['custom_group_id'] == -1) {
222 $errors['custom_group_id'] = ts('Please select a set of Custom Data (or ALL Custom Data).');
223 $showHide->addShow("id-custom-acl");
224 $showHide->addHide("id-group-acl");
225 $showHide->addHide("id-profile-acl");
226 $showHide->addHide("id-event-acl");
227 }
228 if (!in_array($params['operation'], $validOperations)) {
229 $errors['operation'] = $operationMessage;
230 }
231 break;
232
233 case 4:
234 if ($params['event_id'] == -1) {
235 $errors['event_id'] = ts('Please select an Event (or ALL Events).');
236 $showHide->addShow("id-event-acl");
237 $showHide->addHide("id-custom-acl");
238 $showHide->addHide("id-group-acl");
239 $showHide->addHide("id-profile-acl");
240 }
241 if (!in_array($params['operation'], $validOperations)) {
242 $errors['operation'] = $operationMessage;
243 }
244 break;
245 }
246
247 $showHide->addToTemplate();
248
249 return empty($errors) ? TRUE : $errors;
250 }
251
252 /**
253 * Process the form submission.
254 */
255 public function postProcess() {
256 // note this also resets any ACL cache
257 CRM_Core_BAO_Cache::deleteGroup('contact fields');
258
259 if ($this->_action & CRM_Core_Action::DELETE) {
260 CRM_ACL_BAO_ACL::del($this->_id);
261 CRM_Core_Session::setStatus(ts('Selected ACL has been deleted.'), ts('Record Deleted'), 'success');
262 }
263 else {
264 $params = $this->controller->exportValues($this->_name);
265 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
266 $params['deny'] = 0;
267 $params['entity_table'] = 'civicrm_acl_role';
268
269 // Figure out which type of object we're permissioning on and set object_table and object_id.
270 switch ($params['object_type']) {
271 case 1:
272 $params['object_table'] = 'civicrm_saved_search';
273 $params['object_id'] = $params['group_id'];
274 break;
275
276 case 2:
277 $params['object_table'] = 'civicrm_uf_group';
278 $params['object_id'] = $params['uf_group_id'];
279 break;
280
281 case 3:
282 $params['object_table'] = 'civicrm_custom_group';
283 $params['object_id'] = $params['custom_group_id'];
284 break;
285
286 case 4:
287 $params['object_table'] = 'civicrm_event';
288 $params['object_id'] = $params['event_id'];
289 break;
290 }
291
292 if ($this->_id) {
293 $params['id'] = $this->_id;
294 }
295
296 CRM_ACL_BAO_ACL::create($params);
297 }
298 }
299
300 }