<?php

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

require_once XOOPS_MODULE_PATH . "/base/class/AbstractEditAction.class.php";
require_once XOOPS_MODULE_PATH . "/base/admin/forms/CommentAdminEditForm.class.php";

class Legacy_CommentEditAction extends Legacy_AbstractEditAction
{
	/**
	 * Override. At first, call _setupObject().
	 */
	function prepare(&$controller, &$xoopsUser)
	{
		$this->_setupObject();
		$this->_setupActionForm();
	}
	
	function _getId()
	{
		return isset($_REQUEST['com_id']) ? $_REQUEST['com_id'] : 0;
	}

	function &_getHandler()
	{
		$handler =& xoops_getmodulehandler('comment');
		return $handler;
	}
	
	function isEnableCreate()
	{
		return false;
	}

	/**
	 * Choose appropriate ActionForm by the value of com_status.
	 */
	function _setupActionForm()
	{
		if ($this->mObject->get('com_status') == 1) {
			$this->mActionForm =& new Legacy_PendingCommentAdminEditForm();

			$root =& XCube_Root::getSingleton();
			$root->mEventManager->add("Module.Legacy.Comment.Update.Success", new XCube_InstanceDelegate($this, "doApprove"));
		}
		else {
			$this->mActionForm =& new Legacy_ApprovalCommentAdminEditForm();
		
			$root =& XCube_Root::getSingleton();
			$root->mEventManager->add("Module.Legacy.Comment.Update.Success", new XCube_InstanceDelegate($this, "doUpdate"));
		}
		$this->mActionForm->prepare();
	}

	function executeViewInput(&$controller, &$xoopsUser, &$render)
	{
		$this->mObject->loadUser();
		$this->mObject->loadModule();
		$this->mObject->loadStatus();
		
		$render->setTemplateName("comment_edit.html");
		$render->setAttribute('actionForm', $this->mActionForm);
		$render->setAttribute('object', $this->mObject);
		
		$subjectHandler =& xoops_gethandler('subjecticon');
		$subjectIconArr =& $subjectHandler->getObjects();
		
		$render->setAttribute('subjectIconArr', $subjectIconArr);

		$statusHandler =& xoops_getmodulehandler('commentstatus');
		if ($this->mObject->get('com_status') == XOOPS_COMMENT_PENDING) {
			$statusArr =& $statusHandler->getObjects();
		}
		else {
			$statusArr = array();
			$statusArr[0] =& $statusHandler->get(XOOPS_COMMENT_ACTIVE);
			$statusArr[1] =& $statusHandler->get(XOOPS_COMMENT_HIDDEN);
		}
		
		$render->setAttribute('statusArr', $statusArr);
	}

	function executeViewSuccess(&$controller, &$xoopsUser, &$render)
	{
		$controller->executeForward("./index.php?action=CommentList");
	}

	function executeViewError(&$controller, &$xoopsUser, &$render)
	{
		redirect_header("./index.php?action=CommentList", 1, _AD_BASE_ERROR_DBUPDATE_FAILED);
	}
	
	/**
	 * @static
	 * @return Return array as the informations of comments. If $comment has fatal status, return false.
	 */
	function loadCallbackFile(&$comment)
	{
		$handler =& xoops_gethandler('module');
		$module =& $handler->get($comment->get('com_modid'));
		
		if (!is_object($module)) {
			return false;
		}
		
		$comment_config = $module->getInfo('comments');
		
		//
		// Load call-back file
		//
		$file = XOOPS_MODULE_PATH . "/" . $module->get('dirname') . "/" . $comment_config['callbackFile'];
		if (!file_exists($file)) {
			return false;
		}
		
		require_once $file;
		
		return $comment_config;
	}
	
	function doApprove(&$sender, &$eventArgs)
	{
		$comment =& $eventArgs['comment'];
		$comment_config = Legacy_CommentEditAction::loadCallbackFile($comment);
		
		if ($comment_config == false) {
			return;
		}
		
		$function = $comment_config['callback']['approve'];
		
		if (function_exists($function)) {
			call_user_func($function, $comment);
		}
		
		Legacy_CommentEditAction::addUserPost($comment);		
	}
	
	function doUpdate(&$sender, &$eventArgs)
	{
		$comment =& $eventArgs['comment'];
		
		//
		// Adjust user's post count.
		//
		if ($comment->get('com_uid') > 0) {
			$handler =& xoops_gethandler('member');

			//
			// TODO We should adjust the following lines and handler's design.
			// We think we should not use getUser() and updateUserByField in XCube 2.1.
			//
			$user =& $handler->getUser($comment->get('com_uid'));
			$handler->updateUserByField($user, 'posts', $user->get('posts') + 1);
		}
		
		//
		// call back
		//
		$comment_config = Legacy_CommentEditAction::loadCallbackFile($comment);
		
		if ($comment_config == false) {
			return;
		}
		
		$function = $comment_config['callback']['update'];
		
		if (function_exists($function)) {
			call_user_func($function, $comment);
		}
	}
}

?>
