Code

Fixed undefined index
[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_.*/","/^item_selected/","/^remove_multiple_departments/");
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("/^remove_multiple_departments/",$key)){
96         $s_action="del_multiple";
97       }elseif(preg_match("/dep_new.*/",$key)){
98         $s_action="new";
99       }
100     }
103     /***************
104       Create a new department
105      ***************/
107     /* New Entry if Posted action (s_action) == new
108      */
109     if ($s_action=="new"){
110       $this->dn= "new";
111       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
112       $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
113     }
116     /***************
117       Edit entry
118      ***************/
120     /* Edit Entry if Posted action (s_action) == edit 
121      * The entry which will be edited is defined in $s_entry
122      */
123     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
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       /* Register deptabs to trigger edit dialog */
134       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
135       $this->deptabs->set_acl_base($this->dn);
137       $_SESSION['objectinfo']= $this->dn;
138     }
141     /********************
142       Delete MULTIPLE entries requested, display confirm dialog
143      ********************/
145     if ($s_action=="del_multiple"){
146       $ids = $this->list_get_selected_items();
149       if(count($ids)){
150         foreach($ids as $id){
151           $id = base64_decode($id);
152           $dn = $this->config->departments[$id];
153   
154           if (($user= get_lock($dn)) != ""){
155             return(gen_locked_message ($user, $dn));
156           }
157           $this->dns[$id] = $dn;
158         }
160         $dns_names = "<br><pre>";
161         foreach($this->dns as $dn){
162           add_lock ($dn, $this->ui->dn);
163           $dns_names .= $dn."\n";
164         }
165         $dns_names .="</pre>";
167         /* Lock the current entry, so nobody will edit it during deletion */
168         if (count($this->dns) == 1){
169           $smarty->assign("info",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
170         } else {
171           $smarty->assign("info",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
172         }
173         $smarty->assign("multiple", true);
174         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
175       }
176     }
177    
179     /********************
180       Delete MULTIPLE entries confirmed
181      ********************/
183     /* Confirmation for deletion has been passed. Users should be deleted. */
184     if (isset($_POST['delete_multiple_department_confirm'])){
186       /* Remove user by user and check acls before removeing them */
187       foreach($this->dns as $key => $dn){
188         $acl = $this->ui->get_permissions($dn,"department/department");
189         if (preg_match('/d/', $acl)){
191           /* Delete request is permitted, perform LDAP action */
192           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
193           $this->deptabs->set_acl_base();
194           $this->deptabs->delete ();
195           $this->deptabs = NULL;
196           gosa_log ("Department object'".$this->dn."' has been removed");
197         } else {
198           print_red (_("You have no permission to remove this department."));
199         }
200         /* Remove lock file after successfull deletion */
201         del_lock ($dn);
202         unset($this->dns[$key]);
203       }
204     }
206  
207     /********************
208       Delete MULTIPLE entries Canceled
209      ********************/
211     /* Remove lock */
212     if(isset($_POST['delete_multiple_department_cancel'])){
213       foreach($this->dns as $key => $dn){
214         del_lock ($dn);
215         unset($this->dns[$key]);
216       }
217     }
220     /***************
221       Delete entry 
222      ***************/
224     /* Delete Entry if Posted action (s_action) == del 
225      * The entry which will be deleted is defined in $s_entry
226      */
227     if ($s_action =="del"){
228       $this->dn= $this->config->departments[trim($s_entry)];
230       /* check acls */
231       $acl = $this->ui->get_permissions($this->dn,"department/department");
232       if(preg_match("/d/",$acl)){
234         /* Check locking */
235         if (($user= get_lock($this->dn)) != ""){
236           $_SESSION['dn']= $this->dn;
237           return(gen_locked_message($user, $this->dn));
238         } else {
239           add_lock ($this->dn, $this->ui->dn);
240           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
241           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
242           return ($display);
243         }
244       }else{
245         print_red (_("You have no permission to remove this department."));
246       }
247     }
250     /***************
251       Delete department confirmed  
252      ***************/
254     /* If department deletion is accepted ...
255      * Finally delete department 
256      */
257     if (isset($_POST['delete_department_confirm'])){
259       /* check acls */
260       $acl = $this->ui->get_permissions($this->dn,"department/department");
261       if(preg_match("/d/",$acl)){
262         $this->remove_from_parent();
263         gosa_log ("Department object'".$this->dn."' has been removed");
264       } else {
265         print_red (_("You have no permission to remove this department."));
266       }
267     }
270     /***************
271       Edit department finished 
272      ***************/
274     if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
275       $this->deptabs->save_object();
276       $obj = $this->deptabs->by_object['department'];
277       if($obj->orig_dn != "new"){
278         if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
279           return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
280         }
281       }
282     }
284     /* Save changes */
285     if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
287       /* Check tabs, will feed message array */
288       $message= $this->deptabs->check();
290       /* Save, or display error message? */
291       if (count($message) == 0){
293         $this->deptabs->save(true);
294         $this->config->get_departments();
296         /* This object must be tagged, so set ObjectTaggingRequested to true */
297         if($this->deptabs->by_object['department']->must_be_tagged()){
298           $this->ObjectTaggingRequested   = true; 
299         }               
301         /* Get recursive move is required, set RecursiveRemoveRequested to true */
302         if($this->deptabs->by_object['department']->am_i_moved()){      
303           $this->RecursiveRemoveRequested = true;
304         }               
305       
306         /* This var indicated that there is an object which isn't saved right now. */
307         $this->ObjectInSaveMode = true;
309       } else {
310         /* Ok. There seem to be errors regarding to the tab data,
311            show message and continue as usual. */
312         show_errors($message);
313       }
314     }
317     /***************
318       Handle Tagging (Return output for an iframe)
319      ***************/
321     /* This department must be tagged (Is called from iframe, generates output)*/
322     if(isset($_GET['TagDepartment'])){
323       $this->deptabs->by_object['department']->tag_objects();
324       exit();  
325     }
328     /***************
329       Handle recursive move (Return output for an iframe)
330      ***************/
332     /* initiate recursive remove  (Is called from iframe, generates output)*/
333     if(isset($_GET['PerformRecMove'])){
334       $this->deptabs->by_object['department']->recursive_move("","",true);
335       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
336       exit();
337     }
340     /***************
341       Return iframes, which call tagging / recusrsive move 
342      ***************/
344     /* While one of these vars below isset, we must return an iframe, 
345        to perform requested operation */
346     if($this->ObjectTaggingRequested){
347       $this->ObjectTaggingRequested = false;
348       return($this->deptabs->by_object['department']->ShowTagFrame());
349     }
350     if($this->RecursiveRemoveRequested){
351       $this->RecursiveRemoveRequested = false;
352       return($this->deptabs->by_object['department']->ShowMoveFrame());
353     }
356     /***************
357       In case of tagging/moving the object wasn't deleted, do it know
358      ***************/
360     /* If there is an unsaved object and all operations are done
361        remove locks & save object tab & unset current object */
362     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
363       $this->deptabs->save();
364       $this->config->get_departments();
365       $this->ObjectInSaveMode = false;
366       if ($this->dn != "new"){
367         del_lock ($this->dn);
368       }
369       gosa_log ("Department object'".$this->dn."' has been saved");
370       unset ($this->deptabs);
371       $this->deptabs= NULL;
372       unset ($_SESSION['objectinfo']);
373     }
376     /***************
377       Dialog canceled  
378      ***************/
380     /* User canceled edit oder delete
381      * Cancel dialog 
382      */
383     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
384       del_lock ($this->dn);
385       unset($this->depdabs);
386       $this->deptabs= NULL;
387       unset ($_SESSION['objectinfo']);
388     }
390     /* Headpage or normal plugin screen? */
391     if ($this->deptabs != NULL){
393       /* Show main page (tabs) */
394       $display= $this->deptabs->execute();
395       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
396         $display.= "<p style=\"text-align:right\">\n";
397         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
398         $display.= "&nbsp;\n";
399         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
400         $display.= "</p>";
401       }
402       return ($display);
403     }else{
404       /* Display dialog with group list */
405       $this->DivListDepartment->parent = $this;
406       $this->DivListDepartment->execute();
407       $this->reload();
408       $this->DivListDepartment->DepartmentsAdded = true;
409       $this->DivListDepartment->setEntries($this->departments);
410       return($this->DivListDepartment->Draw());
411     }
412   }
415   function reload()
416   {
417     /* Vairaible init */
418     $base         = $this->DivListDepartment->selectedBase;
419     $base_back    = preg_replace("/^[^,]+,/","",$base);
420     $Regex        = $this->DivListDepartment->Regex;
421   
422     // Create Array to Test if we have a valid back button
423     $tmp = $_SESSION['config']->idepartments;
425     // In case of a valid back button create entry
426     if(isset($tmp[$base_back])){
427       $tmp2    ['dn']          = convert_department_dn($base_back);
429       // If empty always go to top
430       if(empty($tmp2['dn'])){
431         $tmp2['dn']="/";
432       }
433       $tmp2    ['description'][0] = _("..");
434       $result[$tmp[$base_back]]=$tmp2;
435     }
437     if($this->DivListDepartment->SubSearch){
438       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
439           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
440     }else{
441       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
442           "department", $base, array("ou", "description"), GL_SIZELIMIT );
443     }
445     $this->departments= array();
447     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
448     if(preg_match("/^ou=/",$base)){
449       $this->departments [ convert_department_dn($base) ] = ".";
450     }
452     foreach ($res as $key => $value){
454       /* Don't display base as entry on subsearch */
455       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
456         continue;
457       }
459       $cdn= convert_department_dn($value['dn']);
461       /* Append to dep list */
462       if(isset($value["description"][0])){
463         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
464       }else{
465         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
466       }
467     }
468     natcasesort ($this->departments);
469     reset ($this->departments);
470   }
472   function remove_from_parent()
473   {
474     $ldap= $this->config->get_ldap_link();
475     $ldap->cd ($this->dn);
476     $ldap->recursive_remove();
478     /* Optionally execute a command after we're done */
479     $this->postremove();
481     /* Delete references to object groups */
482     $ldap->cd ($this->config->current['BASE']);
483     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
484     while ($ldap->fetch()){
485       $og= new ogroup($this->config, $ldap->getDN());
486       unset($og->member[$this->dn]);
487       $og->save ();
488     }
490   }
493   function list_get_selected_items()
494   {
495     $ids = array();
496     foreach($_POST as $name => $value){
497       if(preg_match("/^item_selected_[a-z0-9\=]*$/i",$name)){
498         $id   = preg_replace("/^item_selected_/","",$name);
499         $ids[$id] = $id;
500       }
501     }
502     return($ids);
503   }
506   function remove_lock()
507   {
508     if (isset($this->dn)){
509       del_lock ($this->dn);
510     }
511   }
513   function save_object()
514   {
515     /* reload department */
516     $this->config->get_departments();
518     $this->config->make_idepartments();
519     $this->DivListDepartment->config= $this->config;
520     $this->DivListDepartment->save_object();
521   }
524 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
525 ?>