Code

Fixed convert_department_dn.
[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
42   function departmentManagement (&$config, &$ui)
43   {
44     $this->ui= &$ui;
45     $this->dn= "";
46     $this->config= &$config;
47     $this->DivListDepartment = new divListDepartment($this->config,$this);
48   }
50   function execute()
51   {
52     global $config;
53   
54     /* Call parent execute */
55     plugin::execute();
57     /***************
58       Var init 
59      ***************/
61     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/"));
63     /* Reload departments */
64     $smarty                                             = get_smarty();
65     $display                                    = "";
66     $s_action                                   = "";  // Will contain an action, like del or edit
67     $s_entry                                    = "";  // The entry name for edit delete -...
70     /***************
71       Check posts  
72      ***************/
74     // Check Post action
75     foreach($_POST as $key => $val){
76       // Post for delete
77       if(preg_match("/dep_del.*/",$key)){
78         $s_action = "del";
79         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
80         $s_entry  = preg_replace("/_.*$/","",$s_entry);
81         $s_entry  = base64_decode($s_entry);
82         // Post for edit
83       }elseif(preg_match("/dep_edit_.*/",$key)){
84         $s_action="edit";
85         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
86         $s_entry  = preg_replace("/_.*$/","",$s_entry);
87         $s_entry  = base64_decode($s_entry);
88         // Post for new
89       }elseif(preg_match("/^remove_multiple_departments/",$key)){
90         $s_action="del_multiple";
91       }elseif(preg_match("/dep_new.*/",$key)){
92         $s_action="new";
93       }
94     }
96     /* Create options */
97     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
98       $s_action = "new";
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";
115       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
116       $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
117     }
120     /***************
121       Edit entry
122      ***************/
124     /* Edit Entry if Posted action (s_action) == edit 
125      * The entry which will be edited is defined in $s_entry
126      */
127     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
128       $this->dn= $this->config->departments[trim($s_entry)];
130       if (($user= get_lock($this->dn)) != ""){
131         return(gen_locked_message ($user, $this->dn));
132       }
134       /* Lock the current entry, so everyone will get the  above dialog */
135       add_lock ($this->dn, $this->ui->dn);
137       /* Register deptabs to trigger edit dialog */
138       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
139       $this->deptabs->set_acl_base($this->dn);
141       session::set('objectinfo',$this->dn);
142     }
145     /********************
146       Delete MULTIPLE entries requested, display confirm dialog
147      ********************/
149     if ($s_action=="del_multiple"){
150       $ids = $this->list_get_selected_items();
153       if(count($ids)){
154         foreach($ids as $id){
155           $id = base64_decode($id);
156           $dn = $this->config->departments[$id];
157   
158           if (($user= get_lock($dn)) != ""){
159             return(gen_locked_message ($user, $dn));
160           }
161           $this->dns[$id] = $dn;
162         }
164         $dns_names = array();
165         foreach($this->dns as $dn){
166           add_lock ($dn, $this->ui->dn);
167           $dns_names[] = @LDAP::fix($dn);
168         }
170         /* Lock the current entry, so nobody will edit it during deletion */
171         $smarty->assign("info", msgPool::deleteInfo($dns_names));
172         $smarty->assign("multiple", true);
173         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
174       }
175     }
176    
178     /********************
179       Delete MULTIPLE entries confirmed
180      ********************/
182     /* Confirmation for deletion has been passed. Users should be deleted. */
183     if (isset($_POST['delete_multiple_department_confirm'])){
185       /* Remove user by user and check acls before removeing them */
186       foreach($this->dns as $key => $dn){
187         $acl = $this->ui->get_permissions($dn,"department/department");
188         if (preg_match('/d/', $acl)){
190           /* Delete request is permitted, perform LDAP action */
191           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
192           $this->deptabs->set_acl_base();
193           $this->deptabs->delete ();
194           $this->deptabs = NULL;
195         } else {
196           msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
197         }
198         /* Remove lock file after successfull deletion */
199         del_lock ($dn);
200         unset($this->dns[$key]);
201       }
202     }
204  
205     /********************
206       Delete MULTIPLE entries Canceled
207      ********************/
209     /* Remove lock */
210     if(isset($_POST['delete_multiple_department_cancel'])){
211       foreach($this->dns as $key => $dn){
212         del_lock ($dn);
213         unset($this->dns[$key]);
214       }
215     }
218     /***************
219       Delete entry 
220      ***************/
222     /* Delete Entry if Posted action (s_action) == del 
223      * The entry which will be deleted is defined in $s_entry
224      */
225     if ($s_action =="del"){
226       $this->dn= $this->config->departments[trim($s_entry)];
228       /* check acls */
229       $acl = $this->ui->get_permissions($this->dn,"department/department");
230       if(preg_match("/d/",$acl)){
232         /* Check locking */
233         if (($user= get_lock($this->dn)) != ""){
234           session::set('dn',$this->dn);
235           return(gen_locked_message($user, $this->dn));
236         } else {
237           add_lock ($this->dn, $this->ui->dn);
238           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
239           $smarty->assign("multiple", false);
240           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
241           return ($display);
242         }
243       }else{
244         msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
245       }
246     }
249     /***************
250       Delete department confirmed  
251      ***************/
253     /* If department deletion is accepted ...
254      * Finally delete department 
255      */
256     if (isset($_POST['delete_department_confirm'])){
258       /* check acls */
259       $acl = $this->ui->get_permissions($this->dn,"department/department");
260       if(preg_match("/d/",$acl)){
261         $this->remove_from_parent();
262       } else {
263         msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
264       }
265     }
268     /***************
269       Handle tagging/recursive move (Return output for an iframe)
270      ***************/
272     /* initiate recursive remove  (Is called from iframe, generates output)*/
273     if(isset($_GET['PerformRecMove'])){
274       $this->deptabs->move_me();
275       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
276       exit();
277     }
279     /* This department must be tagged (Is called from iframe, generates output)*/
280     if(isset($_GET['TagDepartment'])){
281       $this->deptabs->by_object['department']->tag_objects();
282       exit();  
283     }
286     /***************
287       Edit department finished 
288      ***************/
290     if (is_object($this->deptabs) &&          // Ensure we have a valid deptab here 
291         (isset($_POST['edit_finish']) ||      // If 'Save' button is pressed in the edit dialog.
292          isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
293          $this->deptabs->move_done())){       // The move(rename) is done, we have to save the rest now.
295       /* Check tabs, will feed message array.
296          This call will also initiate a sav_object() call.
297           So don't move it below the moved check !.
298        */
299       $message= $this->deptabs->check();
300       $obj = $this->deptabs->by_object['department'];
302       /*************
303         MOVED ? 
304          Check if this department has to be moved 
305        *************/  
306       if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
307         return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
308       }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
309         $smarty = get_smarty();
310         $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove&no_output_compression");
311         $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."));
312         return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
313       }
315       /* Save, or display error message? */
316       if (count($message) == 0){
317         global $config;
319         $this->deptabs->save();
320         $config->get_departments();
321         $config->make_idepartments();
322         $this->config = $config;
324         /* This var indicated that there is an object which isn't saved right now. */
325         $this->ObjectInSaveMode = true;
327         /* This object must be tagged, so set ObjectTaggingRequested to true */
328         if($this->deptabs->by_object['department']->must_be_tagged()){
329           $smarty = get_smarty();
330           $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
331           $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."));
332           return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
333         }               
335       } else {
336         /* Ok. There seem to be errors regarding to the tab data,
337            show message and continue as usual. */
338         msg_dialog::displayChecks($message);
339       }
340     }
343    /***************
344      In case of tagging/moving the object wasn't deleted, do it know
345     ***************/
347    /* If there is an unsaved object and all operations are done
348       remove locks & save object tab & unset current object */
349    if($this->ObjectInSaveMode){
350      $this->config->get_departments();
351      $this->ObjectInSaveMode = false;
352      if ($this->dn != "new"){
353        del_lock ($this->dn);
354      }
355      unset ($this->deptabs);
356      $this->deptabs= NULL;
357      session::un_set('objectinfo');
358    }
361    /***************
362       Dialog canceled  
363      ***************/
365     /* User canceled edit oder delete
366      * Cancel dialog 
367      */
368     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
369       del_lock ($this->dn);
370       unset($this->depdabs);
371       $this->deptabs= NULL;
372       session::un_set('objectinfo');
373     }
375     /* Headpage or normal plugin screen? */
376     if ($this->deptabs !== NULL){
378       /* Show main page (tabs) */
379       $display= $this->deptabs->execute();
380       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
381         $display.= "<p style=\"text-align:right\">\n";
382         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
383         $display.= "&nbsp;\n";
384         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
385         $display.= "</p>";
386       }
387       return ($display);
388     }else{
389       /* Display dialog with group list */
390       $this->DivListDepartment->parent = $this;
391       $this->DivListDepartment->execute();
392       $this->reload();
393       $this->DivListDepartment->DepartmentsAdded = true;
394       $this->DivListDepartment->setEntries($this->departments);
395       return($this->DivListDepartment->Draw());
396     }
397   }
400   function reload()
401   {
402     /* Vairaible init */
403     $base         = $this->DivListDepartment->selectedBase;
404     $base_back    = preg_replace("/^[^,]+,/","",$base);
405     $Regex        = $this->DivListDepartment->Regex;
406   
407     // Create Array to Test if we have a valid back button
408     $config = session::get('config');
409     $tmp = $config->idepartments;
411     // In case of a valid back button create entry
412     if(isset($tmp[$base_back])){
413       $tmp2    ['dn']          = convert_department_dn($base_back);
415       // If empty always go to top
416       if(empty($tmp2['dn'])){
417         $tmp2['dn']="/";
418       }
419       $tmp2    ['description'][0] = ".. ".msgPool::backButton();
420       $result[$tmp[$base_back]]=$tmp2;
421     }
423     if($this->DivListDepartment->SubSearch){
424       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
425           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
426     }else{
427       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
428           "department", $base, array("ou", "description"), GL_SIZELIMIT );
429     }
431     $this->departments= array();
433     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
434     if(preg_match("/^ou=/",$base)){
435       $this->departments [ convert_department_dn($base) ] = ".";
436     }
438     foreach ($res as $key => $value){
440       /* Don't display base as entry on subsearch */
441       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
442         continue;
443       }
445       $cdn= convert_department_dn($value['dn']);
447       /* Append to dep list */
448       if(isset($value["description"][0])){
449         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
450       }else{
451         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
452       }
453     }
454     natcasesort ($this->departments);
455     reset ($this->departments);
456     print_a($this->departments) ;
457   }
459   function remove_from_parent()
460   {
461     $ldap= $this->config->get_ldap_link();
462     $ldap->cd ($this->dn);
463     $ldap->recursive_remove();
465     /* Optionally execute a command after we're done */
466     $this->postremove();
468     /* Delete references to object groups */
469     $ldap->cd ($this->config->current['BASE']);
470     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
471     while ($ldap->fetch()){
472       $og= new ogroup($this->config, $ldap->getDN());
473       unset($og->member[$this->dn]);
474       $og->save ();
475     }
477   }
480   function list_get_selected_items()
481   {
482     $ids = array();
483     foreach($_POST as $name => $value){
484       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
485         $id   = preg_replace("/^item_selected_/","",$name);
486         $ids[$id] = $id;
487       }
488     }
489     return($ids);
490   }
493   function remove_lock()
494   {
495     if (isset($this->dn)){
496       del_lock ($this->dn);
497     }
498   }
500   function save_object()
501   {
502     /* reload department */
503     $this->config->get_departments();
505     $this->config->make_idepartments();
506     $this->DivListDepartment->config= $this->config;
507     $this->DivListDepartment->save_object();
508   }
511 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
512 ?>