<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Controller;
/**
 * @package JREALTIME::GOOGLE::administrator::components::com_jrealtimeanalytics
 * @subpackage controllers
 * @author Joomla! Extensions Store
 * @copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html  
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Controller as JRealtimeController;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Renderers\Adapter\Xls as RenderersAdapterXls;

/**
 * Main controller
 * @package JREALTIME::GOOGLE::administrator::components::com_jrealtimeanalytics
 * @subpackage controllers
 * @since 2.6
 */
class WebmastersController extends JRealtimeController {
	/**
	 * Set model state from session userstate
	 * @access protected
	 * @param string $scope
	 * @return object
	 */
	protected function setModelState($scope = 'default', $ordering = true): object {
		$option = $this->option;
		
		// Get default model
		$defaultModel = $this->getModel();
		
		$defaultStartPeriod = date ( "Y-m-01", strtotime ( date ( "Y-m-d" ) ) );
		$defaultEndPeriod = date ( "Y-m-d", strtotime ( "-1 day", strtotime ( "+1 month", strtotime ( date ( "Y-m-01" ) ) ) ) );
		$fromPeriod = $this->getUserStateFromRequest( "$option.$scope.fromperiod", 'fromperiod', strval($defaultStartPeriod));
		$toPeriod = $this->getUserStateFromRequest( "$option.$scope.toperiod", 'toperiod', strval($defaultEndPeriod));
		
		// Set model state
		$defaultModel->setState('fromPeriod', $fromPeriod);
		$defaultModel->setState('toPeriod', $toPeriod);
		$defaultModel->setState ( 'option', $option );
		
		return $defaultModel;
	}
	
	/**
	 * Default listEntities
	 * 
	 * @access public
	 * @param $cachable string
	 *       	 the view output will be cached
	 * @return void
	 */
	public function display($cachable = false, $urlparams = false) {
		// Access check.
		if (!$this->user->authorise ( 'jrealtime.webmasters', $this->option )) {
			$this->setRedirect('index.php?option=com_jrealtimeanalytics&task=cpanel.display', Text::_('COM_JREALTIME_ERROR_ALERT_NOACCESS'));
			return false;
		}
		
		$model = $this->setModelState('webmasters');
		
		// Switch task for XLS rendering
		if($this->task == 'displayxls') {
			// Get view always HTML format
			$view =  $this->getView ('webmasters', 'html', '', array('base_path' => $this->basePath, 'layout' => 'default'));
			// Push the model into the view (as default)
			$view->setModel ( $model, true );
			$view->setLayout('xls');
			
			//Creazione buffer output
			ob_start ();
			// Parent construction and view display
			$view->display('webmasters');
			$bufferContent = ob_get_contents ();
			ob_end_clean ();
			
			$xlsRenderer = new RenderersAdapterXls();
			$xlsRenderer->renderContent($bufferContent, $model, 'google_searchconsole_stats_');
		} else {
			parent::display($cachable, $urlparams);
		}
	}
	
	/**
	 * Delete a db table entity
	 *
	 * @access public
	 * @return bool
	 */
	public function deleteEntity(): bool {
		// Load della model e checkin before exit
		$model = $this->getModel ();

		if (! $model->deleteEntity ( null )) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getException ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getExceptionLevel () );
			$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".display", Text::_ ( 'COM_JREALTIME_GOOGLE_WEBMASTERS_ERROR_' . 'LOGOUT' ) );
			return false;
		}
	
		$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".display", Text::_ ( 'COM_JREALTIME_GOOGLE_WEBMASTERS_SUCCESS_LOGOUT' ) );
		
		return true;
	}
	
	/**
	 * Class Constructor
	 *
	 * @access public
	 * @return Object&
	 */
	public function __construct($config = array(), ?MVCFactoryInterface $factory = null, $app = null, $input = null) {
		parent::__construct($config, $factory, $app, $input);
		
		$this->registerTask('displayxls', 'display');

		// Composer autoloader
		if (PHP_VERSION_ID >= 70205) {
			require_once JPATH_COMPONENT_ADMINISTRATOR. '/Framework/composer/autoload_real.php';
			\ComposerAutoloaderInitcb4c0ac1dedbbba2f0b42e9cdf4d93d7JReal::getLoader();
		} else {
			// Fallback to legacy API
			require_once JPATH_COMPONENT_ADMINISTRATOR. '/Framework/composerlegacy/autoload_real.php';
			\ComposerAutoloaderInitfc5c9af51413a149e4084a610a3ab6deJReal::getLoader();
		}
	}
}