<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * VikRestaurants cron job logs view.
 *
 * @since 1.10
 */
class VikRestaurantsViewcronjoblogs extends JViewVRE
{
	/**
	 * VikRestaurants view display method.
	 *
	 * @return  void
	 */
	function display($tpl = null)
	{
		$app = JFactory::getApplication();
		$dbo = JFactory::getDbo();

		// set the toolbar
		$this->addToolBar();

		$filters = [];
		$filters['search']  = $app->getUserStateFromRequest($this->getPoolName() . '.search', 'search', '', 'string');
		$filters['date']    = $app->getUserStateFromRequest($this->getPoolName() . '.date', 'date', '', 'string');
		$filters['id_cron'] = $app->input->getUint('id_cron', 0);

		$this->filters = $filters;

		$lim 	= $app->getUserStateFromRequest($this->getPoolName() . '.limit', 'limit', $app->get('list_limit'), 'int');
		$lim0 	= $this->getListLimitStart($filters + ['limit' => $lim]);
		$navbut	= "";

		$rows = [];

		$q = $dbo->getQuery(true)
			->select('SQL_CALC_FOUND_ROWS l.*')
			->from($dbo->qn('#__vikrestaurants_cronjob_log', 'l'))
			->where($dbo->qn('l.id_cronjob') . ' = ' . $filters['id_cron'])
			->order($dbo->qn('l.id') . ' DESC');

		if ($filters['search'])
		{
			if (preg_match("/^id:\s*(\d+)/i", $filters['search'], $match))
			{
				// we are searching by ID
				$q->where($dbo->qn('l.id') . ' = ' . (int) $match[1]);
			}
			else
			{
				$q->where($dbo->qn('l.content') . ' LIKE ' . $dbo->q("%{$filters['search']}%"));
			}
		}

		if (E4J\VikRestaurants\Helpers\DateHelper::isNull($filters['date']) === false)
		{
			 // Get locale SQL strings to have dates adjusted to the current
			 // timezone. This way the dates will be refactored for being
			 // used in UTC, even if the locale is different.
			$start = E4J\VikRestaurants\Helpers\DateHelper::getSqlDateLocale($filters['date'],  0,  0,  0);
			$end   = E4J\VikRestaurants\Helpers\DateHelper::getSqlDateLocale($filters['date'], 23, 59, 59);

			$q->where($dbo->qn('l.createdon') . ' BETWEEN ' . $dbo->q($start) . ' AND ' . $dbo->q($end));
		}

		/**
		 * It is possible to lean on the "onBeforeListQueryCronjoblogs" plugin event
		 * to manipulate the query used to load the list of records.
		 *
		 * @since 1.10
		 */
		$this->onBeforeListQuery($q);

		$dbo->setQuery($q, $lim0, $lim);
		$dbo->execute();

		// assert limit used for list query
		$this->assertListQuery($lim0, $lim, $filters['id_cron']);

		if ($dbo->getNumRows())
		{
			$rows = $dbo->loadAssocList();
			$dbo->setQuery('SELECT FOUND_ROWS();');
			jimport('joomla.html.pagination');
			$pageNav = new JPagination($dbo->loadResult(), $lim0, $lim);
			$navbut = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]);
		}

		$this->rows   = $rows;
		$this->navbut = $navbut;
		
		// display the template (default.php)
		parent::display($tpl);
	}

	/**
	 * Setting the toolbar.
	 *
	 * @return 	void
	 */
	protected function addToolBar()
	{
		// add menu title and some buttons to the page
		JToolBarHelper::title(JText::translate('VREMAINTITLEVIEWCRONJOBLOGS'), 'vikrestaurants');

		JToolBarHelper::back('JTOOLBAR_BACK', 'index.php?option=com_vikrestaurants&view=cronjobs');
		
		if (JFactory::getUser()->authorise('core.delete', 'com_vikrestaurants'))
		{
			JToolBarHelper::deleteList(VikRestaurants::getConfirmSystemMessage(), 'cronjoblog.delete');

			JToolbarHelper::custom('cronjoblog.truncate', 'trash', 'trash', JText::translate('VRDELETEALL'), false);
		}
	}
}
