1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class departmentManagement extends plugin
24 {
25 /* Definitions */
26 var $plHeadline= "Departments";
27 var $plDescription= "Manage Departments";
29 /* Headpage attributes */
30 var $last_dep_sorting= "invalid";
31 var $departments= array();
32 var $deptabs= NULL;
34 /* attribute list for save action */
35 var $attributes= array();
36 var $objectclasses= array();
38 /* Vars to handle operations after saving the department
39 Recursive move && tagging */
40 var $ObjectInSaveMode = false; // Is true, if current object wasn't saved right now
41 var $dns = array();
43 var $acl_module = array("department");
45 function departmentManagement (&$config, &$ui)
46 {
47 $this->ui= &$ui;
48 $this->dn= "";
49 $this->config= &$config;
50 $this->DivListDepartment = new divListDepartment($this->config,$this);
51 }
53 function execute()
54 {
55 global $config;
57 /* Call parent execute */
58 plugin::execute();
60 /***************
61 Var init
62 ***************/
64 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/","/^menu_action/"));
66 /* Reload departments */
67 $smarty = get_smarty();
68 $display = "";
69 $s_action = ""; // Will contain an action, like del or edit
70 $s_entry = ""; // The entry name for edit delete -...
73 /***************
74 Check posts
75 ***************/
77 // Check Post action
78 foreach($_POST as $key => $val){
79 // Post for delete
80 if(preg_match("/dep_del.*/",$key)){
81 $s_action = "del";
82 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
83 $s_entry = preg_replace("/_.*$/","",$s_entry);
84 $s_entry = base64_decode($s_entry);
85 // Post for edit
86 }elseif(preg_match("/dep_edit_.*/",$key)){
87 $s_action="edit";
88 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
89 $s_entry = preg_replace("/_.*$/","",$s_entry);
90 $s_entry = base64_decode($s_entry);
91 // Post for new
92 }elseif(preg_match("/^remove_multiple_departments/",$key)){
93 $s_action="del_multiple";
94 }elseif(preg_match("/dep_new.*/",$key)){
95 $s_action="new";
96 }
97 }
99 /* Create options */
100 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
101 $s_action = "new";
102 }
104 /* handle remove from layers menu */
105 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
106 $s_action = "del_multiple";
107 }
110 /***************
111 Create a new department
112 ***************/
114 /* New Entry if Posted action (s_action) == new
115 */
116 if ($s_action=="new"){
117 $this->dn= "new";
118 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
119 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
120 }
123 /***************
124 Edit entry
125 ***************/
127 /* Edit Entry if Posted action (s_action) == edit
128 * The entry which will be edited is defined in $s_entry
129 */
130 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
131 $this->dn= $this->config->departments[trim($s_entry)];
133 if (($user= get_lock($this->dn)) != ""){
134 return(gen_locked_message ($user, $this->dn));
135 }
137 /* Lock the current entry, so everyone will get the above dialog */
138 add_lock ($this->dn, $this->ui->dn);
140 /* Register deptabs to trigger edit dialog */
141 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
142 $this->deptabs->set_acl_base($this->dn);
144 session::set('objectinfo',$this->dn);
145 }
148 /********************
149 Delete MULTIPLE entries requested, display confirm dialog
150 ********************/
152 if ($s_action=="del_multiple"){
153 $ids = $this->list_get_selected_items();
156 if(count($ids)){
157 $this->dns = array();
158 foreach($ids as $id){
159 $id = base64_decode($id);
160 $this->dns[$id] = $dn = $this->config->departments[$id];
161 }
163 /* Check locks */
164 if ($user= get_multiple_locks($this->dns)){
165 return(gen_locked_message($user,$this->dns));
166 }
168 $dns_names = array();
169 foreach($this->dns as $dn){
170 $dns_names[] = @LDAP::fix($dn);
171 }
172 add_lock ($this->dns, $this->ui->dn);
174 /* Lock the current entry, so nobody will edit it during deletion */
175 $smarty->assign("info", msgPool::deleteInfo($dns_names));
176 $smarty->assign("multiple", true);
177 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
178 }
179 }
182 /********************
183 Delete MULTIPLE entries confirmed
184 ********************/
186 /* Confirmation for deletion has been passed. Users should be deleted. */
187 if (isset($_POST['delete_multiple_department_confirm'])){
189 /* Remove user by user and check acls before removeing them */
190 foreach($this->dns as $key => $dn){
191 $acl = $this->ui->get_permissions($dn,"department/department");
192 if (preg_match('/d/', $acl)){
194 /* Delete request is permitted, perform LDAP action */
195 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
196 $this->deptabs->set_acl_base();
197 $this->deptabs->delete ();
198 $this->deptabs = NULL;
199 } else {
200 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
201 }
202 }
204 /* Remove lock file after successfull deletion */
205 $this->remove_lock();
206 $this->dns = array();
207 }
210 /********************
211 Delete MULTIPLE entries Canceled
212 ********************/
214 /* Remove lock */
215 if(isset($_POST['delete_multiple_department_cancel'])){
217 /* Remove lock file after successfull deletion */
218 $this->remove_lock();
219 $this->dns = array();
220 }
223 /***************
224 Delete entry
225 ***************/
227 /* Delete Entry if Posted action (s_action) == del
228 * The entry which will be deleted is defined in $s_entry
229 */
230 if ($s_action =="del"){
231 $this->dn= $this->config->departments[trim($s_entry)];
233 /* check acls */
234 $acl = $this->ui->get_permissions($this->dn,"department/department");
235 if(preg_match("/d/",$acl)){
237 /* Check locking */
238 if (($user= get_lock($this->dn)) != ""){
239 session::set('dn',$this->dn);
240 return(gen_locked_message($user, $this->dn));
241 } else {
242 add_lock ($this->dn, $this->ui->dn);
243 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
244 $smarty->assign("multiple", false);
245 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
246 return ($display);
247 }
248 }else{
249 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
250 }
251 }
254 /***************
255 Delete department confirmed
256 ***************/
258 /* If department deletion is accepted ...
259 * Finally delete department
260 */
261 if (isset($_POST['delete_department_confirm'])){
263 /* check acls */
264 $acl = $this->ui->get_permissions($this->dn,"department/department");
265 if(preg_match("/d/",$acl)){
266 $this->remove_from_parent();
267 } else {
268 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
269 }
270 }
273 /***************
274 Handle tagging/recursive move (Return output for an iframe)
275 ***************/
277 /* initiate recursive remove (Is called from iframe, generates output)*/
278 if(isset($_GET['PerformRecMove'])){
279 $this->deptabs->move_me();
280 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
281 exit();
282 }
284 /* This department must be tagged (Is called from iframe, generates output)*/
285 if(isset($_GET['TagDepartment'])){
286 $this->deptabs->by_object['department']->tag_objects();
287 exit();
288 }
291 /***************
292 Edit department finished
293 ***************/
295 if (is_object($this->deptabs) && // Ensure we have a valid deptab here
296 (isset($_POST['edit_finish']) || // If 'Save' button is pressed in the edit dialog.
297 isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
298 $this->deptabs->move_done())){ // The move(rename) is done, we have to save the rest now.
300 /* Check tabs, will feed message array.
301 This call will also initiate a sav_object() call.
302 So don't move it below the moved check !.
303 */
304 $message= $this->deptabs->check();
305 $obj = $this->deptabs->by_object['department'];
307 /*************
308 MOVED ?
309 Check if this department has to be moved
310 *************/
311 if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
312 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
313 }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
314 $smarty = get_smarty();
315 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove&no_output_compression");
316 $smarty->assign("message",_("As soon as the move operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog."));
317 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
318 }
320 /* Save, or display error message? */
321 if (count($message) == 0){
322 global $config;
324 $this->deptabs->save();
325 $config->get_departments();
326 $config->make_idepartments();
327 $this->config = $config;
329 /* This var indicated that there is an object which isn't saved right now. */
330 $this->ObjectInSaveMode = true;
332 /* This object must be tagged, so set ObjectTaggingRequested to true */
333 if($this->deptabs->by_object['department']->must_be_tagged()){
334 $smarty = get_smarty();
335 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
336 $smarty->assign("message",_("As soon as the tag operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog."));
337 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
338 }
340 } else {
341 /* Ok. There seem to be errors regarding to the tab data,
342 show message and continue as usual. */
343 msg_dialog::displayChecks($message);
344 }
345 }
348 /***************
349 In case of tagging/moving the object wasn't deleted, do it know
350 ***************/
352 /* If there is an unsaved object and all operations are done
353 remove locks & save object tab & unset current object */
354 if($this->ObjectInSaveMode){
355 $this->config->get_departments();
356 $this->ObjectInSaveMode = false;
357 if ($this->dn != "new"){
358 $this->remove_lock();
359 }
360 unset ($this->deptabs);
361 $this->deptabs= NULL;
362 session::un_set('objectinfo');
363 }
366 /***************
367 Dialog canceled
368 ***************/
370 /* User canceled edit oder delete
371 * Cancel dialog
372 */
373 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
374 $this->remove_lock();
375 $this->deptabs= NULL;
376 session::un_set('objectinfo');
377 }
379 /* Headpage or normal plugin screen? */
380 if ($this->deptabs !== NULL){
382 /* Show main page (tabs) */
383 $display= $this->deptabs->execute();
384 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
385 $display.= "<p style=\"text-align:right\">\n";
386 $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
387 $display.= " \n";
388 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
389 $display.= "</p>";
390 }
391 return ($display);
392 }else{
393 /* Display dialog with group list */
394 $this->DivListDepartment->parent = $this;
395 $this->DivListDepartment->execute();
396 $this->reload();
397 $this->DivListDepartment->DepartmentsAdded = true;
398 $this->DivListDepartment->setEntries($this->departments);
399 return($this->DivListDepartment->Draw());
400 }
401 }
404 function reload()
405 {
406 /* Vairaible init */
407 $base = $this->DivListDepartment->selectedBase;
408 $base_back = preg_replace("/^[^,]+,/","",$base);
409 $Regex = $this->DivListDepartment->Regex;
411 // Create Array to Test if we have a valid back button
412 $config = session::get('config');
413 $tmp = $config->idepartments;
415 // In case of a valid back button create entry
416 if(isset($tmp[$base_back])){
417 $tmp2 ['dn'] = convert_department_dn($base_back);
419 // If empty always go to top
420 if(empty($tmp2['dn'])){
421 $tmp2['dn']="/";
422 }
423 $tmp2 ['description'][0] = ".. ".msgPool::backButton();
424 $result[$tmp[$base_back]]=$tmp2;
425 }
427 if($this->DivListDepartment->SubSearch){
428 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
429 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
430 }else{
431 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
432 "department", $base, array("ou", "description"), GL_SIZELIMIT );
433 }
435 $this->departments= array();
437 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
438 # if(preg_match("/^ou=/",$base)){
439 $this->departments [ convert_department_dn($base) ] = ".";
440 # }
442 foreach ($res as $key => $value){
444 /* Don't display base as entry on subsearch */
445 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
446 continue;
447 }
449 $cdn= convert_department_dn($value['dn']);
451 /* Append to dep list */
452 if(isset($value["description"][0])){
453 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
454 }else{
455 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
456 }
457 }
459 natcasesort ($this->departments);
460 reset ($this->departments);
461 }
463 function remove_from_parent()
464 {
465 $ldap= $this->config->get_ldap_link();
466 $ldap->cd ($this->dn);
467 $ldap->recursive_remove();
469 /* Optionally execute a command after we're done */
470 $this->postremove();
472 /* Delete references to object groups */
473 $ldap->cd ($this->config->current['BASE']);
474 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
475 while ($ldap->fetch()){
476 $og= new ogroup($this->config, $ldap->getDN());
477 unset($og->member[$this->dn]);
478 $og->save ();
479 }
481 }
484 function list_get_selected_items()
485 {
486 $ids = array();
487 foreach($_POST as $name => $value){
488 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
489 $id = preg_replace("/^item_selected_/","",$name);
490 $ids[$id] = $id;
491 }
492 }
493 return($ids);
494 }
497 function remove_lock()
498 {
499 if (isset($this->dn)){
500 del_lock ($this->dn);
501 }
502 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
503 del_lock($this->dn);
504 }
505 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
506 del_lock($this->dns);
507 }
508 }
510 function save_object()
511 {
512 /* reload department */
513 $this->config->get_departments();
515 $this->config->make_idepartments();
516 $this->DivListDepartment->config= $this->config;
517 $this->DivListDepartment->save_object();
518 }
520 }
521 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
522 ?>