<?php
/*
EasyGUI XP is free software released under the terms of the following BSD license.

Copyright (c) 2005,2006 Gaetan Lauff (glauff@plansoft.de)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

- Neither the name of the developer nor the names of its contributors may 
be used to endorse or promote products derived from this software without 
specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * Action Handling Class
 *
 * This class handles all default actions. In addition to that the class tries to
 * detect a direct callback to a php function from the dialog. In that case the callback
 * handler is called. An appropriate Place to define a callback handler is the
 * class.webapp.php or a class related to the current dialog. Consult EasyGUI XP User's Guide
 * for more Information on how to write an EasyGUI XP Web Application
 * 
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @version	XP $Id: class.ek_actionhandler.php 44 2006-02-28 13:40:01Z glauff $
 * @package action
 */
class EK_ActionHandler
{
	/**
	 * Web Application Instance
	 */
	var $webapp;
	
	/**
	 * Web View
	 * @var object
	 */
	var $view;
	
	/**
	 * Constructor
	 */
	function EK_ActionHandler( &$view, &$webapp ) 
	{
		$this->view		=& $view;
		$this->webapp 	=& $webapp;
		$this->init();
	}
	
	/**
	 * Abstract
	 * shou
	 *
	 */
	function init() {;}
	
	/**
	 * Handles all Default Actions for Application
	 *
	 * @return	void
	 * @access 	public
	 */
	function handleActions( &$actions )
	{
		$action = EK_Common::getSubmitValue('EK_ACTION');
		$handled = false;
		
		ob_start();
		
		if (!empty( $action ) ) {
			$handled = $this->handleCallback( $action );
			
			if ( $handled === false ) {
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * Transfers Handling to a callback handler
	 *
	 * @param 	string	$callback	Name of callback function
	 *
	 * @return 	boolean	TRUE if the callback handler exists, FALSE otherwise
	 *
	 * @access 	public
	 */
	function handleCallback( &$callback )
	{
		$args 		= null;
		$argsstart 	= strpos($callback, '(');
		$argsend 	= strpos($callback, ')');
		
		if (!empty($argsstart)) {
			$function = substr( $callback, 0, $argsstart );
			$args = substr( $callback, ($argsstart + 1) , ($argsend - ($argsstart + 1)) );
		} else {
			$function = $callback;
		}
		
		/**
		 * Check if the Application is handling the Action
		 */
		if ( method_exists ( $this->webapp, $function ) ) {
			if ( is_null( $args ) ) {
				return $this->webapp->$function();
			} 
			
			return $this->webapp->$function( $args );
		}
		
		/**
		 * Check if the View is handling the Action
		 */
		if ( method_exists ( $this->view, $function ) ) {
			if ( is_null( $args ) ) {
				return $this->view->$function();
			} 
			
			return $this->view->$function( $args );
		}
		
		/**
		 * Check if an ActionHandler himself is taking care of the Action
		 */
		if ( method_exists( $this, $function ) ) {
			if (is_null( $args ) ) {
				return $this->$function();
			}

			return $this->$function( $args );
		}
		
		return -1;
	}
} // End of class ek_actionhandler
?>