<?php

/**
 *
 * PHP Pro Bid $Id$ +tjxGm01mTMPY7jkHnBOzigNuwV0I5HgdsXEZKsMhvx/zdvoA4ImlSkW8rjG9fJOwSmRzljRHHkuTlsdfwkkdA==
 *
 * @link        http://www.phpprobid.com
 * @copyright   Copyright (c) 2017 Online Ventures Software & CodeCube SRL
 * @license     http://www.phpprobid.com/license Commercial License
 *
 * @version     7.10 [rev.7.10.01]
 */

/**
 * user verification check controller plugin class
 * the plugin will be called when trying to list or to purchase an item.
 * - if mandatory seller verification is enabled and the listing create/edit page is accessed,
 * the user will be redirected to the verification page
 * - if mandatory buyer verification is enabled and the purchase confirm page is accessed (or the shopping cart checkout button is clicked),
 * the user will be redirected to the verification page
 *
 * @7.9: added clearRequestMethod method so that the verification page isnt accidentally submitted for example when clicking on "place bid"
 * and being automatically redirected
 * @7.10: added the cart checkout action as part of the mandatory buyer verification check
 */

namespace Listings\Controller\Plugin;

use Cube\Controller\Plugin\AbstractPlugin,
    Cube\Controller\Front;

class UserVerificationCheck extends AbstractPlugin
{

    public function preDispatch()
    {
        $request = $this->getRequest();

        $controller = $request->getController();
        $action = $request->getAction();

        if (
            $controller == 'Purchase' ||
            ($controller == 'Cart' && $action == 'Checkout') ||
            ($controller == 'Listing' && $action == 'Create')
        ) {
            $bootstrap = Front::getInstance()->getBootstrap();
            $user = $bootstrap->getResource('user');
            $settings = $bootstrap->getResource('settings');

            if (count($user) > 0) {
                if (!$user->isVerified()) {
                    if (
                        ((($controller == 'Purchase') || ($controller == 'Cart' && $action == 'Checkout')) && $settings['buyer_verification_mandatory']) ||
                        ($controller == 'Listing' && $action == 'Create' && $settings['seller_verification_mandatory'])
                    ) {
                        $url = $bootstrap->getResource('view')->url(array(
                            'module'     => 'members',
                            'controller' => 'user',
                            'action'     => 'verification'
                        ));

                        $this->getResponse()
                            ->setRedirect($url, 302)
                            ->sendHeaders();
                    }
                }
            }
        }
    }

}

