<?php
/**
 * @package    VP Advanced User
 * @author     Abhishek Das <info@virtueplanet.com>
 * @copyright  Copyright (C) 2012-2019 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

JLoader::register('VPAdvancedUserController', JPATH_COMPONENT . '/controller.php');

/**
 * Reset controller class.
 *
 * @since  1.0
 */
class VPAdvancedUserControllerReset extends VPAdvancedUserController
{
	/**
	 * Method to request a password reset.
	 *
	 * @return  boolean
	 *
	 * @since   1.6
	 */
	public function request()
	{
		// Check for request forgeries.
		$this->_checkToken();

		$app   = JFactory::getApplication();
		$model = $this->getModel('Reset', 'VPAdvancedUserModel');
		$data  = $this->input->post->get('jform', array(), 'array');

		// Submit the password reset request.
		$return = $model->processResetRequest($data);

		// Check for a hard error.
		if ($return instanceof Exception)
		{
			// Get the error message to display.
			if ($app->get('error_reporting'))
			{
				$message = $return->getMessage();
			}
			else
			{
				$message = JText::_('COM_USERS_RESET_REQUEST_ERROR');
			}

			// Get the route to the next page.
			$itemid = VPAdvancedUserHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
			$route  = 'index.php?option=com_vpadvanceduser&view=reset' . $itemid;

			// Go back to the request form.
			$this->setRedirect(JRoute::_($route, false), $message, 'error');
			return false;
		}
		elseif ($return === false)
		{
			// The request failed.
			// Get the route to the next page.
			$itemid = VPAdvancedUserHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
			$route  = 'index.php?option=com_vpadvanceduser&view=reset' . $itemid;

			// Go back to the request form.
			$message = JText::sprintf('COM_USERS_RESET_REQUEST_FAILED', $model->getError());
			$this->setRedirect(JRoute::_($route, false), $message, 'notice');
			return false;
		}

		// The request succeeded.
		// Get the route to the next page.
		$itemid = VPAdvancedUserHelperRoute::getResetRoute();
		$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
		$route  = 'index.php?option=com_vpadvanceduser&view=reset&layout=confirm' . $itemid;

		// Proceed to step two.
		$this->setRedirect(JRoute::_($route, false));
		return true;
	}

	/**
	 * Method to confirm the password request.
	 *
	 * @return  boolean
	 *
	 * @access	public
	 * @since   1.6
	 */
	public function confirm()
	{
		// Check for request forgeries.
		$this->_checkToken('request');

		$app   = JFactory::getApplication();
		$model = $this->getModel('Reset', 'VPAdvancedUserModel');
		$data  = $this->input->get('jform', array(), 'array');

		// Confirm the password reset request.
		$return = $model->processResetConfirm($data);

		// Check for a hard error.
		if ($return instanceof Exception)
		{
			// Get the error message to display.
			if ($app->get('error_reporting'))
			{
				$message = $return->getMessage();
			}
			else
			{
				$message = JText::_('COM_USERS_RESET_CONFIRM_ERROR');
			}

			// Get the route to the next page.
			$itemid = VPAdvancedUserHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
			$route  = 'index.php?option=com_vpadvanceduser&view=reset&layout=confirm' . $itemid;

			// Go back to the confirm form.
			$this->setRedirect(JRoute::_($route, false), $message, 'error');
			return false;
		}
		elseif ($return === false)
		{
			// Confirm failed.
			// Get the route to the next page.
			$itemid = VPAdvancedUserHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
			$route  = 'index.php?option=com_vpadvanceduser&view=reset&layout=confirm' . $itemid;

			// Go back to the confirm form.
			$message = JText::sprintf('COM_USERS_RESET_CONFIRM_FAILED', $model->getError());
			$this->setRedirect(JRoute::_($route, false), $message, 'notice');
			return false;
		}

		// Confirm succeeded.
		// Get the route to the next page.
		$itemid = VPAdvancedUserHelperRoute::getResetRoute();
		$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
		$route  = 'index.php?option=com_vpadvanceduser&view=reset&layout=complete' . $itemid;

		// Proceed to step three.
		$this->setRedirect(JRoute::_($route, false));
		return true;
	}

	/**
	 * Method to complete the password reset process.
	 *
	 * @return  boolean
	 *
	 * @since   1.6
	 */
	public function complete()
	{
		// Check for request forgeries.
		$this->_checkToken();

		$app   = JFactory::getApplication();
		$model = $this->getModel('Reset', 'VPAdvancedUserModel');
		$data  = $this->input->post->get('jform', array(), 'array');

		// Complete the password reset request.
		$return = $model->processResetComplete($data);

		// Check for a hard error.
		if ($return instanceof Exception)
		{
			// Get the error message to display.
			if ($app->get('error_reporting'))
			{
				$message = $return->getMessage();
			}
			else
			{
				$message = JText::_('COM_USERS_RESET_COMPLETE_ERROR');
			}

			// Get the route to the next page.
			$itemid = VPAdvancedUserHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
			$route  = 'index.php?option=com_vpadvanceduser&view=reset&layout=complete' . $itemid;

			// Go back to the complete form.
			$this->setRedirect(JRoute::_($route, false), $message, 'error');
			return false;
		}
		elseif ($return === false)
		{
			// Complete failed.
			// Get the route to the next page.
			$itemid = VPAdvancedUserHelperRoute::getResetRoute();
			$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
			$route  = 'index.php?option=com_vpadvanceduser&view=reset&layout=complete' . $itemid;

			// Go back to the complete form.
			$message = JText::sprintf('COM_USERS_RESET_COMPLETE_FAILED', $model->getError());
			$this->setRedirect(JRoute::_($route, false), $message, 'notice');
			return false;
		}

		// Complete succeeded.
		// Get the route to the next page.
		$itemid = VPAdvancedUserHelperRoute::getLoginRoute();
		$itemid = $itemid !== null ? '&Itemid=' . $itemid : '';
		$route  = 'index.php?option=com_vpadvanceduser&view=login' . $itemid;

		// Proceed to the login form.
		$message = JText::_('COM_USERS_RESET_COMPLETE_SUCCESS');
		$this->setRedirect(JRoute::_($route, false), $message);
		return true;
	}
}
