~critecia/critecia/trunk

« back to all changes in this revision

Viewing changes to src/app/controllers/groups_controller.php

  • Committer: Christian A. Reiter
  • Date: 2011-11-16 20:08:35 UTC
  • Revision ID: christian.a.reiter@gmail.com-20111116200835-h3xx0ekm47lububw
fixed jQuery file links

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 /**
3
 
 *    This file is part of Critecia
4
 
 *    Copyright (C) 2011 Christian A. Reiter
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
 
20
 
        class GroupsController extends AppController {
21
 
                public $uses = array('User', 'Group', 'Hospital', 'Right', 'Grant');
22
 
                public $components = array('RequestHandler', 'Reflector');
23
 
 
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'),
45
 
                                );
46
 
 
47
 
                
48
 
                function beforeFilter() {
49
 
                        parent::beforeFilter();
50
 
                        $this->see_hidden = $this->AccessControl->check('Groups/see_hidden');
51
 
                        $this->set('see_hidden', $this->see_hidden);
52
 
                }
53
 
 
54
 
                function index() {
55
 
                        $conditions = array('hospital_id' => $this->current_hospital['Hospital']['id']);
56
 
                        if(!$this->see_hidden) {
57
 
                                $conditions['hidden'] = 0;
58
 
                        }
59
 
                        $this->set('groups', $this->Group->find('all' , array('conditions' => $conditions)));
60
 
                        $hospital = $this->current_hospital;
61
 
                        $this->set(compact('hospital'));        
62
 
                }
63
 
 
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');
69
 
                }
70
 
 
71
 
                function view($id = null) {
72
 
                        // TODO: check if group is hidden + I can see hidden groups
73
 
                        if (!$id) {
74
 
                                $this->Session->setFlash('Ungültige Gruppe');
75
 
                                $this->redirect($this->referer());
76
 
                        }
77
 
                        $this->set('group', $this->Group->read(null, $id));
78
 
                        $this->render('view');
79
 
                }
80
 
                                
81
 
                function global_view($id = null) {
82
 
                        if (!$id) {
83
 
                                $this->Session->setFlash('Ungültige Gruppe');
84
 
                                $this->redirect(array('action' => 'index'));
85
 
                        }
86
 
                        $this->set('group', $this->Group->read(null, $id));
87
 
                        $this->render('view');
88
 
                }
89
 
 
90
 
                function global_add($hospital_id = null) {
91
 
                        if($hospital_id) {
92
 
                                $hospital = $this->Hospital->read(null, $hospital_id);
93
 
                                if(!$hospital) {
94
 
                                        $this->Session->setFlash('Ungültiges Krankenhaus: '.$hospital_id);
95
 
                                        $this->redirect(array('action' => 'index'));
96
 
                                }
97
 
                        } else {
98
 
                                $hospital = null;
99
 
                                $hospitals = $this->Hospital->find('list');
100
 
                        }
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'));
107
 
                                } else {
108
 
                                        $this->Session->setFlash('Gruppe konnte nicht gespeichert werden.');
109
 
                                }
110
 
                        }
111
 
                        $users = $this->Group->User->find('list');
112
 
                        $this->set(compact('users', 'hospital', 'hospitals'));
113
 
                        $this->render('add');
114
 
                }
115
 
 
116
 
                function 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'));
124
 
                                } else {
125
 
                                        $this->Session->setFlash('Gruppe konnte nicht gespeichert werden.');
126
 
                                }
127
 
                        }
128
 
                        $users = $this->Group->User->find('list');
129
 
                        $this->set(compact('users'));
130
 
                        $this->set('see_hidden', $this->AccessControl->check('Groups/see_hidden'));
131
 
                }
132
 
 
133
 
/**************************  edit   **************************/
134
 
 
135
 
                private function _edit($id) {
136
 
                        if(!$id && empty($this->data)) {
137
 
                                $this->Session->setFlash('Ungültige Gruppe');
138
 
                                $this->redirect(array('action' => 'index'));
139
 
                        }
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'));
145
 
                                } else {
146
 
                                        $this->Session->setFlash('Gruppe konnte nicht gespeichert werden.');
147
 
                                }
148
 
                        } else {
149
 
                                // TODO: check if I can edit requested group!
150
 
                                $this->data = $this->Group->read(null, $id);
151
 
                        }
152
 
                        $users = $this->User->find('list');
153
 
                        $this->set(compact('users'));
154
 
                }
155
 
                
156
 
                public function edit($id) {
157
 
                        // if called empty, check if department is in current hospital
158
 
                        $check_hospital = empty($this->data);
159
 
                        
160
 
                        $this->_edit($id);
161
 
 
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.");
166
 
                                        $this->data = null;
167
 
                                        $this->redirect(array('action' => 'index'));
168
 
                                }
169
 
                        }
170
 
                        $this->set('see_hidden', $this->AccessControl->check('Groups/see_hidden'));
171
 
                }
