Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sd/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sd/Sd.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\View\Sd;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;

class Sd
{
    public static function get_output($type, $data = [])
    {
        if (\is_object($data))
        {
            $data = (array) $data;
        }

        switch ($type)
        {
            case 'Article' :
            case 'NewsArticle' :
            case 'BlogPosting' :
                $data['type'] = $type;
                SdArticle::get_output($data);
                break;

            case 'Book' :
                SdBook::get_output($data);
                break;

            case 'Car' :
                SdCar::get_output($data);
                break;

            case 'Course' :
                SdCourse::get_output($data);
                break;

            case 'Dataset' :
                SdDataset::get_output($data);
                break;

            case 'DiscussionForumPosting' :
                SdDiscussionForum::get_output($data);
                break;

            case 'Event' :
                SdEvent::get_output($data);
                break;
            
            case 'FAQPage' :
                SdFaq::get_output($data);
                break;

            case 'JobPosting' :
                SdJobPosting::get_output($data);
                break;

            case 'Movie' :
                SdMovie::get_output($data);
                break;

            case 'Product' :
                SdProduct::get_output($data);
                break;

            case 'Quiz' :
                SdQuiz::get_output($data);
                break;

            case 'Recipe' :
                SdRecipe::get_output($data);
                break;

            case 'SpecialAnnouncement' :
                SdSpecialAnnouncement::get_output($data);
                break;

            case 'VideoObject' :
                SdVideo::get_output($data);
                break;
        }
    }

    // Display structured data
    public static function display_sd($json)
    {
        /*
        $jsonld = '<script type="application/ld+json">';
        $jsonld .= json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        $jsonld .= '</script>';
        echo $jsonld;
        */
        $app = Factory::getApplication();
        
        $wa = $app->getDocument()->getWebAssetManager();
        
	    $wa->addInline('script', json_encode($json, JSON_UNESCAPED_UNICODE), array(), ['type' => 'application/ld+json']);
    }

    // Get date in ISO 8601 format
    public static function get_date($date)
    {
        $config = Factory::getConfig();
        $tz = $config->get('offset');
        
        // Dates should be in ISO 8601 format: YYYY-MM-DDThh:mm:ss+05:30
        if (isset($date) && $date)
        {
            return Factory::getDate($date, $tz)->toISO8601(true);
        }

        return false;
    }

    // Get author
    public static function get_author($type = 'Organization', $user_id = '', $url = '')
    {
        // $type: Person or Organization
        $author = array(
            "@type" => $type,
            //"name" => $name,
            //"url" => $url,
        );

        if ($type == 'Person' && $user_id)
        {
            $user = Factory::getUser($user_id);
            $author["name"] = $user->name;
        }
        
        return $author;
    }
    
    // Depreciated: Create json array - with context and type
    public static function start_sd($type = '')
    {
        $json = array(
            "@context" => "https://schema.org",
        );
            
        if ($type != '')
        {
            $json['@type'] = $type;
        }
            
        return $json;
    }

    // Depreciated: Add image to JSON
    public static function add_image($image)
    {   
        $uri = Uri::getInstance();

        $clean_image = HTMLHelper::cleanImageURL($image);
        
        $image = array(
            '@type' => 'ImageObject',
            'url' => $uri->base() . $clean_image->url,
        );
        
        return $image;
    }
    
    // Depreciated: Add property and value to array
    public static function set_property($array, $name, $value)
    {
        if ($value != '')
        {
            $array[$name] = $value;
        }
        
        return $array;
    }
    
    // Depreciated: Add property and value (search from $mapping array) to array
    public static function set_prop($array, $name, $mapping = array())
    {
        $property_name = strtolower($name);
        
        if (isset($mapping[$property_name]) && $mapping[$property_name] != '')
        {
            $array[$name] = $mapping[$property_name];
        }
        
        return $array;
    }
    
    // Depreciated: Add multiple properties and values (search from $mapping array) to array
    public static function set_props($array, $names = array(), $mapping = array())
    {
        foreach ($names as $name)
        {
            $array = self::set_prop($array, $name, $mapping);
        }
        
        return $array;
    }
    
    // Depreciated
    public static function set_property_image($array, $image)
    {
        if (empty($image))
        {
            return $array;
        }
        
        $uri = Uri::getInstance();
        
        $array['image'] = array(
            '@type' => 'ImageObject',
            'url' => $uri->base() . $image,
        );
        
        return $array;
    }
    
    // Depreciated:
    public static function set_property_author($array, $type, $name, $url)
    {
        $array['author'] = array(
                        '@type' => $type,
                        'name' => $name,
                        'url' => $url
                        );
        
        return $array;
    }
    
    // Depreciated:
    public static function set_property_publisher($array, $name, $logo)
    {
        $uri = Uri::getInstance();
        
        $array['publisher'] = array(
                        '@type' => 'Organization',
                        'name' => $name,
                        'url' => $uri->base(),
                        'logo' => array(
                            '@type' => 'ImageObject',
                            'url' => $uri->base() . $logo
                            )
                        );
        
        return $array;
    }
    
    // Depreciated
    public static function set_properties($array, $names, $values)
    {
        foreach ($names as $k => $v)
        {
            $array[$v] = $values[$k];
        }
        
        return $array;
    }
}