Code

Updated department handling
[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       $objects['c'] ['ACL'] = "country";
117       $objects['c'] ['TAB'] = "COUNTRY_TABS";
118       $objects['ou']['ACL'] = "department";
119       $objects['ou']['TAB'] = "DEPTABS";
120       $objects['l'] ['ACL'] = "locatity";
121       $objects['l'] ['TAB'] = "LOCALITY_TABS";
122       $objects['o'] ['ACL'] = "organization";
123       $objects['o'] ['TAB'] = "ORGANIZATION_TABS";
125       if(isset($objects[$s_entry])){
126         $tab = $objects[$s_entry]['TAB'];
127         $acl = $objects[$s_entry]['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       /* Possible department types
146        */
147       $types['dcObject']           = array("ACL" => "",             "TAB" => "");
148       $types['country']            = array("ACL" => "country",      "TAB" => "COUNTRY_TABS");
149       $types['locality']           = array("ACL" => "locality",     "TAB" => "LOCALITY_TABS");
150       $types['organizationalUnit'] = array("ACL" => "department",   "TAB" => "DEPTABS");
151       $types['organization']       = array("ACL" => "organization", "TAB" => "ORGANIZATION_TABS");
152       $types['Alias??']            = array("ACL" => "alias"     ,   "TAB" => "");
153       $types['Referal??']          = array("ACL" => "referal",      "TAB" => "");
155       $this->dn= $this->departments[trim($s_entry)]['dn'];
156       $entry   = $this->departments[trim($s_entry)];
158       /* Detect department type 
159        */ 
160       $data = array();
161       foreach($types as $oc => $type){
162         if(in_array($oc,$entry['objectClass'])){
163           $data = $type;
164           break;
165         }
166       }
168       /* Check if the department type was editable 
169        */
170       if(!count($data)){
171         trigger_error("Unknown department type skipped '".$this->dn."'.");
172       }elseif(empty($data['TAB'])){
173         // Do nothing, this object is currently not editable
174       }else{
176         if (($user= get_lock($this->dn)) != ""){
177           return(gen_locked_message ($user, $this->dn));
178         }
180         /* Lock the current entry, so everyone will get the  above dialog */
181         add_lock ($this->dn, $this->ui->dn);
183         /* Register deptabs to trigger edit dialog */
184         $this->deptabs= new deptabs($this->config,$this->config->data['TABS'][$data['TAB']], $this->dn,"department");
185         $this->deptabs->set_acl_base($this->dn);
187         session::set('objectinfo',$this->dn);
188       }
189     }
192     /********************
193       Delete MULTIPLE entries requested, display confirm dialog
194      ********************/
196     if ($s_action=="del_multiple"){
197       $ids = $this->list_get_selected_items();
200       if(count($ids)){
201         $this->dns = array();
202         foreach($ids as $id){
203           $id = base64_decode($id);
204           $this->dns[$id] = $dn = $this->config->departments[$id];
205         }
207         /* Check locks */
208         if ($user= get_multiple_locks($this->dns)){
209           return(gen_locked_message($user,$this->dns));
210         }
212         $dns_names = array();
213         foreach($this->dns as $dn){
214           $dns_names[] = @LDAP::fix($dn);
215         }
216         add_lock ($this->dns, $this->ui->dn);
218         /* Lock the current entry, so nobody will edit it during deletion */
219         $smarty->assign("info", msgPool::deleteInfo($dns_names));
220         $smarty->assign("multiple", true);
221         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
222       }
223     }
224    
226     /********************
227       Delete MULTIPLE entries confirmed
228      ********************/
230     /* Confirmation for deletion has been passed. Users should be deleted. */
231     if (isset($_POST['delete_multiple_department_confirm'])){
233       /* Remove user by user and check acls before removeing them */
234       foreach($this->dns as $key => $dn){
235         $acl = $this->ui->get_permissions($dn,"department/department");
236         if (preg_match('/d/', $acl)){
238           /* Delete request is permitted, perform LDAP action */
239           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
240           $this->deptabs->set_acl_base();
241           $this->deptabs->delete ();
242           $this->deptabs = NULL;
243         } else {
244           msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
245         }
246       }
248       /* Remove lock file after successfull deletion */
249       $this->remove_lock();
250       $this->dns = array();
251     }
253  
254     /********************
255       Delete MULTIPLE entries Canceled
256      ********************/
258     /* Remove lock */
259     if(isset($_POST['delete_multiple_department_cancel'])){
261       /* Remove lock file after successfull deletion */
262       $this->remove_lock();
263       $this->dns = array();
264     }
267     /***************
268       Delete entry 
269      ***************/
271     /* Delete Entry if Posted action (s_action) == del 
272      * The entry which will be deleted is defined in $s_entry
273      */
274     if ($s_action =="del"){
275       $this->dn= $this->config->departments[trim($s_entry)];
277       /* check acls */
278       $acl = $this->ui->get_permissions($this->dn,"department/department");
279       if(preg_match("/d/",$acl)){
281         /* Check locking */
282         if (($user= get_lock($this->dn)) != ""){
283           session::set('dn',$this->dn);
284           return(gen_locked_message($user, $this->dn));
285         } else {
286           add_lock ($this->dn, $this->ui->dn);
287           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
288           $smarty->assign("multiple", false);
289           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
290           return ($display);
291         }
292       }else{
293         msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
294       }
295     }
298     /***************
299       Delete department confirmed  
300      ***************/
302     /* If department deletion is accepted ...
303      * Finally delete department 
304      */
305     if (isset($_POST['delete_department_confirm'])){
307       /* check acls */
308       $acl = $this->ui->get_permissions($this->dn,"department/department");
309       if(preg_match("/d/",$acl)){
310         $this->remove_from_parent();
311       } else {
312         msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
313       }
314     }
317     /***************
318       Handle tagging/recursive move (Return output for an iframe)
319      ***************/
321     /* initiate recursive remove  (Is called from iframe, generates output)*/
322     if(isset($_GET['PerformRecMove'])){
323       $this->deptabs->move_me();
324       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
325       exit();
326     }
328     /* This department must be tagged (Is called from iframe, generates output)*/
329     if(isset($_GET['TagDepartment'])){
330       $base_name = $this->deptabs->base_name;
331       $this->deptabs->by_object[$base_name]->tag_objects();
332       exit();  
333     }
336     /***************
337       Edit department finished 
338      ***************/
340     if (is_object($this->deptabs) &&          // Ensure we have a valid deptab here 
341         (isset($_POST['edit_finish']) ||      // If 'Save' button is pressed in the edit dialog.
342          isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
343          $this->deptabs->move_done())){       // The move(rename) is done, we have to save the rest now.
345       /* Check tabs, will feed message array.
346          This call will also initiate a sav_object() call.
347           So don't move it below the moved check !.
348        */
349       $message= $this->deptabs->check();
351       /*************
352         MOVED ? 
353          Check if this department has to be moved 
354        *************/  
355       if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
356         return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
357       }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
358         $smarty = get_smarty();
359         $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove&no_output_compression");
360         $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."));
361         return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
362       }
364       /* Save, or display error message? */
365       if (count($message) == 0){
366         global $config;
368         $this->deptabs->save();
369         $config->get_departments();
370         $config->make_idepartments();
371         $this->config = $config;
373         /* This var indicated that there is an object which isn't saved right now. */
374         $this->ObjectInSaveMode = true;
376         /* This object must be tagged, so set ObjectTaggingRequested to true */
377         if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
378           $smarty = get_smarty();
379           $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
380           $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."));
381           return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
382         }               
384       } else {
385         /* Ok. There seem to be errors regarding to the tab data,
386            show message and continue as usual. */
387         msg_dialog::displayChecks($message);
388       }
389     }
392    /***************
393      In case of tagging/moving the object wasn't deleted, do it know
394     ***************/
396    /* If there is an unsaved object and all operations are done
397       remove locks & save object tab & unset current object */
398    if($this->ObjectInSaveMode){
399      $this->config->get_departments();
400      $this->ObjectInSaveMode = false;
401      if ($this->dn != "new"){
402        $this->remove_lock();
403      }
404      unset ($this->deptabs);
405      $this->deptabs= NULL;
406      session::un_set('objectinfo');
407    }
410    /***************
411       Dialog canceled  
412      ***************/
414     /* User canceled edit oder delete
415      * Cancel dialog 
416      */
417     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
418       $this->remove_lock();
419       $this->deptabs= NULL;
420       session::un_set('objectinfo');
421     }
423     /* Headpage or normal plugin screen? */
424     if ($this->deptabs !== NULL){
426       /* Show main page (tabs) */
427       $display= $this->deptabs->execute();
428       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
429         $display.= "<p style=\"text-align:right\">\n";
430         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
431         $display.= "&nbsp;\n";
432         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
433         $display.= "</p>";
434       }
435       return ($display);
436     }else{
437       /* Display dialog with group list */
438       $this->DivListDepartment->parent = $this;
439       $this->DivListDepartment->execute();
440       $this->reload();
441       $this->DivListDepartment->DepartmentsAdded = true;
442       $this->DivListDepartment->setEntries($this->departments);
443       return($this->DivListDepartment->Draw());
444     }
445   }
448   function reload()
449   {
450     /* Vairaible init */
451     $base         = $this->DivListDepartment->selectedBase;
452     $base_back    = preg_replace("/^[^,]+,/","",$base);
453     $Regex        = $this->DivListDepartment->Regex;
454   
455     // Create Array to Test if we have a valid back button
456     $config = session::get('config');
457     $tmp = $config->idepartments;
459     /* Possible department types 
460      */ 
461     $types['dcObject']  = array("ACL" => "department",      "ATTR" => "dc",
462         "CLS" => "(objectClass=dcObject)",
463         "IMG"  => "plugins/departments/images/department.png");
465     $types['country']            = array("ACL" => "country",      "ATTR" => "c",
466         "CLS" => "(objectClass=country)",
467         "IMG" => "plugins/departments/images/country.png", "ABBR" => "c");
468     $types['locality']           = array("ACL" => "locality",     "ATTR" => "l",
469         "CLS" => "(objectClass=locality)",
470         "IMG" => "plugins/departments/images/country.png", "ABBR" => "l");
471     $types['organizationalUnit'] = array("ACL" => "department",   "ATTR" => "ou",
472         "CLS" => "(&(objectClass=gosaDepartment)(objectClass=organizationalUnit))",
473         "IMG" => "plugins/departments/images/country.png", "ABBR" => "ou");
474     $types['organization']       = array("ACL" => "organization", "ATTR" => "o",
475         "CLS" => "(objectClass=organization)",
476         "IMG" => "plugins/departments/images/country.png", "ABBR" => "o");
477     $types['Alias??']            = array("ACL" => "alias"     ,    "ATTR" => "alias?",
478         "CLS" => "(objectClass=alias??)",
479         "IMG" => "plugins/departments/images/country.png", "ABBR" => "alias");
480     $types['Referal??']          = array("ACL" => "referal",       "ATTR" => "referal?",
481         "CLS" => "(objectClass=referal??)",
482         "IMG" => "plugins/departments/images/country.png", "ABBR" => "referal");
485  
486  
487     /* Create search filter for each department type
488      */
489     $res = array();
490     if($this->DivListDepartment->SubSearch){
491       foreach($types as $oc => $data){
492         $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$data['CLS'].")",
493             "department", $base, array($data['ATTR'],"objectClass", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
494       }
495     }else{
496       foreach($types as $oc => $data){
497         $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$data['CLS'].")",
498             "department", $base, array($data['ATTR'], "objectClass","description"), GL_SIZELIMIT ));
499       }
500     }
502     /* Prepare list (sortable index) 
503      */
504     $deps = array();
505     $this->departments = array();
506     foreach($res as $attr){
508       /* Don't display base as entry on subsearch */
509       if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
510         continue;
511       }
513       /* Detect type and create sortable index 
514        */
515       $found =FALSE;
516       foreach($types as $oc => $data){
517         if(in_array($oc,$attr['objectClass'])){
518           $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
519           $deps[$name] = $attr;
520           $found =TRUE;
521           break;
522         }
523       }
524       if(!$found ) trigger_error("Not found!");
525     }
526     uksort($deps, 'strnatcasecmp');
528     /* In case of a valid back button create entry 
529      */
530     if(isset($tmp[$base])){
531       $ldap = $this->config->get_ldap_link();
532       $ldap->cd($this->config->current['BASE']);
533       $ldap->cat($base );
534       $attr = $ldap->fetch();
535       foreach($types as $oc => $type){
536         if(in_array($oc,$attr['objectClass'])){
537           $attr[$type['ATTR']][0] = ".";
538         }
539       }
540       if(isset($attr['description'])){
541          unset($attr['description']);
542       }
543       $deps= array_merge(array($attr),$deps);
544     }
546     $this->departments = array_values($deps);
547   }
549   function remove_from_parent()
550   {
551     $ldap= $this->config->get_ldap_link();
552     $ldap->cd ($this->dn);
553     $ldap->recursive_remove();
555     /* Optionally execute a command after we're done */
556     $this->postremove();
558     /* Delete references to object groups */
559     $ldap->cd ($this->config->current['BASE']);
560     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
561     while ($ldap->fetch()){
562       $og= new ogroup($this->config, $ldap->getDN());
563       unset($og->member[$this->dn]);
564       $og->save ();
565     }
567   }
570   function list_get_selected_items()
571   {
572     $ids = array();
573     foreach($_POST as $name => $value){
574       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
575         $id   = preg_replace("/^item_selected_/","",$name);
576         $ids[$id] = $id;
577       }
578     }
579     return($ids);
580   }
583   function remove_lock()
584   {
585     if (isset($this->dn)){
586       del_lock ($this->dn);
587     }
588     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
589       del_lock($this->dn);
590     }
591     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
592       del_lock($this->dns);
593     }
594   }
596   function save_object()
597   {
598     /* reload department */
599     $this->config->get_departments();
601     $this->config->make_idepartments();
602     $this->DivListDepartment->config= $this->config;
603     $this->DivListDepartment->save_object();
604   }
607 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
608 ?>