172
 
                
173
 
                public function global_edit($id) { 
174
 
                        $this->_edit($id);
175
 
                        $this->set('see_hidden', true);
176
 
                        $this->render('edit');
177
 
                }
178
 
 
179
 
 
180
 
/**************************  delete   **************************/
181
 
 
182
 
                function global_delete($id = null) {
183
 
                        if (!$id) {
184
 
                                $this->Session->setFlash('Ungültige Gruppe.');
185
 
                                $this->redirect(array('action'=>'index'));
186
 
                        }
187
 
                        if ($this->Group->delete($id)) {
188
 
                                $this->Session->setFlash('Gruppe gelöscht.');
189
 
                                $this->redirect(array('action'=>'index'));
190
 
                        }
191
 
                        $this->Session->setFlash('Gruppe konnte nicht gelöscht werden.');
192
 
                        $this->redirect(array('action' => 'index'));
193
 
                }
194
 
                
195
 
                function delete($id = null) {
196
 
                        $this->global_delete($id);
197
 
                }
198
 
 
199
 
 
200
 
/**
201
 
* compares 2 actions in the form "Controller/action" like given from Reflector->get_all_actions()
202
 
* FUNCTION IS NOT PLUGIN-SAFE!
203
 
*/
204
 
                private function action_cmp($a, $b) {
205
 
                        if(is_null($a) or is_null($b))
206
 
                                return 0;
207
 
                        foreach($this->default_actions as $name => $tmp) {
208
 
                                $default_names[] = $name;
209
 
                        }
210
 
 
211
 
                        $arr = String::tokenize($a, '/');
212
 
                        switch(count($arr)) {
213
 
                                case 2:
214
 
                                        $a_plugin_name     = null;
215
 
                                        $a_controller_name = $arr[0];
216
 
                                        $a_action          = $arr[1];
217
 
                                        break;
218
 
                                case 3:
219
 
                                        $a_plugin_name     = $arr[0];
220
 
                                        $a_controller_name = $arr[1];
221
 
                                        $a_action          = $arr[2];
222
 
                                        break;
223
 
                        }
224
 
 
225
 
                        $arr = String::tokenize($b, '/');
226
 
                        switch(count($arr)) {
227
 
                                case 2:
228
 
                                        $b_plugin_name     = null;
229
 
                                        $b_controller_name = $arr[0];
230
 
                                        $b_action          = $arr[1];
231
 
                                        break;
232
 
                                case 3:
233
 
                                        $b_plugin_name     = $arr[0];
234
 
                                        $b_controller_name = $arr[1];
235
 
                                        $b_action          = $arr[2];
236
 
                                        break;
237
 
                        }
238
 
                        if($a_controller_name != $b_controller_name) {
239
 
                                return $a_controller_name < $b_controller_name ? -1 : 1;
240
 
                        } else {
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;
245
 
                                        }
246
 
                                        // $a is default, $b not
247
 
                                        return -1;
248
 
                                } elseif(in_array($b_action, $default_names)) { // only $b is default action, $a not
249
 
                                        return 1;
250
 
                                } else { // none is default action
251
 
                                        return 0;
252
 
                                }
253
 
                        }
254
 
                }
255
 
 
256
 
                public function  global_edit_rights($id) {
257
 
                        $this->edit_rights($id);
258
 
                        $this->render('edit_rights');
259
 
                }
260
 
                
261
 
/**
262
 
* edits the grants of a group
263
 
* @return $grants[$group['Group']['id']]:
264
 
*                1: granted
265
 
*                0: denied
266
 
*               -1: ACO not found
267
 
*/
268
 
                public function edit_rights($id) {
269
 
//TODO: implement new rights system
270
 
                        if(!$id){
271
 
                                //TODO: error
272
 
                        }
273
 
                        $this->Group->recursive = -1;
274
 
                        $group = $this->Group->findById($id);
275
 
                        if(!$group){
276
 
                                $this->Session->setFlash('Ungültige Gruppe.');
277
 
                        }
278
 
                        //TODO: check if I may access this group!
279
 
 
280
 
 
281
 
                        $action_array = $this->Reflector->get_actions_array();
282
 
                        $grants = $this->Grant->find('all', array('conditions' => array(
283
 
                                'group_id' => $group['Group']['id']
284
 
                        )));
285
 
                        $rights = $this->Right->find('list');
286
 
                        $hospitals = $this->Hospital->find('list'); // TODO: only ones I have access for!
287
 
 
288
 
                        $this->set(compact('group', 'grants', 'rights', 'hospitals'));
289
 
                }
290
 
 
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
296
 
*/
297
 
                public function set_grant($group_id, $controller, $action, $grant = 0) {
298
 
 
299
 
                        if(!isset($group_id) or is_null($group_id)) die();
300
 
 
301
 
                        if($grant == 1) {
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");
304
 
                                $enabled = $success;
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");
308
 
                                $enabled = false;
309
 
                        }
310
 
 
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);
315
 
                }
316
 
        }
317
 
?>