<?php

use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Factory;

// No direct access.
defined('_JEXEC') or die;

class ClubsController extends BaseController
{
    
    public function display($cachable = false, $params = array())
    {
        $auth = new ClubsHelperAuth();
        
        $app = Factory::getApplication();
        $view = $app->input->getCmd('view');
        
        // Most of the pages can only be viewd as a logged-in user
        if($auth->isValidUserLoggedIn())
        {
            if($this->isUrlAllowed($view))
            {
                $this->doDisplay($cachable, $params);
                return;
            }
            else
            {
                // User is not allowed to see the site, give useful information
            }
        }
        else
        {
            if($view === 'login' || $view === 'publicclubs')
            {
                $this->doDisplay($cachable, $params);
                return;
            }
            else
            {
                // User needs to login in order to see the site
                $this->redirectToLogin();
            }
        }
        
        $this->redirectToLogin();
    }
    
    private function doDisplay($cachable, $params)
    {
        Factory::getDocument()->addStyleSheet(Uri::base(true) .  "components/com_clubs/css/clubs.css");
        parent::display($cachable, $params);
    }
    
    private function redirectToLogin()
    {
        // XXX Attach URL to forward later
        $this->setRedirect('index.php?opion=com_clubs&view=login');
    }
    
    private function isUrlAllowed($view)
    {
        // FIXME Insert ACLs checking here
        return true;
    }
}