3
* This file is part of Critecia
4
* Copyright (C) 2011 Christian A. Reiter
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Affero General Public License as
8
* published by the Free Software Foundation, either version 3 of the
9
* License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Affero General Public License for more details.
16
* You should have received a copy of the GNU Affero General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
class GroupsController extends AppController {
21
public $uses = array('User', 'Group', 'Hospital', 'Right', 'Grant');
22
public $components = array('RequestHandler', 'Reflector');
24
private $default_actions = array(
25
'index' => array('order' => 1,
26
'img' => '22x22/actions/list-index.png',
27
'img-disabled' => '22x22/actions/list-index-disabled.png',
28
'description' => 'auflisten' ),
29
'view' => array('order' => 2,
30
'img' => '22x22/actions/list-view.png',
31
'img-disabled' => '22x22/actions/list-view-disabled.png',
32
'description' => 'ansehen'),
33
'edit' => array('order' => 3,
34
'img' => '22x22/actions/edit.png',
35
'img-disabled' => '22x22/actions/edit-disabled.png',
36
'description' => 'bearbeiten'),
37
'add' => array('order' => 4,
38
'img' => '22x22/actions/edit-add.png',
39
'img-disabled' => '22x22/actions/edit-add-disabled.png',
40
'description' => 'hinzufügen'),
41
'delete' => array('order' => 5,
42
'img' => '22x22/actions/delete.png',
43
'img-disabled' => '22x22/actions/delete-disabled.png',
44
'description' => 'löschen'),
48
function beforeFilter() {
49
parent::beforeFilter();
50
$this->see_hidden = $this->AccessControl->check('Groups/see_hidden');
51
$this->set('see_hidden', $this->see_hidden);
55
$conditions = array('hospital_id' => $this->current_hospital['Hospital']['id']);
56
if(!$this->see_hidden) {
57
$conditions['hidden'] = 0;
59
$this->set('groups', $this->Group->find('all' , array('conditions' => $conditions)));
60
$hospital = $this->current_hospital;
61
$this->set(compact('hospital'));
64
function global_index($hospital_id = null) {
65
$this->set('groups', $this->Group->find('all'));
66
$hospital = $this->Hospital->read(null, $hospital_id);
67
$this->set(compact('hospital'));
68
$this->render('index');
71
function view($id = null) {
72
// TODO: check if group is hidden + I can see hidden groups
74
$this->Session->setFlash('Ungültige Gruppe');
75
$this->redirect($this->referer());
77
$this->set('group', $this->Group->read(null, $id));
78
$this->render('view');
81
function global_view($id = null) {
83
$this->Session->setFlash('Ungültige Gruppe');
84
$this->redirect(array('action' => 'index'));
86
$this->set('group', $this->Group->read(null, $id));
87
$this->render('view');
90
function global_add($hospital_id = null) {
92
$hospital = $this->Hospital->read(null, $hospital_id);
94
$this->Session->setFlash('Ungültiges Krankenhaus: '.$hospital_id);
95
$this->redirect(array('action' => 'index'));
99
$hospitals = $this->Hospital->find('list');
101
if (!empty($this->data)) {
102
$this->Group->create();
103
if ($this->Group->save($this->data)) {
104
$this->updateActiveHospital();
105
$this->Session->setFlash('Gruppe wurde gespeichert');
106
$this->redirect(array('action' => 'index'));
108
$this->Session->setFlash('Gruppe konnte nicht gespeichert werden.');
111
$users = $this->Group->User->find('list');
112
$this->set(compact('users', 'hospital', 'hospitals'));
113
$this->render('add');
117
if (!empty($this->data)) {
118
$this->Group->create();
119
$this->data['Group']['hospital_id'] = $this->current_hospital['Hospital']['id'];
120
if ($this->Group->save($this->data)) {
121
$this->updateActiveHospital();
122
$this->Session->setFlash('Gruppe wurde gespeichert');
123
$this->redirect(array('action' => 'index'));
125
$this->Session->setFlash('Gruppe konnte nicht gespeichert werden.');
128
$users = $this->Group->User->find('list');
129
$this->set(compact('users'));
130
$this->set('see_hidden', $this->AccessControl->check('Groups/see_hidden'));
133
/************************** edit **************************/
135
private function _edit($id) {
136
if(!$id && empty($this->data)) {
137
$this->Session->setFlash('Ungültige Gruppe');
138
$this->redirect(array('action' => 'index'));
140
if(!empty($this->data)) {
141
if ($this->Group->save($this->data)) {
142
$this->updateActiveHospital();
143
$this->Session->setFlash('Gruppe wurde gespeichert');
144
$this->redirect(array('action' => 'index'));
146
$this->Session->setFlash('Gruppe konnte nicht gespeichert werden.');
149
// TODO: check if I can edit requested group!
150
$this->data = $this->Group->read(null, $id);
152
$users = $this->User->find('list');
153
$this->set(compact('users'));
156
public function edit($id) {
157
// if called empty, check if department is in current hospital
158
$check_hospital = empty($this->data);
162
if($check_hospital) {
163
// check if I can edit requested group!
164
if($this->data['Group']['hospital_id'] != $this->current_hospital['Hospital']['id']) {
165
$this->Session->setFlash("Diese Gruppe befindet sich nicht im aktuellen Krankenhaus.");
167
$this->redirect(array('action' => 'index'));
170
$this->set('see_hidden', $this->AccessControl->check('Groups/see_hidden'));
173
public function global_edit($id) {
175
$this->set('see_hidden', true);
176
$this->render('edit');
180
/************************** delete **************************/
182
function global_delete($id = null) {
184
$this->Session->setFlash('Ungültige Gruppe.');
185
$this->redirect(array('action'=>'index'));
187
if ($this->Group->delete($id)) {
188
$this->Session->setFlash('Gruppe gelöscht.');
189
$this->redirect(array('action'=>'index'));
191
$this->Session->setFlash('Gruppe konnte nicht gelöscht werden.');
192
$this->redirect(array('action' => 'index'));
195
function delete($id = null) {
196
$this->global_delete($id);
201
* compares 2 actions in the form "Controller/action" like given from Reflector->get_all_actions()
202
* FUNCTION IS NOT PLUGIN-SAFE!
204
private function action_cmp($a, $b) {
205
if(is_null($a) or is_null($b))
207
foreach($this->default_actions as $name => $tmp) {
208
$default_names[] = $name;
211
$arr = String::tokenize($a, '/');
212
switch(count($arr)) {
214
$a_plugin_name = null;
215
$a_controller_name = $arr[0];
219
$a_plugin_name = $arr[0];
220
$a_controller_name = $arr[1];
225
$arr = String::tokenize($b, '/');
226
switch(count($arr)) {
228
$b_plugin_name = null;
229
$b_controller_name = $arr[0];
233
$b_plugin_name = $arr[0];
234
$b_controller_name = $arr[1];
238
if($a_controller_name != $b_controller_name) {
239
return $a_controller_name < $b_controller_name ? -1 : 1;
241
if(in_array($a_action, $default_names)) { // $a is default action
242
if(in_array($b_action, $default_names)) { // both default actions
243
//sort by default order
244
return $this->default_actions[$a_action]['order'] < $this->default_actions[$b_action]['order'] ? -1 : 1;
246
// $a is default, $b not
248
} elseif(in_array($b_action, $default_names)) { // only $b is default action, $a not
250
} else { // none is default action
256
public function global_edit_rights($id) {
257
$this->edit_rights($id);
258
$this->render('edit_rights');
262
* edits the grants of a group
263
* @return $grants[$group['Group']['id']]:
268
public function edit_rights($id) {
269
//TODO: implement new rights system
273
$this->Group->recursive = -1;
274
$group = $this->Group->findById($id);
276
$this->Session->setFlash('Ungültige Gruppe.');
278
//TODO: check if I may access this group!
281
$action_array = $this->Reflector->get_actions_array();
282
$grants = $this->Grant->find('all', array('conditions' => array(
283
'group_id' => $group['Group']['id']
285
$rights = $this->Right->find('list');
286
$hospitals = $this->Hospital->find('list'); // TODO: only ones I have access for!
288
$this->set(compact('group', 'grants', 'rights', 'hospitals'));
291
/** AJAX function to set a grants for a group
292
* @param $id UUID Group-ID which grants should be changed
293
* @param $controller string controller name that should be accessible
294
* @param $action string action name of that controller that should be accessible
295
* @param $grant boolean if the grant should be granted or not
297
public function set_grant($group_id, $controller, $action, $grant = 0) {
299
if(!isset($group_id) or is_null($group_id)) die();
302
// FIXME: remove ACl --> Grants etc.
303
$success = $this->Acl->allow(array('model' => 'Group', 'foreign_key' => $group_id), "All/Hospital_root/Hospital_".$this->current_hospital['Hospital']['id']."/$controller/$action");
305
} elseif($grant == 0) {
306
// TODO: is it really necessary to DENY? not enough to delete grant?
307
$success = $this->Acl->deny(array('model' => 'Group', 'foreign_key' => $group_id), "All/Hospital_root/Hospital_".$this->current_hospital['Hospital']['id']."/$controller/$action");
311
//reverse $granted for the new link
312
$grant = ($grant == 0)? 1 : 0;
313
$this->set(compact('enabled', 'group_id', 'controller', 'action', 'grant'));
314
$this->set('default_actions', $this->default_actions);