Your IP : 216.73.216.240


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

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

namespace TechFry\Library;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use TechFry\Library\View\Html\TfHtml;

// Handling of Dynamic Tags - Current Page, Current User, User, Articles, Category, Tag, Menu Item, File, Dates, Site, Database
// Placeholders - USER, GLOBAL, JA, JC, JT, JM, FIELD, FILE

class TDynamic
{
  	public $template; // HTML String with shortcodes

    public $config = [];

    public $data; // Array or Object

	public $regex_prefix;

    public function __construct($template = '', $config = [], $data = [])
    {
        $this->template = $template;

        $this->config = (array) $config;

        $this->data = (array) $data;

		$this->regex_prefix = 'FIELD';
    }

    // Replace array keys by values in a string
  	public function replace_fields($template = '', $data = [])
    {
        if ($template)
        {
            $this->template = $template;
        }

        if ($data)
        {
            $this->data = (array) $data;
            $this->regex_prefix = 'FIELD';
        }
        
        $regex = '/\{' . $this->regex_prefix . '\s(\w+)(\|([^\}]+))?\}/i'; // Example: {FIELD title|option1:value1;option2:value2}
      
      	preg_match_all($regex, $this->template, $matches, PREG_SET_ORDER);

      	if ($matches)
		{
          	foreach ($matches as $match)
			{
				$key = trim($match[1]);
                $attributes = isset($match[3]) ? $match[3] : ''; // format:Y-m-d;tag:div

                $value = isset($this->data[$key]) ? $this->data[$key] : '';

                if ($attributes)
                {
                    $options = explode(';', $attributes);
                    $details = [];
                    foreach ($options as $option)
                    {
                        // $option - tag:div
                        $temp = explode(':', $option);
                        $k = $temp[0];
                        $details[$k] = $temp[1];
                    }

                    if (isset($details['type']))
                    {
                        $function = 'do_type';
                        $value = $this->$function($value, $details);
                    }

                    if (isset($details['format']))
                    {
                        $function = 'do_format';
                        $value = $this->$function($value, $details);
                    }

                    if (isset($details['tag']))
                    {
                        $function = 'do_tag';
                        $value = $this->$function($value, $details);
                    }
                }

                $this->template = str_replace($match[0], $value, $this->template);
            }
		}
      
      	return $this->template;
    }

    // 01. Get and Replace User Information
  	public function replace_user()
    {
		// If user id is null, then get current logged in user

        $user_id = isset($this->config['user_id']) && $this->config['user_id'] ? $this->config['user_id'] : 0;
		
		$u = new TUser();
        $user = $u->get_user($user_id);

		$name = $user->name ?? '';
		$email = $user->email ?? '';
		$username = $user->username ?? '';

      	// A. Direct fields - {NAME}, {EMAIL}, {USERNAME}
      	$this->template = str_replace('{NAME}', $name, $this->template);
      	$this->template = str_replace('{EMAIL}', $email, $this->template);
      	$this->template = str_replace('{USERNAME}', $username, $this->template);

        // C. User custom fields
        $regex = '/{USER\s([1-9][0-9]*)}/i'; // Example: {USER 12}
      	preg_match_all($regex, $this->template, $matches, PREG_SET_ORDER);
        if ($matches)
        {
            foreach ($matches as $match)
            {
                $cf = new TField();
                $custom_field = $cf->get_custom_field(trim($match[1]), $user->id);
                if ($custom_field != '')
				{
					$this->template = str_replace($match[0], $custom_field, $this->template);
				}
				else
				{
					$this->template = str_replace($match[0], '', $this->template);
				}
            }
        }
	
	  	// B. From users table
		$this->regex_prefix = 'USER';

		$this->reset_data($user);
		$this->replace_fields();

		// D. User profile fields
		$this->regex_prefix = 'PROFILE'; // Example: {PROFILE city}

		$u = new TUser();
        $profile = $u->get_profile($user_id);
		$this->reset_data($profile);

		$this->replace_fields();

      	return $this->template;
    }

