<?php
/**
 * @package Legacy
 * @version $Id: HelpAction.class.php,v 1.1.2.15 2006/09/25 09:34:40 minahito Exp $
 */

 if (!defined('XOOPS_ROOT_PATH')) exit();

/**
 * The sub-class of smarty for help viewer, make it possible to use smarty in
 * help html file. This class extends Smarty to mediate the collision compiled
 * file name.
 * 
 * To support help view, there are some original modifiers.
 * 
 * 'helpurl' modify a relativity URL for connecting the dynamic page link.
 * 'helpimage' modify a image URL. These modifiers consider the existence of
 * language files.
 */
class Legacy_HelpSmarty extends Smarty
{
	/**
	 * @var string
	 */
	var $mDirname = null;
	
	/**
	 * @var XoopsModule
	 */
	var $mModuleObject = null;
	
	/**
	 * @var string
	 */
	var $mFilename = null;

	function Legacy_HelpSmarty()
	{
		parent::Smarty();

		$this->compile_id = null;
		$this->_canUpdateFromFile = true;
		$this->compile_check = true;
		$this->compile_dir = XOOPS_COMPILE_PATH;
		$this->left_delimiter = "<{";
		$this->right_delimiter = "}>";
		
		$this->force_compile = true;

		$this->register_modifier("helpurl", "Legacy_modifier_helpurl");
		$this->register_modifier("helpimage", "Legacy_modifier_helpimage");
	}
	
	function setDirname($dirname)
	{
		$this->mDirname = $dirname;
	}

	function _get_auto_filename($autoBase, $autoSource = null, $auotId = null)
	{
		$autoSource = $this->mDirname . "_help_" . $autoSource;
		return parent::_get_auto_filename($autoBase, $autoSource, $auotId);
	}
}

function Legacy_modifier_helpurl($file, $dirname = null )
{
	$root =& XCube_Root::getSingleton();
	$language = $root->mController->getConfig('language');

	if ( $dirname == null ) {
		$modController =& $root->mController->getModuleController();
		$moduleObject =& $modController->getXoopsModule();
		$dirname = $moduleObject->get('dirname');
	}

	//
	// TODO We should check file_exists.
	//

	$url = XOOPS_MODULE_URL . "/base/admin/index.php?action=Help&amp;dirname=${dirname}&amp;file=${file}";

	return $url;
}

function Legacy_modifier_helpimage($file)
{
	$root =& XCube_Root::getSingleton();
	$modController =& $root->mController->getModuleController();
	$moduleObject =& $modController->getXoopsModule();
	
	$language = $root->mController->getConfig('language');
	$dirname = $moduleObject->get('dirname');

	$url = XOOPS_MODULE_URL . "/${dirname}/language/${language}/help/images/${file}";
	if (!file_exists($url) && $language != "english") {
		$url = XOOPS_MODULE_URL . "/${dirname}/language/english/help/images/${file}";
	}

	return $url;
}

/**
 * This action will show the information of a module specified to user.
 */
class Legacy_HelpAction extends Legacy_Action
{
	var $mModuleObject = null;
	var $mContents = null;

	var $mErrorMessage = null;
	
	/**
	 * @access private
	 */
	var $_mDirname = null;
	
	/**
	 * @var XCube_Delegate
	 */
	var $mCreateHelpSmarty = null;
	
	function Legacy_HelpAction($flag)
	{
		parent::Legacy_Action($flag);
		
		$this->mCreateHelpSmarty =& new XCube_Delegate();
		$this->mCreateHelpSmarty->add(array(&$this, '_createHelpSmarty'));
		$this->mCreateHelpSmarty->register('Legacy_HelpAction.CreateHelpSmarty');
	}
	
	function hasPermission(&$controller, &$xoopsUser)
	{
		$this->_mDirname = xoops_getrequest('dirname');
		
		$moduleHandler =& xoops_gethandler('module');
		$permHandler =& xoops_gethandler('groupperm');
		
		$module =& $moduleHandler->getByDirname($this->_mDirname);
		
		return $permHandler->checkRight('module_admin', $module->get('mid'), $xoopsUser->getGroups());
	}
	
	function getDefaultView(&$controller, &$xoopsUser)
	{
		$moduleHandler =& xoops_gethandler('module');
		$this->mModuleObject =& $moduleHandler->getByDirname($this->_mDirname);
		
		$language =& $controller->getConfig('language');

		//
		// TODO We must change the following lines to ActionForm.
		//
		$helpfile = xoops_getrequest('file') ? xoops_getrequest('file') : $this->mModuleObject->getHelp();

		//
		// Smarty
		//
		$smarty = null;
		$this->mCreateHelpSmarty->call(new XCube_Ref($smarty));
		$smarty->setDirname($this->_mDirname);

		//
		// file check
		//
		// TODO We should not access files in language directory directly.
		//
		$template_dir = XOOPS_MODULE_PATH . "/" . $this->_mDirname . "/language/${language}/help";
		if (!file_exists($template_dir . "/" . $helpfile)) {
			$template_dir = XOOPS_MODULE_PATH . "/" . $this->_mDirname . "/language/english/help";
			if (!file_exists($template_dir . "/" . $helpfile)) {
				$this->mErrorMessage = _AD_BASE_ERROR_NO_HELP_FILE;
				return LEGACY_FRAME_VIEW_ERROR;
			}
		}

		$smarty->template_dir = $template_dir;
		$this->mContents = $smarty->fetch("file:" . $helpfile);

		return LEGACY_FRAME_VIEW_SUCCESS;
	}

	function _createHelpSmarty(&$smarty)
	{
		if (!is_object($smarty)) {
			$smarty = new Legacy_HelpSmarty();
		}
	}
	
	function executeViewSuccess(&$controller, &$xoopsUser, &$renderer)
	{
		$renderer->setTemplateName("help.html");
		$renderer->setAttribute('module', $this->mModuleObject);
		$renderer->setAttribute('contents', $this->mContents);
	}

	function executeViewError(&$controller, &$xoopsUser, &$renderer)
	{
		$controller->executeRedirect('./index.php?action=ModuleList', 1, $this->mErrorMessage);
	}
}

?>