<?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\Factory;
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 gift card design form.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardViewDesign extends HtmlView
{
	/**
	 * The Form object.
	 *
	 * @var  Form
	 */
	protected $form;

	/**
	 * The active item.
	 *
	 * @var  object
	 */
	protected $item;

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

	/**
	 * Display the view.
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  mixed  A string if successful, otherwise a Error object.
	 */
	public function display($tpl = null)
	{
		$this->form		= $this->get('Form');
		$this->item		= $this->get('Item');
		$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', '<'))
		{
			$this->addToolbar3();
		}
		else
		{
			$this->addToolbar();
		}

		return parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar for Joomla 3.
	 *
	 * @return  void
	 *
	 * @since   2.0.0-alpha1
	 */
	protected function addToolbar3()
	{
		Factory::getApplication()->input->set('hidemainmenu', true);

		$user		= Factory::getUser();
		$userId		= $user->id;
		$isNew		= ($this->item->id == 0);
		$checkedOut	= !($this->item->checked_out == 0 || $this->item->checked_out == $userId);

		$canDo = ContentHelper::getActions('com_cmgiftcard');

		ToolbarHelper::title($isNew ? Text::_('COM_CMGIFTCARD_MANAGER_DESIGN_NEW') : Text::_('COM_CMGIFTCARD_MANAGER_DESIGN_EDIT'), 'copy');

		if (!$checkedOut && ($canDo->get('core.edit') || $canDo->get('core.create')))
		{
			ToolbarHelper::apply('design.apply');
			ToolbarHelper::save('design.save');

			if ($canDo->get('core.create'))
			{
				ToolbarHelper::save2new('design.save2new');
			}
		}

		if (!$isNew && $canDo->get('core.create'))
		{
			ToolbarHelper::save2copy('design.save2copy');
		}

		if (empty($this->item->id))
		{
			ToolbarHelper::cancel('design.cancel');
		}
		else
		{
			ToolbarHelper::cancel('design.cancel', 'JTOOLBAR_CLOSE');
		}
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.0.0
	 */
	protected function addToolbar()
	{
		Factory::getApplication()->input->set('hidemainmenu', true);

		$user		= Factory::getUser();
		$userId		= $user->id;
		$isNew		= ($this->item->id == 0);
		$checkedOut	= !($this->item->checked_out == 0 || $this->item->checked_out == $userId);

		$canDo		= ContentHelper::getActions('com_cmgiftcard');
		$canEdit	= $canDo->get('core.edit');
		$canCreate	= $canDo->get('core.create');

		ToolbarHelper::title($isNew ? Text::_('COM_CMGIFTCARD_MANAGER_DESIGN_NEW') : Text::_('COM_CMGIFTCARD_MANAGER_DESIGN_EDIT'), 'copy');

		if ($isNew && $canCreate)
		{
			ToolbarHelper::apply('design.apply');
			ToolbarHelper::saveGroup(
				[
					['save', 'design.save'],
					['save2new', 'design.save2new']
				],
				'btn-success'
			);

			ToolbarHelper::cancel('design.cancel');
		}
		else
		{
			$toolbarButtons = [];

			if (!$checkedOut && $canEdit)
			{
				ToolbarHelper::apply('design.apply');

				$toolbarButtons[] = ['save', 'design.save'];

				if ($canCreate)
				{
					$toolbarButtons[] = ['save2new', 'design.save2new'];
				}
			}

			if (!$isNew && $canCreate)
			{
				$toolbarButtons[] = ['save2copy', 'design.save2copy'];
			}

			ToolbarHelper::saveGroup(
				$toolbarButtons,
				'btn-success'
			);

			ToolbarHelper::cancel('design.cancel', 'JTOOLBAR_CLOSE');
		}
	}
}
