<?php

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Uri\Uri;
use NP\Factory as NpFactory;

$text = $this->item->introtext;
if (property_exists($this->item, 'nicepage') && strpos($text, '<!--np_landing-->') !== false) {
    echo $text;
    return;
}

$app = Factory::getApplication();
$document = $app->getDocument();
$template = $app->getTemplate(true);

$funcPath = JPATH_SITE . '/templates/' . $template->template . '/functions.php';
if (File::exists($funcPath)) {
    include_once $funcPath;
}

$data = array(
    'item' => $this->item,
);

$page = NpFactory::getTemplate('post');
$page->setTemplateData($data);
$page->buildPageElements();
echo $page->getHtml();

$article = null;
if (class_exists('Core')) {
    Core::load("Core_Content");
    $component = new CoreContent($this, $this->params);
    $article = $component->article('article', $this->item, $this->item->params, array('print' => $this->print));
}

if (!$article) {
    return;
}

$logoInfo = getLogoInfo(array('src' => "/images/default-logo.png"));

$themeHelper = class_exists('ThemeHelper') ? ThemeHelper::getInstance() : new stdClass();
$themeHelper->seoTags['og:type'] = array('tag' => 'meta', 'property' => 'og:type', 'content' => 'article');
$themeHelper->seoTags['og:title'] = array('tag' => 'meta', 'property' => 'og:title', 'content' => $article->title);

$themeParams = $app->getTemplate(true)->params;
if ($themeParams->get('jsonld', '0') == '1') {
    ob_start();
    $config = $app->getConfig();
    ?>
    <script type="application/ld+json">
        {
            "@context": "http://schema.org",
            "@type": "Article",
            "articleBody": <?php echo json_encode(strip_tags($article->text($article->text))); ?>,
    "mainEntityOfPage": {
        "@type": "WebPage",
        "@id": "<?php echo Uri::getInstance()->toString(); ?>"
    },
    "headline": "<?php echo $article->title; ?>",
    "description": <?php echo json_encode(strip_tags($article->intro($article->intro))); ?>,
        <?php
        $image = $article->images['fulltext']['image'];
        if ($image) {
            $rootUrl = rtrim(Uri::root(), '/');
            $rootPath = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', JPATH_ROOT), '/');
            $isFullUrl = strpos($image, $rootUrl) !== false;
            if (!$isFullUrl) {
                $imageUrl = $rootUrl . '/' . $image;
                $imagePath = $rootPath .  '/' . $image;
            } else {
                $imageUrl = $image;
                $imagePath = str_replace($rootUrl, $rootPath, $image);
            }
            $info = @getimagesize($imagePath);

            $themeHelper->seoTags['og:image'] = array('tag' => 'meta', 'property' => 'og:image', 'content' => $imageUrl);
            $themeHelper->seoTags['og:secure_url'] = array('tag' => 'meta', 'property' => 'og:image:secure_url', 'content' => str_replace('http:', 'https:', $imageUrl));
            $themeHelper->seoTags['og:height'] = array('tag' => 'meta', 'property' => 'og:image:height', 'content' => @$info[1]);
            $themeHelper->seoTags['og:width'] = array('tag' => 'meta', 'property' => 'og:image:width', 'content' => @$info[0]);
            $alt = $article->images['fulltext']['alt'];
            if ($alt) {
                $themeHelper->seoTags['og:alt'] = array('tag' => 'meta', 'property' => 'og:image:alt', 'content' => $alt);
            }
            ?>
    "image": {
        "@type": "ImageObject",
        "url": "<?php echo $imageUrl; ?>",
        "height": <?php echo ($info && isset($info[1])) ? $info[1] : '""'; ?>,
        "width": <?php echo ($info && isset($info[0])) ? $info[0] : '""'; ?>
    },
        <?php } ?>
        "datePublished": "<?php echo date(DATE_ISO8601, strtotime($this->item->publish_up)); ?>",
    "dateCreated": "<?php echo date(DATE_ISO8601, strtotime($this->item->created)); ?>",
    "dateModified": "<?php echo date(DATE_ISO8601, strtotime($this->item->modified)); ?>",
    "author": {
        "@type": "Person",
        "name": "<?php echo $article->author; ?>"
    },
    "publisher": {
        "@type": "Organization",
          "logo": {
            "@type": "ImageObject",
            "height": "<?php echo $logoInfo['height']; ?>",
            "width": "<?php echo $logoInfo['width']; ?>",
            "url": "<?php echo $logoInfo['src']; ?>"
          },
        "name": "<?php echo $config->get('sitename'); ?>"
    }
}

    </script>
    <?php
    $articleLdJson = ob_get_clean();
    $document->addCustomTag($articleLdJson);
}
