<?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\HTML\HTMLHelper;
use Joomla\CMS\MVC\View\HtmlView;

/**
 * View class for form page.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardViewForm extends HtmlView
{
	/**
	 * 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->state			= $this->get('State');
		$this->form				= $this->get('Form');
		$this->params			= &$this->state->params;
		$this->pageclass_sfx	= htmlspecialchars($this->params->get('pageclass_sfx', ''), ENT_COMPAT, 'UTF-8');

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

		HTMLHelper::_('jquery.framework');

		$this->_prepareDocument();
		parent::display($tpl);
	}

	/**
	 * Prepare document.
	 *
	 * @return  void
	 *
	 * @since   1.0.0
	 */
	protected function _prepareDocument()
	{
		$app	= Factory::getApplication();
		$menus	= $app->getMenu();
		$menu	= $menus->getActive();

		$type = $app->input->get('type');

		if ($type == 'physical')
		{
			$title = Text::_('COM_CMGIFTCARD_FORM_HEADING_PHYSICAL');
		}
		elseif ($type == 'electronic')
		{
			$title = Text::_('COM_CMGIFTCARD_FORM_HEADING_ELECTRONIC');
		}
		else
		{
			$title = $menu->title;
		}

		$this->params->def('page_heading', $title);

		if ($app->getCfg('sitename_pagetitles', 0) == 1)
		{
			$title = Text::sprintf('JPAGETITLE', $app->getCfg('sitename'), $title);
		}
		elseif ($app->getCfg('sitename_pagetitles', 0) == 2)
		{
			$title = Text::sprintf('JPAGETITLE', $title, $app->getCfg('sitename'));
		}

		$this->document->setTitle($title);

		if ($this->params->get('menu-meta_description') != '')
		{
			$this->document->setDescription($this->params->get('menu-meta_description'));
		}

		if ($this->params->get('menu-meta_keywords') != '')
		{
			$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
		}

		if ($this->params->get('robots'))
		{
			$this->document->setMetadata('robots', $this->params->get('robots'));
		}

		$pathway = $app->getPathway();
		$pathwayArray = $pathway->getPathway();

		$pathway->addItem($title);
	}
}
