Code

Updated comments.
[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 = "<br><pre>";
165         foreach($this->dns as $dn){
166           add_lock ($dn, $this->ui->dn);
167           $dns_names .= $dn."\n";
168         }
169         $dns_names .="</pre>";
171         /* Lock the current entry, so nobody will edit it during deletion */
172         if (count($this->dns) == 1){
173           $smarty->assign("info",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
174         } else {
175           $smarty->assign("info",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
176         }
177         $smarty->assign("multiple", true);
178         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
179       }
180     }
181    
183     /********************
184       Delete MULTIPLE entries confirmed
185      ********************/
187     /* Confirmation for deletion has been passed. Users should be deleted. */
188     if (isset($_POST['delete_multiple_department_confirm'])){
190       /* Remove user by user and check acls before removeing them */
191       foreach($this->dns as $key => $dn){
192         $acl = $this->ui->get_permissions($dn,"department/department");
193         if (preg_match('/d/', $acl)){
195           /* Delete request is permitted, perform LDAP action */
196           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
197           $this->deptabs->set_acl_base();
198           $this->deptabs->delete ();
199           $this->deptabs = NULL;
200         } else {
201           msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
202         }
203         /* Remove lock file after successfull deletion */
204         del_lock ($dn);
205         unset($this->dns[$key]);
206       }
207     }
209  
210     /********************
211       Delete MULTIPLE entries Canceled
212      ********************/
214     /* Remove lock */
215     if(isset($_POST['delete_multiple_department_cancel'])){
216       foreach($this->dns as $key => $dn){
217         del_lock ($dn);
218         unset($this->dns[$key]);
219       }
220     }
223     /***************
224       Delete entry 
225      ***************/
227     /* Delete Entry if Posted action (s_action) == del 
228      * The entry which will be deleted is defined in $s_entry
229      */
230     if ($s_action =="del"){
231       $this->dn= $this->config->departments[trim($s_entry)];
233       /* check acls */
234       $acl = $this->ui->get_permissions($this->dn,"department/department");
235       if(preg_match("/d/",$acl)){
237         /* Check locking */
238         if (($user= get_lock($this->dn)) != ""){
239           session::set('dn',$this->dn);
240           return(gen_locked_message($user, $this->dn));
241         } else {
242           add_lock ($this->dn, $this->ui->dn);
243           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
244           $smarty->assign("multiple", false);
245           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
246           return ($display);
247         }
248       }else{
249         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
250       }
251     }
254     /***************
255       Delete department confirmed  
256      ***************/
258     /* If department deletion is accepted ...
259      * Finally delete department 
260      */
261     if (isset($_POST['delete_department_confirm'])){
263       /* check acls */
264       $acl = $this->ui->get_permissions($this->dn,"department/department");
265       if(preg_match("/d/",$acl)){
266         $this->remove_from_parent();
267       } else {
268         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
269       }
270     }
273     /***************
274       Handle tagging/recursive move (Return output for an iframe)
275      ***************/
277     /* initiate recursive remove  (Is called from iframe, generates output)*/
278     if(isset($_GET['PerformRecMove'])){
279       $this->deptabs->move_me();
280       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
281       exit();
282     }
284     /* This department must be tagged (Is called from iframe, generates output)*/
285     if(isset($_GET['TagDepartment'])){
286       $this->deptabs->by_object['department']->tag_objects();
287       exit();  
288     }
291     /***************
292       Edit department finished 
293      ***************/
295     if (is_object($this->deptabs) &&          // Ensure we have a valid deptab here 
296         (isset($_POST['edit_finish']) ||      // If 'Save' button is pressed in the edit dialog.
297          isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
298          $this->deptabs->move_done())){       // The move(rename) is done, we have to save the rest now.
300       /* Check tabs, will feed message array.
301          This call will also initiate a sav_object() call.
302           So don't move it below the moved check !.
303        */
304       $message= $this->deptabs->check();
305       $obj = $this->deptabs->by_object['department'];
307       /*************
308         MOVED ? 
309          Check if this department has to be moved 
310        *************/  
311       if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
312         return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
313       }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
314         $smarty = get_smarty();
315         $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove&no_output_compression");
316         $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."));
317         return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
318       }
320       /* Save, or display error message? */
321       if (count($message) == 0){
322         global $config;
324         $this->deptabs->save();
325         $config->get_departments();
326         $config->make_idepartments();
327         $this->config = $config;
329         /* This var indicated that there is an object which isn't saved right now. */
330         $this->ObjectInSaveMode = true;
332         /* This object must be tagged, so set ObjectTaggingRequested to true */
333         if($this->deptabs->by_object['department']->must_be_tagged()){
334           $smarty = get_smarty();
335           $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
336           $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."));
337           return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
338         }               
340       } else {
341         /* Ok. There seem to be errors regarding to the tab data,
342            show message and continue as usual. */
343         msg_dialog::displayChecks($message);
344       }
345     }
348    /***************
349      In case of tagging/moving the object wasn't deleted, do it know
350     ***************/
352    /* If there is an unsaved object and all operations are done
353       remove locks & save object tab & unset current object */
354    if($this->ObjectInSaveMode){
355      $this->config->get_departments();
356      $this->ObjectInSaveMode = false;
357      if ($this->dn != "new"){
358        del_lock ($this->dn);
359      }
360      unset ($this->deptabs);
361      $this->deptabs= NULL;
362      session::un_set('objectinfo');
363    }
366    /***************
367       Dialog canceled  
368      ***************/
370     /* User canceled edit oder delete
371      * Cancel dialog 
372      */
373     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
374       del_lock ($this->dn);
375       unset($this->depdabs);
376       $this->deptabs= NULL;
377       session::un_set('objectinfo');
378     }
380     /* Headpage or normal plugin screen? */
381     if ($this->deptabs !== NULL){
383       /* Show main page (tabs) */
384       $display= $this->deptabs->execute();
385       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
386         $display.= "<p style=\"text-align:right\">\n";
387         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
388         $display.= "&nbsp;\n";
389         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
390         $display.= "</p>";
391       }
392       return ($display);
393     }else{
394       /* Display dialog with group list */
395       $this->DivListDepartment->parent = $this;
396       $this->DivListDepartment->execute();
397       $this->reload();
398       $this->DivListDepartment->DepartmentsAdded = true;
399       $this->DivListDepartment->setEntries($this->departments);
400       return($this->DivListDepartment->Draw());
401     }
402   }
405   function reload()
406   {
407     /* Vairaible init */
408     $base         = $this->DivListDepartment->selectedBase;
409     $base_back    = preg_replace("/^[^,]+,/","",$base);
410     $Regex        = $this->DivListDepartment->Regex;
411   
412     // Create Array to Test if we have a valid back button
413     $config = session::get('config');
414     $tmp = $config->idepartments;
416     // In case of a valid back button create entry
417     if(isset($tmp[$base_back])){
418       $tmp2    ['dn']          = convert_department_dn($base_back);
420       // If empty always go to top
421       if(empty($tmp2['dn'])){
422         $tmp2['dn']="/";
423       }
424       $tmp2    ['description'][0] = ".. "._("Back");
425       $result[$tmp[$base_back]]=$tmp2;
426     }
428     if($this->DivListDepartment->SubSearch){
429       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
430           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
431     }else{
432       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
433           "department", $base, array("ou", "description"), GL_SIZELIMIT );
434     }
436     $this->departments= array();
438     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
439     if(preg_match("/^ou=/",$base)){
440       $this->departments [ convert_department_dn($base) ] = ".";
441     }
443     foreach ($res as $key => $value){
445       /* Don't display base as entry on subsearch */
446       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
447         continue;
448       }
450       $cdn= convert_department_dn($value['dn']);
452       /* Append to dep list */
453       if(isset($value["description"][0])){
454         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
455       }else{
456         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
457       }
458     }
459     natcasesort ($this->departments);
460     reset ($this->departments);
461   }
463   function remove_from_parent()
464   {
465     $ldap= $this->config->get_ldap_link();
466     $ldap->cd ($this->dn);
467     $ldap->recursive_remove();
469     /* Optionally execute a command after we're done */
470     $this->postremove();
472     /* Delete references to object groups */
473     $ldap->cd ($this->config->current['BASE']);
474     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
475     while ($ldap->fetch()){
476       $og= new ogroup($this->config, $ldap->getDN());
477       unset($og->member[$this->dn]);
478       $og->save ();
479     }
481   }
484   function list_get_selected_items()
485   {
486     $ids = array();
487     foreach($_POST as $name => $value){
488       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
489         $id   = preg_replace("/^item_selected_/","",$name);
490         $ids[$id] = $id;
491       }
492     }
493     return($ids);
494   }
497   function remove_lock()
498   {
499     if (isset($this->dn)){
500       del_lock ($this->dn);
501     }
502   }
504   function save_object()
505   {
506     /* reload department */
507     $this->config->get_departments();
509     $this->config->make_idepartments();
510     $this->DivListDepartment->config= $this->config;
511     $this->DivListDepartment->save_object();
512   }
515 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
516 ?>