<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\MVC\View\HtmlView;

/**
 * View class for email template list.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardViewTemplates extends HtmlView
{
	/**
	 * An array of items.
	 *
	 * @var  array
	 */
	protected $items;

	/**
	 * The pagination object.
	 *
	 * @var  Pagination
	 */
	protected $pagination;

	/**
	 * The model state.
	 *
	 * @var  object
	 */
	protected $state;

	/**
	 * Method to display the view.
	 *
	 * @param   string  $tpl  A template file to load. [optional]
	 *
	 * @return  mixed  A string if successful, otherwise an Error object.
	 *
	 * @since   1.0.0
	 */
	public function display($tpl = null)
	{
		$this->items			= $this->get('Items');
		$this->pagination		= $this->get('Pagination');
		$this->state			= $this->get('State');
		$this->params			= $this->state->get('params');

		if (count($errors = $this->get('Errors')))
		{
			return CMGiftCardHelper::throwException(implode("\n", $errors), 500);
		}

		if (version_compare(JVERSION, '4.0.0-alpha1', '<'))
		{
			CMGiftCardHelper::addSubmenu('templates');
			$this->sidebar = \JHtmlSidebar::render();
			$this->setLayout('default3');
			$this->addToolbar3();
		}
		else
		{
			$this->addToolbar();
		}

		return parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.0.0
	 */
	protected function addToolbar()
	{
		$canDo = ContentHelper::getActions('com_cmgiftcard');

		$toolbar = Toolbar::getInstance('toolbar');
		ToolbarHelper::title(Text::_('COM_CMGIFTCARD_MANAGER_TEMPLATES'), 'envelope');

		if ($canDo->get('core.edit.state'))
		{
			$toolbar->checkin('templates.checkin')->listCheck(true);
		}

		if ($canDo->get('core.admin') || $canDo->get('core.options'))
		{
			$toolbar->preferences('com_cmgiftcard');
		}
	}

	/**
	 * Add the page title and toolbar for Joomla 3.
	 *
	 * @return  void
	 *
	 * @since   2.0.0-alpha1
	 */
	protected function addToolbar3()
	{
		$canDo = ContentHelper::getActions('com_cmgiftcard');

		ToolbarHelper::title(Text::_('COM_CMGIFTCARD_MANAGER_TEMPLATES'), 'envelope');

		if ($canDo->get('core.edit'))
		{
			ToolbarHelper::editList('template.edit');
		}

		if ($canDo->get('core.edit.state'))
		{
			ToolbarHelper::checkin('templates.checkin');
		}

		if ($canDo->get('core.admin') || $canDo->get('core.options'))
		{
			ToolbarHelper::preferences('com_cmgiftcard');
		}
	}
}
