Code

5c715a2c482d0a3550f7ae5d2cd1cb2e12db1c13
[gosa.git] / plugins / admin / departments / class_departmentManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (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 General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require "tabs_department.inc";
22 class departmentManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline= "Departments";
26   var $plDescription= "This does something";
28   /* Headpage attributes */
29   var $last_dep_sorting= "invalid";
30   var $departments= array();
31   var $deptabs= NULL;
33   /* attribute list for save action */
34   var $attributes= array();
35   var $objectclasses= array();
37   /* Vars to handle operations after saving the department 
38      Recursive move && tagging   */
39   var $ObjectInSaveMode         = false;    // Is true, if current object wasn't saved right now
40   var $ObjectTaggingRequested   = false;    // Object must be tagged, an iframe will be shown. 
41   var $RecursiveRemoveRequested = false;    // Is true, if this object must be moved, an iframe will be displayed in this case
43   function departmentManagement ($config, $ui)
44   {
45     $this->ui= $ui;
46     $this->dn= "";
47     $this->config= $config;
48     $this->DivListDepartment = new divListDepartment($this->config,$this);
49   }
51   function execute()
52   {
53     global $config;
54   
55     /* Call parent execute */
56     plugin::execute();
58     /***************
59       Var init 
60      ***************/
62     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
64     /* Reload departments */
65     $smarty                                             = get_smarty();
66     $display                                    = "";
67     $s_action                                   = "";  // Will contain an action, like del or edit
68     $s_entry                                    = "";  // The entry name for edit delete -...
71     /***************
72       Check posts  
73      ***************/
75     // Check Post action
76     foreach($_POST as $key => $val){
77       // Post for delete
78       if(preg_match("/dep_del.*/",$key)){
79         $s_action = "del";
80         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
81         $s_entry  = preg_replace("/_.*$/","",$s_entry);
82         $s_entry  = base64_decode($s_entry);
83         // Post for edit
84       }elseif(preg_match("/dep_edit_.*/",$key)){
85         $s_action="edit";
86         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
87         $s_entry  = preg_replace("/_.*$/","",$s_entry);
88         $s_entry  = base64_decode($s_entry);
89         // Post for new
90       }elseif(preg_match("/dep_new.*/",$key)){
91         $s_action="new";
92       }
94     }
97     /***************
98       Create a new department
99      ***************/
101     /* New Entry if Posted action (s_action) == new
102      */
103     if ($s_action=="new"){
104       $this->dn= "new";
105       $this->deptabs= new deptabs($this->config,
106           $this->config->data['TABS']['DEPTABS'], $this->dn);
107     }
110     /***************
111       Edit entry
112      ***************/
114     /* Edit Entry if Posted action (s_action) == edit 
115      * The entry which will be edited is defined in $s_entry
116      */
117     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
118       $this->dn= $this->config->departments[trim($s_entry)];
120       if (($user= get_lock($this->dn)) != ""){
121         return(gen_locked_message ($user, $this->dn));
122       }
124       /* Lock the current entry, so everyone will get the  above dialog */
125       add_lock ($this->dn, $this->ui->dn);
127       /* Set up the users ACL's for this 'dn' */
128       $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
130       /* Register deptabs to trigger edit dialog */
131       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
132       $this->deptabs->set_acl($this->acl);
133       $_SESSION['objectinfo']= $this->dn;
134     }
137     /***************
138       Delete entry 
139      ***************/
141     /* Delete Entry if Posted action (s_action) == del 
142      * The entry which will be deleted is defined in $s_entry
143      */
144     if ($s_action =="del"){
145       $this->dn= $this->config->departments[trim($s_entry)];
147       /* Check locking */
148       if (($user= get_lock($this->dn)) != ""){
149         $_SESSION['dn']= $this->dn;
150         return(gen_locked_message($user, $this->dn));
151       } else {
152         add_lock ($this->dn, $this->ui->dn);
153         $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
154         $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
155         return ($display);
156       }
157     }
160     /***************
161       Delete department confirmed  
162      ***************/
164     /* If department deletion is accepted ...
165      * Finally delete department 
166      */
167     if (isset($_POST['delete_department_confirm'])){
168       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
169       $acl= get_module_permission($acl, "department", $this->dn);
170       if (chkacl($acl, "delete") == ""){
171         $this->remove_from_parent();
172         gosa_log ("Department object'".$this->dn."' has been removed");
173       } else {
174         print_red (_("You have no permission to remove this department."));
175       }
176     }
179     /***************
180       Edit department finished 
181      ***************/
183     if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config))){
185       /* Check tabs, will feed message array */
186       $message= $this->deptabs->check();
188       /* Save, or display error message? */
189       if (count($message) == 0){
191         $this->deptabs->save(true);
193         /* This object must be tagged, so set ObjectTaggingRequested to true */
194         if($this->deptabs->by_object['department']->must_be_tagged()){
195           $this->ObjectTaggingRequested   = true; 
196         }               
198         /* Get recursive move is required, set RecursiveRemoveRequested to true */
199         if($this->deptabs->by_object['department']->am_i_moved()){      
200           $this->RecursiveRemoveRequested = true;
201         }               
203         /* This var indicated that there is an object which isn't saved right now. */
204         $this->ObjectInSaveMode = true;
205       
206         $this->config->get_departments($this->dn);
207         $this->config->make_idepartments();
209         $this->DivListDepartment->config=$this->config;
211       } else {
212         /* Ok. There seem to be errors regarding to the tab data,
213            show message and continue as usual. */
214         show_errors($message);
215       }
216     }
219     /***************
220       Handle Tagging (Return output for an iframe)
221      ***************/
223     /* This department must be tagged (Is called from iframe, generates output)*/
224     if(isset($_GET['TagDepartment'])){
225       $this->deptabs->by_object['department']->tag_objects();
226       exit();  
227     }
230     /***************
231       Handle recursive move (Return output for an iframe)
232      ***************/
234     /* initiate recursive remove  (Is called from iframe, generates output)*/
235     if(isset($_GET['PerformRecMove'])){
236       $this->deptabs->by_object['department']->recursive_move("","",true);
237       exit();
238     }
241     /***************
242       Return iframes, which call tagging / recusrsive move 
243      ***************/
245     /* While one of these vars below isset, we must return an iframe, 
246        to perform requested operation */
247     if($this->ObjectTaggingRequested){
248       $this->ObjectTaggingRequested = false;
249       return($this->deptabs->by_object['department']->ShowTagFrame());
250     }
251     if($this->RecursiveRemoveRequested){
252       $this->RecursiveRemoveRequested = false;
253       return($this->deptabs->by_object['department']->ShowMoveFrame());
254     }
257     /***************
258       In case of tagging/moving the object wasn't deleted, do it know
259      ***************/
261     /* If there is an unsaved object and all operations are done
262        remove locks & save object tab & unset current object */
263     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
264       $this->deptabs->save();
265       $this->ObjectInSaveMode = false;
266       if ($this->dn != "new"){
267         del_lock ($this->dn);
268       }
269       gosa_log ("Department object'".$this->dn."' has been saved");
270       unset ($this->deptabs);
271       $this->deptabs= NULL;
272       unset ($_SESSION['objectinfo']);
273     }
276     /***************
277       Dialog canceled  
278      ***************/
280     /* User canceled edit oder delete
281      * Cancel dialog 
282      */
283     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
284       del_lock ($this->dn);
285       unset($this->depdabs);
286       $this->deptabs= NULL;
287       unset ($_SESSION['objectinfo']);
288     }
290     /* Headpage or normal plugin screen? */
291     if ($this->deptabs != NULL){
293       /* Show main page (tabs) */
294       $display= $this->deptabs->execute();
295       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
296         $display.= "<p style=\"text-align:right\">\n";
297         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
298         $display.= "&nbsp;\n";
299         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
300         $display.= "</p>";
301       }
302       return ($display);
303     }else{
304       /* Display dialog with group list */
305       $this->DivListDepartment->parent = $this;
306       $this->DivListDepartment->execute();
307       $this->reload();
308       $this->DivListDepartment->DepartmentsAdded = true;
309       $this->DivListDepartment->setEntries($this->departments);
310       return($this->DivListDepartment->Draw());
311     }
312   }
315   function reload()
316   {
317     /* Vairaible init */
318     $base         = $this->DivListDepartment->selectedBase;
319     $base_back    = preg_replace("/^[^,]+,/","",$base);
320     $Regex        = $this->DivListDepartment->Regex;
321     $this->departments= array();
322   
323     if($this->DivListDepartment->SubSearch){
324       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
325           $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
326     }else{
327       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
328           $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT );
329     }
331     foreach ($res as $key => $value){
333       /* Don't display base as entry on subsearch */
334       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
335         continue;
336       }
338       $cdn= convert_department_dn($value['dn']);
340       /* Append to dep list */
341       if(isset($value["description"][0])){
342         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
343       }else{
344         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
345       }
346     }
347     natcasesort ($this->departments);
348     reset ($this->departments);
349   }
351   function remove_from_parent()
352   {
353     $ldap= $this->config->get_ldap_link();
354     $ldap->cd ($this->dn);
355     $ldap->recursive_remove();
357     /* Optionally execute a command after we're done */
358     $this->postremove();
360     /* Delete references to object groups */
361     $ldap->cd ($this->config->current['BASE']);
362     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
363     while ($ldap->fetch()){
364       $og= new ogroup($this->config, $ldap->getDN());
365       unset($og->member[$this->dn]);
366       $og->save ();
367     }
369   }
371   function remove_lock()
372   {
373     if (isset($this->dn)){
374       del_lock ($this->dn);
375     }
376   }
378   function save_object()
379   {
380     /* reload department */
381     $this->config->get_departments();
382     $config = $this->config;
384     $this->config->make_idepartments();
385     $this->DivListDepartment->save_object();
386   }
387   
389 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
390 ?>