<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\SmartyHelper;

use \ModulesGarden\HostingQuotaNotifications\Core\ModuleConstants;
use \Smarty;

/**
 * Description of SmartyHelper
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class SmartyHelper
{
    protected $smarty;

    public function __construct()
    {
        $this->smarty = new Smarty;

        global $templates_compiledir;
        $this->smarty->compile_dir = $templates_compiledir;
    }

    public function assignVariables($string, $data)
    {
        $str = $this->smarty->assign($data, $string);

        return $this->smarty->fetch('string:' . $string, $data);
    }

    public function smartyFetch($name, $client = false, $path = null, $params = [])
    {
        $dir = 'admin';
        if ($client)
        {
            $dir = 'client';
        }
        $modulePath = ModuleConstants::getModuleRootDir();

        $this->smarty->assign($params);
        if (!$path)
        {
            $path = $modulePath . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . 'default';
        }
        else
        {
            $path = $modulePath . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $path;
        }

        $file = $path . DIRECTORY_SEPARATOR . $name . '.tpl';

        if (file_exists($file))
        {
            return $this->smarty->fetch($file);
        }
    }
}
