<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Site\Controllers;

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use BlackSheepResearch\Component\UserPoints\Site\UserController;
use BlackSheepResearch\Component\UserPoints\Site\Utils\Session;

/**
 * @package UserPoints
 */
class UsersControl extends UserController {

    /**
     * Custom Constructor
     */
    public function __construct() {
        parent::__construct();
    }

    public function display($cachable = false, $urlparams = false) {
        $com_params = ComponentHelper::getParams('com_userpoints');
        if (!$com_params->get('allow_user_lists')) {
            Factory::getApplication()->setHeader('Status', '403', true);
            return;
        }
        $_useAvatarFrom = $com_params->get('useAvatarFrom', '');
        $_profilelink = $com_params->get('linkToProfile', '');
        $_allowGuestUserViewProfil = $com_params->get('allowGuestUserViewProfil', 1);

        $model = $this->getModel('userpoint');
        $view = $this->getView('users', 'html');

        $result = $model->_getUsersList();

        // Get the parameters of the active menu item
        $params = $model->_getParamsAUP();

        $this->viewdata['params'] = $params;
        $this->viewdata['allowGuestUserViewProfil'] = $_allowGuestUserViewProfil;
        $this->viewdata['rows'] = $result[0];
        $this->viewdata['total'] = $result[1];
        $this->viewdata['limit'] = $result[2];
        $this->viewdata['limitstart'] = $result[3];
        $this->viewdata['lists'] = $result[4];
        $this->viewdata['useAvatarFrom'] = $_useAvatarFrom;
        $this->viewdata['linkToProfile'] = $_profilelink;

        // Display
        $view->_display();
    }
    
    public function register () {
        $referrer = Factory::getApplication()->input->get('referrer', '');
        Session::set('referrer', $referrer);
        $this->redirect('index.php?option=com_users&view=registration/');
    }
}
