<?php

/**
 * @package JPageBuilder
 * @author Joomla! Extensions Store
 * @copyright (C) 2024 - Joomla! Extensions Store
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */
// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );

use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Component\ComponentHelper;
class JpagebuilderViewPage extends HtmlView {
	protected $item;
	protected $canEdit;
	protected $additionalAttributes = [ ];
	function display($tpl = null) {
		$app = Factory::getApplication ();
		$user = Factory::getApplication ()->getIdentity ();
		$this->item = $this->getModel()->getItem();

		// If a page is unpublished/trashed and a user tries to preview it.
		if (is_string ( $this->item )) {
			throw new Exception ( $this->item, 404 );
		}

		$this->item = JpagebuilderApplicationHelper::preparePageData ( $this->item );

		$this->canEdit = $user->authorise ( 'core.edit', 'com_jpagebuilder' ) || $user->authorise ( 'core.edit', 'com_jpagebuilder.page.' . $this->item->id ) || ($user->authorise ( 'core.edit.own', 'com_jpagebuilder.page.' . $this->item->id ) && $this->item->created_by == $user->id);
		$this->checked_out = ($this->item->checked_out == 0 || $this->item->checked_out == $user->id);

		// Temporary disabled
		if ($this->item->access_view == false) {
			$app->enqueueMessage ( Text::_ ( 'JERROR_ALERTNOAUTHOR' ), 'error' );
			$app->setHeader ( 'status', 403, true );

			return;
		}

		$this->_prepareDocument ( $this->item->title );

		parent::display ( $tpl );
	}
	protected function _prepareDocument($title = '') {
		$config = Factory::getApplication ()->getConfig ();
		$app = Factory::getApplication ();
		$doc = Factory::getApplication ()->getDocument ();
		$menus = $app->getMenu ();
		$menu = $menus->getActive ();
		$config_params = ComponentHelper::getParams ( 'com_jpagebuilder' );
		$disable_og = $config_params->get ( 'disable_og', 0 );
		$disable_tc = $config_params->get ( 'disable_tc', 0 );

		// Title
		if (isset ( $meta ['title'] ) && $meta ['title']) {
			$title = $meta ['title'];
		} else {
			if ($menu) {
				if ($menu->getParams ()->get ( 'page_title', '' )) {
					$title = $menu->getParams ()->get ( 'page_title' );
				} else {
					$title = $menu->title;
				}
			}
		}

		// Include Site title
		$sitetitle = $title;
		if ($config->get ( 'sitename_pagetitles' ) == 2) {
			$sitetitle = Text::sprintf ( 'JPAGETITLE', $sitetitle, $app->get ( 'sitename' ) );
		} elseif ($config->get ( 'sitename_pagetitles' ) == 1) {
			$sitetitle = Text::sprintf ( 'JPAGETITLE', $app->get ( 'sitename' ), $sitetitle );
		}
		$doc->setTitle ( $sitetitle );

		$og_title = $this->item->og_title;
		$language = ($this->item->language == '*') ? Factory::getApplication ()->getLanguage ()->getTag () : $this->item->language;

		$doc->addCustomTag ( '<meta property="article:author" content="' . $this->item->author_name . '"/>' );
		$doc->addCustomTag ( '<meta property="article:published_time" content="' . $this->item->created_on . '"/>' );
		$doc->addCustomTag ( '<meta property="article:modified_time" content="' . $this->item->modified . '"/>' );
		$doc->addCustomTag ( '<meta property="og:locale" content="' . $language . '" />' );

		// Page Meta
		if (isset ( $this->item->attribs )) {
			$attribs = json_decode ( $this->item->attribs );
		} else {
			$attribs = new stdClass ();
		}

		if (! $disable_og) {
			if ($og_title) {
				$doc->addCustomTag ( '<meta property="og:title" content="' . $og_title . '" />' );
			} else {
				$doc->addCustomTag ( '<meta property="og:title" content="' . $title . '" />' );
			}

			$og_type = (isset ( $attribs->og_type ) && $attribs->og_type) ? $attribs->og_type : 'website';

			$doc->addCustomTag ( '<meta property="og:type" content="' . $og_type . '" />' );
			$doc->addCustomTag ( '<meta property="og:url" content="' . Uri::getInstance ()->toString () . '" />' );

			if ($fb_app_id = $config_params->get ( 'fb_app_id', '' )) {
				$doc->addCustomTag ( '<meta property="fb:app_id" content="' . $fb_app_id . '" />' );
			}

			if ($config->get ( 'sitename', '' )) {
				$doc->addCustomTag ( '<meta property="og:site_name" content="' . htmlspecialchars ( $config->get ( 'sitename', '' ) ) . '" />' );
			}
		}

		$og_image = "";

		if (! empty ( $this->item->og_image )) {
			$og_image = preg_match ( "@^{@", $this->item->og_image ) ? json_decode ( $this->item->og_image )->src : $this->item->og_image;
		}

		if (! $disable_og && $og_image) {
			$doc->addCustomTag ( '<meta property="og:image" content="' . Uri::root () . $og_image . '" />' );
			$doc->addCustomTag ( '<meta property="og:image:width" content="1200" />' );
			$doc->addCustomTag ( '<meta property="og:image:height" content="630" />' );
		}

		$og_description = $this->item->og_description;
		if (! $disable_og && $og_description) {
			$doc->addCustomTag ( '<meta property="og:description" content="' . $og_description . '" />' );
		}

		if (! $disable_tc) {
			// Twitter
			$doc->addCustomTag ( '<meta name="twitter:card" content="summary" />' );

			if ($config->get ( 'sitename', '' )) {
				$doc->addCustomTag ( '<meta name="twitter:site" content="' . htmlspecialchars ( $config->get ( 'sitename', '' ) ) . '" />' );
			}

			if ($og_description) {
				$doc->addCustomTag ( '<meta name="twitter:description" content="' . $og_description . '" />' );
			}

			if ($og_image) {
				$doc->addCustomTag ( '<meta name="twitter:image:src" content="' . Uri::root () . $og_image . '" />' );
			}
		}

		$meta_title = (isset ( $attribs->meta_title ) && $attribs->meta_title) ? $attribs->meta_title : '';
		$meta_description = (isset ( $attribs->meta_description ) && $attribs->meta_description) ? $attribs->meta_description : '';
		$meta_keywords = (isset ( $attribs->meta_keywords ) && $attribs->meta_keywords) ? $attribs->meta_keywords : '';
		$robots = (isset ( $attribs->robots ) && $attribs->robots) ? $attribs->robots : '';

		if ($menu) {
			if ($menu->getParams ()->get ( 'page_title' )) {
				$meta_title = $menu->getParams ()->get ( 'page_title' );
			}
			
			if ($menu->getParams ()->get ( 'menu-meta_description' )) {
				$meta_description = $menu->getParams ()->get ( 'menu-meta_description' );
			}

			if ($menu->getParams ()->get ( 'menu-meta_keywords' )) {
				$meta_keywords = $menu->getParams ()->get ( 'menu-meta_keywords' );
			}

			if ($menu->getParams ()->get ( 'robots' )) {
				$robots = $menu->getParams ()->get ( 'robots' );
			}
		}

		if (! empty ( $meta_title )) {
			$this->setDocumentTitle($meta_title);
		}
		
		if (! empty ( $meta_description )) {
			$doc->setDescription ( $meta_description );
		}

		if (! empty ( $meta_keywords )) {
			$doc->setMetadata ( 'keywords', $meta_keywords );
		}

		if (! empty ( $robots )) {
			$doc->setMetadata ( 'robots', $robots );
		}
	}
}
