Code

5d306e3c8837a1027314a0e38e02b39dbe34680c
[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       Handle recursive move (Return output for an iframe)
277      ***************/
280     /* initiate recursive remove  (Is called from iframe, generates output)*/
281     if(isset($_GET['PerformRecMove'])){
282       $this->deptabs->move_me();
283       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
284       exit();
285     }
288     /***************
289       Edit department finished 
290      ***************/
292     if (is_object($this->deptabs) && 
293           (isset($_POST['edit_finish']) || isset($_POST['dep_move_confirm']) || $this->deptabs->move_done())){
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 is 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         return($this->deptabs->by_object['department']->ShowMoveFrame());
310       }
312       if($this->deptabs->am_i_moved()){
313         echo "stop";
314         exit();
315       }
317       /* Save, or display error message? */
318       if (count($message) == 0){
319         global $config;
321         $this->deptabs->save();
322         $config->get_departments();
323         $config->make_idepartments();
324         $this->config = $config;
326         /* This object must be tagged, so set ObjectTaggingRequested to true */
327         if($this->deptabs->by_object['department']->must_be_tagged()){
328           $this->ObjectTaggingRequested   = true; 
329         }               
331         /* This var indicated that there is an object which isn't saved right now. */
332         $this->ObjectInSaveMode = true;
334       } else {
335         /* Ok. There seem to be errors regarding to the tab data,
336            show message and continue as usual. */
337         msg_dialog::displayChecks($message);
338       }
339     }
342     /***************
343       Handle Tagging (Return output for an iframe)
344      ***************/
346     /* This department must be tagged (Is called from iframe, generates output)*/
347     if(isset($_GET['TagDepartment'])){
348       $this->deptabs->by_object['department']->tag_objects();
349       exit();  
350     }
353     /***************
354       Return iframes, which call tagging / recusrsive move 
355      ***************/
357     /* While one of these vars below isset, we must return an iframe, 
358        to perform requested operation */
359     if($this->ObjectTaggingRequested){
360       $this->ObjectTaggingRequested = false;
361       return($this->deptabs->by_object['department']->ShowTagFrame());
362     }
363     if($this->RecursiveRemoveRequested){
364       $this->RecursiveRemoveRequested = false;
365       return($this->deptabs->by_object['department']->ShowMoveFrame());
366     }
369    /***************
370      In case of tagging/moving the object wasn't deleted, do it know
371     ***************/
373    /* If there is an unsaved object and all operations are done
374       remove locks & save object tab & unset current object */
375    if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
376      $this->config->get_departments();
377      $this->ObjectInSaveMode = false;
378      if ($this->dn != "new"){
379        del_lock ($this->dn);
380      }
381      unset ($this->deptabs);
382      $this->deptabs= NULL;
383      session::un_set('objectinfo');
384    }
387    /***************
388       Dialog canceled  
389      ***************/
391     /* User canceled edit oder delete
392      * Cancel dialog 
393      */
394     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
395       del_lock ($this->dn);
396       unset($this->depdabs);
397       $this->deptabs= NULL;
398       session::un_set('objectinfo');
399     }
401     /* Headpage or normal plugin screen? */
402     if ($this->deptabs !== NULL){
404       /* Show main page (tabs) */
405       $display= $this->deptabs->execute();
406       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
407         $display.= "<p style=\"text-align:right\">\n";
408         $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
409         $display.= "&nbsp;\n";
410         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
411         $display.= "</p>";
412       }
413       return ($display);
414     }else{
415       /* Display dialog with group list */
416       $this->DivListDepartment->parent = $this;
417       $this->DivListDepartment->execute();
418       $this->reload();
419       $this->DivListDepartment->DepartmentsAdded = true;
420       $this->DivListDepartment->setEntries($this->departments);
421       return($this->DivListDepartment->Draw());
422     }
423   }
426   function reload()
427   {
428     /* Vairaible init */
429     $base         = $this->DivListDepartment->selectedBase;
430     $base_back    = preg_replace("/^[^,]+,/","",$base);
431     $Regex        = $this->DivListDepartment->Regex;
432   
433     // Create Array to Test if we have a valid back button
434     $config = session::get('config');
435     $tmp = $config->idepartments;
437     // In case of a valid back button create entry
438     if(isset($tmp[$base_back])){
439       $tmp2    ['dn']          = convert_department_dn($base_back);
441       // If empty always go to top
442       if(empty($tmp2['dn'])){
443         $tmp2['dn']="/";
444       }
445       $tmp2    ['description'][0] = ".. "._("Back");
446       $result[$tmp[$base_back]]=$tmp2;
447     }
449     if($this->DivListDepartment->SubSearch){
450       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
451           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
452     }else{
453       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
454           "department", $base, array("ou", "description"), GL_SIZELIMIT );
455     }
457     $this->departments= array();
459     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
460     if(preg_match("/^ou=/",$base)){
461       $this->departments [ convert_department_dn($base) ] = ".";
462     }
464     foreach ($res as $key => $value){
466       /* Don't display base as entry on subsearch */
467       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
468         continue;
469       }
471       $cdn= convert_department_dn($value['dn']);
473       /* Append to dep list */
474       if(isset($value["description"][0])){
475         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
476       }else{
477         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
478       }
479     }
480     natcasesort ($this->departments);
481     reset ($this->departments);
482   }
484   function remove_from_parent()
485   {
486     $ldap= $this->config->get_ldap_link();
487     $ldap->cd ($this->dn);
488     $ldap->recursive_remove();
490     /* Optionally execute a command after we're done */
491     $this->postremove();
493     /* Delete references to object groups */
494     $ldap->cd ($this->config->current['BASE']);
495     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
496     while ($ldap->fetch()){
497       $og= new ogroup($this->config, $ldap->getDN());
498       unset($og->member[$this->dn]);
499       $og->save ();
500     }
502   }
505   function list_get_selected_items()
506   {
507     $ids = array();
508     foreach($_POST as $name => $value){
509       if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
510         $id   = preg_replace("/^item_selected_/","",$name);
511         $ids[$id] = $id;
512       }
513     }
514     return($ids);
515   }
518   function remove_lock()
519   {
520     if (isset($this->dn)){
521       del_lock ($this->dn);
522     }
523   }
525   function save_object()
526   {
527     /* reload department */
528     $this->config->get_departments();
530     $this->config->make_idepartments();
531     $this->DivListDepartment->config= $this->config;
532     $this->DivListDepartment->save_object();
533   }
536 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
537 ?>