Code

Updated pst handling
[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$$
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   // The plugin description
30   public $plugname      = "unconfigured";
31   public $plIcon        = "unconfigured";
32   public $plDescription = "unconfigured";
33   public $plHeadline    = "unconfigured";
35   // The currently used object(s) (e.g. in edit, removal)
36   public $dn = "";  // this is public due to some compatibility problems with class plugin..
37   protected $dns = array();
39   // The last used object(s).
40   protected $last_dn = "";
41   protected $last_dns = array();
43   // The common places the displayed objects are stored in. (e.g. array("ou=groups,",".."))
44   protected $storagePoints = array();
46   // The tab definitions to use for the current object.
47   protected $tabClass = "";         // e.g. usertabs
48   protected $tabType = "";          // e.g. USERTABS
49   protected $aclPlugin = "";        // e.g. generic
50   protected $aclCategory = "";      // e.g. users
51   protected $objectName = "";       // e.g. users
53   // The opened object.
54   protected $tabObject = null;
55   protected $dialogObject = null;
57   // The last opened object.
58   protected $last_tabObject = null;
59   protected $last_dialogObject = null;
61   // Whether to display the apply button or not
62   protected $displayApplyBtn = FALSE;
64   // Whether to display a header or not.
65   protected $skipHeader = false;
67   // Whether to display a footer or not.
68   protected $skipFooter = false;
70   // Copy&Paste handler
71   protected $cpHandler = null;
73   // Indicates that we want to paste objects right now.
74   protected $cpPastingStarted = FALSE;
76   // The Snapshot handler class.
77   protected $snapHandler = null;
79   // The listing handlers
80   private $headpage = null;
81   private $filter = null;
83   // A list of configured actions/events
84   protected $actions = array();
86   // Attributes managed by this plugin, can be used in post events;
87   public $attributes = array(); 
89   function  __construct(&$config,$ui,$plugname, $headpage)
90   {
91     $this->plugname = $plugname;
92     $this->headpage = $headpage;
93     $this->ui = $ui;
94     $this->config = $config;
96     if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
97     if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
99     if(empty($this->plIcon)){
100       $this->plIcon = "plugins/".$plugname."/images/plugin.png";
101     }
103     // Register default actions
104     $this->registerAction("new",    "newEntry");
105     $this->registerAction("edit",   "editEntry");
106     $this->registerAction("apply",  "applyChanges");
107     $this->registerAction("save",   "saveChanges");
108     $this->registerAction("cancel", "cancelEdit");
109     $this->registerAction("cancelDelete", "cancelEdit");
110     $this->registerAction("remove", "removeEntryRequested");
111     $this->registerAction("removeConfirmed", "removeEntryConfirmed");
113     $this->registerAction("copy",   "copyPasteHandler");
114     $this->registerAction("cut",    "copyPasteHandler");
115     $this->registerAction("paste",  "copyPasteHandler");
117     $this->registerAction("snapshot",    "createSnapshotDialog");
118     $this->registerAction("restore",     "restoreSnapshotDialog");
119     $this->registerAction("saveSnapshot","saveSnapshot");
120     $this->registerAction("restoreSnapshot","restoreSnapshot");
121     $this->registerAction("removeSnapshotConfirmed","removeSnapshotConfirmed");
122     $this->registerAction("cancelSnapshot","closeDialogs");
124     $this->registerAction("config-filter","editFilter");
125     $this->registerAction("saveFilter","saveFilter");
126     $this->registerAction("cancelFilter","cancelFilter");
128     // To temporay disable the filter caching UNcomment this line.
129     #session::global_un_set(get_class($this)."_filter");
130   }
132   /*! \brief  Execute this plugin
133    *          Handle actions/events, locking, snapshots, dialogs, tabs,...
134    */
135   function execute()
136   {
137     // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
138     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
139     session::set('LOCK_VARS_TO_USE',$vars);
141     pathNavigator::registerPlugin($this);
143     /* Display the copy & paste dialog, if it is currently open */
144     $ret = $this->copyPasteHandler("",array());
145     if($ret){
146       return($this->getHeader().$ret);
147     }
149     // Update filter
150     if ($this->filter) {
151       $this->filter->update();
152       session::global_set(get_class($this)."_filter", $this->filter);
153       session::set('autocomplete', $this->filter);
154     }
156     // Handle actions (POSTs and GETs)
157     $str = $this->handleActions($this->detectPostActions());
158     if($str) return($this->getHeader().$str);
160     // Open single dialog objects
161     if(is_object($this->dialogObject)){
162       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
163       if(method_exists($this->dialogObject,'execute')){
164         $display = $this->dialogObject->execute(); 
165         $display.= $this->_getTabFooter();
166         return($this->getHeader().$display);
167       } 
168     }
170     // Display tab object.
171     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
172 #      $this->tabObject->save_object();
173       $display = $this->tabObject->execute();
174       $display.= $this->_getTabFooter();
175       return($this->getHeader().$display);
176     }
178     // Set current restore base for snapshot handling.
179     if(is_object($this->snapHandler)){
180       $bases = array();
181       foreach($this->storagePoints as $sp){
182         $bases[] = $sp.$this->headpage->getBase();
183       }
185       // No bases specified? Try base  
186       if(!count($bases)) $bases[] = $this->headpage->getBase();
188       $this->snapHandler->setSnapshotBases($bases);
189     }
190   
191     // Display list
192     return($this->renderList());
193   }
194  
195   function editFilter()
196   {
197     $this->dialogObject = new userFilter($this->config,$this->getHeadpage());
198   }
199  
200   function renderList()
201   {
202     $this->headpage->update();
203     $display = $this->headpage->render();
204     return($this->getHeader().$display);
205   }
207   function getHeadpage()
208   {
209     return($this->headpage);
210   }
212   function getFilter()
213   {
214     return($this->filter);
215   }
217   /*! \brief  Generates the plugin header which is displayed whenever a tab object is 
218    *           opened.
219    */
220   protected function getHeader()
221   {
222     // We do not display any headers right now.
223     if(1 || $this->skipHeader) return("");
224   }
227   /*! \brief  Generates the footer which is used whenever a tab object is 
228    *           displayed.
229    */
230   protected function _getTabFooter()
231   {
232     // Do not display tab footer for non tab objects 
233     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
234       return("");
235     }
237     // Check if there is a dialog opened - We don't need any buttons in this case. 
238     if($this->tabObject->by_object[$this->tabObject->current]){
239       $current = $this->tabObject->by_object[$this->tabObject->current];  
240       if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
241         return("");
242       }
243     }
245     // Skip footer if requested;
246     if($this->skipFooter) return("");
248     // In case an of locked entry, we may have opened a read-only tab.
249     $str = "";
250     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
251       $str.= "
252         <p style=\"text-align:right\">
253           <button type=submit name=\"edit_cancel\">".msgPool::cancelButton()."</button>
254         </p>";
255       return($str);
256     }else{
258       // Display ok, (apply) and cancel buttons
259       $str.= "<p style=\"text-align:right\">\n";
260       $str.= "<button type=\"submit\" name=\"edit_finish\">".msgPool::okButton()."</button>\n";
261       $str.= "&nbsp;\n";
262       if($this->displayApplyBtn){
263         $str.= "<button type=\"submit\" name=\"edit_apply\">".msgPool::applyButton()."</button>\n";
264         $str.= "&nbsp;\n";
265       }
266       $str.= "<button type=\"submit\" name=\"edit_cancel\">".msgPool::cancelButton()."</button>\n";
267       $str.= "</p>";
268     }
269     return($str);
270   }
273   /*! \brief  Initiates the removal for the given entries
274    *           and displays a confirmation dialog.
275    *      
276    *  @param  String  'action'  The name of the action which was the used as trigger.
277    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
278    *  @param  Array   'all'     A combination of both 'action' and 'target'.
279    */
280   protected function removeEntryRequested($action="",$target=array(),$all=array())
281   {
282     // Close dialogs and remove locks for currently handled dns
283     $this->cancelEdit();
284   
285     $disallowed = array();
286     $this->dns = array();
288     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
290     // Check permissons for each target 
291     $h = $this->getHeadpage();
292     $oTypes = array_reverse($h->objectTypes);
293     foreach($target as $dn){
294       $entry = $h->getEntry($dn);
295       $obj   = $h->getObjectType($oTypes, $entry['objectClass']);
296       $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
297       if(preg_match("/d/",$acl)){
298         $this->dns[] = $dn;
299       }else{
300         $disallowed[] = $dn;
301       }
302     }
303     if(count($disallowed)){
304       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
305     }
307     // We've at least one entry to delete.
308     if(count($this->dns)){
310       // check locks
311       if ($user= get_multiple_locks($this->dns)){
312         return(gen_locked_message($user,$this->dns));
313       }
315       // Add locks
316       $dns_names = array();
317       $types = array();
319       // Build list of object -labels
320       foreach($h->objectTypes as $type){
321         $map[$type['objectClass']]= $type['label'];
322       }
324       foreach($this->dns as $dn){
325         $tmp = $h->getType($dn);
326         if(isset($map[$tmp])){
327           $dns_names[LDAP::fix($dn)] = _($map[$tmp]);
328         }else{
329           $dns_names[] =LDAP::fix($dn);
330         }
331       }
332       add_lock ($this->dns, $this->ui->dn);
334       // Display confirmation dialog.
335       $smarty = get_smarty();
336       $smarty->assign("info", msgPool::deleteInfo($dns_names));
337       $smarty->assign("multiple", true);
338       return($smarty->fetch(get_template_path('removeEntries.tpl')));
339     }
340   }  
343   /*! \brief  Object removal was confirmed, now remove the requested entries. 
344    *      
345    *  @param  String  'action'  The name of the action which was the used as trigger.
346    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
347    *  @param  Array   'all'     A combination of both 'action' and 'target'.
348    */
349   function removeEntryConfirmed($action="",$target=array(),$all=array(),
350       $altTabClass="",$altTabType="",  $altAclCategory="",$altAclPlugin="")
351   {
352     $tabType = $this->tabType;
353     $tabClass = $this->tabClass;
354     $aclCategory = $this->aclCategory;
355     $aclPlugin = $this->aclPlugin;
356     if(!empty($altTabClass)) $tabClass = $altTabClass;
357     if(!empty($altTabType)) $tabType = $altTabType;
358     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
359     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
361     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
363     // Check permissons for each target 
364     $h = $this->getHeadpage();
365     $oTypes = array_reverse($h->objectTypes);
366     foreach($this->dns as $key => $dn){
367       $entry = $h->getEntry($dn);
368       $obj   = $h->getObjectType($oTypes, $entry['objectClass']);
369       $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
371       // Check permissions, are we allowed to remove this object? 
372       if(preg_match("/d/",$acl)){
374         // Delete the object
375         $this->dn = $dn;
376         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, 
377             $aclCategory, true, true);
378         $this->tabObject->set_acl_base($this->dn);
379         $this->tabObject->parent = &$this;
380         $this->tabObject->delete ();
382         // Remove the lock for the current object.
383         del_lock($this->dn);        
384       } else {
385         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
386         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
387       }
388     }
390     // Cleanup
391     $this->remove_lock();
392     $this->closeDialogs();
393   }
396   /*! \brief  Detects actions/events send by the ui
397    *           and the corresponding targets.
398    */
399   function detectPostActions()
400   {
401     if(!is_object($this->headpage)){
402       trigger_error("No valid headpage given....!");
403       return(array());
404     }
405     $action= $this->headpage->getAction();
406     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
407     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
408     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
409     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
410     if(isset($_POST['delete_snapshot_confirm'])) $action['action'] = "removeSnapshotConfirmed";   
411     if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";   
412     if(isset($_POST['saveFilter'])) $action['action'] = "saveFilter";   
413     if(isset($_POST['cancelFilter'])) $action['action'] = "cancelFilter";   
415     // Detect Snapshot actions
416     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
417     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
418     foreach($_POST as $name => $value){
419       $once =TRUE;
420       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
421         $once = FALSE;
422         $entry = base64_decode(preg_replace("/^RestoreSnapShot_(.*)$/i","\\1",$name));
423         $action['action'] = "restoreSnapshot";
424         $action['targets'] = array($entry);
425       }
426     }
428     return($action);
429   }
432   /*! \brief  Calls the registered method for a given action/event.
433    */
434   function handleActions($action)
435   {
436     // Start action  
437     if(isset($this->actions[$action['action']])){
438       $func = $this->actions[$action['action']];
439       if(!isset($action['targets']))$action['targets']= array(); 
440       return($this->$func($action['action'],$action['targets'],$action));
441     }
442   } 
445   /*! \brief  Opens the snapshot creation dialog for the given target.
446    *      
447    *  @param  String  'action'  The name of the action which was the used as trigger.
448    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
449    *  @param  Array   'all'     A combination of both 'action' and 'target'.
450    */
451   function createSnapshotDialog($action="",$target=array(),$all=array())
452   {
453     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
455     foreach($target as $entry){
456       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
457         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
458         $this->dialogObject->aclCategories = array($this->aclCategory);
459         $this->dialogObject->parent = &$this;
461       }else{
462         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s!"), bold($entry)),
463             ERROR_DIALOG);
464       }
465     }
466   }
469   /*! \brief  Creates a snapshot new entry - This method is called when the somebody
470    *           clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
471    *      
472    *  @param  String  'action'  The name of the action which was the used as trigger.
473    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
474    *  @param  Array   'all'     A combination of both 'action' and 'target'.
475    */
476   function saveSnapshot($action="",$target=array(),$all=array())
477   { 
478     if(!is_object($this->dialogObject)) return;
479     $this->dialogObject->save_object();
480     $msgs = $this->dialogObject->check();
481     if(count($msgs)){
482       foreach($msgs as $msg){
483         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
484       }
485     }else{
486       $this->dn =  $this->dialogObject->dn;
487       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
488       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
489       $this->closeDialogs();
490     }
491   }
494   /*! \brief  Restores a snapshot object.
495    *          The dn of the snapshot entry has to be given as ['target'] parameter.  
496    *      
497    *  @param  String  'action'  The name of the action which was the used as trigger.
498    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
499    *  @param  Array   'all'     A combination of both 'action' and 'target'.
500    */
501   function restoreSnapshot($action="",$target=array(),$all=array())
502   {
503     $entry = array_pop($target);
504     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
505       $this->snapHandler->restore_snapshot($entry);
506       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
507       $this->closeDialogs();
508     }else{
509       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
510           ERROR_DIALOG);
511     }
512   }
515   /*! \brief  Removes a snapshot object.
516    */
517   function removeSnapshotConfirmed($action="",$target=array(),$all=array())
518   {
519     $entry = $this->dialogObject->del_dn;
520     if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
521       $this->snapHandler->remove_snapshot($entry);
522       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot removed!");
523     }else{
524       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to remove a snapshot for %s!"), bold($entry)),
525           ERROR_DIALOG);
526     }
527   }
530   /*! \brief  Displays the "Restore snapshot dialog" for a given target. 
531    *          If no target is specified, open the restore removed object 
532    *           dialog.
533    *  @param  String  'action'  The name of the action which was the used as trigger.
534    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
535    *  @param  Array   'all'     A combination of both 'action' and 'target'.
536    */
537   function restoreSnapshotDialog($action="",$target=array(),$all=array())
538   {
539     // Set current restore base for snapshot handling.
540     if(is_object($this->snapHandler)){
541       $bases = array();
542       foreach($this->storagePoints as $sp){
543         $bases[] = $sp.$this->headpage->getBase();
544       }
545     }
547     // No bases specified? Try base  
548     if(!count($bases)) $bases[] = $this->headpage->getBase();
550     // No target, open the restore removed object dialog.
551     if(!count($target)){ 
552       $entry = $this->headpage->getBase();
553       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
554         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
555         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
556         $this->dialogObject->set_snapshot_bases($bases);
557         $this->dialogObject->display_all_removed_objects = true;
558         $this->dialogObject->display_restore_dialog = true;
559         $this->dialogObject->parent = &$this;
560       }else{
561         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
562             ERROR_DIALOG);
563       } 
564     }else{
566       // Display the restore points for a given object.
567       $entry = array_pop($target);
568       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
569         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
570         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
571         $this->dialogObject->set_snapshot_bases($bases);
572         $this->dialogObject->display_restore_dialog = true;
573         $this->dialogObject->parent = &$this;
574       }else{
575         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
576             ERROR_DIALOG);
577       } 
578     }
579   }
582   /*! \brief  This method intiates the object creation.
583    *          
584    *  @param  String  'action'  The name of the action which was the used as trigger.
585    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
586    *  @param  Array   'all'     A combination of both 'action' and 'target'.
587    */
588   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
589   {
590     /* To handle mutliple object types overload this method.
591      * ...
592      *   registerAction('newUser', 'newEntry');
593      *   registerAction('newGroup','newEntry');
594      * ... 
595      * 
596      * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
597      * {
598      *   switch($action){
599      *     case 'newUser' : {
600      *       mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
601      *     }
602      *     case 'newGroup' : {
603      *       mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
604      *     }
605      *   }
606      * }
607      **/ 
608     $tabType = $this->tabType;
609     $tabClass = $this->tabClass;
610     $aclCategory = $this->aclCategory;
611     if(!empty($altTabClass)) $tabClass = $altTabClass;
612     if(!empty($altTabType)) $tabType = $altTabType;
613     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
615     // Check locking & lock entry if required 
616     $this->displayApplyBtn = FALSE;
617     $this->dn = "new";
618     $this->is_new = TRUE;
619     $this->is_single_edit = FALSE;
620     $this->is_multiple_edit = FALSE;
622     set_object_info($this->dn);
624     // Open object.
625     if(empty($tabClass) || empty($tabType)){
626       // No tab type defined
627     }else{
629         if (isset($this->config->data['TABS'][$tabType])) {
631             // Check if the base plugin is available - it is mostly responsible for object creation and removal.
632             $first  = $this->config->data['TABS'][$tabType][0];
633             if(!class_available($first['CLASS'])){
634                 msg_dialog::display(_("Internal error"),
635                         sprintf(_("Cannot instantiate tabbed-plugin, the base plugin (%s) is not available!"), $first['CLASS']),
636                         ERROR_DIALOG);
637             }else{
638                 $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
639                 $this->tabObject->set_acl_base($this->headpage->getBase());
640                 $this->tabObject->parent = &$this;
641                 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
642             }
643       } else {
644         msg_dialog::display(_("Error"), sprintf(_("No tab definition for %s found in configuration file: cannot create plugin instance!"), bold($tabType)), ERROR_DIALOG);
645       }
646     }
647   }
650   /*! \brief  This method opens an existing object or a list of existing objects to be edited. 
651    *                  
652    * 
653    *  @param  String  'action'  The name of the action which was the used as trigger.
654    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
655    *  @param  Array   'all'     A combination of both 'action' and 'target'.
656    */
657   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
658   {
659     /* To handle mutliple object types overload this method.
660      * ...
661      *   registerAction('editUser', 'editEntry');
662      *   registerAction('editGroup','editEntry');
663      * ... 
664      * 
665      * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
666      * {
667      *   switch($action){
668      *     case 'editUser' : {
669      *       mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
670      *     }
671      *     case 'editGroup' : {
672      *       mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
673      *     }
674      *   }
675      * }
676      **/
678     // Do not create a new tabObject while there is already one opened,
679     //  the user may have just pressed F5 to reload the page.
680     if(is_object($this->tabObject)){
681       return;
682     }
683  
684     $tabType = $this->tabType;
685     $tabClass = $this->tabClass;
686     $aclCategory = $this->aclCategory;
687     if(!empty($altTabClass)) $tabClass = $altTabClass;
688     if(!empty($altTabType)) $tabType = $altTabType;
689     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
691     // Single edit - we only got one object dn.
692     if(count($target) == 1){
693       $this->displayApplyBtn = TRUE;
694       $this->is_new = FALSE;
695       $this->is_single_edit = TRUE;
696       $this->is_multiple_edit = FALSE;
698       // Get the dn of the object and creates lock
699       $this->dn = array_pop($target);
700       set_object_info($this->dn);
701       $user = get_lock($this->dn);
702       if ($user != ""){
703         return(gen_locked_message ($user, array($this->dn),TRUE));
704       }
705       add_lock ($this->dn, $this->ui->dn);
707       // Open object.
708       if(empty($tabClass) || empty($tabType)){
709         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
710       }else{
712         $tab = $tabClass;
713         
714         // Check if the base plugin is available - it is mostly responsible for object creation and removal.
715         $first  = $this->config->data['TABS'][$tabType][0];
716         if(!class_available($first['CLASS'])){
717             msg_dialog::display(_("Internal error"), 
718                     sprintf(_("Cannot instantiate tabbed-plugin, the base plugin (%s) is not available!"), $first['CLASS']), 
719                     ERROR_DIALOG);
720         }else{
721             $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
722             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
723             $this->tabObject->set_acl_base($this->dn);
724             $this->tabObject->parent = &$this;
725         }
726       }
727     }else{
729       // We've multiple entries to edit.
730       $this->is_new = FALSE;
731       $this->is_singel_edit = FALSE;
732       $this->is_multiple_edit = TRUE;
734       // Open multiple edit handler.
735       if(empty($tabClass) || empty($tabType)){
736         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
737       }else{
738         $this->dns = $target;
739         $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
740             $this->dns,$this->headpage->getBase(),$aclCategory);
742         // Check for locked entries
743         if ($tmp->entries_locked()){
744           return($tmp->display_lock_message());
745         }
747         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
749         // Now lock entries.
750         if($tmp->multiple_available()){
751           $tmp->lock_entries($this->ui->dn);
752           $this->tabObject = $tmp;
753           set_object_info($this->tabObject->get_object_info());
754         }
755       }
756     }
757   }
760   /*! \brief  Close filter dialog
761    */
762   protected function cancelFilter()
763   {
764       if($this->dialogObject instanceOf userFilter){
765           $this->remove_lock();
766           $this->closeDialogs();
767       }
768   }
771   /*! \brief  Save filter modifcations.
772    */
773   protected function saveFilter()
774   {
775     if($this->dialogObject instanceOf userFilter){
776       $msgs = $this->dialogObject->check();
777       if(count($msgs)){
778         msg_dialog::displayChecks($msgs); 
779         return("");
780       }else{
781         $this->dialogObject->save();
782         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
783         $this->remove_lock();
784         $this->closeDialogs();
786         // Ask filter to reload information
787         $this->filter->reloadFilters();
788       }
789     }
790   }
793   /*! \brief  Save object modifications and closes dialogs (returns to object listing).
794    *          - Calls '::check' to validate the given input.
795    *          - Calls '::save' to save back object modifications (e.g. to ldap).
796    *          - Calls '::remove_locks' to remove eventually created locks.
797    *          - Calls '::closeDialogs' to return to the object listing.
798    */
799   protected function saveChanges()
800   {
801     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
802       $this->tabObject->save_object();
803       $msgs = $this->tabObject->check();
804       if(count($msgs)){
805         msg_dialog::displayChecks($msgs); 
806         return("");
807       }else{
808         $this->tabObject->save();
809         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
810         $this->remove_lock();
811         $this->closeDialogs();
812       }
813     }elseif($this->dialogObject instanceOf plugin){
814       $this->dialogObject->save_object();
815       $msgs = $this->dialogObject->check();
816       if(count($msgs)){
817         msg_dialog::displayChecks($msgs); 
818         return("");
819       }else{
820         $this->dialogObject->save();
821         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
822         $this->remove_lock();
823         $this->closeDialogs();
824       }
825     }
826   }
829   /*! \brief  Save object modifications and keep dialogs opened. 
830    *          - Calls '::check' to validate the given input.
831    *          - Calls '::save' to save back object modifications (e.g. to ldap).
832    */
833   protected function applyChanges()
834   {
835     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
836       $this->tabObject->save_object();
837       $msgs = $this->tabObject->check();
838       if(count($msgs)){
839         msg_dialog::displayChecks($msgs); 
840         return("");
841       }else{
842         $this->tabObject->save();
843         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
844         $this->tabObject->re_init();
845       }
846     }
847   }
850   /*! \brief  This method closes dialogs
851    *           and cleans up the cached object info and the ui.
852    */
853   protected function closeDialogs()
854   {
855     $this->last_dn = $this->dn;
856     $this->last_dns = $this->dns;
857     $this->last_tabObject = $this->tabObject;
858     $this->last_dialogObject = $this->dialogObject;
859     $this->dn = "";
860     $this->dns = array();
861     $this->tabObject = null;
862     $this->dialogObject = null;
863     $this->skipFooter = FALSE;
864     set_object_info();
865   }
868   /*! \brief  Editing an object was caneled. 
869    *          Close dialogs/tabs and remove locks.
870    */
871   protected function cancelEdit()
872   {
873     $this->remove_lock();
874     $this->closeDialogs();
875   }
878   /*! \brief  Every click in the list user interface sends an event
879    *           here can we connect those events to a method. 
880    *          eg.  ::registerEvent('new','createUser')
881    *          When the action/event new is send, the method 'createUser' 
882    *           will be called.
883    */
884   function registerAction($action,$target)
885   {
886     $this->actions[$action] = $target;
887   }
890   /*! \brief  Removes ldap object locks created by this class.
891    *          Whenever an object is edited, we create locks to avoid 
892    *           concurrent modifications.
893    *          This locks will automatically removed here.
894    */
895   function remove_lock()
896   {
897     if(!empty($this->dn) && $this->dn != "new"){
898       del_lock($this->dn);
899     }
900     if(count($this->dns)){
901       del_lock($this->dns);
902     }
903   }
906   /*! \brief  This method is used to queue and process copy&paste actions. 
907    *          Allows to copy, cut and paste mutliple entries at once.
908    *  @param  String  'action'  The name of the action which was the used as trigger.
909    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
910    *  @param  Array   'all'     A combination of both 'action' and 'target'.
911    */
912   function copyPasteHandler($action="",$target=array(),$all=array(), 
913       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
914   {
915     // Return without any actions while copy&paste handler is disabled.
916     if(!is_object($this->cpHandler))  return("");
918     $tabType = $this->tabType;
919     $tabClass = $this->tabClass;
920     $aclCategory = $this->aclCategory;
921     $aclPlugin = $this->aclPlugin;
922     if(!empty($altTabClass)) $tabClass = $altTabClass;
923     if(!empty($altTabType)) $tabType = $altTabType;
924     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
925     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
927     // Save user input
928     $this->cpHandler->save_object();
930     // Add entries to queue 
931     if($action == "copy" || $action == "cut"){
932       $this->cpHandler->cleanup_queue();
933       foreach($target as $dn){
934         if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
935           $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
936           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
937         }
938         if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
939           $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
940           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
941         }
942       }
943     }
945     // Initiate pasting
946     if($action == "paste"){
947       $this->cpPastingStarted = TRUE;
948     }
950     // Display any c&p dialogs, eg. object modifications required before pasting.
951     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
952       $this->cpHandler->SetVar("base",$this->headpage->getBase());
953       $data = $this->cpHandler->execute();
954       if(!empty($data)){
955         return($data);
956       }
957     }
959     // Automatically disable pasting process since there is no entry left to paste.
960     if(!$this->cpHandler->entries_queued()){
961       $this->cpPastingStarted = FALSE;
962     }
963     return("");
964   }
967   function setFilter($str) {
968     $this->filter = $str;
969   }
972   function postcreate() {
973     $this->handle_post_events('create');
974   }
975   function postmodify(){
976     $this->handle_post_events('modify');
977   }
978   function postremove(){
979     $this->handle_post_events('remove');
980   }
982   function is_modal_dialog()
983   {
984     return(is_object($this->tabObject) || is_object($this->dialogObject));
985   }
988   /*! \brief    Forward command execution request
989    *             to the correct method.
990    */
991   function handle_post_events($mode, $addAttrs= array())
992   {
993     if(!in_array($mode, array('add','remove','modify'))){
994       trigger_error(sprintf("Invalid post event type given %s! Valid types are [add,modify,remove].", bold($mode)));
995       return;
996     }
997     switch ($mode){
998       case "add":
999         plugin::callHook($this,"POSTCREATE", $addAttrs);
1000       break;
1002       case "modify":
1003         plugin::callHook($this,"POSTMODIFY", $addAttrs);
1004       break;
1006       case "remove":
1007         plugin::callHook($this,"POSTREMOVE", $addAttrs);
1008       break;
1009     }
1010   }
1013 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1014 ?>