Code

Added FAIstate update
[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,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
111       $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
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       $this->dn= $this->config->departments[trim($s_entry)];
125       if (($user= get_lock($this->dn)) != ""){
126         return(gen_locked_message ($user, $this->dn));
127       }
129       /* Lock the current entry, so everyone will get the  above dialog */
130       add_lock ($this->dn, $this->ui->dn);
132       /* Register deptabs to trigger edit dialog */
133       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
134       $this->deptabs->set_acl_base($this->dn);
136       $_SESSION['objectinfo']= $this->dn;
137     }
140     /***************
141       Delete entry 
142      ***************/
144     /* Delete Entry if Posted action (s_action) == del 
145      * The entry which will be deleted is defined in $s_entry
146      */
147     if ($s_action =="del"){
148       $this->dn= $this->config->departments[trim($s_entry)];
150       /* Check locking */
151       if (($user= get_lock($this->dn)) != ""){
152         $_SESSION['dn']= $this->dn;
153         return(gen_locked_message($user, $this->dn));
154       } else {
155         add_lock ($this->dn, $this->ui->dn);
156         $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
157         $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
158         return ($display);
159       }
160     }
163     /***************
164       Delete department confirmed  
165      ***************/
167     /* If department deletion is accepted ...
168      * Finally delete department 
169      */
170     if (isset($_POST['delete_department_confirm'])){
171       if($this->acl_is_removeable()){
172         $this->remove_from_parent();
173         gosa_log ("Department object'".$this->dn."' has been removed");
174       } else {
175         print_red (_("You have no permission to remove this department."));
176       }
177     }
180     /***************
181       Edit department finished 
182      ***************/
184     if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config))){
186       /* Check tabs, will feed message array */
187       $message= $this->deptabs->check();
189       /* Save, or display error message? */
190       if (count($message) == 0){
192         $this->deptabs->save(true);
193         $this->deptabs->by_object['department']->save();     
195         /* This object must be tagged, so set ObjectTaggingRequested to true */
196         if($this->deptabs->by_object['department']->must_be_tagged()){
197           $this->ObjectTaggingRequested   = true; 
198         }               
200         /* Get recursive move is required, set RecursiveRemoveRequested to true */
201         if($this->deptabs->by_object['department']->am_i_moved()){      
202           $this->RecursiveRemoveRequested = true;
203         }               
204       
205         /* This var indicated that there is an object which isn't saved right now. */
206         $this->ObjectInSaveMode = true;
208       } else {
209         /* Ok. There seem to be errors regarding to the tab data,
210            show message and continue as usual. */
211         show_errors($message);
212       }
213     }
216     /***************
217       Handle Tagging (Return output for an iframe)
218      ***************/
220     /* This department must be tagged (Is called from iframe, generates output)*/
221     if(isset($_GET['TagDepartment'])){
222       $this->deptabs->by_object['department']->tag_objects();
223       exit();  
224     }
227     /***************
228       Handle recursive move (Return output for an iframe)
229      ***************/
231     /* initiate recursive remove  (Is called from iframe, generates output)*/
232     if(isset($_GET['PerformRecMove'])){
233       $this->deptabs->by_object['department']->recursive_move("","",true);
234       exit();
235     }
238     /***************
239       Return iframes, which call tagging / recusrsive move 
240      ***************/
242     /* While one of these vars below isset, we must return an iframe, 
243        to perform requested operation */
244     if($this->ObjectTaggingRequested){
245       $this->ObjectTaggingRequested = false;
246       return($this->deptabs->by_object['department']->ShowTagFrame());
247     }
248     if($this->RecursiveRemoveRequested){
249       $this->RecursiveRemoveRequested = false;
250       return($this->deptabs->by_object['department']->ShowMoveFrame());
251     }
254     /***************
255       In case of tagging/moving the object wasn't deleted, do it know
256      ***************/
258     /* If there is an unsaved object and all operations are done
259        remove locks & save object tab & unset current object */
260     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
261       $this->deptabs->save();
262       $this->ObjectInSaveMode = false;
263       if ($this->dn != "new"){
264         del_lock ($this->dn);
265       }
266       gosa_log ("Department object'".$this->dn."' has been saved");
267       unset ($this->deptabs);
268       $this->deptabs= NULL;
269       unset ($_SESSION['objectinfo']);
270     }
273     /***************
274       Dialog canceled  
275      ***************/
277     /* User canceled edit oder delete
278      * Cancel dialog 
279      */
280     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
281       del_lock ($this->dn);
282       unset($this->depdabs);
283       $this->deptabs= NULL;
284       unset ($_SESSION['objectinfo']);
285     }
287     /* Headpage or normal plugin screen? */
288     if ($this->deptabs != NULL){
290       /* Show main page (tabs) */
291       $display= $this->deptabs->execute();
292       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
293         $display.= "<p style=\"text-align:right\">\n";
294         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
295         $display.= "&nbsp;\n";
296         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
297         $display.= "</p>";
298       }
299       return ($display);
300     }else{
301       /* Display dialog with group list */
302       $this->DivListDepartment->parent = $this;
303       $this->DivListDepartment->execute();
304       $this->reload();
305       $this->DivListDepartment->DepartmentsAdded = true;
306       $this->DivListDepartment->setEntries($this->departments);
307       return($this->DivListDepartment->Draw());
308     }
309   }
312   function reload()
313   {
314     /* Vairaible init */
315     $base         = $this->DivListDepartment->selectedBase;
316     $base_back    = preg_replace("/^[^,]+,/","",$base);
317     $Regex        = $this->DivListDepartment->Regex;
318   
319     // Create Array to Test if we have a valid back button
320     $tmp = $_SESSION['config']->idepartments;
322     // In case of a valid back button create entry
323     if(isset($tmp[$base_back])){
324       $tmp2    ['dn']          = convert_department_dn($base_back);
326       // If empty always go to top
327       if(empty($tmp2['dn'])){
328         $tmp2['dn']="/";
329       }
330       $tmp2    ['description'][0] = _("..");
331       $result[$tmp[$base_back]]=$tmp2;
332     }
334     if($this->DivListDepartment->SubSearch){
335       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
336           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
337     }else{
338       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
339           "department", $base, array("ou", "description"), GL_SIZELIMIT );
340     }
342     $this->departments= array();
344     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
345     if(preg_match("/^ou=/",$base)){
346       $this->departments [ convert_department_dn($base) ] = ".";
347     }
349     foreach ($res as $key => $value){
351       /* Don't display base as entry on subsearch */
352       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
353         continue;
354       }
356       $cdn= convert_department_dn($value['dn']);
358       /* Append to dep list */
359       if(isset($value["description"][0])){
360         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
361       }else{
362         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
363       }
364     }
365     natcasesort ($this->departments);
366     reset ($this->departments);
367   }
369   function remove_from_parent()
370   {
371     $ldap= $this->config->get_ldap_link();
372     $ldap->cd ($this->dn);
373     $ldap->recursive_remove();
375     /* Optionally execute a command after we're done */
376     $this->postremove();
378     /* Delete references to object groups */
379     $ldap->cd ($this->config->current['BASE']);
380     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
381     while ($ldap->fetch()){
382       $og= new ogroup($this->config, $ldap->getDN());
383       unset($og->member[$this->dn]);
384       $og->save ();
385     }
387   }
389   function remove_lock()
390   {
391     if (isset($this->dn)){
392       del_lock ($this->dn);
393     }
394   }
396   function save_object()
397   {
398     /* reload department */
399     $this->config->get_departments();
401     $this->config->make_idepartments();
402     $this->DivListDepartment->config= $this->config;
403     $this->DivListDepartment->save_object();
404   }
407 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
408 ?>