Code

5d6ea8ed4b28919f62c97d083f6fec479d5cd235
[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 $last_dn = "";
38   protected $last_dns = array();
40   protected $tabClass = "";
41   protected $tabType = "";
42   protected $aclCategory = "";
43   protected $objectName = "";
44   protected $tabObject = null;
45   protected $dialogObject = null;
47   protected $last_tabObject = null;
48   protected $last_dialogObject = null;
50   protected $displayApplyBtn = "";
51   protected $cpHandler = null;
52   protected $cpPastingStarted = FALSE;
53  
54   protected $snapHandler = null;
56   // Private
57   protected $plugname = "";
58   protected $headpage = null;
59   protected $filter = null;
60   protected $actions = array();
61   
62   function  __construct($config,$ui,$plugname, $headpage)
63   {
64     $this->plugname = $plugname;
65     $this->headpage = $headpage;
66     $this->ui = $ui;
67     $this->config = $config;
69     if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
70     if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
72     if(empty($this->plIcon)){
73       $this->plIcon = "plugins/".$plugname."/images/plugin.png";
74     }
75   }
77   function execute()
78   {
79     // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
80     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
81     session::set('LOCK_VARS_TO_USE',$vars);
83     /* Display the copy & paste dialog, if it is currently open */
84     $ret = $this->copyPasteHandler("",array());
85     if($ret){
86       return($this->getHeader().$ret);
87     }
89     // Update filter
90     if ($this->filter) {
91       $this->filter->update();
92       session::set('autocomplete', $this->filter);
93       if (!$this->filter->isValid()){
94         msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
95       }
96     }
98     // Handle actions (POSTs and GETs)
99     $str = $this->handleActions($this->detectPostActions());
100     if($str) return($this->getHeader().$str);
102     // Open single dialog objects
103     if(is_object($this->dialogObject)){
104       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
105       if(method_exists($this->dialogObject,'execute')){
106         $display = $this->dialogObject->execute(); 
107         $display.= $this->_getTabFooter();
108         return($this->getHeader().$display);
109       } 
110     }
112     // Display tab object.
113     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
114       $this->tabObject->save_object();
115       $display = $this->tabObject->execute();
116       $display.= $this->_getTabFooter();
117       return($this->getHeader().$display);
118     }
120     $this->headpage->update();
121     $display = $this->headpage->render();
122     return($this->getHeader().$display);
123   }
125   protected function getHeader()
126   {
127     if (get_object_info() != ""){
128       $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
129       "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
130         LDAP::fix(get_object_info()));
131     } else {
132       $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
133     }
134     return($display);
135   }
138   protected function _getTabFooter()
139   { 
140     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
141       return("");
142     }
143   
144     if($this->tabObject->by_object[$this->tabObject->current]){
145       $current = $this->tabObject->by_object[$this->tabObject->current];  
146       if(is_object($current->dialog)){
147         return("");
148       }
149     }
151     $str = "";
152     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
153       $str.= "<p style=\"text-align:right\">
154         <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
155         </p>";
156       return($str);
157     }else{
158       $str.= "<p style=\"text-align:right\">\n";
159       $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
160       $str.= "&nbsp;\n";
161       if($this->displayApplyBtn){
162         $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
163         $str.= "&nbsp;\n";
164       }
165       $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
166       $str.= "</p>";
167     }
168     return($str);
169   }
172   protected function removeEntryRequested($action,$entry,$all)
173   {
174     $disallowed = array();
175     $this->dns = array();
176     foreach($entry as $dn){
177       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
178       if(preg_match("/d/",$acl)){
179         $this->dns[] = $dn;
180       }else{
181         $disallowed[] = $dn;
182       }
183     }
185     if(count($disallowed)){
186       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
187     }
189     if(count($this->dns)){
191       /* Check locks */
192       if ($user= get_multiple_locks($this->dns)){
193         return(gen_locked_message($user,$this->dns));
194       }
196       $dns_names = array();
197       foreach($this->dns as $dn){
198         $dns_names[] =LDAP::fix($dn);
199       }
200       add_lock ($this->dns, $this->ui->dn);
202       /* Lock the current entry, so nobody will edit it during deletion */
203       $smarty = get_smarty();
204       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
205       $smarty->assign("multiple", true);
206       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
207     }
208   }  
211   protected function removeEntryConfirmed()
212   {
213     foreach($this->dns as $key => $dn){
215       /* Load permissions for selected 'dn' and check if
216          we're allowed to remove this 'dn' */
217       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
218       if(preg_match("/d/",$acl)){
220         /* Delete request is permitted, perform LDAP action */
221         $this->dn = $dn;
222         $tab = $this->tabClass;
223         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
224         $this->tabObject->set_acl_base($this->dn);
225         $this->tabObject->delete ();
226         del_lock($this->dn);        
227       } else {
229         /* Normally this shouldn't be reached, send some extra
230            logs to notify the administrator */
231         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
232         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
233       }
234     }
236     $this->remove_lock();
237     $this->closeDialogs();
238   }
241   function detectPostActions()
242   {
243     $action= $this->headpage->getAction();
245     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
246     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
247     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
248     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";    
250     return($action);
251   }
253   function handleActions($action)
254   {
255     // Start action  
256     if(isset($this->actions[$action['action']])){
257       $func = $this->actions[$action['action']];
258       if(!isset($action['targets']))$action['targets']= array(); 
259       return($this->$func($action['action'],$action['targets'],$action));
260     }
261   } 
263   function newEntry($action="",$target=array(),$all=array())
264   {
265     // Check locking & lock entry if required 
266     $this->displayApplyBtn = FALSE;
267     $this->dn = "new";
268     $this->is_new = TRUE;
269     $this->is_single_edit = FALSE;
270     $this->is_multiple_edit = FALSE;
272     set_object_info($this->dn);
274     // Open object.
275     if(empty($this->tabClass) || empty($this->tabType)){
276       // No tab type defined
277     }else{
278       $tab = $this->tabClass;
279       $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
280       $this->tabObject->set_acl_base($this->headpage->getBase());
281     }
282   }
284   function editEntry($action,$target,$all)
285   {
286     if(count($target) == 0){
287       //nothing 
288     }elseif(count($target) == 1){
290       // Check locking & lock entry if required 
291       $this->displayApplyBtn = TRUE;
293       $this->is_new = FALSE;
294       $this->is_single_edit = TRUE;
295       $this->is_multiple_edit = FALSE;
297       $this->dn = array_pop($target);
298       set_object_info($this->dn);
299       $user = get_lock($this->dn);
300       if ($user != ""){
301         return(gen_locked_message ($user, $this->dn,TRUE));
302       }
303       add_lock ($this->dn, $this->ui->dn);
305       // Open object.
306       if(empty($this->tabClass) || empty($this->tabType)){
307         // No tab type defined
308       }else{
309         $tab = $this->tabClass;
310         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn,$this->aclCategory);
311         $this->tabObject->set_acl_base($this->dn);
312       }
313     }else{
315       $this->is_new = FALSE;
316       $this->is_singel_edit = FALSE;
317       $this->is_multiple_edit = TRUE;
319       $this->dns = $target;
320       $tmp = new multi_plug($this->config,$this->tabClass,$this->config->data['TABS'][$this->tabType],
321             $this->dns,$this->headpage->getBase(),$this->aclCategory);
322       if ($tmp->entries_locked()){
323         return($tmp->display_lock_message());
324       }
325       $tmp->lock_entries($this->ui->dn);
326       if($tmp->multiple_available()){
327         $this->tabObject = $tmp;
328         set_object_info($this->tabObject->get_object_info());
329       }
331     }
332   }
334   protected function saveChanges()
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->remove_lock();
345         $this->closeDialogs();
346       }
347     }
348   }
350   protected function applyChanges()
351   {
352   if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
353     $this->tabObject->save_object();
354       $msgs = $this->tabObject->check();
355       if(count($msgs)){
356         msg_dialog::displayChecks($msgs); 
357         return("");
358       }else{
359         $this->tabObject->save();
360         $this->tabObject->re_init();
361       }
362     }
363   }
364   
365   protected function closeDialogs()
366   {
367     $this->last_dn = $this->dn;
368     $this->last_dns = $this->dns;
369     $this->last_tabObject = $this->tabObject;
370     $this->last_dialogObject = $this->dialogObject;
372     $this->dn = "";
373     $this->dns = array();
374     $this->tabObject = null;
375     $this->dialogObject = null;
376     set_object_info();
377   }
379   protected function cancelEdit()
380   {
381     $this->remove_lock();
382     $this->closeDialogs();
383   }
386  
388   function registerAction($action,$target)
389   {
390     $this->actions[$action] = $target;
391   }
393   function remove_lock()
394   {
395     if(!empty($this->dn) && $this->dn != "new"){
396       del_lock($this->dn);
397     }
398     if(count($this->dns)){
399       del_lock($this->dns);
400     }
401   }
403   function copyPasteHandler($s_action,$s_entry)
404   {
406     /* Check if Copy & Paste is disabled */
407     if(!is_object($this->cpHandler)){
408       return("");
409     }
411     $this->cpHandler->save_object();
413     /* Add entries to queue */
414     if($s_action == "copy" || $s_action == "cut"){
416       /* Cleanup object queue */
417       $this->cpHandler->cleanup_queue();
419       /* Add new entries to CP queue */
420       foreach($s_entry as $dn){
421         if($s_action == "copy" && $this->ui->is_copyable($dn,$this->aclCategory,$this->aclPlugin)){
422           $this->cpHandler->add_to_queue($dn,"copy",$this->tabClass,$this->tabType,$this->aclCategory);
423         }
424         if($s_action == "cut" && $this->ui->is_cutable($dn,$this->aclCategory,$this->aclPlugin)){
425           $this->cpHandler->add_to_queue($dn,"cut",$this->tabClass,$this->tabType,$this->aclCategory);
426         }
427       }
428     }
430     /* Start pasting entries */
431     if($s_action == "paste"){
432       $this->cpPastingStarted = TRUE;
433     }
435     /* Return C&P dialog */
436     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
438       /* Get dialog */
439       $this->cpHandler->SetVar("base",$this->headpage->getBase());
440       $data = $this->cpHandler->execute();
442       /* Return dialog data */
443       if(!empty($data)){
444         return($data);
445       }
446     }
448     /* Automatically disable status for pasting */
449     if(!$this->cpHandler->entries_queued()){
450       $this->cpPastingStarted = FALSE;
451     }
452     return("");
453   }
456   function setDescription($str) {
457     $this->plDescription = $str;
458   } 
459   function setHeadpage($str) {
460     $this->headpage = $str;
461   } 
462   function setFilter($str) {
463     $this->filter = $str;
464   } 
465   function setIcon($str) {
466     $this->plIcon = $str;
467   } 
468   function setHeadline($str) {
469     $this->plHeadline = $str;
470   } 
473 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
474 ?>