Code

Fixed tagging
[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 $ObjectTaggingRequested   = false;    // Object must be tagged, an iframe will be shown. 
42   var $RecursiveRemoveRequested = false;    // Is true, if this object must be moved, an iframe will be displayed in this case
44   function departmentManagement (&$config, &$ui)
45   {
46     $this->ui= &$ui;
47     $this->dn= "";
48     $this->config= &$config;
49     $this->DivListDepartment = new divListDepartment($this->config,$this);
50   }
52   function execute()
53   {
54     global $config;
55   
56     /* Call parent execute */
57     plugin::execute();
59     /***************
60       Var init 
61      ***************/
63     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/"));
65     /* Reload departments */
66     $smarty                                             = get_smarty();
67     $display                                    = "";
68     $s_action                                   = "";  // Will contain an action, like del or edit
69     $s_entry                                    = "";  // The entry name for edit delete -...
72     /***************
73       Check posts  
74      ***************/
76     // Check Post action
77     foreach($_POST as $key => $val){
78       // Post for delete
79       if(preg_match("/dep_del.*/",$key)){
80         $s_action = "del";
81         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
82         $s_entry  = preg_replace("/_.*$/","",$s_entry);
83         $s_entry  = base64_decode($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         $s_entry  = base64_decode($s_entry);
90         // Post for new
91       }elseif(preg_match("/^remove_multiple_departments/",$key)){
92         $s_action="del_multiple";
93       }elseif(preg_match("/dep_new.*/",$key)){
94         $s_action="new";
95       }
96     }
98     /* Create options */
99     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
100       $s_action = "new";
101     }
103     /* handle remove from layers menu */
104     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
105       $s_action = "del_multiple";
106     }
109     /***************
110       Create a new department
111      ***************/
113     /* New Entry if Posted action (s_action) == new
114      */
115     if ($s_action=="new"){
116       $this->dn= "new";
117       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
118       $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
119     }
122     /***************
123       Edit entry
124      ***************/
126     /* Edit Entry if Posted action (s_action) == edit 
127      * The entry which will be edited is defined in $s_entry
128      */
129     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
130       $this->dn= $this->config->departments[trim($s_entry)];
132       if (($user= get_lock($this->dn)) != ""){
133         return(gen_locked_message ($user, $this->dn));
134       }
136       /* Lock the current entry, so everyone will get the  above dialog */
137       add_lock ($this->dn, $this->ui->dn);
139       /* Register deptabs to trigger edit dialog */
140       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
141       $this->deptabs->set_acl_base($this->dn);
143       session::set('objectinfo',$this->dn);
144     }
147     /********************
148       Delete MULTIPLE entries requested, display confirm dialog
149      ********************/
151     if ($s_action=="del_multiple"){
152       $ids = $this->list_get_selected_items();
155       if(count($ids)){
156         foreach($ids as $id){
157           $id = base64_decode($id);
158           $dn = $this->config->departments[$id];
159   
160           if (($user= get_lock($dn)) != ""){
161             return(gen_locked_message ($user, $dn));
162           }
163           $this->dns[$id] = $dn;
164         }
166         $dns_names = "<br><pre>";
167         foreach($this->dns as $dn){
168           add_lock ($dn, $this->ui->dn);
169           $dns_names .= $dn."\n";
170         }
171         $dns_names .="</pre>";
173         /* Lock the current entry, so nobody will edit it during deletion */
174         if (count($this->dns) == 1){
175           $smarty->assign("info",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
176         } else {
177           $smarty->assign("info",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
178         }
179         $smarty->assign("multiple", true);
180         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
181       }
182     }
183    
185     /********************
186       Delete MULTIPLE entries confirmed
187      ********************/
189     /* Confirmation for deletion has been passed. Users should be deleted. */
190     if (isset($_POST['delete_multiple_department_confirm'])){
192       /* Remove user by user and check acls before removeing them */
193       foreach($this->dns as $key => $dn){
194         $acl = $this->ui->get_permissions($dn,"department/department");
195         if (preg_match('/d/', $acl)){
197           /* Delete request is permitted, perform LDAP action */
198           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
199           $this->deptabs->set_acl_base();
200           $this->deptabs->delete ();
201           $this->deptabs = NULL;
202         } else {
203           msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
204         }
205         /* Remove lock file after successfull deletion */
206         del_lock ($dn);
207         unset($this->dns[$key]);
208       }
209     }
211  
212     /********************
213       Delete MULTIPLE entries Canceled
214      ********************/
216     /* Remove lock */
217     if(isset($_POST['delete_multiple_department_cancel'])){
218       foreach($this->dns as $key => $dn){
219         del_lock ($dn);
220         unset($this->dns[$key]);
221       }
222     }
225     /***************
226       Delete entry 
227      ***************/
229     /* Delete Entry if Posted action (s_action) == del 
230      * The entry which will be deleted is defined in $s_entry
231      */
232     if ($s_action =="del"){
233       $this->dn= $this->config->departments[trim($s_entry)];
235       /* check acls */
236       $acl = $this->ui->get_permissions($this->dn,"department/department");
237       if(preg_match("/d/",$acl)){
239         /* Check locking */
240         if (($user= get_lock($this->dn)) != ""){
241           session::set('dn',$this->dn);
242           return(gen_locked_message($user, $this->dn));
243         } else {
244           add_lock ($this->dn, $this->ui->dn);
245           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
246           $smarty->assign("multiple", false);
247           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
248           return ($display);
249         }
250       }else{
251         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
252       }
253     }
256     /***************
257       Delete department confirmed  
258      ***************/
260     /* If department deletion is accepted ...
261      * Finally delete department 
262      */
263     if (isset($_POST['delete_department_confirm'])){
265       /* check acls */
266       $acl = $this->ui->get_permissions($this->dn,"department/department");
267       if(preg_match("/d/",$acl)){
268         $this->remove_from_parent();
269       } else {
270         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
271       }
272     }
275     /***************
276       Edit department finished 
277      ***************/
279     if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
280       $this->deptabs->save_object();
281       $obj = $this->deptabs->by_object['department'];
282       if($obj->orig_dn != "new"){
283         if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
284           return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
285         }
286       }
287     }
289     /* Save changes */
290     if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
292       /* Check tabs, will feed message array */
293       $message= $this->deptabs->check();
295       /* Save, or display error message? */
296       if (count($message) == 0){
297         global $config;
299         $this->deptabs->save(true);
300         $config->get_departments();
301         $config->make_idepartments();
302         $this->config = $config;
304         /* This object must be tagged, so set ObjectTaggingRequested to true */
305         $this->deptabs->by_object['department']->tag_objects(TRUE);
306         if($this->deptabs->by_object['department']->must_be_tagged()){
307           $this->ObjectTaggingRequested   = true; 
308         }               
310         /* Get recursive move is required, set RecursiveRemoveRequested to true */
311         if($this->deptabs->by_object['department']->am_i_moved()){      
312           $this->RecursiveRemoveRequested = true;
313         }               
314       
315         /* This var indicated that there is an object which isn't saved right now. */
316         $this->ObjectInSaveMode = true;
318       } else {
319         /* Ok. There seem to be errors regarding to the tab data,
320            show message and continue as usual. */
321         msg_dialog::displayChecks($message);
322       }
323     }
326     /***************
327       Handle Tagging (Return output for an iframe)
328      ***************/
330     /* This department must be tagged (Is called from iframe, generates output)*/
331     if(isset($_GET['TagDepartment'])){
332       $this->deptabs->by_object['department']->tag_objects();
333       exit();  
334     }
337     /***************
338       Handle recursive move (Return output for an iframe)
339      ***************/
341     /* initiate recursive remove  (Is called from iframe, generates output)*/
342     if(isset($_GET['PerformRecMove'])){
343       $this->deptabs->by_object['department']->recursive_move("","",true);
344       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
345       exit();
346     }
349     /***************
350       Return iframes, which call tagging / recusrsive move 
351      ***************/
353     /* While one of these vars below isset, we must return an iframe, 
354        to perform requested operation */
355     if($this->ObjectTaggingRequested){
356       $this->ObjectTaggingRequested = false;
357       return($this->deptabs->by_object['department']->ShowTagFrame());
358     }
359     if($this->RecursiveRemoveRequested){
360       $this->RecursiveRemoveRequested = false;
361       return($this->deptabs->by_object['department']->ShowMoveFrame());
362     }
365     /***************
366       In case of tagging/moving the object wasn't deleted, do it know
367      ***************/
369     /* If there is an unsaved object and all operations are done
370        remove locks & save object tab & unset current object */
371     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
372       $this->deptabs->save();
373       $this->config->get_departments();
374       $this->ObjectInSaveMode = false;
375       if ($this->dn != "new"){
376         del_lock ($this->dn);
377       }
378       unset ($this->deptabs);
379       $this->deptabs= NULL;
380       session::un_set('objectinfo');
381     }
384     /***************
385       Dialog canceled  
386      ***************/
388     /* User canceled edit oder delete
389      * Cancel dialog 
390      */
391     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
392       del_lock ($this->dn);
393       unset($this->depdabs);
394       $this->deptabs= NULL;
395       session::un_set('objectinfo');
396     }
398     /* Headpage or normal plugin screen? */
399     if ($this->deptabs !== NULL){
401       /* Show main page (tabs) */
402       $display= $this->deptabs->execute();
403       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
404         $display.= "<p style=\"text-align:right\">\n";
405         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
406         $display.= "&nbsp;\n";
407         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
408         $display.= "</p>";
409       }
410       return ($display);
411     }else{
412       /* Display dialog with group list */
413       $this->DivListDepartment->parent = $this;
414       $this->DivListDepartment->execute();
415       $this->reload();
416       $this->DivListDepartment->DepartmentsAdded = true;
417       $this->DivListDepartment->setEntries($this->departments);
418       return($this->DivListDepartment->Draw());
419     }
420   }
423   function reload()
424   {
425     /* Vairaible init */
426     $base         = $this->DivListDepartment->selectedBase;
427     $base_back    = preg_replace("/^[^,]+,/","",$base);
428     $Regex        = $this->DivListDepartment->Regex;
429   
430     // Create Array to Test if we have a valid back button
431     $config = session::get('config');
432     $tmp = $config->idepartments;
434     // In case of a valid back button create entry
435     if(isset($tmp[$base_back])){
436       $tmp2    ['dn']          = convert_department_dn($base_back);
438       // If empty always go to top
439       if(empty($tmp2['dn'])){
440         $tmp2['dn']="/";
441       }
442       $tmp2    ['description'][0] = ".. "._("Back");
443       $result[$tmp[$base_back]]=$tmp2;
444     }
446     if($this->DivListDepartment->SubSearch){
447       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
448           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
449     }else{
450       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
451           "department", $base, array("ou", "description"), GL_SIZELIMIT );
452     }
454     $this->departments= array();
456     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
457     if(preg_match("/^ou=/",$base)){
458       $this->departments [ convert_department_dn($base) ] = ".";
459     }
461     foreach ($res as $key => $value){
463       /* Don't display base as entry on subsearch */
464       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
465         continue;
466       }
468       $cdn= convert_department_dn($value['dn']);
470       /* Append to dep list */
471       if(isset($value["description"][0])){
472         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
473       }else{
474         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
475       }
476     }
477     natcasesort ($this->departments);
478     reset ($this->departments);
479   }
481   function remove_from_parent()
482   {
483     $ldap= $this->config->get_ldap_link();
484     $ldap->cd ($this->dn);
485     $ldap->recursive_remove();
487     /* Optionally execute a command after we're done */
488     $this->postremove();
490     /* Delete references to object groups */
491     $ldap->cd ($this->config->current['BASE']);
492     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
493     while ($ldap->fetch()){
494       $og= new ogroup($this->config, $ldap->getDN());
495       unset($og->member[$this->dn]);
496       $og->save ();
497     }
499   }
502   function list_get_selected_items()
503   {
504     $ids = array();
505     foreach($_POST as $name => $value){
506       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
507         $id   = preg_replace("/^item_selected_/","",$name);
508         $ids[$id] = $id;
509       }
510     }
511     return($ids);
512   }
515   function remove_lock()
516   {
517     if (isset($this->dn)){
518       del_lock ($this->dn);
519     }
520   }
522   function save_object()
523   {
524     /* reload department */
525     $this->config->get_departments();
527     $this->config->make_idepartments();
528     $this->DivListDepartment->config= $this->config;
529     $this->DivListDepartment->save_object();
530   }
533 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
534 ?>