Code

Fixed department deletion.
[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 $dns = array();
43   var $acl_module = array("department");
45   function departmentManagement (&$config, &$ui)
46   {
47     $this->ui= &$ui;
48     $this->dn= "";
49     $this->config= &$config;
50     $this->DivListDepartment = new divListDepartment($this->config,$this);
51   }
53   function execute()
54   {
55     global $config;
56  
57     /* Call parent execute */
58     plugin::execute();
60     /***************
61       Var init 
62      ***************/
64     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/","/^menu_action/"));
66     /* Reload departments */
67     $smarty                                             = get_smarty();
68     $display                                    = "";
69     $s_action                                   = "";  // Will contain an action, like del or edit
70     $s_entry                                    = "";  // The entry name for edit delete -...
73     /***************
74       Check posts  
75      ***************/
77     // Check Post action
78     foreach($_POST as $key => $val){
79       // Post for delete
80       if(preg_match("/dep_del.*/",$key)){
81         $s_action = "del";
82         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
83         $s_entry  = preg_replace("/_.*$/","",$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         // Post for new
90       }elseif(preg_match("/^remove_multiple_departments/",$key)){
91         $s_action="del_multiple";
92       }
93     }
95     /* Create options */
96     if(isset($_POST['menu_action']) && preg_match("/^dep_new_/",$_POST['menu_action'])){
97       $s_action = "new";
98       $s_entry  = preg_replace("/^dep_new_([a-z]*)/","\\1",$_POST['menu_action']);
99     }
101     /* handle remove from layers menu */
102     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
103       $s_action = "del_multiple";
104     }
107     /***************
108       Create a new department
109      ***************/
111     /* New Entry if Posted action (s_action) == new
112      */
113     if ($s_action=="new"){
114       $this->dn= "new";
116       $types = $this->get_support_departments();
117       $type = "";
118       foreach($types as $key => $data){
119         if($data['ATTR'] == $s_entry){
120           $type = $key;
121           break;
122         }
123       }
125       if(isset($types[$type])){
126         $tab = $types[$type]['TAB'];
127         $acl = $types[$type]['ACL'];
128         $this->deptabs= new deptabs($this->config,$this->config->data['TABS'][$tab], $this->dn,$acl);
129         $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
130       }else{
131         trigger_error("Invalid / Not implemented countainer type.");
132       }
133     }
136     /***************
137       Edit entry
138      ***************/
140     /* Edit Entry if Posted action (s_action) == edit 
141      * The entry which will be edited is defined in $s_entry
142      */
143     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
145       $types = $this->get_support_departments();
147       $this->dn= $this->departments[trim($s_entry)]['dn'];
148       $entry   = $this->departments[trim($s_entry)];
150       /* Detect department type 
151        */ 
152       $data = array();
153       foreach($types as $oc => $type){
154         if(in_array($oc,$entry['objectClass'])){
155           $data = $type;
156           break;
157         }
158       }
160       /* Check if the department type was editable 
161        */
162       if(!count($data)){
163         trigger_error("Unknown department type skipped '".$this->dn."'.");
164       }elseif(empty($data['TAB'])){
165         // Do nothing, this object is currently not editable
166       }else{
168         if (($user= get_lock($this->dn)) != ""){
169           return(gen_locked_message ($user, $this->dn));
170         }
172         /* Lock the current entry, so everyone will get the  above dialog */
173         add_lock ($this->dn, $this->ui->dn);
175         /* Register deptabs to trigger edit dialog */
176         $this->deptabs= new deptabs($this->config,$this->config->data['TABS'][$data['TAB']], $this->dn,"department");
177         $this->deptabs->set_acl_base($this->dn);
179         session::set('objectinfo',$this->dn);
180       }
181     }
184     /********************
185       Delete MULTIPLE entries requested, display confirm dialog
186      ********************/
188     if ($s_action=="del_multiple"){
189       $ids = $this->list_get_selected_items();
191       if(count($ids)){
192         $this->dns = array();
193         foreach($ids as $id){
194           $this->dns[$id] = $dn = $this->departments[$id]['dn'];
195         }
197         /* Check locks */
198         if ($user= get_multiple_locks($this->dns)){
199           return(gen_locked_message($user,$this->dns));
200         }
202         $dns_names = array();
203         foreach($this->dns as $dn){
204           $dns_names[] = @LDAP::fix($dn);
205         }
206         add_lock ($this->dns, $this->ui->dn);
208         /* Lock the current entry, so nobody will edit it during deletion */
209         $smarty->assign("info", msgPool::deleteInfo($dns_names));
210         $smarty->assign("multiple", true);
211         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
212       }
213     }
214    
216     /********************
217       Delete MULTIPLE entries confirmed
218      ********************/
220     /* Confirmation for deletion has been passed. Users should be deleted. */
221     if (isset($_POST['delete_multiple_department_confirm'])){
223       /* Remove user by user and check acls before removeing them */
224       foreach($this->dns as $key => $dn){
225         $acl = $this->ui->get_permissions($dn,"department/department");
226         if (preg_match('/d/', $acl)){
228           /* Delete request is permitted, perform LDAP action */
229           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
230           $this->deptabs->set_acl_base();
231           $this->deptabs->delete ();
232           $this->deptabs = NULL;
233         } else {
234           msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
235         }
236       }
238       /* Remove lock file after successfull deletion */
239       $this->remove_lock();
240       $this->dns = array();
241     }
243  
244     /********************
245       Delete MULTIPLE entries Canceled
246      ********************/
248     /* Remove lock */
249     if(isset($_POST['delete_multiple_department_cancel'])){
251       /* Remove lock file after successfull deletion */
252       $this->remove_lock();
253       $this->dns = array();
254     }
257     /***************
258       Delete entry 
259      ***************/
261     /* Delete Entry if Posted action (s_action) == del 
262      * The entry which will be deleted is defined in $s_entry
263      */
264     if ($s_action =="del"){
265       $this->dn= $this->config->departments[trim($s_entry)];
267       /* check acls */
268       $acl = $this->ui->get_permissions($this->dn,"department/department");
269       if(preg_match("/d/",$acl)){
271         /* Check locking */
272         if (($user= get_lock($this->dn)) != ""){
273           session::set('dn',$this->dn);
274           return(gen_locked_message($user, $this->dn));
275         } else {
276           add_lock ($this->dn, $this->ui->dn);
277           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
278           $smarty->assign("multiple", false);
279           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
280           return ($display);
281         }
282       }else{
283         msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
284       }
285     }
288     /***************
289       Delete department confirmed  
290      ***************/
292     /* If department deletion is accepted ...
293      * Finally delete department 
294      */
295     if (isset($_POST['delete_department_confirm'])){
297       /* check acls */
298       $acl = $this->ui->get_permissions($this->dn,"department/department");
299       if(preg_match("/d/",$acl)){
300         $this->remove_from_parent();
301       } else {
302         msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
303       }
304     }
307     /***************
308       Handle tagging/recursive move (Return output for an iframe)
309      ***************/
311     /* initiate recursive remove  (Is called from iframe, generates output)*/
312     if(isset($_GET['PerformRecMove'])){
313       $this->deptabs->move_me();
314       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
315       exit();
316     }
318     /* This department must be tagged (Is called from iframe, generates output)*/
319     if(isset($_GET['TagDepartment'])){
320       $base_name = $this->deptabs->base_name;
321       $this->deptabs->by_object[$base_name]->tag_objects();
322       exit();  
323     }
326     /***************
327       Edit department finished 
328      ***************/
330     if (is_object($this->deptabs) &&          // Ensure we have a valid deptab here 
331         (isset($_POST['edit_finish']) ||      // If 'Save' button is pressed in the edit dialog.
332          isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
333          $this->deptabs->move_done())){       // The move(rename) is done, we have to save the rest now.
335       /* Check tabs, will feed message array.
336          This call will also initiate a sav_object() call.
337           So don't move it below the moved check !.
338        */
339       $message= $this->deptabs->check();
341       /*************
342         MOVED ? 
343          Check if this department has to be moved 
344        *************/  
345       if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
346         return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
347       }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
348         $smarty = get_smarty();
349         $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove&no_output_compression");
350         $smarty->assign("message",_("As soon as the move operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog."));
351         return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
352       }
354       /* Save, or display error message? */
355       if (count($message) == 0){
356         global $config;
358         $this->deptabs->save();
359         $config->get_departments();
360         $config->make_idepartments();
361         $this->config = $config;
363         /* This var indicated that there is an object which isn't saved right now. */
364         $this->ObjectInSaveMode = true;
366         /* This object must be tagged, so set ObjectTaggingRequested to true */
367         if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
368           $smarty = get_smarty();
369           $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
370           $smarty->assign("message",_("As soon as the tag operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog."));
371           return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
372         }               
374       } else {
375         /* Ok. There seem to be errors regarding to the tab data,
376            show message and continue as usual. */
377         msg_dialog::displayChecks($message);
378       }
379     }
382    /***************
383      In case of tagging/moving the object wasn't deleted, do it know
384     ***************/
386    /* If there is an unsaved object and all operations are done
387       remove locks & save object tab & unset current object */
388    if($this->ObjectInSaveMode){
389      $this->config->get_departments();
390      $this->ObjectInSaveMode = false;
391      if ($this->dn != "new"){
392        $this->remove_lock();
393      }
394      unset ($this->deptabs);
395      $this->deptabs= NULL;
396      session::un_set('objectinfo');
397    }
400    /***************
401       Dialog canceled  
402      ***************/
404     /* User canceled edit oder delete
405      * Cancel dialog 
406      */
407     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
408       $this->remove_lock();
409       $this->deptabs= NULL;
410       session::un_set('objectinfo');
411     }
413     /* Headpage or normal plugin screen? */
414     if ($this->deptabs !== NULL){
416       /* Show main page (tabs) */
417       $display= $this->deptabs->execute();
418       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
419         $display.= "<p style=\"text-align:right\">\n";
420         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
421         $display.= "&nbsp;\n";
422         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
423         $display.= "</p>";
424       }
425       return ($display);
426     }else{
427       /* Display dialog with group list */
428       $this->DivListDepartment->parent = $this;
429       $this->DivListDepartment->execute();
430       $this->reload();
431       $this->DivListDepartment->DepartmentsAdded = true;
432       $this->DivListDepartment->setEntries($this->departments);
433       return($this->DivListDepartment->Draw());
434     }
435   }
438   function reload()
439   {
440     /* Vairaible init */
441     $base         = $this->DivListDepartment->selectedBase;
442     $base_back    = preg_replace("/^[^,]+,/","",$base);
443     $Regex        = $this->DivListDepartment->Regex;
444   
445     // Create Array to Test if we have a valid back button
446     $config = session::get('config');
447     $tmp = $config->idepartments;
449     $types = $this->get_support_departments();
451     /* Create search filter for each department type
452      */
453     $res = array();
454     if($this->DivListDepartment->SubSearch){
455       foreach($types as $oc => $data){
456         $oc_f = "(&(objectClass=".$data['OC'].")(objectClass=gosaDepartment))";
457         $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$oc_f.")",
458             "department", $base, array($data['ATTR'],"objectClass", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
459       }
460     }else{
461       foreach($types as $oc => $data){
462         $oc_f = "(&(objectClass=".$data['OC'].")(objectClass=gosaDepartment))";
463         $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$oc_f.")",
464             "department", $base, array($data['ATTR'], "objectClass","description"), GL_SIZELIMIT ));
465       }
466     }
468     /* Prepare list (sortable index) 
469      */
470     $deps = array();
471     $this->departments = array();
472     foreach($res as $attr){
474       /* Don't display base as entry on subsearch */
475       if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
476         continue;
477       }
479       /* Detect type and create sortable index 
480        */
481       $found =FALSE;
482       foreach($types as $oc => $data){
483         if(in_array($data['OC'],$attr['objectClass']) && isset($attr[$data['ATTR']][0])){
484           $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
485           $deps[$name] = $attr;
486           $found =TRUE;
487           break;
488         }
489       }
490     }
491     uksort($deps, 'strnatcasecmp');
493     /* In case of a valid back button create entry 
494      */
495     if(isset($tmp[$base])){
496       $ldap = $this->config->get_ldap_link();
497       $ldap->cd($this->config->current['BASE']);
498       $ldap->cat($base );
499       $attr = $ldap->fetch();
500       foreach($types as $oc => $type){
501         if(in_array($oc,$attr['objectClass'])){
502           $attr[$type['ATTR']][0] = ".";
503         }
504       }
505       if(isset($attr['description'])){
506          unset($attr['description']);
507       }
508       $deps= array_merge(array($attr),$deps);
509     }
511     $this->departments = array_values($deps);
512   }
514   function remove_from_parent()
515   {
516     $ldap= $this->config->get_ldap_link();
517     $ldap->cd ($this->dn);
518     $ldap->recursive_remove();
520     /* Optionally execute a command after we're done */
521     $this->postremove();
523     /* Delete references to object groups */
524     $ldap->cd ($this->config->current['BASE']);
525     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
526     while ($ldap->fetch()){
527       $og= new ogroup($this->config, $ldap->getDN());
528       unset($og->member[$this->dn]);
529       $og->save ();
530     }
532   }
535   function list_get_selected_items()
536   {
537     $ids = array();
538     foreach($_POST as $name => $value){
539       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
540         $id   = preg_replace("/^item_selected_/","",$name);
541         $ids[$id] = $id;
542       }
543     }
544     return($ids);
545   }
548   function remove_lock()
549   {
550     if (isset($this->dn)){
551       del_lock ($this->dn);
552     }
553     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
554       del_lock($this->dn);
555     }
556     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
557       del_lock($this->dns);
558     }
559   }
561   function save_object()
562   {
563     /* reload department */
564     $this->config->get_departments();
566     $this->config->make_idepartments();
567     $this->DivListDepartment->config= $this->config;
568     $this->DivListDepartment->save_object();
569   }
572   /*! \brief  Returns information about all container types that GOsa con handle.
573     @return Array   Informations about departments supported by GOsa.
574    */
575   public static function get_support_departments()
576   {
577     /* Domain component 
578      */
579     $types = array();
580     $types['dcObject']['ACL']     = "dcObject";
581     $types['dcObject']['CLASS']   = "dcObject";
582     $types['dcObject']['ATTR']    = "dc";
583     $types['dcObject']['TAB']     = "DCOBJECT_TABS";
584     $types['dcObject']['OC']      = "dcObject";
585     $types['dcObject']['IMG']     = "plugins/departments/images/dc.png";
586     $types['dcObject']['IMG_FULL']= "plugins/departments/images/dc.png";
587     $types['dcObject']['TITLE']   = _("Domain Component");
588     $types['dcObject']['TPL']     = "dcObject.tpl";
590     /* Country object
591      */
592     $types['country']['ACL']     = "country";
593     $types['country']['CLASS']   = "country";
594     $types['country']['TAB']     = "COUNTRY_TABS";
595     $types['country']['ATTR']    = "c";
596     $types['country']['OC']      = "country";
597     $types['country']['IMG']     = "plugins/departments/images/country.png";
598     $types['country']['IMG_FULL']= "plugins/departments/images/country.png";
599     $types['country']['TITLE']   = _("Country");
600     $types['country']['TPL']     = "country.tpl";
602     /* Locality object
603      */
604     $types['locality']['ACL']     = "locality";
605     $types['locality']['CLASS']   = "locality";
606     $types['locality']['TAB']     = "LOCALITY_TABS";
607     $types['locality']['ATTR']    = "l";
608     $types['locality']['OC']      = "locality";
609     $types['locality']['IMG']     = "plugins/departments/images/locality.png";
610     $types['locality']['IMG_FULL']= "plugins/departments/images/locality.png";
611     $types['locality']['TITLE']   = _("Locality");
612     $types['locality']['TPL']     = "locality.tpl";
614     /* Organization
615      */
616     $types['organization']['ACL']     = "organization";
617     $types['organization']['CLASS']   = "organization";
618     $types['organization']['TAB']     = "ORGANIZATION_TABS";
619     $types['organization']['ATTR']    = "o";
620     $types['organization']['OC']      = "organization";
621     $types['organization']['IMG']     = "plugins/departments/images/organization.png";
622     $types['organization']['IMG_FULL']= "plugins/departments/images/organization.png";
623     $types['organization']['TITLE']   = _("Organization");
624     $types['organization']['TPL']     = "organization.tpl";
626     /* Organization
627      */
628     $types['organizationalUnit']['ACL']     = "department";
629     $types['organizationalUnit']['CLASS']   = "department";
630     $types['organizationalUnit']['TAB']     = "DEPTABS";
631     $types['organizationalUnit']['ATTR']    = "ou";
632     $types['organizationalUnit']['OC']      = "organizationalUnit";
633     $types['organizationalUnit']['IMG']     = "images/lists/folder.png";//plugins/departments/images/department.png";
634     $types['organizationalUnit']['IMG_FULL']= "images/lists/folder-full.png";//:wplugins/departments/images/department.png";
635     $types['organizationalUnit']['TITLE']   = _("Department");
636     $types['organizationalUnit']['TPL']     = "generic.tpl";
639 #  /* Alias
640 #   */
641 #  $types['alias']['ACL']     = "alias";
642 #  $types['alias']['CLASS']   = "alias";
643 #  $types['alias']['TAB']     = "ALIASTABS";
644 #  $types['alias']['ATTR']    = "???";
645 #  $types['alias']['OC']      = "???";
646 #  $types['alias']['IMG']     = "plugins/departments/images/department_alias.png";
647 #  $types['alias']['IMG_FULL']= "plugins/departments/images/department_alias.png";
648 #  $types['alias']['TITLE']   = _("Alias");
649 #  $types['alias']['TPL']     = "alias.tpl";
651 #  /* Referal
652 #   */
653 #  $types['referal']['ACL']     = "referal";
654 #  $types['referal']['CLASS']   = "referal";
655 #  $types['referal']['TAB']     = "REFERALTABS";
656 #  $types['referal']['ATTR']    = "???";
657 #  $types['referal']['OC']      = "???";
658 #  $types['referal']['IMG']     = "plugins/departments/images/department_referal.png";
659 #  $types['referal']['IMG_FULL']= "plugins/departments/images/department_referal.png";
660 #  $types['referal']['TITLE']   = _("Referal");
661 #  $types['referal']['TPL']     = "referal.tpl";
663     return($types);
664   }
666 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
667 ?>