    // 02. Get and Replace Current Date
  	public function replace_dates($template = '')
    {
		if ($template)
        {
            $this->template = $template;
        }
        
        $date_format = isset($this->config['date_format']) && $this->config['date_format'] ? $this->config['date_format'] : 'd M Y';
		
		$date = Factory::getDate(); // Joomla\CMS\Date\Date Object

		$today = $date->format($date_format);
      	$year = $date->format('Y');
        $month = $date->format('F');
        $day = $date->format('l');
        $week = $date->format('W');
      	
        // Direct fields
      	$this->template = str_replace('{DATE}', $today, $this->template);
      	$this->template = str_replace('{MONTH}', $month, $this->template);
      	$this->template = str_replace('{YEAR}', $year, $this->template);
        $this->template = str_replace('{DAY}', $day, $this->template);
        $this->template = str_replace('{WEEK}', $week, $this->template);

        $obj = new \stdClass();
        $obj->current = $date;

        $this->reset_data($obj);
        $this->regex_prefix = 'DATE';

        $this->replace_fields();

      	return $this->template;
    }

    // 03. Get and Replace Joomla Site Information
  	public function replace_site($template = '')
    {
        // Example: {SITE sitename}, {SITE mailfrom}, {SITE fromname}

        if ($template)
        {
            $this->template = $template;
        }

        $site = Factory::getApplication()->getConfig();
        $site_data = array(
            'sitename' => $site->get('sitename'),
			'MetaDesc' => $site->get('MetaDesc'),
			'mailfrom' => $site->get('mailfrom'),
			'fromname' => $site->get('fromname'),
			'replyto' => $site->get('replyto'),
        );

        $this->regex_prefix = 'SITE';

		$this->reset_data($site_data);

		$this->replace_fields();

        return $this->template;
    }

    // 04. Get and Replace Joomla Article
  	public function replace_article()
    {
		// Find article id
		$article_id = $this->config['article_id'] ?? 0;
		if (!$article_id)
		{
			return $this->template;
		}

		$a = new TArticle();
        $article = $a->get_article($article_id);

        $this->regex_prefix = 'JA';

		$this->reset_data($article);

		$this->replace_fields();

      	return $this->template;
    }

    // 05. Get and Replace Joomla Category
	public function replace_category()
	{		
		// Find category id
		$category_id = $this->config['category_id'] ?? 0;
		if (!$category_id)
		{
			return $this->template;
		}

		$c = new TCategory();
        $category = $c->get_category($category_id);

        $this->regex_prefix = 'JC';

		$this->reset_data($category);

		$this->replace_fields();

		return $this->template;
	}

    // 06. Get and Replace Joomla Tag
	public function replace_tag()
	{
		// Find tag id
		$tag_id = $this->config['tag_id'] ?? 0;
		if (!$tag_id)
		{
			return $this->template;
		}

		$t = new TTag();
        $tag = $t->get_tag($tag_id);
		
        $this->regex_prefix = 'JT';

		$this->reset_data($tag);

		$this->replace_fields();

		return $this->template;
	}

    // 07. Database
	public function replace_dbtable()
	{
		$db = new TDb($this->config['dbtable']);
        $item = $db->get_item(array('id' => $this->config['id']));
        
        $this->regex_prefix = 'DB';

		$this->reset_data($item);

		$this->replace_fields();

		return $this->template;
	}

    // 08. File information
	public function replace_file()
	{
        $f = new TFile();
        $info = $f->get_info($this->config['file']);
		
        $this->regex_prefix = 'FILE';

		$this->reset_data($info);

		$this->replace_fields();

		return $this->template;
	}

