<?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\Administrator\Controllers;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use BlackSheepResearch\Component\UserPoints\Administrator\AdminController;
use BlackSheepResearch\Component\UserPoints\Site\Problems\PointsDetails;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

class CpanelControl extends AdminController {

    /**
     * Show Control Panel
     */
    public function cpanel() {

        $synch = Factory::getApplication()->input->get('synch', '', 'cmd');
        $recalculate = Factory::getApplication()->input->get('recalculate', '', 'cmd');

        $_params = ComponentHelper::getParams('com_userpoints');

        $updatekey = $_params->get('Update_Key', '');
        if ($updatekey) {
            $db = SQL::getInstance();
            $db->setQuery("UPDATE #__update_sites SET extra_query = 'hash=$updatekey' WHERE name LIKE '%UserPoints%'");
            $db->execute();
        }

        $model = $this->getModel('statistics');
        $model3 = $this->getModel('users');
        $modelUpdate = $this->getModel('upgrade');

        $view = $this->getView('CpanelHTML', 'html');

        $_top10 = $model->_load_top10();
        $_unapproved = PointsDetails::getUnapproved();
        $_needsync = $model->_needsync();
        $_last_Activities = $model3->_last_Activities();
        $_communitypoints = $model->_totalcurrentcommunitypoints();

        if ($_params->get('showUpdateCheck', 0)) {
            $modelUpdate->_getUpdate();
            /*
              $cache = Factory::getCache('com_userpoints');
              // save configured lifetime
              @$lifetime = $cache->lifetime;
              $cache->setLifeTime(15 * 60); // 15 minutes to seconds
              // save cache conf
              $conf = Factory::getConfig();
              // check if cache is enabled in configuration
              //$cacheactive = $conf->getValue('config.caching');
              $cacheactive = $conf->get('config.caching');
              $cache->setCaching(1); //enable caching
              // if the cache expired, the method will be called again and the result will be stored for 'lifetime' seconds
              $_check = $cache->call(array($modelUpdate, '_getUpdate'));
              // revert configuration
              $cache->setCaching($cacheactive);
             *
             */
        } else {
            $_check = '';
        }

        // check if general configuration is Ok
        $configset = $modelUpdate->_checkConfig();

        // Check new official rules installed
        $modelUpdate->_checkNewRule();

        $this->viewdata['configset'] = $configset;
        $this->viewdata['top10'] = $_top10;
        $this->viewdata['unapproved10'] = $_unapproved;
        $this->viewdata['totalunapproved'] = count($_unapproved);
        $this->viewdata['needSync'] = $_needsync;
        $this->viewdata['check'] = $_check;
        $this->viewdata['params'] = $_params;
        $this->viewdata['lastactivities'] = $_last_Activities;
        $this->viewdata['synch'] = $synch;
        $this->viewdata['recalculate'] = $recalculate;
        $this->viewdata['communitypoints'] = $_communitypoints;

        $view->show();
    }

}
