Code

gosa-core is now free of print_red
[gosa.git] / gosa-core / plugins / admin / departments / class_departmentManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class departmentManagement extends plugin
22 {
23   /* Definitions */
24   var $plHeadline= "Departments";
25   var $plDescription= "This does something";
27   /* CLI vars */
28   var $cli_summary= "Handling of LDAP subtrees";
29   var $cli_description= "Some longer text\nfor help";
30   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
32   /* Headpage attributes */
33   var $last_dep_sorting= "invalid";
34   var $departments= array();
35   var $deptabs= NULL;
37   /* attribute list for save action */
38   var $attributes= array();
39   var $objectclasses= array();
41   /* Vars to handle operations after saving the department 
42      Recursive move && tagging   */
43   var $ObjectInSaveMode         = false;    // Is true, if current object wasn't saved right now
44   var $ObjectTaggingRequested   = false;    // Object must be tagged, an iframe will be shown. 
45   var $RecursiveRemoveRequested = false;    // Is true, if this object must be moved, an iframe will be displayed in this case
47   function departmentManagement (&$config, &$ui)
48   {
49     $this->ui= &$ui;
50     $this->dn= "";
51     $this->config= &$config;
52     $this->DivListDepartment = new divListDepartment($this->config,$this);
53   }
55   function execute()
56   {
57     global $config;
58   
59     /* Call parent execute */
60     plugin::execute();
62     /***************
63       Var init 
64      ***************/
66     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/"));
68     /* Reload departments */
69     $smarty                                             = get_smarty();
70     $display                                    = "";
71     $s_action                                   = "";  // Will contain an action, like del or edit
72     $s_entry                                    = "";  // The entry name for edit delete -...
75     /***************
76       Check posts  
77      ***************/
79     // Check Post action
80     foreach($_POST as $key => $val){
81       // Post for delete
82       if(preg_match("/dep_del.*/",$key)){
83         $s_action = "del";
84         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
85         $s_entry  = preg_replace("/_.*$/","",$s_entry);
86         $s_entry  = base64_decode($s_entry);
87         // Post for edit
88       }elseif(preg_match("/dep_edit_.*/",$key)){
89         $s_action="edit";
90         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
91         $s_entry  = preg_replace("/_.*$/","",$s_entry);
92         $s_entry  = base64_decode($s_entry);
93         // Post for new
94       }elseif(preg_match("/^remove_multiple_departments/",$key)){
95         $s_action="del_multiple";
96       }elseif(preg_match("/dep_new.*/",$key)){
97         $s_action="new";
98       }
99     }
101     /* Create options */
102     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
103       $s_action = "new";
104     }
106     /* handle remove from layers menu */
107     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
108       $s_action = "del_multiple";
109     }
112     /***************
113       Create a new department
114      ***************/
116     /* New Entry if Posted action (s_action) == new
117      */
118     if ($s_action=="new"){
119       $this->dn= "new";
120       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
121       $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
122     }
125     /***************
126       Edit entry
127      ***************/
129     /* Edit Entry if Posted action (s_action) == edit 
130      * The entry which will be edited is defined in $s_entry
131      */
132     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
133       $this->dn= $this->config->departments[trim($s_entry)];
135       if (($user= get_lock($this->dn)) != ""){
136         return(gen_locked_message ($user, $this->dn));
137       }
139       /* Lock the current entry, so everyone will get the  above dialog */
140       add_lock ($this->dn, $this->ui->dn);
142       /* Register deptabs to trigger edit dialog */
143       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
144       $this->deptabs->set_acl_base($this->dn);
146       session::set('objectinfo',$this->dn);
147     }
150     /********************
151       Delete MULTIPLE entries requested, display confirm dialog
152      ********************/
154     if ($s_action=="del_multiple"){
155       $ids = $this->list_get_selected_items();
158       if(count($ids)){
159         foreach($ids as $id){
160           $id = base64_decode($id);
161           $dn = $this->config->departments[$id];
162   
163           if (($user= get_lock($dn)) != ""){
164             return(gen_locked_message ($user, $dn));
165           }
166           $this->dns[$id] = $dn;
167         }
169         $dns_names = "<br><pre>";
170         foreach($this->dns as $dn){
171           add_lock ($dn, $this->ui->dn);
172           $dns_names .= $dn."\n";
173         }
174         $dns_names .="</pre>";
176         /* Lock the current entry, so nobody will edit it during deletion */
177         if (count($this->dns) == 1){
178           $smarty->assign("info",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
179         } else {
180           $smarty->assign("info",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
181         }
182         $smarty->assign("multiple", true);
183         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
184       }
185     }
186    
188     /********************
189       Delete MULTIPLE entries confirmed
190      ********************/
192     /* Confirmation for deletion has been passed. Users should be deleted. */
193     if (isset($_POST['delete_multiple_department_confirm'])){
195       /* Remove user by user and check acls before removeing them */
196       foreach($this->dns as $key => $dn){
197         $acl = $this->ui->get_permissions($dn,"department/department");
198         if (preg_match('/d/', $acl)){
200           /* Delete request is permitted, perform LDAP action */
201           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
202           $this->deptabs->set_acl_base();
203           $this->deptabs->delete ();
204           $this->deptabs = NULL;
205         } else {
206           msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
207         }
208         /* Remove lock file after successfull deletion */
209         del_lock ($dn);
210         unset($this->dns[$key]);
211       }
212     }
214  
215     /********************
216       Delete MULTIPLE entries Canceled
217      ********************/
219     /* Remove lock */
220     if(isset($_POST['delete_multiple_department_cancel'])){
221       foreach($this->dns as $key => $dn){
222         del_lock ($dn);
223         unset($this->dns[$key]);
224       }
225     }
228     /***************
229       Delete entry 
230      ***************/
232     /* Delete Entry if Posted action (s_action) == del 
233      * The entry which will be deleted is defined in $s_entry
234      */
235     if ($s_action =="del"){
236       $this->dn= $this->config->departments[trim($s_entry)];
238       /* check acls */
239       $acl = $this->ui->get_permissions($this->dn,"department/department");
240       if(preg_match("/d/",$acl)){
242         /* Check locking */
243         if (($user= get_lock($this->dn)) != ""){
244           session::set('dn',$this->dn);
245           return(gen_locked_message($user, $this->dn));
246         } else {
247           add_lock ($this->dn, $this->ui->dn);
248           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
249           $smarty->assign("multiple", false);
250           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
251           return ($display);
252         }
253       }else{
254         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
255       }
256     }
259     /***************
260       Delete department confirmed  
261      ***************/
263     /* If department deletion is accepted ...
264      * Finally delete department 
265      */
266     if (isset($_POST['delete_department_confirm'])){
268       /* check acls */
269       $acl = $this->ui->get_permissions($this->dn,"department/department");
270       if(preg_match("/d/",$acl)){
271         $this->remove_from_parent();
272       } else {
273         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
274       }
275     }
278     /***************
279       Edit department finished 
280      ***************/
282     if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
283       $this->deptabs->save_object();
284       $obj = $this->deptabs->by_object['department'];
285       if($obj->orig_dn != "new"){
286         if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
287           return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
288         }
289       }
290     }
292     /* Save changes */
293     if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
295       /* Check tabs, will feed message array */
296       $message= $this->deptabs->check();
298       /* Save, or display error message? */
299       if (count($message) == 0){
301         $this->deptabs->save(true);
302         global $config;
303         $config->get_departments();
304         $config->make_idepartments();
305         $this->config = $config;
307         /* This object must be tagged, so set ObjectTaggingRequested to true */
308         if($this->deptabs->by_object['department']->must_be_tagged()){
309           $this->ObjectTaggingRequested   = true; 
310         }               
312         /* Get recursive move is required, set RecursiveRemoveRequested to true */
313         if($this->deptabs->by_object['department']->am_i_moved()){      
314           $this->RecursiveRemoveRequested = true;
315         }               
316       
317         /* This var indicated that there is an object which isn't saved right now. */
318         $this->ObjectInSaveMode = true;
320       } else {
321         /* Ok. There seem to be errors regarding to the tab data,
322            show message and continue as usual. */
323         show_errors($message);
324       }
325     }
328     /***************
329       Handle Tagging (Return output for an iframe)
330      ***************/
332     /* This department must be tagged (Is called from iframe, generates output)*/
333     if(isset($_GET['TagDepartment'])){
334       $this->deptabs->by_object['department']->tag_objects();
335       exit();  
336     }
339     /***************
340       Handle recursive move (Return output for an iframe)
341      ***************/
343     /* initiate recursive remove  (Is called from iframe, generates output)*/
344     if(isset($_GET['PerformRecMove'])){
345       $this->deptabs->by_object['department']->recursive_move("","",true);
346       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
347       exit();
348     }
351     /***************
352       Return iframes, which call tagging / recusrsive move 
353      ***************/
355     /* While one of these vars below isset, we must return an iframe, 
356        to perform requested operation */
357     if($this->ObjectTaggingRequested){
358       $this->ObjectTaggingRequested = false;
359       return($this->deptabs->by_object['department']->ShowTagFrame());
360     }
361     if($this->RecursiveRemoveRequested){
362       $this->RecursiveRemoveRequested = false;
363       return($this->deptabs->by_object['department']->ShowMoveFrame());
364     }
367     /***************
368       In case of tagging/moving the object wasn't deleted, do it know
369      ***************/
371     /* If there is an unsaved object and all operations are done
372        remove locks & save object tab & unset current object */
373     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
374       $this->deptabs->save();
375       $this->config->get_departments();
376       $this->ObjectInSaveMode = false;
377       if ($this->dn != "new"){
378         del_lock ($this->dn);
379       }
380       unset ($this->deptabs);
381       $this->deptabs= NULL;
382       session::un_set('objectinfo');
383     }
386     /***************
387       Dialog canceled  
388      ***************/
390     /* User canceled edit oder delete
391      * Cancel dialog 
392      */
393     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
394       del_lock ($this->dn);
395       unset($this->depdabs);
396       $this->deptabs= NULL;
397       session::un_set('objectinfo');
398     }
400     /* Headpage or normal plugin screen? */
401     if ($this->deptabs !== NULL){
403       /* Show main page (tabs) */
404       $display= $this->deptabs->execute();
405       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
406         $display.= "<p style=\"text-align:right\">\n";
407         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
408         $display.= "&nbsp;\n";
409         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
410         $display.= "</p>";
411       }
412       return ($display);
413     }else{
414       /* Display dialog with group list */
415       $this->DivListDepartment->parent = $this;
416       $this->DivListDepartment->execute();
417       $this->reload();
418       $this->DivListDepartment->DepartmentsAdded = true;
419       $this->DivListDepartment->setEntries($this->departments);
420       return($this->DivListDepartment->Draw());
421     }
422   }
425   function reload()
426   {
427     /* Vairaible init */
428     $base         = $this->DivListDepartment->selectedBase;
429     $base_back    = preg_replace("/^[^,]+,/","",$base);
430     $Regex        = $this->DivListDepartment->Regex;
431   
432     // Create Array to Test if we have a valid back button
433     $config = session::get('config');
434     $tmp = $config->idepartments;
436     // In case of a valid back button create entry
437     if(isset($tmp[$base_back])){
438       $tmp2    ['dn']          = convert_department_dn($base_back);
440       // If empty always go to top
441       if(empty($tmp2['dn'])){
442         $tmp2['dn']="/";
443       }
444       $tmp2    ['description'][0] = ".. "._("Back");
445       $result[$tmp[$base_back]]=$tmp2;
446     }
448     if($this->DivListDepartment->SubSearch){
449       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
450           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
451     }else{
452       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
453           "department", $base, array("ou", "description"), GL_SIZELIMIT );
454     }
456     $this->departments= array();
458     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
459     if(preg_match("/^ou=/",$base)){
460       $this->departments [ convert_department_dn($base) ] = ".";
461     }
463     foreach ($res as $key => $value){
465       /* Don't display base as entry on subsearch */
466       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
467         continue;
468       }
470       $cdn= convert_department_dn($value['dn']);
472       /* Append to dep list */
473       if(isset($value["description"][0])){
474         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
475       }else{
476         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
477       }
478     }
479     natcasesort ($this->departments);
480     reset ($this->departments);
481   }
483   function remove_from_parent()
484   {
485     $ldap= $this->config->get_ldap_link();
486     $ldap->cd ($this->dn);
487     $ldap->recursive_remove();
489     /* Optionally execute a command after we're done */
490     $this->postremove();
492     /* Delete references to object groups */
493     $ldap->cd ($this->config->current['BASE']);
494     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
495     while ($ldap->fetch()){
496       $og= new ogroup($this->config, $ldap->getDN());
497       unset($og->member[$this->dn]);
498       $og->save ();
499     }
501   }
504   function list_get_selected_items()
505   {
506     $ids = array();
507     foreach($_POST as $name => $value){
508       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
509         $id   = preg_replace("/^item_selected_/","",$name);
510         $ids[$id] = $id;
511       }
512     }
513     return($ids);
514   }
517   function remove_lock()
518   {
519     if (isset($this->dn)){
520       del_lock ($this->dn);
521     }
522   }
524   function save_object()
525   {
526     /* reload department */
527     $this->config->get_departments();
529     $this->config->make_idepartments();
530     $this->DivListDepartment->config= $this->config;
531     $this->DivListDepartment->save_object();
532   }
535 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
536 ?>