Code

Removed debug output
[gosa.git] / gosa-core / plugins / admin / departments / class_departmentManagement.inc
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 $ObjectTaggingRequested   = false;    // Object must be tagged, an iframe will be shown. 
42   var $RecursiveRemoveRequested = false;    // Is true, if this object must be moved, an iframe will be displayed in this case
44   function departmentManagement (&$config, &$ui)
45   {
46     $this->ui= &$ui;
47     $this->dn= "";
48     $this->config= &$config;
49     $this->DivListDepartment = new divListDepartment($this->config,$this);
50   }
52   function execute()
53   {
54     global $config;
55   
56     /* Call parent execute */
57     plugin::execute();
59     /***************
60       Var init 
61      ***************/
63     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/"));
65     /* Reload departments */
66     $smarty                                             = get_smarty();
67     $display                                    = "";
68     $s_action                                   = "";  // Will contain an action, like del or edit
69     $s_entry                                    = "";  // The entry name for edit delete -...
72     /***************
73       Check posts  
74      ***************/
76     // Check Post action
77     foreach($_POST as $key => $val){
78       // Post for delete
79       if(preg_match("/dep_del.*/",$key)){
80         $s_action = "del";
81         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
82         $s_entry  = preg_replace("/_.*$/","",$s_entry);
83         $s_entry  = base64_decode($s_entry);
84         // Post for edit
85       }elseif(preg_match("/dep_edit_.*/",$key)){
86         $s_action="edit";
87         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
88         $s_entry  = preg_replace("/_.*$/","",$s_entry);
89         $s_entry  = base64_decode($s_entry);
90         // Post for new
91       }elseif(preg_match("/^remove_multiple_departments/",$key)){
92         $s_action="del_multiple";
93       }elseif(preg_match("/dep_new.*/",$key)){
94         $s_action="new";
95       }
96     }
98     /* Create options */
99     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
100       $s_action = "new";
101     }
103     /* handle remove from layers menu */
104     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
105       $s_action = "del_multiple";
106     }
109     /***************
110       Create a new department
111      ***************/
113     /* New Entry if Posted action (s_action) == new
114      */
115     if ($s_action=="new"){
116       $this->dn= "new";
117       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
118       $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
119     }
122     /***************
123       Edit entry
124      ***************/
126     /* Edit Entry if Posted action (s_action) == edit 
127      * The entry which will be edited is defined in $s_entry
128      */
129     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
130       $this->dn= $this->config->departments[trim($s_entry)];
132       if (($user= get_lock($this->dn)) != ""){
133         return(gen_locked_message ($user, $this->dn));
134       }
136       /* Lock the current entry, so everyone will get the  above dialog */
137       add_lock ($this->dn, $this->ui->dn);
139       /* Register deptabs to trigger edit dialog */
140       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
141       $this->deptabs->set_acl_base($this->dn);
143       session::set('objectinfo',$this->dn);
144     }
147     /********************
148       Delete MULTIPLE entries requested, display confirm dialog
149      ********************/
151     if ($s_action=="del_multiple"){
152       $ids = $this->list_get_selected_items();
155       if(count($ids)){
156         foreach($ids as $id){
157           $id = base64_decode($id);
158           $dn = $this->config->departments[$id];
159   
160           if (($user= get_lock($dn)) != ""){
161             return(gen_locked_message ($user, $dn));
162           }
163           $this->dns[$id] = $dn;
164         }
166         $dns_names = "<br><pre>";
167         foreach($this->dns as $dn){
168           add_lock ($dn, $this->ui->dn);
169           $dns_names .= $dn."\n";
170         }
171         $dns_names .="</pre>";
173         /* Lock the current entry, so nobody will edit it during deletion */
174         if (count($this->dns) == 1){
175           $smarty->assign("info",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
176         } else {
177           $smarty->assign("info",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
178         }
179         $smarty->assign("multiple", true);
180         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
181       }
182     }
183    
185     /********************
186       Delete MULTIPLE entries confirmed
187      ********************/
189     /* Confirmation for deletion has been passed. Users should be deleted. */
190     if (isset($_POST['delete_multiple_department_confirm'])){
192       /* Remove user by user and check acls before removeing them */
193       foreach($this->dns as $key => $dn){
194         $acl = $this->ui->get_permissions($dn,"department/department");
195         if (preg_match('/d/', $acl)){
197           /* Delete request is permitted, perform LDAP action */
198           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
199           $this->deptabs->set_acl_base();
200           $this->deptabs->delete ();
201           $this->deptabs = NULL;
202         } else {
203           msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
204         }
205         /* Remove lock file after successfull deletion */
206         del_lock ($dn);
207         unset($this->dns[$key]);
208       }
209     }
211  
212     /********************
213       Delete MULTIPLE entries Canceled
214      ********************/
216     /* Remove lock */
217     if(isset($_POST['delete_multiple_department_cancel'])){
218       foreach($this->dns as $key => $dn){
219         del_lock ($dn);
220         unset($this->dns[$key]);
221       }
222     }
225     /***************
226       Delete entry 
227      ***************/
229     /* Delete Entry if Posted action (s_action) == del 
230      * The entry which will be deleted is defined in $s_entry
231      */
232     if ($s_action =="del"){
233       $this->dn= $this->config->departments[trim($s_entry)];
235       /* check acls */
236       $acl = $this->ui->get_permissions($this->dn,"department/department");
237       if(preg_match("/d/",$acl)){
239         /* Check locking */
240         if (($user= get_lock($this->dn)) != ""){
241           session::set('dn',$this->dn);
242           return(gen_locked_message($user, $this->dn));
243         } else {
244           add_lock ($this->dn, $this->ui->dn);
245           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
246           $smarty->assign("multiple", false);
247           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
248           return ($display);
249         }
250       }else{
251         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
252       }
253     }
256     /***************
257       Delete department confirmed  
258      ***************/
260     /* If department deletion is accepted ...
261      * Finally delete department 
262      */
263     if (isset($_POST['delete_department_confirm'])){
265       /* check acls */
266       $acl = $this->ui->get_permissions($this->dn,"department/department");
267       if(preg_match("/d/",$acl)){
268         $this->remove_from_parent();
269       } else {
270         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
271       }
272     }
275     /***************
276       Edit department finished 
277      ***************/
279     if ((isset($_POST['edit_finish']) || isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
281       /* Check tabs, will feed message array.
282          This call will also initiate a sav_object() call.
283           So don't move it below the moved check !.
284        */
285       $message= $this->deptabs->check();
286       $obj = $this->deptabs->by_object['department'];
288       /*************
289         MOVED ? 
290          Check if this department is moved 
291        *************/  
292       if(!isset($_POST['dep_move_confirm']) && $obj->orig_dn != "new"){
293         if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
294           return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
295         }
296       }
298       /* Save, or display error message? */
299       if (count($message) == 0){
300         global $config;
302         $this->deptabs->save();
303         $config->get_departments();
304         $config->make_idepartments();
305         $this->config = $config;
307         /* This object must be tagged, so set ObjectTaggingRequested to true */
308         if($this->deptabs->by_object['department']->must_be_tagged()){
309           $this->ObjectTaggingRequested   = true; 
310         }               
312         /* Get recursive move is required, set RecursiveRemoveRequested to true */
313         if($this->deptabs->by_object['department']->am_i_moved()){      
314           $this->RecursiveRemoveRequested = true;
315         }               
316       
317         /* This var indicated that there is an object which isn't saved right now. */
318         $this->ObjectInSaveMode = true;
320       } else {
321         /* Ok. There seem to be errors regarding to the tab data,
322            show message and continue as usual. */
323         msg_dialog::displayChecks($message);
324       }
325     }
328     /***************
329       Handle Tagging (Return output for an iframe)
330      ***************/
332     /* This department must be tagged (Is called from iframe, generates output)*/
333     if(isset($_GET['TagDepartment'])){
334       $this->deptabs->by_object['department']->tag_objects();
335       exit();  
336     }
339     /***************
340       Handle recursive move (Return output for an iframe)
341      ***************/
343     /* initiate recursive remove  (Is called from iframe, generates output)*/
344     if(isset($_GET['PerformRecMove'])){
345       $this->deptabs->by_object['department']->recursive_move("","",true);
346       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
347       exit();
348     }
351     /***************
352       Return iframes, which call tagging / recusrsive move 
353      ***************/
355     /* While one of these vars below isset, we must return an iframe, 
356        to perform requested operation */
357     if($this->ObjectTaggingRequested){
358       $this->ObjectTaggingRequested = false;
359       return($this->deptabs->by_object['department']->ShowTagFrame());
360     }
361     if($this->RecursiveRemoveRequested){
362       $this->RecursiveRemoveRequested = false;
363       return($this->deptabs->by_object['department']->ShowMoveFrame());
364     }
367    /***************
368      In case of tagging/moving the object wasn't deleted, do it know
369     ***************/
371    /* If there is an unsaved object and all operations are done
372       remove locks & save object tab & unset current object */
373    if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
374      $this->config->get_departments();
375      $this->ObjectInSaveMode = false;
376      if ($this->dn != "new"){
377        del_lock ($this->dn);
378      }
379      unset ($this->deptabs);
380      $this->deptabs= NULL;
381      session::un_set('objectinfo');
382    }
385    /***************
386       Dialog canceled  
387      ***************/
389     /* User canceled edit oder delete
390      * Cancel dialog 
391      */
392     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
393       del_lock ($this->dn);
394       unset($this->depdabs);
395       $this->deptabs= NULL;
396       session::un_set('objectinfo');
397     }
399     /* Headpage or normal plugin screen? */
400     if ($this->deptabs !== NULL){
402       /* Show main page (tabs) */
403       $display= $this->deptabs->execute();
404       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
405         $display.= "<p style=\"text-align:right\">\n";
406         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
407         $display.= "&nbsp;\n";
408         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
409         $display.= "</p>";
410       }
411       return ($display);
412     }else{
413       /* Display dialog with group list */
414       $this->DivListDepartment->parent = $this;
415       $this->DivListDepartment->execute();
416       $this->reload();
417       $this->DivListDepartment->DepartmentsAdded = true;
418       $this->DivListDepartment->setEntries($this->departments);
419       return($this->DivListDepartment->Draw());
420     }
421   }
424   function reload()
425   {
426     /* Vairaible init */
427     $base         = $this->DivListDepartment->selectedBase;
428     $base_back    = preg_replace("/^[^,]+,/","",$base);
429     $Regex        = $this->DivListDepartment->Regex;
430   
431     // Create Array to Test if we have a valid back button
432     $config = session::get('config');
433     $tmp = $config->idepartments;
435     // In case of a valid back button create entry
436     if(isset($tmp[$base_back])){
437       $tmp2    ['dn']          = convert_department_dn($base_back);
439       // If empty always go to top
440       if(empty($tmp2['dn'])){
441         $tmp2['dn']="/";
442       }
443       $tmp2    ['description'][0] = ".. "._("Back");
444       $result[$tmp[$base_back]]=$tmp2;
445     }
447     if($this->DivListDepartment->SubSearch){
448       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
449           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
450     }else{
451       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
452           "department", $base, array("ou", "description"), GL_SIZELIMIT );
453     }
455     $this->departments= array();
457     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
458     if(preg_match("/^ou=/",$base)){
459       $this->departments [ convert_department_dn($base) ] = ".";
460     }
462     foreach ($res as $key => $value){
464       /* Don't display base as entry on subsearch */
465       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
466         continue;
467       }
469       $cdn= convert_department_dn($value['dn']);
471       /* Append to dep list */
472       if(isset($value["description"][0])){
473         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
474       }else{
475         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
476       }
477     }
478     natcasesort ($this->departments);
479     reset ($this->departments);
480   }
482   function remove_from_parent()
483   {
484     $ldap= $this->config->get_ldap_link();
485     $ldap->cd ($this->dn);
486     $ldap->recursive_remove();
488     /* Optionally execute a command after we're done */
489     $this->postremove();
491     /* Delete references to object groups */
492     $ldap->cd ($this->config->current['BASE']);
493     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
494     while ($ldap->fetch()){
495       $og= new ogroup($this->config, $ldap->getDN());
496       unset($og->member[$this->dn]);
497       $og->save ();
498     }
500   }
503   function list_get_selected_items()
504   {
505     $ids = array();
506     foreach($_POST as $name => $value){
507       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
508         $id   = preg_replace("/^item_selected_/","",$name);
509         $ids[$id] = $id;
510       }
511     }
512     return($ids);
513   }
516   function remove_lock()
517   {
518     if (isset($this->dn)){
519       del_lock ($this->dn);
520     }
521   }
523   function save_object()
524   {
525     /* reload department */
526     $this->config->get_departments();
528     $this->config->make_idepartments();
529     $this->DivListDepartment->config= $this->config;
530     $this->DivListDepartment->save_object();
531   }
534 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
535 ?>