Code

50c2160ebd44655731edf29c0f1bcd467c29effe
[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   /* CLI vars */
29   var $cli_summary= "Handling of LDAP subtrees";
30   var $cli_description= "Some longer text\nfor help";
31   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
33   /* Headpage attributes */
34   var $last_dep_sorting= "invalid";
35   var $departments= array();
36   var $deptabs= NULL;
38   /* attribute list for save action */
39   var $attributes= array();
40   var $objectclasses= array();
42   /* Vars to handle operations after saving the department 
43      Recursive move && tagging   */
44   var $ObjectInSaveMode         = false;    // Is true, if current object wasn't saved right now
45   var $ObjectTaggingRequested   = false;    // Object must be tagged, an iframe will be shown. 
46   var $RecursiveRemoveRequested = false;    // Is true, if this object must be moved, an iframe will be displayed in this case
48   function departmentManagement ($config, $ui)
49   {
50     $this->ui= $ui;
51     $this->dn= "";
52     $this->config= $config;
53     $this->DivListDepartment = new divListDepartment($this->config,$this);
54   }
56   function execute()
57   {
58     global $config;
59   
60     /* Call parent execute */
61     plugin::execute();
63     /***************
64       Var init 
65      ***************/
67     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
69     /* Reload departments */
70     $smarty                                             = get_smarty();
71     $display                                    = "";
72     $s_action                                   = "";  // Will contain an action, like del or edit
73     $s_entry                                    = "";  // The entry name for edit delete -...
76     /***************
77       Check posts  
78      ***************/
80     // Check Post action
81     foreach($_POST as $key => $val){
82       // Post for delete
83       if(preg_match("/dep_del.*/",$key)){
84         $s_action = "del";
85         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
86         $s_entry  = preg_replace("/_.*$/","",$s_entry);
87         $s_entry  = base64_decode($s_entry);
88         // Post for edit
89       }elseif(preg_match("/dep_edit_.*/",$key)){
90         $s_action="edit";
91         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
92         $s_entry  = preg_replace("/_.*$/","",$s_entry);
93         $s_entry  = base64_decode($s_entry);
94         // Post for new
95       }elseif(preg_match("/dep_new.*/",$key)){
96         $s_action="new";
97       }
99     }
102     /***************
103       Create a new department
104      ***************/
106     /* New Entry if Posted action (s_action) == new
107      */
108     if ($s_action=="new"){
109       $this->dn= "new";
110       $this->deptabs= new deptabs($this->config,
111           $this->config->data['TABS']['DEPTABS'], $this->dn);
112     }
115     /***************
116       Edit entry
117      ***************/
119     /* Edit Entry if Posted action (s_action) == edit 
120      * The entry which will be edited is defined in $s_entry
121      */
122     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
123       $s_entry = convert_department_dn(base64_decode($s_entry));
124       $this->dn= $this->config->departments[trim($s_entry)];
126       if (($user= get_lock($this->dn)) != ""){
127         return(gen_locked_message ($user, $this->dn));
128       }
130       /* Lock the current entry, so everyone will get the  above dialog */
131       add_lock ($this->dn, $this->ui->dn);
133       /* Set up the users ACL's for this 'dn' */
134       $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
136       /* Register deptabs to trigger edit dialog */
137       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
138       $this->deptabs->set_acl($this->acl);
139       $_SESSION['objectinfo']= $this->dn;
140     }
143     /***************
144       Delete entry 
145      ***************/
147     /* Delete Entry if Posted action (s_action) == del 
148      * The entry which will be deleted is defined in $s_entry
149      */
150     if ($s_action =="del"){
151       $s_entry = convert_department_dn(base64_decode($s_entry));
152       $this->dn= $this->config->departments[trim($s_entry)];
154       /* Check locking */
155       if (($user= get_lock($this->dn)) != ""){
156         $_SESSION['dn']= $this->dn;
157         return(gen_locked_message($user, $this->dn));
158       } else {
159         add_lock ($this->dn, $this->ui->dn);
160         $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), LDAP::fix($this->dn)));
161         $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
162         return ($display);
163       }
164     }
167     /***************
168       Delete department confirmed  
169      ***************/
171     /* If department deletion is accepted ...
172      * Finally delete department 
173      */
174     if (isset($_POST['delete_department_confirm'])){
175       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
176       $acl= get_module_permission($acl, "department", $this->dn);
177       if (chkacl($acl, "delete") == ""){
178         $this->remove_from_parent();
179         gosa_log ("Department object'".$this->dn."' has been removed");
180       } else {
181         print_red (_("You have no permission to remove this department."));
182       }
183     }
186     /***************
187       Edit department finished 
188      ***************/
190     if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config))){
192       /* Check tabs, will feed message array */
193       $message= $this->deptabs->check();
195       /* Save, or display error message? */
196       if (count($message) == 0){
198         $this->deptabs->save(true);
200         if(!$this->deptabs->by_object['department']->am_i_moved()){
201           $this->deptabs->save(true);
202         }
204         /* This object must be tagged, so set ObjectTaggingRequested to true */
205         if($this->deptabs->by_object['department']->must_be_tagged()){
206           $this->ObjectTaggingRequested   = true; 
207         }               
209         /* Get recursive move is required, set RecursiveRemoveRequested to true */
210         if($this->deptabs->by_object['department']->am_i_moved()){      
211           $this->RecursiveRemoveRequested = true;
212         }               
214         /* This var indicated that there is an object which isn't saved right now. */
215         $this->ObjectInSaveMode = true;
217       } else {
218         /* Ok. There seem to be errors regarding to the tab data,
219            show message and continue as usual. */
220         show_errors($message);
221       }
222     }
225     /***************
226       Handle Tagging (Return output for an iframe)
227      ***************/
229     /* This department must be tagged (Is called from iframe, generates output)*/
230     if(isset($_GET['TagDepartment'])){
231       $this->deptabs->by_object['department']->tag_objects();
232       exit();  
233     }
236     /***************
237       Handle recursive move (Return output for an iframe)
238      ***************/
240     /* initiate recursive remove  (Is called from iframe, generates output)*/
241     if(isset($_GET['PerformRecMove'])){
242       $this->deptabs->by_object['department']->recursive_move("","",true);
243       exit();
244     }
247     /***************
248       Return iframes, which call tagging / recusrsive move 
249      ***************/
251     /* While one of these vars below isset, we must return an iframe, 
252        to perform requested operation */
253     if($this->ObjectTaggingRequested){
254       $this->ObjectTaggingRequested = false;
255       return($this->deptabs->by_object['department']->ShowTagFrame());
256     }
257     if($this->RecursiveRemoveRequested){
258       $this->RecursiveRemoveRequested = false;
259       return($this->deptabs->by_object['department']->ShowMoveFrame());
260     }
263     /***************
264       In case of tagging/moving the object wasn't deleted, do it know
265      ***************/
267     /* If there is an unsaved object and all operations are done
268        remove locks & save object tab & unset current object */
269     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
270       $this->deptabs->save();
271       $this->ObjectInSaveMode = false;
272       if ($this->dn != "new"){
273         del_lock ($this->dn);
274       }
275       gosa_log ("Department object'".$this->dn."' has been saved");
276       unset ($this->deptabs);
277       $this->deptabs= NULL;
278       unset ($_SESSION['objectinfo']);
279     }
282     /***************
283       Dialog canceled  
284      ***************/
286     /* User canceled edit oder delete
287      * Cancel dialog 
288      */
289     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
290       del_lock ($this->dn);
291       unset($this->depdabs);
292       $this->deptabs= NULL;
293       unset ($_SESSION['objectinfo']);
294     }
296     /* Headpage or normal plugin screen? */
297     if ($this->deptabs != NULL){
299       /* Show main page (tabs) */
300       $display= $this->deptabs->execute();
301       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
302         $display.= "<p style=\"text-align:right\">\n";
303         $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
304         $display.= "&nbsp;\n";
305         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
306         $display.= "</p>";
307       }
308       return ($display);
309     }else{
311       /* Check if there is a snapshot dialog open */
312       if($str = $this->showSnapshotDialog($this->DivListDepartment->selectedBase,$this->DivListDepartment->selectedBase)){
313         return($str);
314       }
316       /* Display dialog with group list */
317       $this->DivListDepartment->parent = $this;
318       $this->DivListDepartment->execute();
319       $this->reload();
320       $this->DivListDepartment->DepartmentsAdded = true;
321       $this->DivListDepartment->setEntries($this->departments);
322       return($this->DivListDepartment->Draw());
323     }
324   }
327   function reload()
328   {
329     /* Vairaible init */
330     $base         = $this->DivListDepartment->selectedBase;
331     $base_back    = preg_replace("/^[^,]+,/","",$base);
332     $Regex        = $this->DivListDepartment->Regex;
333   
334     // Create Array to Test if we have a valid back button
335     $tmp = $_SESSION['config']->idepartments;
337     // In case of a valid back button create entry
338     if(isset($tmp[$base_back])){
339       $tmp2    ['dn']          = convert_department_dn($base_back);
341       // If empty always go to top
342       if(empty($tmp2['dn'])){
343         $tmp2['dn']="/";
344       }
345       $tmp2    ['description'][0] = _("..");
346       $result[$tmp[$base_back]]=$tmp2;
347     }
349     if($this->DivListDepartment->SubSearch){
350       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
351           $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
352     }else{
353       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
354           $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT );
355     }
357     $this->departments= array();
358     $tmp = array();
359     foreach ($res as $key => $value){
360       #$tmp[get_sub_department(convert_department_dn($value['dn']))] = $key;
361       $tmp[$key] = get_sub_department(convert_department_dn($value['dn']));
362     }
363     natcasesort($tmp);
364     foreach($tmp as $key => $q){
365       $value = $res [$key];
366       /* Don't display base as entry on subsearch */
367       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
368         continue;
369       }
371       $this->departments[base64_encode($value['dn'])]= $value;
372     }
373   }
375   function remove_from_parent()
376   {
377     $ldap= $this->config->get_ldap_link();
378     $ldap->cd ($this->dn);
379     $ldap->recursive_remove();
381     /* Optionally execute a command after we're done */
382     $this->postremove();
384     /* Delete references to object groups */
385     $ldap->cd ($this->config->current['BASE']);
386     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
387     while ($ldap->fetch()){
388       $og= new ogroup($this->config, $ldap->getDN());
389       unset($og->member[$this->dn]);
390       $og->save ();
391     }
393   }
395   function remove_lock()
396   {
397     if (isset($this->dn)){
398       del_lock ($this->dn);
399     }
400   }
402   function save_object()
403   {
404     /* reload department */
405     $this->config->get_departments();
406     $config = $this->config;
408     $this->config->make_idepartments();
409     $this->DivListDepartment->save_object();
410   }
411   
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>