<?php
/**
 * @package Legacy
 * @version $Id: ModuleListAction.class.php,v 1.1.2.8 2006/08/04 04:33:01 minahito Exp $
 */

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

require_once XOOPS_BASE_PATH . "/admin/forms/ModuleListFilterForm.class.php";
require_once XOOPS_BASE_PATH . "/admin/forms/ModuleListForm.class.php";

class Legacy_ModuleListAction extends Legacy_Action
{
	var $mModuleObjects = array();
	var $mFilter = null;

	var $mActionForm = null;

	function prepare(&$controller, &$xoopsUser)
	{
		$this->mActionForm =& new Legacy_ModuleListForm();
		$this->mActionForm->prepare();
	}
	

	function getDefaultView(&$controller, &$xoopsUser)
	{
		$this->mFilter =& new Legacy_ModuleListFilterForm($this->mNavi);
		$this->mFilter->fetch();
		
		$moduleHandler =& xoops_gethandler('module');

		// dmy
		$criteria =& new Criteria('mid', 0, '>');
		$criteria->setSort($this->mFilter->getSort());
		$criteria->setOrder($this->mFilter->getOrder());
		
		$this->mModuleObjects =& $moduleHandler->getObjects($criteria);

		return LEGACY_FRAME_VIEW_INDEX;
	}
	
	function execute(&$controller, &$xoopsUser)
	{
		if (xoops_getrequest('_form_control_cancel') != null) {
			return LEGACY_FRAME_VIEW_CANCEL;
		}

		$this->mActionForm->fetch();
		$this->mActionForm->validate();

		if ($this->mActionForm->hasError()) {
			return $this->_processConfirm($controller, $xoopsUser);
		}
		else {
			return $this->_processSave($controller, $xoopsUser);
		}
	}
	
	function _processConfirm(&$controller,&$xoopsUser)
	{
		$moduleHandler =& xoops_gethandler('module');
		$t_objectArr =& $moduleHandler->getObjects();

		//
		// Do mapping.
		//
		foreach ($t_objectArr as $module) {
			$this->mModuleObjects[$module->get('mid')] =& $module;
			unset($module);
		}

		return LEGACY_FRAME_VIEW_INPUT;
	}

	function _processSave(&$controller, &$xoopsUser)
	{
		$moduleHandler =& xoops_gethandler('module');
		$t_objectArr =& $moduleHandler->getObjects();
		
		$successFlag = true;
		foreach($t_objectArr as $module) {
			$mid = $module->get('mid');
			$module->set('name', $this->mActionForm->get('name', $mid));
			$module->set('weight', $this->mActionForm->get('weight', $mid));
			$module->set('isactive', $this->mActionForm->get('isactive', $mid));

			//
			// Store.
			//
			$successFlag &= $moduleHandler->insert($module);
		}
		
		return $successFlag ? LEGACY_FRAME_VIEW_SUCCESS : LEGACY_FRAME_VIEW_ERROR;
	}

	/**
	 * To support a template writer, this send the list of mid that actionForm kept.
	 */
	function executeViewInput(&$controller, &$xoopsUser, &$render)
	{
		$render->setTemplateName("module_list_confirm.html");
		$render->setAttribute('moduleObjects', $this->mModuleObjects);
		$render->setAttribute('actionForm', $this->mActionForm);
		
		//
		// To support a template writer, this send the list of mid that
		// actionForm kept.
		//
		$t_arr =& $this->mActionForm->get('name');
		$render->setAttribute('mids', array_keys($t_arr));
	}

	function executeViewIndex(&$controller, &$xoopsUser, &$render)
	{
		$render->setTemplateName("module_list.html");
		$render->setAttribute('actionForm', $this->mActionForm);

		//
		// Load adminmenu before we assign these to template.
		//
		foreach(array_keys($this->mModuleObjects) as $key) {
			$this->mModuleObjects[$key]->loadAdminMenu();
			$this->mModuleObjects[$key]->loadInfo($this->mModuleObjects[$key]->get('dirname'));
		}
		$render->setAttribute('moduleObjects', $this->mModuleObjects);
	}

	function executeViewSuccess(&$controller,&$xoopsUser,&$renderer)
	{
		$controller->executeForward('./index.php?action=ModuleList');
	}

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

	function executeViewCancel(&$controller,&$xoopsUser,&$renderer)
	{
		$controller->executeForward('./index.php?action=ModuleList');
	}
}

?>