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