Code

359cad23bc8970998fb510a10878639b47b458dc
[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   protected $dn = "";
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 $aclCategory = "";      // e.g. users
50   protected $objectName = "";       // e.g. users
52   // The opened object.
53   protected $tabObject = null;
54   protected $dialogObject = null;
56   // The last opened object.
57   protected $last_tabObject = null;
58   protected $last_dialogObject = null;
60   // Whether to display the apply button or not
61   protected $displayApplyBtn = "";
63   // Whether to display a footer or not.
64   protected $skipFooter = false;
66   // Copy&Paste handler
67   protected $cpHandler = null;
69   // Indicates that we want to paste objects right now.
70   protected $cpPastingStarted = FALSE;
72   // The Snapshot handler class.
73   protected $snapHandler = null;
75   // The listing handlers
76   private $headpage = null;
77   private $filter = null;
79   // A list of configured actions/events
80   protected $actions = array();
82   // Attributes managed by this plugin, can be used in post events;
83   protected $attributes = array(); 
85   function  __construct(&$config,$ui,$plugname, $headpage)
86   {
87     $this->plugname = $plugname;
88     $this->headpage = $headpage;
89     $this->ui = $ui;
90     $this->config = $config;
92     if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
93     if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
95     if(empty($this->plIcon)){
96       $this->plIcon = "plugins/".$plugname."/images/plugin.png";
97     }
99     // Register default actions
100     $this->registerAction("new",    "newEntry");
101     $this->registerAction("edit",   "editEntry");
102     $this->registerAction("apply",  "applyChanges");
103     $this->registerAction("save",   "saveChanges");
104     $this->registerAction("cancel", "cancelEdit");
105     $this->registerAction("cancelDelete", "cancelEdit");
106     $this->registerAction("remove", "removeEntryRequested");
107     $this->registerAction("removeConfirmed", "removeEntryConfirmed");
109     $this->registerAction("copy",   "copyPasteHandler");
110     $this->registerAction("cut",    "copyPasteHandler");
111     $this->registerAction("paste",  "copyPasteHandler");
113     $this->registerAction("snapshot",    "createSnapshotDialog");
114     $this->registerAction("restore",     "restoreSnapshotDialog");
115     $this->registerAction("saveSnapshot","saveSnapshot");
116     $this->registerAction("restoreSnapshot","restoreSnapshot");
117     $this->registerAction("cancelSnapshot","closeDialogs");
118   }
120   /*! \brief  Execute this plugin
121    *          Handle actions/events, locking, snapshots, dialogs, tabs,...
122    */
123   function execute()
124   {
125     // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
126     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
127     session::set('LOCK_VARS_TO_USE',$vars);
129     /* Display the copy & paste dialog, if it is currently open */
130     $ret = $this->copyPasteHandler("",array());
131     if($ret){
132       return($this->getHeader().$ret);
133     }
135     // Update filter
136     if ($this->filter) {
137       $this->filter->update();
138       session::global_set(get_class($this)."_filter", $this->filter);
139       session::set('autocomplete', $this->filter);
140       if (!$this->filter->isValid()){
141         msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
142       }
143     }
145     // Handle actions (POSTs and GETs)
146     $str = $this->handleActions($this->detectPostActions());
147     if($str) return($this->getHeader().$str);
149     // Open single dialog objects
150     if(is_object($this->dialogObject)){
151       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
152       if(method_exists($this->dialogObject,'execute')){
153         $display = $this->dialogObject->execute(); 
154         $display.= $this->_getTabFooter();
155         return($this->getHeader().$display);
156       } 
157     }
159     // Display tab object.
160     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
161       $this->tabObject->save_object();
162       $display = $this->tabObject->execute();
163       $display.= $this->_getTabFooter();
164       return($this->getHeader().$display);
165     }
167     // Set current restore base for snapshot handling.
168     if(is_object($this->snapHandler)){
169       $bases = array();
170       foreach($this->storagePoints as $sp){
171         $bases[] = $sp.$this->headpage->getBase();
172       }
174       // No bases specified? Try base  
175       if(!count($bases)) $bases[] = $this->headpage->getBase();
177       $this->snapHandler->setSnapshotBases($bases);
178     }
179     
180     // Display list
181     return($this->renderList());
182   }
183   
184   function renderList()
185   {
186     $this->headpage->update();
187     $display = $this->headpage->render();
188     return($this->getHeader().$display);
189   }
191   function getHeadpage()
192   {
193     return($this->headpage);
194   }
196   function getFilter()
197   {
198     return($this->filter);
199   }
201   /*! \brief  Generates the plugin header which is displayed whenever a tab object is 
202    *           opened.
203    */
204   protected function getHeader()
205   {
206     if (get_object_info() != ""){
207       $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
208           "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
209           LDAP::fix(get_object_info()));
210     } else {
211       $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
212     }
213     return($display);
214   }
217   /*! \brief  Generates the footer which is used whenever a tab object is 
218    *           displayed.
219    */
220   protected function _getTabFooter()
221   {
222     // Do not display tab footer for non tab objects 
223     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
224       return("");
225     }
227     // Check if there is a dialog opened - We don't need any buttons in this case. 
228     if($this->tabObject->by_object[$this->tabObject->current]){
229       $current = $this->tabObject->by_object[$this->tabObject->current];  
230       if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
231         return("");
232       }
233     }
235     // Skip footer if requested;
236     if($this->skipFooter) return("");
238     // In case an of locked entry, we may have opened a read-only tab.
239     $str = "";
240     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
241       $str.= "<p style=\"text-align:right\">
242         <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
243         </p>";
244       return($str);
245     }else{
247       // Display ok, (apply) and cancel buttons
248       $str.= "<p style=\"text-align:right\">\n";
249       $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
250       $str.= "&nbsp;\n";
251       if($this->displayApplyBtn){
252         $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
253         $str.= "&nbsp;\n";
254       }
255       $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
256       $str.= "</p>";
257     }
258     return($str);
259   }
262   /*! \brief  Initiates the removal for the given entries
263    *           and displays a confirmation dialog.
264    *      
265    *  @param  String  'action'  The name of the action which was the used as trigger.
266    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
267    *  @param  Array   'all'     A combination of both 'action' and 'target'.
268    */
269   protected function removeEntryRequested($action="",$target=array(),$all=array())
270   {
271     $disallowed = array();
272     $this->dns = array();
274     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
276     // Check permissons for each target 
277     foreach($target as $dn){
278       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
279       if(preg_match("/d/",$acl)){
280         $this->dns[] = $dn;
281       }else{
282         $disallowed[] = $dn;
283       }
284     }
285     if(count($disallowed)){
286       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
287     }
289     // We've at least one entry to delete.
290     if(count($this->dns)){
292       // check locks
293       if ($user= get_multiple_locks($this->dns)){
294         return(gen_locked_message($user,$this->dns));
295       }
297       // Add locks
298       $dns_names = array();
299       foreach($this->dns as $dn){
300         $dns_names[] =LDAP::fix($dn);
301       }
302       add_lock ($this->dns, $this->ui->dn);
304       // Display confirmation dialog.
305       $smarty = get_smarty();
306       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
307       $smarty->assign("multiple", true);
308       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
309     }
310   }  
313   /*! \brief  Object removal was confirmed, now remove the requested entries. 
314    *      
315    *  @param  String  'action'  The name of the action which was the used as trigger.
316    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
317    *  @param  Array   'all'     A combination of both 'action' and 'target'.
318    */
319   function removeEntryConfirmed($action="",$target=array(),$all=array(),
320       $altTabClass="",$altTabType="",$altAclCategory="")
321   {
322     $tabType = $this->tabType;
323     $tabClass = $this->tabClass;
324     $aclCategory = $this->aclCategory;
325     if(!empty($altTabClass)) $tabClass = $altTabClass;
326     if(!empty($altTabType)) $tabType = $altTabType;
327     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
329     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
331     foreach($this->dns as $key => $dn){
333       // Check permissions, are we allowed to remove this object? 
334       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
335       if(preg_match("/d/",$acl)){
337         // Delete the object
338         $this->dn = $dn;
339         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory, true, true);
340         $this->tabObject->set_acl_base($this->dn);
341         $this->tabObject->parent = &$this;
342         $this->tabObject->delete ();
344         // Remove the lock for the current object.
345         del_lock($this->dn);        
346       } else {
347         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
348         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
349       }
350     }
352     // Cleanup
353     $this->remove_lock();
354     $this->closeDialogs();
355   }
358   /*! \brief  Detects actions/events send by the ui
359    *           and the corresponding targets.
360    */
361   function detectPostActions()
362   {
363     if(!is_object($this->headpage)){
364       trigger_error("No valid headpage given....!");
365       return(array());
366     }
367     $action= $this->headpage->getAction();
368     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
369     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
370     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
371     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
372     if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";   
374     // Detect Snapshot actions
375     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
376     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
377     foreach($_POST as $name => $value){
378       $once =TRUE;
379       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
380         $once = FALSE;
381         $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
382         $action['action'] = "restoreSnapshot";
383         $action['targets'] = array($entry);
384       }
385     }
387     return($action);
388   }
391   /*! \brief  Calls the registered method for a given action/event.
392    */
393   function handleActions($action)
394   {
395     // Start action  
396     if(isset($this->actions[$action['action']])){
397       $func = $this->actions[$action['action']];
398       if(!isset($action['targets']))$action['targets']= array(); 
399       return($this->$func($action['action'],$action['targets'],$action));
400     }
401   } 
404   /*! \brief  Opens the snapshot creation dialog for the given target.
405    *      
406    *  @param  String  'action'  The name of the action which was the used as trigger.
407    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
408    *  @param  Array   'all'     A combination of both 'action' and 'target'.
409    */
410   function createSnapshotDialog($action="",$target=array(),$all=array())
411   {
412     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
414     foreach($target as $entry){
415       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
416         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
417         $this->dialogObject->aclCategories = array($this->aclCategory);
418         $this->dialogObject->parent = &$this;
420       }else{
421         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
422             ERROR_DIALOG);
423       }
424     }
425   }
428   /*! \brief  Creates a snapshot new entry - This method is called when the somebody
429    *           clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
430    *      
431    *  @param  String  'action'  The name of the action which was the used as trigger.
432    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
433    *  @param  Array   'all'     A combination of both 'action' and 'target'.
434    */
435   function saveSnapshot($action="",$target=array(),$all=array())
436   {
437     $this->dialogObject->save_object();
438     $msgs = $this->dialogObject->check();
439     if(count($msgs)){
440       foreach($msgs as $msg){
441         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
442       }
443     }else{
444       $this->dn =  $this->dialogObject->dn;
445       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
446       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
447       $this->closeDialogs();
448     }
449   }
452   /*! \brief  Restores a snapshot object.
453    *          The dn of the snapshot entry has to be given as ['target'] parameter.  
454    *      
455    *  @param  String  'action'  The name of the action which was the used as trigger.
456    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
457    *  @param  Array   'all'     A combination of both 'action' and 'target'.
458    */
459   function restoreSnapshot($action="",$target=array(),$all=array())
460   {
461     $entry = array_pop($target);
462     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
463       $this->snapHandler->restore_snapshot($entry);
464       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
465       $this->closeDialogs();
466     }else{
467       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
468           ERROR_DIALOG);
469     }
470   }
473   /*! \brief  Displays the "Restore snapshot dialog" for a given target. 
474    *          If no target is specified, open the restore removed object 
475    *           dialog.
476    *  @param  String  'action'  The name of the action which was the used as trigger.
477    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
478    *  @param  Array   'all'     A combination of both 'action' and 'target'.
479    */
480   function restoreSnapshotDialog($action="",$target=array(),$all=array())
481   {
482     // Set current restore base for snapshot handling.
483     if(is_object($this->snapHandler)){
484       $bases = array();
485       foreach($this->storagePoints as $sp){
486         $bases[] = $sp.$this->headpage->getBase();
487       }
488     }
490     // No bases specified? Try base  
491     if(!count($bases)) $bases[] = $this->headpage->getBase();
493     // No target, open the restore removed object dialog.
494     if(!count($target)){ 
495       $entry = $this->headpage->getBase();
496       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
497         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
498         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
499         $this->dialogObject->set_snapshot_bases($bases);
500         $this->dialogObject->display_all_removed_objects = true;
501         $this->dialogObject->display_restore_dialog = true;
502         $this->dialogObject->parent = &$this;
503       }else{
504         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
505             ERROR_DIALOG);
506       } 
507     }else{
509       // Display the restore points for a given object.
510       $entry = array_pop($target);
511       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
512         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
513         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
514         $this->dialogObject->set_snapshot_bases($bases);
515         $this->dialogObject->display_restore_dialog = true;
516         $this->dialogObject->parent = &$this;
517       }else{
518         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
519             ERROR_DIALOG);
520       } 
521     }
522   }
525   /*! \brief  This method intiates the object creation.
526    *          
527    *  @param  String  'action'  The name of the action which was the used as trigger.
528    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
529    *  @param  Array   'all'     A combination of both 'action' and 'target'.
530    */
531   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
532   {
533     /* To handle mutliple object types overload this method.
534      * ...
535      *   registerAction('newUser', 'newEntry');
536      *   registerAction('newGroup','newEntry');
537      * ... 
538      * 
539      * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
540      * {
541      *   switch($action){
542      *     case 'newUser' : {
543      *       mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
544      *     }
545      *     case 'newGroup' : {
546      *       mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
547      *     }
548      *   }
549      * }
550      **/ 
551     $tabType = $this->tabType;
552     $tabClass = $this->tabClass;
553     $aclCategory = $this->aclCategory;
554     if(!empty($altTabClass)) $tabClass = $altTabClass;
555     if(!empty($altTabType)) $tabType = $altTabType;
556     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
558     // Check locking & lock entry if required 
559     $this->displayApplyBtn = FALSE;
560     $this->dn = "new";
561     $this->is_new = TRUE;
562     $this->is_single_edit = FALSE;
563     $this->is_multiple_edit = FALSE;
565     set_object_info($this->dn);
567     // Open object.
568     if(empty($tabClass) || empty($tabType)){
569       // No tab type defined
570     }else{
571       $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
572       $this->tabObject->set_acl_base($this->headpage->getBase());
573       $this->tabObject->parent = &$this;
574       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
575     }
576   }
579   /*! \brief  This method opens an existing object or a list of existing objects to be edited. 
580    *                  
581    * 
582    *  @param  String  'action'  The name of the action which was the used as trigger.
583    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
584    *  @param  Array   'all'     A combination of both 'action' and 'target'.
585    */
586   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
587   {
588     /* To handle mutliple object types overload this method.
589      * ...
590      *   registerAction('editUser', 'editEntry');
591      *   registerAction('editGroup','editEntry');
592      * ... 
593      * 
594      * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
595      * {
596      *   switch($action){
597      *     case 'editUser' : {
598      *       mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
599      *     }
600      *     case 'editGroup' : {
601      *       mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
602      *     }
603      *   }
604      * }
605      **/
607     // Do not create a new tabObject while there is already one opened,
608     //  the user may have just pressed F5 to reload the page.
609     if(is_object($this->tabObject)){
610       return;
611     }
612  
613     $tabType = $this->tabType;
614     $tabClass = $this->tabClass;
615     $aclCategory = $this->aclCategory;
616     if(!empty($altTabClass)) $tabClass = $altTabClass;
617     if(!empty($altTabType)) $tabType = $altTabType;
618     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
620     // Single edit - we only got one object dn.
621     if(count($target) == 1){
622       $this->displayApplyBtn = TRUE;
623       $this->is_new = FALSE;
624       $this->is_single_edit = TRUE;
625       $this->is_multiple_edit = FALSE;
627       // Get the dn of the object and creates lock
628       $this->dn = array_pop($target);
629       set_object_info($this->dn);
630       $user = get_lock($this->dn);
631       if ($user != ""){
632         return(gen_locked_message ($user, $this->dn,TRUE));
633       }
634       add_lock ($this->dn, $this->ui->dn);
636       // Open object.
637       if(empty($tabClass) || empty($tabType)){
638         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
639       }else{
640         $tab = $tabClass;
641         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
642         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
643         $this->tabObject->set_acl_base($this->dn);
644         $this->tabObject->parent = &$this;
645       }
646     }else{
648       // We've multiple entries to edit.
649       $this->is_new = FALSE;
650       $this->is_singel_edit = FALSE;
651       $this->is_multiple_edit = TRUE;
653       // Open multiple edit handler.
654       if(empty($tabClass) || empty($tabType)){
655         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
656       }else{
657         $this->dns = $target;
658         $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
659             $this->dns,$this->headpage->getBase(),$aclCategory);
661         // Check for locked entries
662         if ($tmp->entries_locked()){
663           return($tmp->display_lock_message());
664         }
666         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
668         // Now lock entries.
669         if($tmp->multiple_available()){
670           $tmp->lock_entries($this->ui->dn);
671           $this->tabObject = $tmp;
672           set_object_info($this->tabObject->get_object_info());
673         }
674       }
675     }
676   }
679   /*! \brief  Save object modifications and closes dialogs (returns to object listing).
680    *          - Calls '::check' to validate the given input.
681    *          - Calls '::save' to save back object modifications (e.g. to ldap).
682    *          - Calls '::remove_locks' to remove eventually created locks.
683    *          - Calls '::closeDialogs' to return to the object listing.
684    */
685   protected function saveChanges()
686   {
687     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
688       $this->tabObject->save_object();
689       $msgs = $this->tabObject->check();
690       if(count($msgs)){
691         msg_dialog::displayChecks($msgs); 
692         return("");
693       }else{
694         $this->tabObject->save();
695         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
696         $this->remove_lock();
697         $this->closeDialogs();
698       }
699     }elseif($this->dialogObject instanceOf plugin){
700       $this->dialogObject->save_object();
701       $msgs = $this->dialogObject->check();
702       if(count($msgs)){
703         msg_dialog::displayChecks($msgs); 
704         return("");
705       }else{
706         $this->dialogObject->save();
707         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
708         $this->remove_lock();
709         $this->closeDialogs();
710       }
711     }
712   }
715   /*! \brief  Save object modifications and keep dialogs opened. 
716    *          - Calls '::check' to validate the given input.
717    *          - Calls '::save' to save back object modifications (e.g. to ldap).
718    */
719   protected function applyChanges()
720   {
721     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
722       $this->tabObject->save_object();
723       $msgs = $this->tabObject->check();
724       if(count($msgs)){
725         msg_dialog::displayChecks($msgs); 
726         return("");
727       }else{
728         $this->tabObject->save();
729         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
730         $this->tabObject->re_init();
731       }
732     }
733   }
736   /*! \brief  This method closes dialogs
737    *           and cleans up the cached object info and the ui.
738    */
739   protected function closeDialogs()
740   {
741     $this->last_dn = $this->dn;
742     $this->last_dns = $this->dns;
743     $this->last_tabObject = $this->tabObject;
744     $this->last_dialogObject = $this->dialogObject;
745     $this->dn = "";
746     $this->dns = array();
747     $this->tabObject = null;
748     $this->dialogObject = null;
749     set_object_info();
750   }
753   /*! \brief  Editing an object was caneled. 
754    *          Close dialogs/tabs and remove locks.
755    */
756   protected function cancelEdit()
757   {
758     $this->remove_lock();
759     $this->closeDialogs();
760   }
763   /*! \brief  Every click in the list user interface sends an event
764    *           here can we connect those events to a method. 
765    *          eg.  ::registerEvent('new','createUser')
766    *          When the action/event new is send, the method 'createUser' 
767    *           will be called.
768    */
769   function registerAction($action,$target)
770   {
771     $this->actions[$action] = $target;
772   }
775   /*! \brief  Removes ldap object locks created by this class.
776    *          Whenever an object is edited, we create locks to avoid 
777    *           concurrent modifications.
778    *          This locks will automatically removed here.
779    */
780   function remove_lock()
781   {
782     if(!empty($this->dn) && $this->dn != "new"){
783       del_lock($this->dn);
784     }
785     if(count($this->dns)){
786       del_lock($this->dns);
787     }
788   }
791   /*! \brief  This method is used to queue and process copy&paste actions. 
792    *          Allows to copy, cut and paste mutliple entries at once.
793    *  @param  String  'action'  The name of the action which was the used as trigger.
794    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
795    *  @param  Array   'all'     A combination of both 'action' and 'target'.
796    */
797   function copyPasteHandler($action="",$target=array(),$all=array(), 
798       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
799   {
800     // Return without any actions while copy&paste handler is disabled.
801     if(!is_object($this->cpHandler))  return("");
803     $tabType = $this->tabType;
804     $tabClass = $this->tabClass;
805     $aclCategory = $this->aclCategory;
806     $aclPlugin = $this->aclPlugin;
807     if(!empty($altTabClass)) $tabClass = $altTabClass;
808     if(!empty($altTabType)) $tabType = $altTabType;
809     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
810     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
812     // Save user input
813     $this->cpHandler->save_object();
815     // Add entries to queue 
816     if($action == "copy" || $action == "cut"){
817       $this->cpHandler->cleanup_queue();
818       foreach($target as $dn){
819         if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
820           $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
821           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
822         }
823         if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
824           $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
825           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
826         }
827       }
828     }
830     // Initiate pasting
831     if($action == "paste"){
832       $this->cpPastingStarted = TRUE;
833     }
835     // Display any c&p dialogs, eg. object modifications required before pasting.
836     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
837       $this->cpHandler->SetVar("base",$this->headpage->getBase());
838       $data = $this->cpHandler->execute();
839       if(!empty($data)){
840         return($data);
841       }
842     }
844     // Automatically disable pasting process since there is no entry left to paste.
845     if(!$this->cpHandler->entries_queued()){
846       $this->cpPastingStarted = FALSE;
847     }
848     return("");
849   }
852   function setFilter($str) {
853     $this->filter = $str;
854   }
857   function postcreate() {
858     $this->_handlePostEvent('POSTCREATE');
859   }
860   function postmodify(){
861     $this->_handlePostEvent('POSTMODIFY');
862   }
863   function postremove(){
864     $this->_handlePostEvent('POSTREMOVE');
865   }
867   function _handlePostEvent($type)
868   {
870     /* Find postcreate entries for this class */
871     $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
872     if ($command != ""){
874       /* Walk through attribute list */
875       foreach ($this->attributes as $attr){
876         if (!is_array($this->$attr)){
877           $add_attrs[$attr] = $this->$attr;
878         }
879       }
880       $add_attrs['dn']=$this->dn;
882       $tmp = array();
883       foreach($add_attrs as $name => $value){
884         $tmp[$name] =  strlen($name);
885       }
886       arsort($tmp);
888       /* Additional attributes */
889       foreach ($tmp as $name => $len){
890         $value = $add_attrs[$name];
891         $command= str_replace("%$name", "$value", $command);
892       }
894       if (check_command($command)){
895         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
896             $command, "Execute");
897         exec($command,$arr);
898         foreach($arr as $str){
899           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
900               $command, "Result: ".$str);
901         }
902       } else {
903         $message= msgPool::cmdnotfound($type, get_class($this));
904         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
905       }
906     }
908   }
911 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
912 ?>