Code

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