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

/**
 * View class for gift card detail
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardViewCard extends HtmlView
{
	protected $item;

	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 an Error object.
	 *
	 * @since   1.0.0
	 */
	public function display($tpl = null)
	{
		$this->state			= $this->get('State');
		$this->item				= $this->get('Item');
		$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);
		}

		if (!isset($this->item->id))
		{
			return CMGiftCardHelper::throwException(Text::_('COM_CMGIFTCARD_ERROR_CARD_NOT_FOUND'), 404);
		}

		$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();

		$title = Text::sprintf('COM_CMGIFTCARD_HEADING_CARD', $this->item->card_code);
		$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();

		// Remove the hidden menu item of card detail from path way.
		for ($i = 0; $i < count($pathwayArray); $i++)
		{
			if (strpos($pathwayArray[$i]->link, 'index.php?option=com_cmgiftcard&view=card') !== false)
			{
				unset($pathwayArray[$i]);
			}
		}

		$pathway->setPathway($pathwayArray);

		// Add card list to pathway.
		$cardListMenuItem = $menus->getItems('link', 'index.php?option=com_cmgiftcard&view=cards', true);

		if (isset($cardListMenuItem->id))
		{
			$route = $cardListMenuItem->route;
			$routes = explode('/', $route);

			foreach ($routes as $alias)
			{
				$menuItem = $menus->getItems('alias', $alias, true);

				if ($menuItem->id)
				{
					$pathway->addItem($menuItem->title, Route::_('index.php?Itemid=' . $menuItem->id));
				}
			}
		}

		// Add card detail to pathway.
		$pathway->addItem($this->item->card_code, Route::_('index.php?Itemid=' . $menus->getActive()->id));
	}
}
