<?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\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Plugin\PluginHelper;

/**
 * View class for shopping cart.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardViewCart 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			= Factory::getApplication()->getParams();
		$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);
		}

		$cart = Factory::getSession()->get('cart', array(), 'CMGiftCard');

		if (isset($cart['items']) && count($cart['items']))
		{
			$model = BaseDatabaseModel::getInstance('Design', 'CMGiftCardModel');
			$params = ComponentHelper::getParams('com_cmgiftcard');

			foreach ($cart['items'] as &$item)
			{
				$design = $model->getDesign($item['design_id']);

				$item['design_title'] = isset($design->title) ? $design->title : '';
				$item['design_image'] = isset($design->image) ? $design->image : '';
				$item['amount_formatted'] = HTMLHelper::_('cmgiftcard.formatPrice', $item['amount'], $params);
			}
		}

		$this->cart = $cart;

		$app = Factory::getApplication();

		PluginHelper::importPlugin('cmgiftcard');

		$this->payments = $app->triggerEvent('onCMGCGetPaymentIdentity');

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

		$head = Text::_('COM_CMGIFTCARD_HEADING_CART');

		if ($menu)
		{
			$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
		}
		else
		{
			$this->params->def('page_heading', $head);
		}

		$title = $this->params->def('page_title', $head);

		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'));
		}
	}
}
