Code

Updated deparments
[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))){
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       $this->deptabs->by_object['department']->tag_objects();
328       exit();  
329     }
332     /***************
333       Edit department finished 
334      ***************/
336     if (is_object($this->deptabs) &&          // Ensure we have a valid deptab here 
337         (isset($_POST['edit_finish']) ||      // If 'Save' button is pressed in the edit dialog.
338          isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
339          $this->deptabs->move_done())){       // The move(rename) is done, we have to save the rest now.
341       /* Check tabs, will feed message array.
342          This call will also initiate a sav_object() call.
343           So don't move it below the moved check !.
344        */
345       $message= $this->deptabs->check();
347       /*************
348         MOVED ? 
349          Check if this department has to be moved 
350        *************/  
351       if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
352         return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
353       }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
354         $smarty = get_smarty();
355         $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove&no_output_compression");
356         $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."));
357         return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
358       }
360       /* Save, or display error message? */
361       if (count($message) == 0){
362         global $config;
364         $this->deptabs->save();
365         $config->get_departments();
366         $config->make_idepartments();
367         $this->config = $config;
369         /* This var indicated that there is an object which isn't saved right now. */
370         $this->ObjectInSaveMode = true;
372         /* This object must be tagged, so set ObjectTaggingRequested to true */
373         if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
374           $smarty = get_smarty();
375           $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
376           $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."));
377           return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
378         }               
380       } else {
381         /* Ok. There seem to be errors regarding to the tab data,
382            show message and continue as usual. */
383         msg_dialog::displayChecks($message);
384       }
385     }
388    /***************
389      In case of tagging/moving the object wasn't deleted, do it know
390     ***************/
392    /* If there is an unsaved object and all operations are done
393       remove locks & save object tab & unset current object */
394    if($this->ObjectInSaveMode){
395      $this->config->get_departments();
396      $this->ObjectInSaveMode = false;
397      if ($this->dn != "new"){
398        $this->remove_lock();
399      }
400      unset ($this->deptabs);
401      $this->deptabs= NULL;
402      session::un_set('objectinfo');
403    }
406    /***************
407       Dialog canceled  
408      ***************/
410     /* User canceled edit oder delete
411      * Cancel dialog 
412      */
413     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
414       $this->remove_lock();
415       $this->deptabs= NULL;
416       session::un_set('objectinfo');
417     }
419     /* Headpage or normal plugin screen? */
420     if ($this->deptabs !== NULL){
422       /* Show main page (tabs) */
423       $display= $this->deptabs->execute();
424       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
425         $display.= "<p style=\"text-align:right\">\n";
426         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
427         $display.= "&nbsp;\n";
428         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
429         $display.= "</p>";
430       }
431       return ($display);
432     }else{
433       /* Display dialog with group list */
434       $this->DivListDepartment->parent = $this;
435       $this->DivListDepartment->execute();
436       $this->reload();
437       $this->DivListDepartment->DepartmentsAdded = true;
438       $this->DivListDepartment->setEntries($this->departments);
439       return($this->DivListDepartment->Draw());
440     }
441   }
444   function reload()
445   {
446     /* Vairaible init */
447     $base         = $this->DivListDepartment->selectedBase;
448     $base_back    = preg_replace("/^[^,]+,/","",$base);
449     $Regex        = $this->DivListDepartment->Regex;
450   
451     // Create Array to Test if we have a valid back button
452     $config = session::get('config');
453     $tmp = $config->idepartments;
455     /* Possible department types 
456      */ 
457     $types['country']            = array("ACL" => "country",      "ATTR" => "c",
458         "CLS" => "(objectClass=country)",
459         "IMG" => "plugins/departments/images/country.png", "ABBR" => "c");
460     $types['locality']           = array("ACL" => "locality",     "ATTR" => "l",
461         "CLS" => "(objectClass=locality)",
462         "IMG" => "plugins/departments/images/country.png", "ABBR" => "l");
463     $types['organizationalUnit'] = array("ACL" => "department",   "ATTR" => "ou",
464         "CLS" => "(&(objectClass=gosaDepartment)(objectClass=organizationalUnit))",
465         "IMG" => "plugins/departments/images/country.png", "ABBR" => "ou");
466     $types['organization']       = array("ACL" => "organization", "ATTR" => "o",
467         "CLS" => "(objectClass=organization)",
468         "IMG" => "plugins/departments/images/country.png", "ABBR" => "o");
469     $types['Alias??']            = array("ACL" => "alias"     ,    "ATTR" => "alias?",
470         "CLS" => "(objectClass=alias??)",
471         "IMG" => "plugins/departments/images/country.png", "ABBR" => "alias");
472     $types['Referal??']          = array("ACL" => "referal",       "ATTR" => "referal?",
473         "CLS" => "(objectClass=referal??)",
474         "IMG" => "plugins/departments/images/country.png", "ABBR" => "referal");
476     /* Create search filter for each department type
477      */
478     $res = array();
479     if($this->DivListDepartment->SubSearch){
480       foreach($types as $oc => $data){
481         $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$data['CLS'].")",
482             "department", $base, array($data['ATTR'],"objectClass", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
483       }
484     }else{
485       foreach($types as $oc => $data){
486         $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$data['CLS'].")",
487             "department", $base, array($data['ATTR'], "objectClass","description"), GL_SIZELIMIT ));
488       }
489     }
491     /* Prepare list (sortable index) 
492      */
493     $deps = array();
494     $this->departments = array();
495     foreach($res as $attr){
497       /* Don't display base as entry on subsearch */
498       if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
499         continue;
500       }
502       /* Detect type and create sortable index 
503        */
504       $found =FALSE;
505       foreach($types as $oc => $data){
506         if(in_array($oc,$attr['objectClass'])){
507           $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
508           $deps[$name] = $attr;
509           $found =TRUE;
510           break;
511         }
512       }
513       if(!$found ) trigger_error("Not found!");
514     }
515     uksort($deps, 'strnatcasecmp');
517     /* In case of a valid back button create entry 
518      */
519     if(isset($tmp[$base])){
520       $ldap = $this->config->get_ldap_link();
521       $ldap->cd($this->config->current['BASE']);
522       $ldap->cat($base );
523       $attr = $ldap->fetch();
524       foreach($types as $oc => $type){
525         if(in_array($oc,$attr['objectClass'])){
526           $attr[$type['ATTR']][0] = ".";
527         }
528       }
529       if(isset($attr['description'])){
530          unset($attr['description']);
531       }
532       $deps= array_merge(array($attr),$deps);
533     }
535     $this->departments = array_values($deps);
536   }
538   function remove_from_parent()
539   {
540     $ldap= $this->config->get_ldap_link();
541     $ldap->cd ($this->dn);
542     $ldap->recursive_remove();
544     /* Optionally execute a command after we're done */
545     $this->postremove();
547     /* Delete references to object groups */
548     $ldap->cd ($this->config->current['BASE']);
549     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
550     while ($ldap->fetch()){
551       $og= new ogroup($this->config, $ldap->getDN());
552       unset($og->member[$this->dn]);
553       $og->save ();
554     }
556   }
559   function list_get_selected_items()
560   {
561     $ids = array();
562     foreach($_POST as $name => $value){
563       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
564         $id   = preg_replace("/^item_selected_/","",$name);
565         $ids[$id] = $id;
566       }
567     }
568     return($ids);
569   }
572   function remove_lock()
573   {
574     if (isset($this->dn)){
575       del_lock ($this->dn);
576     }
577     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
578       del_lock($this->dn);
579     }
580     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
581       del_lock($this->dns);
582     }
583   }
585   function save_object()
586   {
587     /* reload department */
588     $this->config->get_departments();
590     $this->config->make_idepartments();
591     $this->DivListDepartment->config= $this->config;
592     $this->DivListDepartment->save_object();
593   }
596 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
597 ?>