Code

Created trunk inside of 2.6-lhm
[gosa.git] / trunk / 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,"department");
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,TRUE));
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         set_object_info($this->dn);
180       }
181     }
184     /********************
185       Delete MULTIPLE entries requested, display confirm dialog
186      ********************/
188     if ($s_action=="del_multiple" || $s_action == "del"){
190       if($s_action == "del"){
191         $ids = array($s_entry);
192       }else{
193         $ids = $this->list_get_selected_items();
194       }
196       if(count($ids)){
197         $this->dns = array();
198         foreach($ids as $id){
199           $this->dns[$id] = $dn = $this->departments[$id]['dn'];
200         }
202         /* Check locks */
203         if ($user= get_multiple_locks($this->dns)){
204           return(gen_locked_message($user,$this->dns));
205         }
207         $dns_names = array();
208         foreach($this->dns as $dn){
209           $dns_names[] = LDAP::fix($dn);
210         }
211         add_lock ($this->dns, $this->ui->dn);
213         /* Lock the current entry, so nobody will edit it during deletion */
214         $smarty->assign("info", msgPool::deleteInfo($dns_names));
215         $smarty->assign("multiple", true);
216         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
217       }
218     }
219    
221     /********************
222       Delete MULTIPLE entries confirmed
223      ********************/
225     /* Confirmation for deletion has been passed. Users should be deleted. */
226     if (isset($_POST['delete_multiple_department_confirm'])){
228       /* Do not remove departments that will already be deleted by its parents 
229        */
230       foreach($this->dns as $key => $dn){
231         foreach($this->dns as $key2 => $dn2){
232           if($dn == $dn2) continue;
233           if(preg_match("/".preg_quote($dn,'/')."$/",$dn2)){
234             unset($this->dns[$key2]);
235           }
236         }
237       }
239       /* Remove user by user and check acls before removeing them */
240       foreach($this->dns as $key => $dn){
241         $acl = $this->ui->get_permissions($dn,"department/department");
242         if (preg_match('/d/', $acl)){
244          /* Delete request is permitted, perform LDAP action */
245           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
246           $this->deptabs->set_acl_base();
247           $this->deptabs->delete ();
248           $this->deptabs = NULL;
249         } else {
250           msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
251         }
252       }
254       /* Remove lock file after successfull deletion */
255       $this->remove_lock();
256       $this->dns = array();
257     }
259  
260     /********************
261       Delete MULTIPLE entries Canceled
262      ********************/
264     /* Remove lock */
265     if(isset($_POST['delete_multiple_department_cancel'])){
267       /* Remove lock file after successfull deletion */
268       $this->remove_lock();
269       $this->dns = array();
270     }
273     /***************
274       Handle tagging/recursive move (Return output for an iframe)
275      ***************/
277     /* initiate recursive remove  (Is called from iframe, generates output)*/
278     if(isset($_GET['PerformRecMove'])){
279       $this->deptabs->move_me();
280       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
281       exit();
282     }
284     /* This department must be tagged (Is called from iframe, generates output)*/
285     if(isset($_GET['TagDepartment'])){
286       $base_name = $this->deptabs->base_name;
287       $this->deptabs->by_object[$base_name]->tag_objects();
288       exit();  
289     }
292     /***************
293       Edit department finished 
294      ***************/
296     if (is_object($this->deptabs) &&          // Ensure we have a valid deptab here 
297         (isset($_POST['edit_finish']) ||      // If 'Save' button is pressed in the edit dialog.
298          isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
299          $this->deptabs->move_done())){       // The move(rename) is done, we have to save the rest now.
301       /* Check tabs, will feed message array.
302          This call will also initiate a sav_object() call.
303           So don't move it below the moved check !.
304        */
305       $message= $this->deptabs->check();
307       /*************
308         MOVED ? 
309          Check if this department has to be moved 
310        *************/  
311       if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
312         return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
313       }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
314         $smarty = get_smarty();
315         $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove&no_output_compression");
316         $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."));
317         return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
318       }
320       /* Save, or display error message? */
321       if (count($message) == 0){
322         global $config;
324         $this->deptabs->save();
325         $config->get_departments();
326         $config->make_idepartments();
327         $this->config = $config;
329         /* This var indicated that there is an object which isn't saved right now. */
330         $this->ObjectInSaveMode = true;
332         /* This object must be tagged, so set ObjectTaggingRequested to true */
333         if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
334           $smarty = get_smarty();
335           $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
336           $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."));
337           return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
338         }               
340       } else {
341         /* Ok. There seem to be errors regarding to the tab data,
342            show message and continue as usual. */
343         msg_dialog::displayChecks($message);
344       }
345     }
348    /***************
349      In case of tagging/moving the object wasn't deleted, do it know
350     ***************/
352    /* If there is an unsaved object and all operations are done
353       remove locks & save object tab & unset current object */
354    if($this->ObjectInSaveMode){
355      $this->config->get_departments();
356      $this->ObjectInSaveMode = false;
357      if ($this->dn != "new"){
358        $this->remove_lock();
359      }
360      unset ($this->deptabs);
361      $this->deptabs= NULL;
362      set_object_info();
363    }
366    /***************
367       Dialog canceled  
368      ***************/
370     /* User canceled edit oder delete
371      * Cancel dialog 
372      */
373     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
374       $this->remove_lock();
375       $this->deptabs= NULL;
376       set_object_info();
377     }
379     /* Headpage or normal plugin screen? */
380     if ($this->deptabs !== NULL){
382       /* Show main page (tabs) */
383       $display= $this->deptabs->execute();
384       if(($this->deptabs instanceOf tabs || $this->deptabs instanceOf plugin) && $this->deptabs->read_only == TRUE){
385         $display.= "<p style=\"text-align:right\">
386           <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
387           </p>";
388       }elseif (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
389         $display.= "<p style=\"text-align:right\">\n";
390         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
391         $display.= "&nbsp;\n";
392         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
393         $display.= "</p>";
394       }
395       return ($display);
396     }else{
397       /* Display dialog with group list */
398       $this->DivListDepartment->parent = $this;
399       $this->DivListDepartment->execute();
400       $this->reload();
401       $this->DivListDepartment->DepartmentsAdded = true;
402       $this->DivListDepartment->setEntries($this->departments);
403       return($this->DivListDepartment->Draw());
404     }
405   }
408   function reload()
409   {
410     /* Vairaible init */
411     $base         = $this->DivListDepartment->selectedBase;
412     $base_back    = preg_replace("/^[^,]+,/","",$base);
413     $Regex        = $this->DivListDepartment->Regex;
414   
415     // Create Array to Test if we have a valid back button
416     $config = session::get('config');
417     $tmp = $config->idepartments;
419     $types = $this->get_support_departments();
421     /* Create search filter for each department type
422      */
424     /* Get all departments within this subtree */
425     $s_filter = "";
426     $s_attrs  = array("description","objectClass");
427     $s_acls   = array();
428     foreach($types as $oc => $data){
429       $s_filter.= "(&(objectClass=gosaDepartment)(objectClass=".$data['OC'].")".
430         "(|(".$data['ATTR']."=".$Regex.")(description=".$Regex.")))";
431       $s_attrs[]= $data['ATTR'];
432       $s_acls[] = "department/".$data['ACL'];
433     }
435     if($this->DivListDepartment->SubSearch){
436       $res = get_list("(|".$s_filter.")",$s_acls,$base,$s_attrs,GL_SIZELIMIT | GL_SUBSEARCH);
437     }else{
438       $res = get_list("(|".$s_filter.")",$s_acls,$base,$s_attrs,GL_SIZELIMIT);
439     }
441     /* Prepare list (sortable index) 
442      */
443     $deps = array();
444     $this->departments = array();
445     foreach($res as $attr){
447       /* Don't display base as entry on subsearch */
448       if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
449         continue;
450       }
452       /* Detect type and create sortable index 
453        */
454       $found =FALSE;
455       foreach($types as $oc => $data){
456         if(in_array($data['OC'],$attr['objectClass']) && isset($attr[$data['ATTR']][0])){
457           $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
458           $deps[$name] = $attr;
459           $found =TRUE;
460           break;
461         }
462       }
463     }
464     uksort($deps, 'strnatcasecmp');
466     /* In case of a valid back button create entry 
467      */
468     if(isset($tmp[$base])){
469       $ldap = $this->config->get_ldap_link();
470       $ldap->cd($this->config->current['BASE']);
471       $ldap->cat($base );
472       $attr = $ldap->fetch();
473       foreach($types as $oc => $type){
474         if(in_array($oc,$attr['objectClass'])){
475           $attr[$type['ATTR']][0] = ".";
476         }
477       }
478       if(isset($attr['description'])){
479          unset($attr['description']);
480       }
481       $deps= array_merge(array($attr),$deps);
482     }
484     $this->departments = array_values($deps);
485   }
487   function remove_from_parent()
488   {
489     $ldap= $this->config->get_ldap_link();
490     $ldap->cd ($this->dn);
491     $ldap->recursive_remove();
493     /* Optionally execute a command after we're done */
494     $this->postremove();
496     /* Delete references to object groups */
497     $ldap->cd ($this->config->current['BASE']);
498     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
499     while ($ldap->fetch()){
500       $og= new ogroup($this->config, $ldap->getDN());
501       unset($og->member[$this->dn]);
502       $og->save ();
503     }
505   }
508   function list_get_selected_items()
509   {
510     $ids = array();
511     foreach($_POST as $name => $value){
512       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
513         $id   = preg_replace("/^item_selected_/","",$name);
514         $ids[$id] = $id;
515       }
516     }
517     return($ids);
518   }
521   function remove_lock()
522   {
523     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
524       del_lock($this->dn);
525     }
526     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
527       del_lock($this->dns);
528     }
529   }
531   function save_object()
532   {
533     /* reload department */
534 #    $this->config->get_departments();
535 #    $this->config->make_idepartments();
536     $this->DivListDepartment->config= $this->config;
537     $this->DivListDepartment->save_object();
538   }
541   /*! \brief  Returns information about all container types that GOsa con handle.
542     @return Array   Informations about departments supported by GOsa.
543    */
544   public static function get_support_departments()
545   {
546     /* Domain component 
547      */
548     $types = array();
549     $types['dcObject']['ACL']     = "dcObject";
550     $types['dcObject']['CLASS']   = "dcObject";
551     $types['dcObject']['ATTR']    = "dc";
552     $types['dcObject']['TAB']     = "DCOBJECT_TABS";
553     $types['dcObject']['OC']      = "dcObject";
554     $types['dcObject']['IMG']     = "plugins/departments/images/dc.png";
555     $types['dcObject']['IMG_FULL']= "plugins/departments/images/dc.png";
556     $types['dcObject']['TITLE']   = _("Domain Component");
557     $types['dcObject']['TPL']     = "dcObject.tpl";
559     /* Country object
560      */
561     $types['country']['ACL']     = "country";
562     $types['country']['CLASS']   = "country";
563     $types['country']['TAB']     = "COUNTRY_TABS";
564     $types['country']['ATTR']    = "c";
565     $types['country']['OC']      = "country";
566     $types['country']['IMG']     = "plugins/departments/images/country.png";
567     $types['country']['IMG_FULL']= "plugins/departments/images/country.png";
568     $types['country']['TITLE']   = _("Country");
569     $types['country']['TPL']     = "country.tpl";
571     /* Locality object
572      */
573     $types['locality']['ACL']     = "locality";
574     $types['locality']['CLASS']   = "locality";
575     $types['locality']['TAB']     = "LOCALITY_TABS";
576     $types['locality']['ATTR']    = "l";
577     $types['locality']['OC']      = "locality";
578     $types['locality']['IMG']     = "plugins/departments/images/locality.png";
579     $types['locality']['IMG_FULL']= "plugins/departments/images/locality.png";
580     $types['locality']['TITLE']   = _("Locality");
581     $types['locality']['TPL']     = "locality.tpl";
583     /* Organization
584      */
585     $types['organization']['ACL']     = "organization";
586     $types['organization']['CLASS']   = "organization";
587     $types['organization']['TAB']     = "ORGANIZATION_TABS";
588     $types['organization']['ATTR']    = "o";
589     $types['organization']['OC']      = "organization";
590     $types['organization']['IMG']     = "plugins/departments/images/organization.png";
591     $types['organization']['IMG_FULL']= "plugins/departments/images/organization.png";
592     $types['organization']['TITLE']   = _("Organization");
593     $types['organization']['TPL']     = "organization.tpl";
595     /* Department
596      */
597     $types['organizationalUnit']['ACL']     = "department";
598     $types['organizationalUnit']['CLASS']   = "department";
599     $types['organizationalUnit']['TAB']     = "DEPTABS";
600     $types['organizationalUnit']['ATTR']    = "ou";
601     $types['organizationalUnit']['OC']      = "organizationalUnit";
602     $types['organizationalUnit']['IMG']     = "images/lists/folder.png";//plugins/departments/images/department.png";
603     $types['organizationalUnit']['IMG_FULL']= "images/lists/folder-full.png";//:wplugins/departments/images/department.png";
604     $types['organizationalUnit']['TITLE']   = _("Department");
605     $types['organizationalUnit']['TPL']     = "generic.tpl";
608 #  /* Alias
609 #   */
610 #  $types['alias']['ACL']     = "alias";
611 #  $types['alias']['CLASS']   = "alias";
612 #  $types['alias']['TAB']     = "ALIASTABS";
613 #  $types['alias']['ATTR']    = "???";
614 #  $types['alias']['OC']      = "???";
615 #  $types['alias']['IMG']     = "plugins/departments/images/department_alias.png";
616 #  $types['alias']['IMG_FULL']= "plugins/departments/images/department_alias.png";
617 #  $types['alias']['TITLE']   = _("Alias");
618 #  $types['alias']['TPL']     = "alias.tpl";
620 #  /* Referal
621 #   */
622 #  $types['referal']['ACL']     = "referal";
623 #  $types['referal']['CLASS']   = "referal";
624 #  $types['referal']['TAB']     = "REFERALTABS";
625 #  $types['referal']['ATTR']    = "???";
626 #  $types['referal']['OC']      = "???";
627 #  $types['referal']['IMG']     = "plugins/departments/images/department_referal.png";
628 #  $types['referal']['IMG_FULL']= "plugins/departments/images/department_referal.png";
629 #  $types['referal']['TITLE']   = _("Referal");
630 #  $types['referal']['TPL']     = "referal.tpl";
632     return($types);
633   }
635 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
636 ?>