Code

Improved speed of department listings.
[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       $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));
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         session::set('objectinfo',$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       /* Remove user by user and check acls before removeing them */
229       foreach($this->dns as $key => $dn){
230         $acl = $this->ui->get_permissions($dn,"department/department");
231         if (preg_match('/d/', $acl)){
233           /* Delete request is permitted, perform LDAP action */
234           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
235           $this->deptabs->set_acl_base();
236           $this->deptabs->delete ();
237           $this->deptabs = NULL;
238         } else {
239           msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
240         }
241       }
243       /* Remove lock file after successfull deletion */
244       $this->remove_lock();
245       $this->dns = array();
246     }
248  
249     /********************
250       Delete MULTIPLE entries Canceled
251      ********************/
253     /* Remove lock */
254     if(isset($_POST['delete_multiple_department_cancel'])){
256       /* Remove lock file after successfull deletion */
257       $this->remove_lock();
258       $this->dns = array();
259     }
262     /***************
263       Handle tagging/recursive move (Return output for an iframe)
264      ***************/
266     /* initiate recursive remove  (Is called from iframe, generates output)*/
267     if(isset($_GET['PerformRecMove'])){
268       $this->deptabs->move_me();
269       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
270       exit();
271     }
273     /* This department must be tagged (Is called from iframe, generates output)*/
274     if(isset($_GET['TagDepartment'])){
275       $base_name = $this->deptabs->base_name;
276       $this->deptabs->by_object[$base_name]->tag_objects();
277       exit();  
278     }
281     /***************
282       Edit department finished 
283      ***************/
285     if (is_object($this->deptabs) &&          // Ensure we have a valid deptab here 
286         (isset($_POST['edit_finish']) ||      // If 'Save' button is pressed in the edit dialog.
287          isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
288          $this->deptabs->move_done())){       // The move(rename) is done, we have to save the rest now.
290       /* Check tabs, will feed message array.
291          This call will also initiate a sav_object() call.
292           So don't move it below the moved check !.
293        */
294       $message= $this->deptabs->check();
296       /*************
297         MOVED ? 
298          Check if this department has to be moved 
299        *************/  
300       if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
301         return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
302       }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
303         $smarty = get_smarty();
304         $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove&no_output_compression");
305         $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."));
306         return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
307       }
309       /* Save, or display error message? */
310       if (count($message) == 0){
311         global $config;
313         $this->deptabs->save();
314         $config->get_departments();
315         $config->make_idepartments();
316         $this->config = $config;
318         /* This var indicated that there is an object which isn't saved right now. */
319         $this->ObjectInSaveMode = true;
321         /* This object must be tagged, so set ObjectTaggingRequested to true */
322         if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
323           $smarty = get_smarty();
324           $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
325           $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."));
326           return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
327         }               
329       } else {
330         /* Ok. There seem to be errors regarding to the tab data,
331            show message and continue as usual. */
332         msg_dialog::displayChecks($message);
333       }
334     }
337    /***************
338      In case of tagging/moving the object wasn't deleted, do it know
339     ***************/
341    /* If there is an unsaved object and all operations are done
342       remove locks & save object tab & unset current object */
343    if($this->ObjectInSaveMode){
344      $this->config->get_departments();
345      $this->ObjectInSaveMode = false;
346      if ($this->dn != "new"){
347        $this->remove_lock();
348      }
349      unset ($this->deptabs);
350      $this->deptabs= NULL;
351      session::un_set('objectinfo');
352    }
355    /***************
356       Dialog canceled  
357      ***************/
359     /* User canceled edit oder delete
360      * Cancel dialog 
361      */
362     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
363       $this->remove_lock();
364       $this->deptabs= NULL;
365       session::un_set('objectinfo');
366     }
368     /* Headpage or normal plugin screen? */
369     if ($this->deptabs !== NULL){
371       /* Show main page (tabs) */
372       $display= $this->deptabs->execute();
373       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
374         $display.= "<p style=\"text-align:right\">\n";
375         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
376         $display.= "&nbsp;\n";
377         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
378         $display.= "</p>";
379       }
380       return ($display);
381     }else{
382       /* Display dialog with group list */
383       $this->DivListDepartment->parent = $this;
384       $this->DivListDepartment->execute();
385       $this->reload();
386       $this->DivListDepartment->DepartmentsAdded = true;
387       $this->DivListDepartment->setEntries($this->departments);
388       return($this->DivListDepartment->Draw());
389     }
390   }
393   function reload()
394   {
395     /* Vairaible init */
396     $base         = $this->DivListDepartment->selectedBase;
397     $base_back    = preg_replace("/^[^,]+,/","",$base);
398     $Regex        = $this->DivListDepartment->Regex;
399   
400     // Create Array to Test if we have a valid back button
401     $config = session::get('config');
402     $tmp = $config->idepartments;
404     $types = $this->get_support_departments();
406     /* Create search filter for each department type
407      */
409     /* Get all departments within this subtree */
410     $s_filter = "";
411     $s_attrs  = array("description","objectClass");
412     $s_acls   = array();
413     foreach($types as $oc => $data){
414       $s_filter.= "(&(objectClass=gosaDepartment)(objectClass=".$data['OC'].")".
415         "(|(".$data['ATTR']."=".$Regex.")(description=".$Regex.")))";
416       $s_attrs[]= $data['ATTR'];
417       $s_acls[] = "department/".$data['ACL'];
418     }
420     if($this->DivListDepartment->SubSearch){
421       $res = get_list("(|".$s_filter.")",$s_acls,$base,$s_attrs,GL_SIZELIMIT | GL_SUBSEARCH);
422     }else{
423       $res = get_list("(|".$s_filter.")",$s_acls,$base,$s_attrs,GL_SIZELIMIT);
424     }
426     /* Prepare list (sortable index) 
427      */
428     $deps = array();
429     $this->departments = array();
430     foreach($res as $attr){
432       /* Don't display base as entry on subsearch */
433       if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
434         continue;
435       }
437       /* Detect type and create sortable index 
438        */
439       $found =FALSE;
440       foreach($types as $oc => $data){
441         if(in_array($data['OC'],$attr['objectClass']) && isset($attr[$data['ATTR']][0])){
442           $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
443           $deps[$name] = $attr;
444           $found =TRUE;
445           break;
446         }
447       }
448     }
449     uksort($deps, 'strnatcasecmp');
451     /* In case of a valid back button create entry 
452      */
453     if(isset($tmp[$base])){
454       $ldap = $this->config->get_ldap_link();
455       $ldap->cd($this->config->current['BASE']);
456       $ldap->cat($base );
457       $attr = $ldap->fetch();
458       foreach($types as $oc => $type){
459         if(in_array($oc,$attr['objectClass'])){
460           $attr[$type['ATTR']][0] = ".";
461         }
462       }
463       if(isset($attr['description'])){
464          unset($attr['description']);
465       }
466       $deps= array_merge(array($attr),$deps);
467     }
469     $this->departments = array_values($deps);
470   }
472   function remove_from_parent()
473   {
474     $ldap= $this->config->get_ldap_link();
475     $ldap->cd ($this->dn);
476     $ldap->recursive_remove();
478     /* Optionally execute a command after we're done */
479     $this->postremove();
481     /* Delete references to object groups */
482     $ldap->cd ($this->config->current['BASE']);
483     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
484     while ($ldap->fetch()){
485       $og= new ogroup($this->config, $ldap->getDN());
486       unset($og->member[$this->dn]);
487       $og->save ();
488     }
490   }
493   function list_get_selected_items()
494   {
495     $ids = array();
496     foreach($_POST as $name => $value){
497       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
498         $id   = preg_replace("/^item_selected_/","",$name);
499         $ids[$id] = $id;
500       }
501     }
502     return($ids);
503   }
506   function remove_lock()
507   {
508     if (isset($this->dn)){
509       del_lock ($this->dn);
510     }
511     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
512       del_lock($this->dn);
513     }
514     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
515       del_lock($this->dns);
516     }
517   }
519   function save_object()
520   {
521     /* reload department */
522     $this->config->get_departments();
524     $this->config->make_idepartments();
525     $this->DivListDepartment->config= $this->config;
526     $this->DivListDepartment->save_object();
527   }
530   /*! \brief  Returns information about all container types that GOsa con handle.
531     @return Array   Informations about departments supported by GOsa.
532    */
533   public static function get_support_departments()
534   {
535     /* Domain component 
536      */
537     $types = array();
538     $types['dcObject']['ACL']     = "dcObject";
539     $types['dcObject']['CLASS']   = "dcObject";
540     $types['dcObject']['ATTR']    = "dc";
541     $types['dcObject']['TAB']     = "DCOBJECT_TABS";
542     $types['dcObject']['OC']      = "dcObject";
543     $types['dcObject']['IMG']     = "plugins/departments/images/dc.png";
544     $types['dcObject']['IMG_FULL']= "plugins/departments/images/dc.png";
545     $types['dcObject']['TITLE']   = _("Domain Component");
546     $types['dcObject']['TPL']     = "dcObject.tpl";
548     /* Country object
549      */
550     $types['country']['ACL']     = "country";
551     $types['country']['CLASS']   = "country";
552     $types['country']['TAB']     = "COUNTRY_TABS";
553     $types['country']['ATTR']    = "c";
554     $types['country']['OC']      = "country";
555     $types['country']['IMG']     = "plugins/departments/images/country.png";
556     $types['country']['IMG_FULL']= "plugins/departments/images/country.png";
557     $types['country']['TITLE']   = _("Country");
558     $types['country']['TPL']     = "country.tpl";
560     /* Locality object
561      */
562     $types['locality']['ACL']     = "locality";
563     $types['locality']['CLASS']   = "locality";
564     $types['locality']['TAB']     = "LOCALITY_TABS";
565     $types['locality']['ATTR']    = "l";
566     $types['locality']['OC']      = "locality";
567     $types['locality']['IMG']     = "plugins/departments/images/locality.png";
568     $types['locality']['IMG_FULL']= "plugins/departments/images/locality.png";
569     $types['locality']['TITLE']   = _("Locality");
570     $types['locality']['TPL']     = "locality.tpl";
572     /* Organization
573      */
574     $types['organization']['ACL']     = "organization";
575     $types['organization']['CLASS']   = "organization";
576     $types['organization']['TAB']     = "ORGANIZATION_TABS";
577     $types['organization']['ATTR']    = "o";
578     $types['organization']['OC']      = "organization";
579     $types['organization']['IMG']     = "plugins/departments/images/organization.png";
580     $types['organization']['IMG_FULL']= "plugins/departments/images/organization.png";
581     $types['organization']['TITLE']   = _("Organization");
582     $types['organization']['TPL']     = "organization.tpl";
584     /* Organization
585      */
586     $types['organizationalUnit']['ACL']     = "department";
587     $types['organizationalUnit']['CLASS']   = "department";
588     $types['organizationalUnit']['TAB']     = "DEPTABS";
589     $types['organizationalUnit']['ATTR']    = "ou";
590     $types['organizationalUnit']['OC']      = "organizationalUnit";
591     $types['organizationalUnit']['IMG']     = "images/lists/folder.png";//plugins/departments/images/department.png";
592     $types['organizationalUnit']['IMG_FULL']= "images/lists/folder-full.png";//:wplugins/departments/images/department.png";
593     $types['organizationalUnit']['TITLE']   = _("Department");
594     $types['organizationalUnit']['TPL']     = "generic.tpl";
597 #  /* Alias
598 #   */
599 #  $types['alias']['ACL']     = "alias";
600 #  $types['alias']['CLASS']   = "alias";
601 #  $types['alias']['TAB']     = "ALIASTABS";
602 #  $types['alias']['ATTR']    = "???";
603 #  $types['alias']['OC']      = "???";
604 #  $types['alias']['IMG']     = "plugins/departments/images/department_alias.png";
605 #  $types['alias']['IMG_FULL']= "plugins/departments/images/department_alias.png";
606 #  $types['alias']['TITLE']   = _("Alias");
607 #  $types['alias']['TPL']     = "alias.tpl";
609 #  /* Referal
610 #   */
611 #  $types['referal']['ACL']     = "referal";
612 #  $types['referal']['CLASS']   = "referal";
613 #  $types['referal']['TAB']     = "REFERALTABS";
614 #  $types['referal']['ATTR']    = "???";
615 #  $types['referal']['OC']      = "???";
616 #  $types['referal']['IMG']     = "plugins/departments/images/department_referal.png";
617 #  $types['referal']['IMG_FULL']= "plugins/departments/images/department_referal.png";
618 #  $types['referal']['TITLE']   = _("Referal");
619 #  $types['referal']['TPL']     = "referal.tpl";
621     return($types);
622   }
624 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
625 ?>