<?php
/**
 * @package user
 * @version $Id$
 */

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

require_once XOOPS_MODULE_PATH . "/user/forms/UserDeleteForm.class.php";

/**
 * This action is for self delete function.
 * 
 * Site owner want various procedure to this action. Therefore, this action may
 * have to implement main logic with Delegate only.
 */
class User_UserDeleteAction extends User_Action
{
	var $mActionForm = null;
	var $mObject = null;
	
	var $mSelfDelete = false;
	var $mSelfDeleteConfirmMessage = "";

	function prepare(&$controller, &$xoopsUser, $moduleConfig)
	{
		$this->mSelfDelete = $moduleConfig['self_delete'];
		$this->mSelfDeleteConfirmMessage = $moduleConfig['self_delete_confirm'];
		
		$this->mActionForm =& new User_UserDeleteForm();
		$this->mActionForm->prepare();
		
		//
		// pre condition check
		//
		if (!$this->mSelfDelete) {
			$controller->executeForward(XOOPS_URL . '/');
		}
		
		if (is_object($xoopsUser)) {
			$handler =& xoops_getmodulehandler('users', 'user');
			$this->mObject =& $handler->get($xoopsUser->get('uid'));
		}
	}
	
	function isSecure()
	{
		return true;
	}

	function hasPermission(&$controller, &$xoopsUser, $moduleConfig)
	{
		if ($xoopsUser->get('uid') == 1) {
			return false;
		}
		
		return true;
	}
	
	function getDefaultView(&$controller, &$xoopsUser)
	{
		return USER_FRAME_VIEW_INPUT;
	}
	
	/**
	 * FIXME: Need FORCE LOGOUT here?
	 */
	function execute(&$controller, &$xoopsUser)
	{
		$this->mActionForm->fetch();
		$this->mActionForm->validate();
		
		if ($this->mActionForm->hasError()) {
			return $this->getDefaultView($controller, $xoopsUser);
		}
		
		if ($this->_doDelete($controller, $xoopsUser)) {
			XCube_DelegateUtils::call('Legacy.Event.UserDelete', new XCube_Ref($this->mObject));
			
			return USER_FRAME_VIEW_SUCCESS;
		}
		
		return USER_FRAME_VIEW_ERROR;
	}
	
	/**
	 * Exection deleting.
	 * 
	 * @return bool
	 */
	function _doDelete(&$controller, &$xoopsUser)
	{
		$handler =& xoops_gethandler('member');
		if ($handler->deleteUser($xoopsUser)) {
			$handler =& xoops_gethandler('online');
			$handler->destroy($this->mObject->get('uid'));
			xoops_notification_deletebyuser($this->mObject->get('uid'));
			
			return true;
		}
		
		return false;
	}
	
	function executeViewInput(&$controller, &$xoopsUser, &$render)
	{
		$render->setTemplateName("user_delete.html");
		$render->setAttribute('object', $this->mObject);
		$render->setAttribute('actionForm', $this->mActionForm);
		$render->setAttribute('self_delete_message', $this->mSelfDeleteConfirmMessage);
	}
	
	function executeViewSuccess(&$controller, &$xoopsUser, &$render)
	{
		$render->setTemplateName("user_delete_success.html");
		$render->setAttribute("object", $this->mObject);
	}
	
	function executeViewError(&$controller, &$xoopsUser, &$render)
	{
		$controller->executeRedirect(XOOPS_URL . '/', 3, _MD_USER_ERROR_DBUPDATE_FAILED);
	}
}

?>