Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | /** | |
46 | * This function sets the default values for the form. | |
47 | * | |
48 | * @access public | |
49 | * | |
355ba699 | 50 | * @return void |
6a488035 TO |
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 | * Function to build the form | |
117 | * | |
355ba699 | 118 | * @return void |
6a488035 TO |
119 | * @access public |
120 | */ | |
121 | public function buildQuickForm() { | |
122 | parent::buildQuickForm(); | |
123 | ||
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 | ||
139 | $objTypes = array('1' => ts('A group of contacts'), | |
140 | '2' => ts('A profile'), | |
141 | '3' => ts('A set of custom data fields'), | |
142 | ); | |
143 | ||
144 | if (CRM_Core_Permission::access('CiviEvent')) { | |
145 | $objTypes['4'] = ts('Events'); | |
146 | } | |
147 | ||
148 | $extra = array('onclick' => "showObjectSelect();"); | |
149 | $this->addRadio('object_type', | |
150 | ts('Type of Data'), | |
151 | $objTypes, | |
152 | $extra, | |
153 | ' ', TRUE | |
154 | ); | |
155 | ||
156 | ||
157 | $label = ts('Role'); | |
158 | $role = array('-1' => ts('- select role -'), | |
159 | '0' => ts('Everyone'), | |
160 | ) + CRM_Core_OptionGroup::values('acl_role'); | |
161 | $this->add('select', 'entity_id', $label, $role, TRUE); | |
162 | ||
163 | $group = array('-1' => ts('- select -'), | |
164 | '0' => ts('All Groups'), | |
165 | ) + CRM_Core_PseudoConstant::group(); | |
166 | ||
167 | $customGroup = array('-1' => ts('- select -'), | |
168 | '0' => ts('All Custom Groups'), | |
cd43c5e3 | 169 | ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id'); |
6a488035 TO |
170 | |
171 | $ufGroup = array('-1' => ts('- select -'), | |
172 | '0' => ts('All Profiles'), | |
ff4f7744 | 173 | ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id'); |
6a488035 TO |
174 | |
175 | $event = array('-1' => ts('- select -'), | |
176 | '0' => ts('All Events'), | |
177 | ) + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); | |
178 | ||
179 | $this->add('select', 'group_id', ts('Group'), $group); | |
180 | $this->add('select', 'custom_group_id', ts('Custom Data'), $customGroup); | |
181 | $this->add('select', 'uf_group_id', ts('Profile'), $ufGroup); | |
182 | $this->add('select', 'event_id', ts('Event'), $event); | |
183 | ||
184 | $this->add('checkbox', 'is_active', ts('Enabled?')); | |
185 | ||
186 | $this->addFormRule(array('CRM_ACL_Form_ACL', 'formRule')); | |
187 | } | |
188 | ||
28518c90 EM |
189 | /** |
190 | * @param $params | |
191 | * | |
192 | * @return bool | |
193 | */ | |
6a488035 TO |
194 | static function formRule($params) { |
195 | $showHide = new CRM_Core_ShowHideBlocks(); | |
196 | ||
197 | // Make sure role is not -1 | |
198 | if ($params['entity_id'] == -1) { | |
199 | $errors['entity_id'] = ts('Please assign this permission to a Role.'); | |
200 | } | |
201 | ||
202 | $validOperations = array('View', 'Edit'); | |
203 | $operationMessage = ts("Only 'View' and 'Edit' operations are valid for this type of data"); | |
204 | ||
205 | // Figure out which type of object we're permissioning on and make sure user has selected a value. | |
206 | switch ($params['object_type']) { | |
207 | case 1: | |
208 | if ($params['group_id'] == -1) { | |
209 | $errors['group_id'] = ts('Please select a Group (or ALL Groups).'); | |
210 | $showHide->addShow("id-group-acl"); | |
211 | $showHide->addHide("id-profile-acl"); | |
212 | $showHide->addHide("id-custom-acl"); | |
213 | $showHide->addHide("id-event-acl"); | |
214 | } | |
215 | if (!in_array($params['operation'], $validOperations)) { | |
216 | $errors['operation'] = $operationMessage; | |
217 | } | |
218 | break; | |
219 | ||
220 | case 2: | |
221 | if ($params['uf_group_id'] == -1) { | |
222 | $errors['uf_group_id'] = ts('Please select a Profile (or ALL Profiles).'); | |
223 | $showHide->addShow("id-profile-acl"); | |
224 | $showHide->addHide("id-group-acl"); | |
225 | $showHide->addHide("id-custom-acl"); | |
226 | $showHide->addHide("id-event-acl"); | |
227 | } | |
228 | break; | |
229 | ||
230 | case 3: | |
231 | if ($params['custom_group_id'] == -1) { | |
232 | $errors['custom_group_id'] = ts('Please select a set of Custom Data (or ALL Custom Data).'); | |
233 | $showHide->addShow("id-custom-acl"); | |
234 | $showHide->addHide("id-group-acl"); | |
235 | $showHide->addHide("id-profile-acl"); | |
236 | $showHide->addHide("id-event-acl"); | |
237 | } | |
238 | if (!in_array($params['operation'], $validOperations)) { | |
239 | $errors['operation'] = $operationMessage; | |
240 | } | |
241 | break; | |
242 | ||
243 | case 4: | |
244 | if ($params['event_id'] == -1) { | |
245 | $errors['event_id'] = ts('Please select an Event (or ALL Events).'); | |
246 | $showHide->addShow("id-event-acl"); | |
247 | $showHide->addHide("id-custom-acl"); | |
248 | $showHide->addHide("id-group-acl"); | |
249 | $showHide->addHide("id-profile-acl"); | |
250 | } | |
251 | if (!in_array($params['operation'], $validOperations)) { | |
252 | $errors['operation'] = $operationMessage; | |
253 | } | |
254 | break; | |
255 | } | |
256 | ||
257 | $showHide->addToTemplate(); | |
258 | ||
259 | return empty($errors) ? TRUE : $errors; | |
260 | } | |
261 | ||
262 | /** | |
263 | * Function to process the form | |
264 | * | |
265 | * @access public | |
266 | * | |
355ba699 | 267 | * @return void |
6a488035 TO |
268 | */ |
269 | public function postProcess() { | |
270 | // note this also resets any ACL cache | |
271 | CRM_Core_BAO_Cache::deleteGroup('contact fields'); | |
272 | ||
273 | ||
274 | if ($this->_action & CRM_Core_Action::DELETE) { | |
275 | CRM_ACL_BAO_ACL::del($this->_id); | |
276 | CRM_Core_Session::setStatus(ts('Selected ACL has been deleted.'), ts('Record Deleted'), 'success'); | |
277 | } | |
278 | else { | |
279 | $params = $this->controller->exportValues($this->_name); | |
280 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
281 | $params['deny'] = 0; | |
282 | $params['entity_table'] = 'civicrm_acl_role'; | |
283 | ||
284 | // Figure out which type of object we're permissioning on and set object_table and object_id. | |
285 | switch ($params['object_type']) { | |
286 | case 1: | |
287 | $params['object_table'] = 'civicrm_saved_search'; | |
288 | $params['object_id'] = $params['group_id']; | |
289 | break; | |
290 | ||
291 | case 2: | |
292 | $params['object_table'] = 'civicrm_uf_group'; | |
293 | $params['object_id'] = $params['uf_group_id']; | |
294 | break; | |
295 | ||
296 | case 3: | |
297 | $params['object_table'] = 'civicrm_custom_group'; | |
298 | $params['object_id'] = $params['custom_group_id']; | |
299 | break; | |
300 | ||
301 | case 4: | |
302 | $params['object_table'] = 'civicrm_event'; | |
303 | $params['object_id'] = $params['event_id']; | |
304 | break; | |
305 | } | |
306 | ||
307 | if ($this->_id) { | |
308 | $params['id'] = $this->_id; | |
309 | } | |
310 | ||
311 | CRM_ACL_BAO_ACL::create($params); | |
312 | } | |
313 | } | |
314 | } | |
315 |