<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\View\Overlook;
/**
 * @package JREALTIMEANALYTICS::OVERVIEW::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage overlook
 * @author Joomla! Extensions Store
 * @copyright (C) 2014 - 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 JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Helpers\Toolbars as ToolbarHelper;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\View as JRealtimeView;

/**
 * @package JREALTIMEANALYTICS::OVERVIEW::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage overlook
 * @since 2.4
 */
class HtmlView extends JRealtimeView {
	// Template view variables
	protected $userid;
	protected $nocache;
	protected $lists;
	protected $monthString;
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addDisplayToolbar() {
		$user = $this->app->getIdentity();
		ToolbarHelper::title( Text::_( 'COM_JREALTIME_OVERVIEW_GRAPH' ), 'jrealtimeanalytics' );
		ToolbarHelper::custom('overlook.displaypdf', 'download', 'download', 'COM_JREALTIME_EXPORTPDF', false);
		ToolbarHelper::custom('cpanel.display', 'home', 'home', 'COM_JREALTIME_CPANEL', false);
	}
	
	/**
	 * Default display listEntities
	 *        	
	 * @access public
	 * @param string $tpl
	 * @return void
	 */
	public function display($tpl = null) {
		$model = $this->getModel();
		
		// Get main records
		$monthString = null;
		$monthSelected = $this->getModel()->getState('statsMonth', null);
		if($monthSelected) {
			$graphData = $model->getDataByMonth();
			$monthString = '- ' . date('F', mktime(0, 0, 0, $monthSelected));
		} else {
			$graphData = $model->getData();
		}
		
		$lists = $model->getLists();
		
		$graphTypeMethod = 'buildGeneric' . ucfirst($this->getModel()->getState('graphType', 'Bars'));
		$graphGenerator = $this->getModel()->getState('graphRenderer');
		$graphGenerator->$graphTypeMethod($graphData, '_serverstats_overview.png', 
													  Text::sprintf('COM_JREALTIME_OVERVIEW_STATS', $monthString), 
													  array('COM_JREALTIME_TOTAL_VISITED_PAGES', 'COM_JREALTIME_TOTAL_VISITORS'),
													  array('x'=>Text::_('COM_JREALTIME_XTITLE_DAYS')));

		$doc = $this->app->getDocument();
		$this->loadJQuery($doc);
		$this->loadJQueryUI($doc); // Required for calendar feature
		$this->loadBootstrap($doc);
		$this->loadJQFancybox($doc);
		
		$doc->getWebAssetManager()->addInlineScript("
			Joomla.submitbutton = function(pressbutton) {
				Joomla.submitform( pressbutton );
				if (pressbutton == 'overlook.displaypdf') {
					jQuery('#adminForm input[name=task]').val('overlook.display');
				}
				return true;
			}
		");
		
		$dates = array('start'=>$this->getModel()->getState('fromPeriod'), 'to'=>$this->getModel()->getState('toPeriod'));
		$this->user = $this->app->getIdentity ();
		$this->userid = $this->user->id;
		$this->nocache = '?time=' . time();
		$this->lists = $lists;
		$this->monthString = $monthString;
		$this->option = $this->getModel ()->getState ( 'option' );
		
		// Aggiunta toolbar
		$this->addDisplayToolbar();
		
		parent::display ( 'list' );
	}
}