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