Code

Removed debug output
[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("cancelSnapshot","closeDialogs");
123     $this->registerAction("config-filter","editFilter");
124   }
126   /*! \brief  Execute this plugin
127    *          Handle actions/events, locking, snapshots, dialogs, tabs,...
128    */
129   function execute()
130   {
131     // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
132     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
133     session::set('LOCK_VARS_TO_USE',$vars);
135     /* Display the copy & paste dialog, if it is currently open */
136     $ret = $this->copyPasteHandler("",array());
137     if($ret){
138       return($this->getHeader().$ret);
139     }
141     // Update filter
142     if ($this->filter) {
143       $this->filter->update();
144       session::global_set(get_class($this)."_filter", $this->filter);
145       session::set('autocomplete', $this->filter);
146     }
148     // Handle actions (POSTs and GETs)
149     $str = $this->handleActions($this->detectPostActions());
150     if($str) return($this->getHeader().$str);
152     // Open single dialog objects
153     if(is_object($this->dialogObject)){
154       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
155       if(method_exists($this->dialogObject,'execute')){
156         $display = $this->dialogObject->execute(); 
157         $display.= $this->_getTabFooter();
158         return($this->getHeader().$display);
159       } 
160     }
162     // Display tab object.
163     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
164 #      $this->tabObject->save_object();
165       $display = $this->tabObject->execute();
166       $display.= $this->_getTabFooter();
167       return($this->getHeader().$display);
168     }
170     // Set current restore base for snapshot handling.
171     if(is_object($this->snapHandler)){
172       $bases = array();
173       foreach($this->storagePoints as $sp){
174         $bases[] = $sp.$this->headpage->getBase();
175       }
177       // No bases specified? Try base  
178       if(!count($bases)) $bases[] = $this->headpage->getBase();
180       $this->snapHandler->setSnapshotBases($bases);
181     }
182   
183     // Display list
184     return($this->renderList());
185   }
186  
187   function editFilter()
188   {
189     $headpage = $this->getHeadpage();
190     $this->dialogObject = new userFilter($this->config,$headpage->categories);
191   }
192  
193   function renderList()
194   {
195     $this->headpage->update();
196     $display = $this->headpage->render();
197     return($this->getHeader().$display);
198   }
200   function getHeadpage()
201   {
202     return($this->headpage);
203   }
205   function getFilter()
206   {
207     return($this->filter);
208   }
210   /*! \brief  Generates the plugin header which is displayed whenever a tab object is 
211    *           opened.
212    */
213   protected function getHeader()
214   {
215     // We do not display any headers right now.
216     if(1 || $this->skipHeader) return("");
217   }
220   /*! \brief  Generates the footer which is used whenever a tab object is 
221    *           displayed.
222    */
223   protected function _getTabFooter()
224   {
225     // Do not display tab footer for non tab objects 
226     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
227       return("");
228     }
230     // Check if there is a dialog opened - We don't need any buttons in this case. 
231     if($this->tabObject->by_object[$this->tabObject->current]){
232       $current = $this->tabObject->by_object[$this->tabObject->current];  
233       if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
234         return("");
235       }
236     }
238     // Skip footer if requested;
239     if($this->skipFooter) return("");
241     // In case an of locked entry, we may have opened a read-only tab.
242     $str = "";
243     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
244       $str.= "<p style=\"text-align:right\">
245         <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
246         </p>";
247       return($str);
248     }else{
250       // Display ok, (apply) and cancel buttons
251       $str.= "<p style=\"text-align:right\">\n";
252       $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
253       $str.= "&nbsp;\n";
254       if($this->displayApplyBtn){
255         $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
256         $str.= "&nbsp;\n";
257       }
258       $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
259       $str.= "</p>";
260     }
261     return($str);
262   }
265   /*! \brief  Initiates the removal for the given entries
266    *           and displays a confirmation dialog.
267    *      
268    *  @param  String  'action'  The name of the action which was the used as trigger.
269    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
270    *  @param  Array   'all'     A combination of both 'action' and 'target'.
271    */
272   protected function removeEntryRequested($action="",$target=array(),$all=array())
273   {
274     $disallowed = array();
275     $this->dns = array();
277     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
279     // Check permissons for each target 
280     foreach($target as $dn){
281       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
282       if(preg_match("/d/",$acl)){
283         $this->dns[] = $dn;
284       }else{
285         $disallowed[] = $dn;
286       }
287     }
288     if(count($disallowed)){
289       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
290     }
292     // We've at least one entry to delete.
293     if(count($this->dns)){
295       // check locks
296       if ($user= get_multiple_locks($this->dns)){
297         return(gen_locked_message($user,$this->dns));
298       }
300       // Add locks
301       $dns_names = array();
302       foreach($this->dns as $dn){
303         $dns_names[] =LDAP::fix($dn);
304       }
305       add_lock ($this->dns, $this->ui->dn);
307       // Display confirmation dialog.
308       $smarty = get_smarty();
309       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
310       $smarty->assign("multiple", true);
311       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
312     }
313   }  
316   /*! \brief  Object removal was confirmed, now remove the requested entries. 
317    *      
318    *  @param  String  'action'  The name of the action which was the used as trigger.
319    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
320    *  @param  Array   'all'     A combination of both 'action' and 'target'.
321    */
322   function removeEntryConfirmed($action="",$target=array(),$all=array(),
323       $altTabClass="",$altTabType="",$altAclCategory="")
324   {
325     $tabType = $this->tabType;
326     $tabClass = $this->tabClass;
327     $aclCategory = $this->aclCategory;
328     if(!empty($altTabClass)) $tabClass = $altTabClass;
329     if(!empty($altTabType)) $tabType = $altTabType;
330     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
332     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
334     foreach($this->dns as $key => $dn){
336       // Check permissions, are we allowed to remove this object? 
337       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
338       if(preg_match("/d/",$acl)){
340         // Delete the object
341         $this->dn = $dn;
342         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory, true, true);
343         $this->tabObject->set_acl_base($this->dn);
344         $this->tabObject->parent = &$this;
345         $this->tabObject->delete ();
347         // Remove the lock for the current object.
348         del_lock($this->dn);        
349       } else {
350         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
351         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
352       }
353     }
355     // Cleanup
356     $this->remove_lock();
357     $this->closeDialogs();
358   }
361   /*! \brief  Detects actions/events send by the ui
362    *           and the corresponding targets.
363    */
364   function detectPostActions()
365   {
366     if(!is_object($this->headpage)){
367       trigger_error("No valid headpage given....!");
368       return(array());
369     }
370     $action= $this->headpage->getAction();
371     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
372     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
373     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
374     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
375     if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";   
376     if(isset($_POST['saveFilter'])) $action['action'] = "save";   
377     if(isset($_POST['cancelFilter'])) $action['action'] = "cancel";   
379     // Detect Snapshot actions
380     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
381     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
382     foreach($_POST as $name => $value){
383       $once =TRUE;
384       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
385         $once = FALSE;
386         $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
387         $action['action'] = "restoreSnapshot";
388         $action['targets'] = array($entry);
389       }
390     }
392     return($action);
393   }
396   /*! \brief  Calls the registered method for a given action/event.
397    */
398   function handleActions($action)
399   {
400     // Start action  
401     if(isset($this->actions[$action['action']])){
402       $func = $this->actions[$action['action']];
403       if(!isset($action['targets']))$action['targets']= array(); 
404       return($this->$func($action['action'],$action['targets'],$action));
405     }
406   } 
409   /*! \brief  Opens the snapshot creation dialog for the given target.
410    *      
411    *  @param  String  'action'  The name of the action which was the used as trigger.
412    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
413    *  @param  Array   'all'     A combination of both 'action' and 'target'.
414    */
415   function createSnapshotDialog($action="",$target=array(),$all=array())
416   {
417     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
419     foreach($target as $entry){
420       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
421         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
422         $this->dialogObject->aclCategories = array($this->aclCategory);
423         $this->dialogObject->parent = &$this;
425       }else{
426         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
427             ERROR_DIALOG);
428       }
429     }
430   }
433   /*! \brief  Creates a snapshot new entry - This method is called when the somebody
434    *           clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
435    *      
436    *  @param  String  'action'  The name of the action which was the used as trigger.
437    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
438    *  @param  Array   'all'     A combination of both 'action' and 'target'.
439    */
440   function saveSnapshot($action="",$target=array(),$all=array())
441   { 
442     if(!is_object($this->dialogObject)) return;
443     $this->dialogObject->save_object();
444     $msgs = $this->dialogObject->check();
445     if(count($msgs)){
446       foreach($msgs as $msg){
447         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
448       }
449     }else{
450       $this->dn =  $this->dialogObject->dn;
451       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
452       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
453       $this->closeDialogs();
454     }
455   }
458   /*! \brief  Restores a snapshot object.
459    *          The dn of the snapshot entry has to be given as ['target'] parameter.  
460    *      
461    *  @param  String  'action'  The name of the action which was the used as trigger.
462    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
463    *  @param  Array   'all'     A combination of both 'action' and 'target'.
464    */
465   function restoreSnapshot($action="",$target=array(),$all=array())
466   {
467     $entry = array_pop($target);
468     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
469       $this->snapHandler->restore_snapshot($entry);
470       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
471       $this->closeDialogs();
472     }else{
473       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
474           ERROR_DIALOG);
475     }
476   }
479   /*! \brief  Displays the "Restore snapshot dialog" for a given target. 
480    *          If no target is specified, open the restore removed object 
481    *           dialog.
482    *  @param  String  'action'  The name of the action which was the used as trigger.
483    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
484    *  @param  Array   'all'     A combination of both 'action' and 'target'.
485    */
486   function restoreSnapshotDialog($action="",$target=array(),$all=array())
487   {
488     // Set current restore base for snapshot handling.
489     if(is_object($this->snapHandler)){
490       $bases = array();
491       foreach($this->storagePoints as $sp){
492         $bases[] = $sp.$this->headpage->getBase();
493       }
494     }
496     // No bases specified? Try base  
497     if(!count($bases)) $bases[] = $this->headpage->getBase();
499     // No target, open the restore removed object dialog.
500     if(!count($target)){ 
501       $entry = $this->headpage->getBase();
502       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
503         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
504         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
505         $this->dialogObject->set_snapshot_bases($bases);
506         $this->dialogObject->display_all_removed_objects = true;
507         $this->dialogObject->display_restore_dialog = true;
508         $this->dialogObject->parent = &$this;
509       }else{
510         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
511             ERROR_DIALOG);
512       } 
513     }else{
515       // Display the restore points for a given object.
516       $entry = array_pop($target);
517       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
518         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
519         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
520         $this->dialogObject->set_snapshot_bases($bases);
521         $this->dialogObject->display_restore_dialog = true;
522         $this->dialogObject->parent = &$this;
523       }else{
524         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
525             ERROR_DIALOG);
526       } 
527     }
528   }
531   /*! \brief  This method intiates the object creation.
532    *          
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 newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
538   {
539     /* To handle mutliple object types overload this method.
540      * ...
541      *   registerAction('newUser', 'newEntry');
542      *   registerAction('newGroup','newEntry');
543      * ... 
544      * 
545      * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
546      * {
547      *   switch($action){
548      *     case 'newUser' : {
549      *       mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
550      *     }
551      *     case 'newGroup' : {
552      *       mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
553      *     }
554      *   }
555      * }
556      **/ 
557     $tabType = $this->tabType;
558     $tabClass = $this->tabClass;
559     $aclCategory = $this->aclCategory;
560     if(!empty($altTabClass)) $tabClass = $altTabClass;
561     if(!empty($altTabType)) $tabType = $altTabType;
562     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
564     // Check locking & lock entry if required 
565     $this->displayApplyBtn = FALSE;
566     $this->dn = "new";
567     $this->is_new = TRUE;
568     $this->is_single_edit = FALSE;
569     $this->is_multiple_edit = FALSE;
571     set_object_info($this->dn);
573     // Open object.
574     if(empty($tabClass) || empty($tabType)){
575       // No tab type defined
576     }else{
577       if (isset($this->config->data['TABS'][$tabType])) {
578         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
579         $this->tabObject->set_acl_base($this->headpage->getBase());
580         $this->tabObject->parent = &$this;
581         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
582       } else {
583         msg_dialog::display(_("Error"), sprintf(_("No tab declaration for '%s' found in your configuration file. Cannot create plugin instance!"), $tabType), ERROR_DIALOG);
584       }
585     }
586   }
589   /*! \brief  This method opens an existing object or a list of existing objects to be edited. 
590    *                  
591    * 
592    *  @param  String  'action'  The name of the action which was the used as trigger.
593    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
594    *  @param  Array   'all'     A combination of both 'action' and 'target'.
595    */
596   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
597   {
598     /* To handle mutliple object types overload this method.
599      * ...
600      *   registerAction('editUser', 'editEntry');
601      *   registerAction('editGroup','editEntry');
602      * ... 
603      * 
604      * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
605      * {
606      *   switch($action){
607      *     case 'editUser' : {
608      *       mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
609      *     }
610      *     case 'editGroup' : {
611      *       mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
612      *     }
613      *   }
614      * }
615      **/
617     // Do not create a new tabObject while there is already one opened,
618     //  the user may have just pressed F5 to reload the page.
619     if(is_object($this->tabObject)){
620       return;
621     }
622  
623     $tabType = $this->tabType;
624     $tabClass = $this->tabClass;
625     $aclCategory = $this->aclCategory;
626     if(!empty($altTabClass)) $tabClass = $altTabClass;
627     if(!empty($altTabType)) $tabType = $altTabType;
628     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
630     // Single edit - we only got one object dn.
631     if(count($target) == 1){
632       $this->displayApplyBtn = TRUE;
633       $this->is_new = FALSE;
634       $this->is_single_edit = TRUE;
635       $this->is_multiple_edit = FALSE;
637       // Get the dn of the object and creates lock
638       $this->dn = array_pop($target);
639       set_object_info($this->dn);
640       $user = get_lock($this->dn);
641       if ($user != ""){
642         return(gen_locked_message ($user, $this->dn,TRUE));
643       }
644       add_lock ($this->dn, $this->ui->dn);
646       // Open object.
647       if(empty($tabClass) || empty($tabType)){
648         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
649       }else{
650         $tab = $tabClass;
651         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
652         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
653         $this->tabObject->set_acl_base($this->dn);
654         $this->tabObject->parent = &$this;
655       }
656     }else{
658       // We've multiple entries to edit.
659       $this->is_new = FALSE;
660       $this->is_singel_edit = FALSE;
661       $this->is_multiple_edit = TRUE;
663       // Open multiple edit handler.
664       if(empty($tabClass) || empty($tabType)){
665         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
666       }else{
667         $this->dns = $target;
668         $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
669             $this->dns,$this->headpage->getBase(),$aclCategory);
671         // Check for locked entries
672         if ($tmp->entries_locked()){
673           return($tmp->display_lock_message());
674         }
676         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
678         // Now lock entries.
679         if($tmp->multiple_available()){
680           $tmp->lock_entries($this->ui->dn);
681           $this->tabObject = $tmp;
682           set_object_info($this->tabObject->get_object_info());
683         }
684       }
685     }
686   }
689   /*! \brief  Save object modifications and closes dialogs (returns to object listing).
690    *          - Calls '::check' to validate the given input.
691    *          - Calls '::save' to save back object modifications (e.g. to ldap).
692    *          - Calls '::remove_locks' to remove eventually created locks.
693    *          - Calls '::closeDialogs' to return to the object listing.
694    */
695   protected function saveChanges()
696   {
697     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
698       $this->tabObject->save_object();
699       $msgs = $this->tabObject->check();
700       if(count($msgs)){
701         msg_dialog::displayChecks($msgs); 
702         return("");
703       }else{
704         $this->tabObject->save();
705         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
706         $this->remove_lock();
707         $this->closeDialogs();
708       }
709     }elseif($this->dialogObject instanceOf plugin){
710       $this->dialogObject->save_object();
711       $msgs = $this->dialogObject->check();
712       if(count($msgs)){
713         msg_dialog::displayChecks($msgs); 
714         return("");
715       }else{
716         $this->dialogObject->save();
717         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
718         $this->remove_lock();
719         $this->closeDialogs();
720       }
721     }
722   }
725   /*! \brief  Save object modifications and keep dialogs opened. 
726    *          - Calls '::check' to validate the given input.
727    *          - Calls '::save' to save back object modifications (e.g. to ldap).
728    */
729   protected function applyChanges()
730   {
731     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
732       $this->tabObject->save_object();
733       $msgs = $this->tabObject->check();
734       if(count($msgs)){
735         msg_dialog::displayChecks($msgs); 
736         return("");
737       }else{
738         $this->tabObject->save();
739         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
740         $this->tabObject->re_init();
741       }
742     }
743   }
746   /*! \brief  This method closes dialogs
747    *           and cleans up the cached object info and the ui.
748    */
749   protected function closeDialogs()
750   {
751     $this->last_dn = $this->dn;
752     $this->last_dns = $this->dns;
753     $this->last_tabObject = $this->tabObject;
754     $this->last_dialogObject = $this->dialogObject;
755     $this->dn = "";
756     $this->dns = array();
757     $this->tabObject = null;
758     $this->dialogObject = null;
759     $this->skipFooter = FALSE;
760     set_object_info();
761   }
764   /*! \brief  Editing an object was caneled. 
765    *          Close dialogs/tabs and remove locks.
766    */
767   protected function cancelEdit()
768   {
769     $this->remove_lock();
770     $this->closeDialogs();
771   }
774   /*! \brief  Every click in the list user interface sends an event
775    *           here can we connect those events to a method. 
776    *          eg.  ::registerEvent('new','createUser')
777    *          When the action/event new is send, the method 'createUser' 
778    *           will be called.
779    */
780   function registerAction($action,$target)
781   {
782     $this->actions[$action] = $target;
783   }
786   /*! \brief  Removes ldap object locks created by this class.
787    *          Whenever an object is edited, we create locks to avoid 
788    *           concurrent modifications.
789    *          This locks will automatically removed here.
790    */
791   function remove_lock()
792   {
793     if(!empty($this->dn) && $this->dn != "new"){
794       del_lock($this->dn);
795     }
796     if(count($this->dns)){
797       del_lock($this->dns);
798     }
799   }
802   /*! \brief  This method is used to queue and process copy&paste actions. 
803    *          Allows to copy, cut and paste mutliple entries at once.
804    *  @param  String  'action'  The name of the action which was the used as trigger.
805    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
806    *  @param  Array   'all'     A combination of both 'action' and 'target'.
807    */
808   function copyPasteHandler($action="",$target=array(),$all=array(), 
809       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
810   {
811     // Return without any actions while copy&paste handler is disabled.
812     if(!is_object($this->cpHandler))  return("");
814     $tabType = $this->tabType;
815     $tabClass = $this->tabClass;
816     $aclCategory = $this->aclCategory;
817     $aclPlugin = $this->aclPlugin;
818     if(!empty($altTabClass)) $tabClass = $altTabClass;
819     if(!empty($altTabType)) $tabType = $altTabType;
820     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
821     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
823     // Save user input
824     $this->cpHandler->save_object();
826     // Add entries to queue 
827     if($action == "copy" || $action == "cut"){
828       $this->cpHandler->cleanup_queue();
829       foreach($target as $dn){
830         if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
831           $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
832           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
833         }
834         if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
835           $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
836           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
837         }
838       }
839     }
841     // Initiate pasting
842     if($action == "paste"){
843       $this->cpPastingStarted = TRUE;
844     }
846     // Display any c&p dialogs, eg. object modifications required before pasting.
847     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
848       $this->cpHandler->SetVar("base",$this->headpage->getBase());
849       $data = $this->cpHandler->execute();
850       if(!empty($data)){
851         return($data);
852       }
853     }
855     // Automatically disable pasting process since there is no entry left to paste.
856     if(!$this->cpHandler->entries_queued()){
857       $this->cpPastingStarted = FALSE;
858     }
859     return("");
860   }
863   function setFilter($str) {
864     $this->filter = $str;
865   }
868   function postcreate() {
869     $this->_handlePostEvent('POSTCREATE');
870   }
871   function postmodify(){
872     $this->_handlePostEvent('POSTMODIFY');
873   }
874   function postremove(){
875     $this->_handlePostEvent('POSTREMOVE');
876   }
878   function _handlePostEvent($type)
879   {
881     /* Find postcreate entries for this class */
882     $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
883     if ($command != ""){
885       /* Walk through attribute list */
886       foreach ($this->attributes as $attr){
887         if (!is_array($this->$attr)){
888           $add_attrs[$attr] = $this->$attr;
889         }
890       }
891       $add_attrs['dn']=$this->dn;
893       $tmp = array();
894       foreach($add_attrs as $name => $value){
895         $tmp[$name] =  strlen($name);
896       }
897       arsort($tmp);
899       /* Additional attributes */
900       foreach ($tmp as $name => $len){
901         $value = $add_attrs[$name];
902         $command= str_replace("%$name", "$value", $command);
903       }
905       if (check_command($command)){
906         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
907             $command, "Execute");
908         exec($command,$arr);
909         foreach($arr as $str){
910           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
911               $command, "Result: ".$str);
912         }
913       } else {
914         $message= msgPool::cmdnotfound($type, get_class($this));
915         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
916       }
917     }
918   }
920   function is_modal_dialog()
921   {
922     return(is_object($this->tabObject) || is_object($this->dialogObject));
923   }
926   /*! \brief    Forward command execution request
927    *             to the correct method.
928    */
929   function handle_post_events($mode, $addAttrs= array())
930   {
931     if(!in_array($mode, array('add','remove','modify'))){
932       trigger_error(sprintf("Invalid post event type given '%s'! Valid types are [add,modify,remove].", $mode));
933       return;
934     }
935     switch ($mode){
936       case "add":
937         plugin::callHook($this,"POSTCREATE", $addAttrs);
938       break;
940       case "modify":
941         plugin::callHook($this,"POSTMODIFY", $addAttrs);
942       break;
944       case "remove":
945         plugin::callHook($this,"POSTREMOVE", $addAttrs);
946       break;
947     }
948   }
951 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
952 ?>