Code

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