Code

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