    // 09. Current page
	public function replace_page()
	{
		$input = Factory::getApplication()->input;

		$option = $input->get('option', '');
		$view = $input->get('view', '');
		$id = $input->get('id', '');

		$item = new \stdClass();

		if ($option == 'com_content' && $view == 'article' && $id)
		{
			$a = new TArticle();
            $item = $a->get_article($id);
		}

		if ($option == 'com_content' && $view == 'categories' && $id)
		{
			$c = new TCategory();
            $item = $c->get_category($id);
		}

		if ($option == 'com_content' && $view == 'category' && $id)
		{
			$c = new TCategory();
            $item = $c->get_category($id);
		}

		$item->option = $option;
		$item->view = $view;
		$item->id = $id;
		
		$this->regex_prefix = 'PAGE';

		$this->reset_data($item);

		$this->replace_fields();

		return $this->template;
	}

// *********** Generate HTML Tags and Format *********************************

    public function do_type($value, $details = [])
    {
        // $value can be json or array
        
        $type = $details['type'];
        unset($details['type']);

        if ($type == 'json') 
        {
            $key = $details['key'];
            unset($details['key']);

			$value = json_decode($value, true)[$key];
        }
        elseif ($type == 'array')
        {
            $key = $details['key'];
            unset($details['key']);

            $value = $value[$key];
        }

        return $value;
    }
    
    public function do_format($value, $details = [])
    {
        $format = $details['format'];
        
        // Format the value if it is a date and a format is specified
        if ($format && strtotime($value) !== false) 
        {
            $value = date($format, strtotime($value));
        }

        return $value;
    }

	public function do_tag($value, $details = [])
    {
		$tag = $details['tag'];
        unset($details['tag']);

		$string_tags = array('p', 'div', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre');
		$array1_tags = array('ol', 'ul', 'dl');
		$array2_tags = array('table');

        if (\in_array($tag, $string_tags))
        {
            $attribs = $details;

            $value = TfHtml::get_element($value, $tag, $attribs);
        }
		elseif (\in_array($tag, $array1_tags))
		{
			$items = json_decode($value, true);
			$value = '<' . $tag . '>';
			foreach ($items as $item)
			{
				$value .= '<li>' . $list_item . '</li>';
			}
			$value .= '</' . $tag . '>';
		}
		elseif ($tag == 'table')
		{
			$items = json_decode($value, true);

            if (isset($details['heads']))
            {
                $heads = str_getcsv($details['heads']);
                unset($details['heads']);
            }
            else
            {
                $heads = [];
            }

            $value = TfHtml::get_table($items, $details, $heads);
		}
		elseif ($tag == 'a')
		{
            if (isset($details['text']) && $details['text'])
            {
                $text = $details['text'];
                unset($details['text']);
            }
            else
            {
                $text = 'Click Here';
            }

            $attribs = $details;

            $value = TfHtml::get_a($text, $value, $attribs);

		}
		elseif ($tag == 'img')
		{
            $attribs = $details;

			$value = TfHtml::get_img($value, '', $attribs);
		}

        return $value;
    }

    public function add_data($more_data = array())
    {
        $this->data = array_merge($this->data, (array) $more_data);
    }

    public function reset_data($new_data = [])
    {
        $this->data = (array) $new_data;
    }

    public function set_config($key, $value)
    {
        $this->config[$key] = $value;
    }

	public function replace_all($types = [])
	{
		// $data can be string or array or 2D array
		
		if (empty($types))
		{
			$types = array('user', 'dates', 'site', 'article', 'category', 'tag', 'dbtable', 'fields');
		}

        $master_template = $this->template;

		foreach ($types as $type)
		{
			$function = 'replace_' . $type;
            
            if (\is_string($master_template))
			{
				$master_template = $this->$function();
			}
			elseif (\is_array($master_template))
			{
				foreach ($master_template as $k1 => $v1)
				{
					if (\is_string($v1))
					{
						$this->template = $v1;
                        $master_template[$k1] = $this->$function();
					}
					elseif (\is_array($v1))
					{
						foreach ($v1 as $k2 => $v2)
						{
							$this->template = $v2;
                            $master_template[$k1][$k2] = $this->$function();
						}
					}
					elseif (\is_object($v1))
					{
						foreach ($v1 as $k2 => $v2)
						{
							$this->template = $v2;
                            $master_template[$k1]->$k2 = $this->$function();
						}
					}
				}
			}
		}

        $this->template = $master_template;

		return $this->template;
	}
}