Code

1d040e2688bbede3441f04b34278b618bd27b7f5
[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($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
280       $this->deptabs->save_object();
281       $obj = $this->deptabs->by_object['department'];
282       if($obj->orig_dn != "new"){
283         if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
284           return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
285         }
286       }
287     }
289     /* Save changes */
290     if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
292       /* Check tabs, will feed message array */
293       $message= $this->deptabs->check();
295       /* Save, or display error message? */
296       if (count($message) == 0){
298         $this->deptabs->save(true);
299         global $config;
300         $config->get_departments();
301         $config->make_idepartments();
302         $this->config = $config;
304         /* This object must be tagged, so set ObjectTaggingRequested to true */
305         if($this->deptabs->by_object['department']->must_be_tagged()){
306           $this->ObjectTaggingRequested   = true; 
307         }               
309         /* Get recursive move is required, set RecursiveRemoveRequested to true */
310         if($this->deptabs->by_object['department']->am_i_moved()){      
311           $this->RecursiveRemoveRequested = true;
312         }               
313       
314         /* This var indicated that there is an object which isn't saved right now. */
315         $this->ObjectInSaveMode = true;
317       } else {
318         /* Ok. There seem to be errors regarding to the tab data,
319            show message and continue as usual. */
320         msg_dialog::displayChecks($message);
321       }
322     }
325     /***************
326       Handle Tagging (Return output for an iframe)
327      ***************/
329     /* This department must be tagged (Is called from iframe, generates output)*/
330     if(isset($_GET['TagDepartment'])){
331       $this->deptabs->by_object['department']->tag_objects();
332       exit();  
333     }
336     /***************
337       Handle recursive move (Return output for an iframe)
338      ***************/
340     /* initiate recursive remove  (Is called from iframe, generates output)*/
341     if(isset($_GET['PerformRecMove'])){
342       $this->deptabs->by_object['department']->recursive_move("","",true);
343       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
344       exit();
345     }
348     /***************
349       Return iframes, which call tagging / recusrsive move 
350      ***************/
352     /* While one of these vars below isset, we must return an iframe, 
353        to perform requested operation */
354     if($this->ObjectTaggingRequested){
355       $this->ObjectTaggingRequested = false;
356       return($this->deptabs->by_object['department']->ShowTagFrame());
357     }
358     if($this->RecursiveRemoveRequested){
359       $this->RecursiveRemoveRequested = false;
360       return($this->deptabs->by_object['department']->ShowMoveFrame());
361     }
364     /***************
365       In case of tagging/moving the object wasn't deleted, do it know
366      ***************/
368     /* If there is an unsaved object and all operations are done
369        remove locks & save object tab & unset current object */
370     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
371       $this->deptabs->save();
372       $this->config->get_departments();
373       $this->ObjectInSaveMode = false;
374       if ($this->dn != "new"){
375         del_lock ($this->dn);
376       }
377       unset ($this->deptabs);
378       $this->deptabs= NULL;
379       session::un_set('objectinfo');
380     }
383     /***************
384       Dialog canceled  
385      ***************/
387     /* User canceled edit oder delete
388      * Cancel dialog 
389      */
390     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
391       del_lock ($this->dn);
392       unset($this->depdabs);
393       $this->deptabs= NULL;
394       session::un_set('objectinfo');
395     }
397     /* Headpage or normal plugin screen? */
398     if ($this->deptabs !== NULL){
400       /* Show main page (tabs) */
401       $display= $this->deptabs->execute();
402       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
403         $display.= "<p style=\"text-align:right\">\n";
404         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
405         $display.= "&nbsp;\n";
406         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
407         $display.= "</p>";
408       }
409       return ($display);
410     }else{
411       /* Display dialog with group list */
412       $this->DivListDepartment->parent = $this;
413       $this->DivListDepartment->execute();
414       $this->reload();
415       $this->DivListDepartment->DepartmentsAdded = true;
416       $this->DivListDepartment->setEntries($this->departments);
417       return($this->DivListDepartment->Draw());
418     }
419   }
422   function reload()
423   {
424     /* Vairaible init */
425     $base         = $this->DivListDepartment->selectedBase;
426     $base_back    = preg_replace("/^[^,]+,/","",$base);
427     $Regex        = $this->DivListDepartment->Regex;
428   
429     // Create Array to Test if we have a valid back button
430     $config = session::get('config');
431     $tmp = $config->idepartments;
433     // In case of a valid back button create entry
434     if(isset($tmp[$base_back])){
435       $tmp2    ['dn']          = convert_department_dn($base_back);
437       // If empty always go to top
438       if(empty($tmp2['dn'])){
439         $tmp2['dn']="/";
440       }
441       $tmp2    ['description'][0] = ".. "._("Back");
442       $result[$tmp[$base_back]]=$tmp2;
443     }
445     if($this->DivListDepartment->SubSearch){
446       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
447           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
448     }else{
449       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
450           "department", $base, array("ou", "description"), GL_SIZELIMIT );
451     }
453     $this->departments= array();
455     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
456     if(preg_match("/^ou=/",$base)){
457       $this->departments [ convert_department_dn($base) ] = ".";
458     }
460     foreach ($res as $key => $value){
462       /* Don't display base as entry on subsearch */
463       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
464         continue;
465       }
467       $cdn= convert_department_dn($value['dn']);
469       /* Append to dep list */
470       if(isset($value["description"][0])){
471         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
472       }else{
473         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
474       }
475     }
476     natcasesort ($this->departments);
477     reset ($this->departments);
478   }
480   function remove_from_parent()
481   {
482     $ldap= $this->config->get_ldap_link();
483     $ldap->cd ($this->dn);
484     $ldap->recursive_remove();
486     /* Optionally execute a command after we're done */
487     $this->postremove();
489     /* Delete references to object groups */
490     $ldap->cd ($this->config->current['BASE']);
491     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
492     while ($ldap->fetch()){
493       $og= new ogroup($this->config, $ldap->getDN());
494       unset($og->member[$this->dn]);
495       $og->save ();
496     }
498   }
501   function list_get_selected_items()
502   {
503     $ids = array();
504     foreach($_POST as $name => $value){
505       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
506         $id   = preg_replace("/^item_selected_/","",$name);
507         $ids[$id] = $id;
508       }
509     }
510     return($ids);
511   }
514   function remove_lock()
515   {
516     if (isset($this->dn)){
517       del_lock ($this->dn);
518     }
519   }
521   function save_object()
522   {
523     /* reload department */
524     $this->config->get_departments();
526     $this->config->make_idepartments();
527     $this->DivListDepartment->config= $this->config;
528     $this->DivListDepartment->save_object();
529   }
532 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
533 ?>