Code

Enabled base selector
[gosa.git] / gosa-core / include / class_management.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: class_plugin.inc 14584 2009-10-12 14:04:22Z hickert $$
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 management
24 {
25   // Public 
26   public $config = null;
27   public $ui     = null;
29   public $plIcon        = "";
30   public $plDescription = "";
31   public $plHeadline    = "";
33   // Protected 
34   protected $dn = "";
35   protected $dns = array();
37   protected $tabClass = "";
38   protected $tabType = "";
39   protected $aclCategory = "";
40   protected $objectName = "";
41   protected $tabObject = null;
43   protected $displayApplyBtn = "";
44   protected $cpHandler = null;
45   protected $cpPastingStarted = FALSE;
46  
47   protected $snapHandler = null;
49   // Private
50   protected $plugname = "";
51   protected $headpage = null;
52   protected $filter = null;
53   protected $actions = array();
54   
55   function  __construct($config,$ui,$plugname, $headpage)
56   {
57     $this->plugname = $plugname;
58     $this->headpage = $headpage;
59     $this->ui = $ui;
60     $this->config = $config;
62     if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
63     if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
65     if(empty($this->plIcon)){
66       $this->plIcon = "plugins/".$plugname."/images/plugin.png";
67     }
68   }
70   function execute()
71   {
72     // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
73     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
74     session::set('LOCK_VARS_TO_USE',$vars);
76     /* Display the copy & paste dialog, if it is currently open */
77     $ret = $this->copyPasteHandler("",array());
78     if($ret){
79       return($this->getHeader().$ret);
80     }
82     // Handle actions (POSTs and GETs)
83     $str = $this->handleActions($this->detectPostActions());
84     if($str) return($this->getHeader().$str);
86     // Open single dialog objects
87     if(is_object($this->dialogObject)){
88       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
89       if(method_exists($this->dialogObject,'execute')){
90         $display = $this->dialogObject->execute(); 
91         $display.= $this->_getTabFooter();
92         return($this->getHeader().$display);
93       } 
94     }
96     // Display tab object.
97     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
98       $this->tabObject->save_object();
99       $display = $this->tabObject->execute();
100       $display.= $this->_getTabFooter();
101       return($this->getHeader().$display);
102     }
104     $this->headpage->update();
105     $display = $this->headpage->render();
106     return($this->getHeader().$display);
107   }
109   protected function getHeader()
110   {
111     if (get_object_info() != ""){
112       $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
113       "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
114         LDAP::fix(get_object_info()));
115     } else {
116       $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
117     }
118     return($display);
119   }
122   protected function _getTabFooter()
123   { 
124     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
125       return("");
126     }
127   
128     if($this->tabObject->by_object[$this->tabObject->current]){
129       $current = $this->tabObject->by_object[$this->tabObject->current];  
130       if(is_object($current->dialog)){
131         return("");
132       }
133     }
135     $str = "";
136     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
137       $str.= "<p style=\"text-align:right\">
138         <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
139         </p>";
140       return($str);
141     }else{
142       $str.= "<p style=\"text-align:right\">\n";
143       $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
144       $str.= "&nbsp;\n";
145       if($this->displayApplyBtn){
146         $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
147         $str.= "&nbsp;\n";
148       }
149       $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
150       $str.= "</p>";
151     }
152     return($str);
153   }
156   protected function removeEntryRequested($action,$entry,$all)
157   {
158     $disallowed = array();
159     $this->dns = array();
160     foreach($entry as $dn){
161       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
162       if(preg_match("/d/",$acl)){
163         $this->dns[] = $dn;
164       }else{
165         $disallowed[] = $dn;
166       }
167     }
169     if(count($disallowed)){
170       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
171     }
173     if(count($this->dns)){
175       /* Check locks */
176       if ($user= get_multiple_locks($this->dns)){
177         return(gen_locked_message($user,$this->dns));
178       }
180       $dns_names = array();
181       foreach($this->dns as $dn){
182         $dns_names[] =LDAP::fix($dn);
183       }
184       add_lock ($this->dns, $this->ui->dn);
186       /* Lock the current entry, so nobody will edit it during deletion */
187       $smarty = get_smarty();
188       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
189       $smarty->assign("multiple", true);
190       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
191     }
192   }  
195   protected function removeEntryConfirmed()
196   {
197     foreach($this->dns as $key => $dn){
199       /* Load permissions for selected 'dn' and check if
200          we're allowed to remove this 'dn' */
201       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
202       if(preg_match("/d/",$acl)){
204         /* Delete request is permitted, perform LDAP action */
205         $this->dn = $dn;
206         $tab = $this->tabClass;
207         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
208         $this->tabObject->set_acl_base($this->dn);
209         $this->tabObject->delete ();
210         del_lock($this->dn);        
211       } else {
213         /* Normally this shouldn't be reached, send some extra
214            logs to notify the administrator */
215         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
216         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
217       }
218     }
220     $this->remove_lock();
221     $this->closeDialogs();
222   }
225   function detectPostActions()
226   {
227     $action= $this->headpage->getAction();
229     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
230     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
231     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
232     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";    
234     return($action);
235   }
237   function handleActions($action)
238   {
239     // Start action  
240     if(isset($this->actions[$action['action']])){
241       $func = $this->actions[$action['action']];
242       if(!isset($action['targets']))$action['targets']= array(); 
243       return($this->$func($action['action'],$action['targets'],$action));
244     }
245   } 
247   function newEntry($action="",$target=array(),$all=array())
248   {
249     // Check locking & lock entry if required 
250     $this->displayApplyBtn = FALSE;
251     $this->dn = "new";
252     $this->is_new = TRUE;
253     $this->is_single_edit = FALSE;
254     $this->is_multiple_edit = FALSE;
256     set_object_info($this->dn);
258     // Open object.
259     if(empty($this->tabClass) || empty($this->tabType)){
260       // No tab type defined
261     }else{
262       $tab = $this->tabClass;
263       $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
264       $this->tabObject->set_acl_base($this->headpage->getBase());
265     }
266   }
268   function editEntry($action,$target,$all)
269   {
270     if(count($target) == 0){
271       //nothing 
272     }elseif(count($target) == 1){
274       // Check locking & lock entry if required 
275       $this->displayApplyBtn = TRUE;
277       $this->is_new = FALSE;
278       $this->is_single_edit = TRUE;
279       $this->is_multiple_edit = FALSE;
281       $this->dn = array_pop($target);
282       set_object_info($this->dn);
283       $user = get_lock($this->dn);
284       if ($user != ""){
285         return(gen_locked_message ($user, $this->dn,TRUE));
286       }
287       add_lock ($this->dn, $this->ui->dn);
289       // Open object.
290       if(empty($this->tabClass) || empty($this->tabType)){
291         // No tab type defined
292       }else{
293         $tab = $this->tabClass;
294         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn,$this->aclCategory);
295         $this->tabObject->set_acl_base($this->dn);
296       }
297     }else{
299       $this->is_new = FALSE;
300       $this->is_singel_edit = FALSE;
301       $this->is_multiple_edit = TRUE;
303       $this->dns = $target;
304       $tmp = new multi_plug($this->config,$this->tabClass,$this->config->data['TABS'][$this->tabType],
305             $this->dns,$this->headpage->getBase(),$this->aclCategory);
306       if ($tmp->entries_locked()){
307         return($tmp->display_lock_message());
308       }
309       $tmp->lock_entries($this->ui->dn);
310       if($tmp->multiple_available()){
311         $this->tabObject = $tmp;
312         set_object_info($this->tabObject->get_object_info());
313       }
315     }
316   }
318   protected function saveChanges()
319   {
320     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
321       $this->tabObject->save_object();
322       $msgs = $this->tabObject->check();
323       if(count($msgs)){
324         msg_dialog::displayChecks($msgs); 
325         return("");
326       }else{
327         $this->tabObject->save();
328         $this->remove_lock();
329         $this->closeDialogs();
330       }
331     }
332   }
334   protected function applyChanges()
335   {
336   if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
337     $this->tabObject->save_object();
338       $msgs = $this->tabObject->check();
339       if(count($msgs)){
340         msg_dialog::displayChecks($msgs); 
341         return("");
342       }else{
343         $this->tabObject->save();
344         $this->tabObject->re_init();
345       }
346     }
347   }
348   
349   protected function closeDialogs()
350   {
351     $this->dn = "";
352     $this->dns = array();
353     $this->tabObject = null;
354     $this->dialogObject = null;
355     set_object_info();
356   }
358   protected function cancelEdit()
359   {
360     $this->remove_lock();
361     $this->closeDialogs();
362   }
365  
367   function registerAction($action,$target)
368   {
369     $this->actions[$action] = $target;
370   }
372   function remove_lock()
373   {
374     if(!empty($this->dn) && $this->dn != "new"){
375       del_lock($this->dn);
376     }
377     if(count($this->dns)){
378       del_lock($this->dns);
379     }
380   }
382   function copyPasteHandler($s_action,$s_entry)
383   {
385     /* Check if Copy & Paste is disabled */
386     if(!is_object($this->cpHandler)){
387       return("");
388     }
390     $this->cpHandler->save_object();
392     /* Add entries to queue */
393     if($s_action == "copy" || $s_action == "cut"){
395       /* Cleanup object queue */
396       $this->cpHandler->cleanup_queue();
398       /* Add new entries to CP queue */
399       foreach($s_entry as $dn){
400         if($s_action == "copy" && $this->ui->is_copyable($dn,$this->aclCategory,$this->aclPlugin)){
401           $this->cpHandler->add_to_queue($dn,"copy",$this->tabClass,$this->tabType,$this->aclCategory);
402         }
403         if($s_action == "cut" && $this->ui->is_cutable($dn,$this->aclCategory,$this->aclPlugin)){
404           $this->cpHandler->add_to_queue($dn,"cut",$this->tabClass,$this->tabType,$this->aclCategory);
405         }
406       }
407     }
409     /* Start pasting entries */
410     if($s_action == "paste"){
411       $this->cpPastingStarted = TRUE;
412     }
414     /* Return C&P dialog */
415     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
417       /* Get dialog */
418       $this->cpHandler->SetVar("base",$this->headpage->getBase());
419       $data = $this->cpHandler->execute();
421       /* Return dialog data */
422       if(!empty($data)){
423         return($data);
424       }
425     }
427     /* Automatically disable status for pasting */
428     if(!$this->cpHandler->entries_queued()){
429       $this->cpPastingStarted = FALSE;
430     }
431     return("");
432   }
435   function setDescription($str) {
436     $this->plDescription = $str;
437   } 
438   function setHeadpage($str) {
439     $this->headpage = $str;
440   } 
441   function setFilter($str) {
442     $this->filter = $str;
443   } 
444   function setIcon($str) {
445     $this->plIcon = $str;
446   } 
447   function setHeadline($str) {
448     $this->plHeadline = $str;
449   } 
452 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
453 ?>