| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\Http\View;
- use ThurData\Servers\KerioEmail\Core\ServiceLocator;
- use ThurData\Servers\KerioEmail\Core\ModuleConstants;
- use function ThurData\Servers\KerioEmail\Core\Helper\isAdmin;
- /**
- * Smarty Wrapper
- *
- * @autor ThurData <info@thurdata.ch>
- * @SuppressWarnings(PHPMD)
- */
- class Smarty
- {
- private static $instance = null;
- private $smarty;
- private $templateDIR;
- private $lang;
- final private function __construct()
- {
- $this->smarty = new \Smarty();
- }
- final private function __clone()
- {
-
- }
- public static function get()
- {
- if (empty(self::$instance))
- {
- self::$instance = new self;
- }
- return self::$instance;
- }
- public function setLang($land)
- {
- $this->lang = $land;
- return $this;
- }
- /**
- * Set Tempalte Dir
- *
- * @autor ThurData <info@thurdata.ch>
- * @param string $dir
- */
- public function setTemplateDir($dir)
- {
- if (is_array($dir))
- {
- ServiceLocator::call('errorManager')->addError(self::class, 'Wrong Template Path : ' . $dir, ['dir' => $dir]);
- }
- $this->templateDIR = $dir;
- return $this;
- }
- /**
- * Parse Template File
- *
- * @autor ThurData <info@thurdata.ch>
- * @global string $templates_compiledir
- * @param string $template
- * @param array $vars
- * @param string $customDir
- * @return string
- * @throws exceptions\System
- */
- public function view($template, $vars = [], $customDir = false)
- {
- if (is_array($customDir))
- {
- ServiceLocator::call('errorManager')->addError(self::class, 'Wrong Template Path : ' . $customDir, ['dir' => $customDir]);
- return '';
- }
- global $templates_compiledir;
- if ($customDir)
- {
- $this->smarty->template_dir = $customDir;
- }
- else
- {
- $this->smarty->template_dir = $this->templateDIR;
- }
- $this->smarty->compile_dir = $templates_compiledir;
- $this->smarty->force_compile = 1;
- $this->smarty->caching = 0;
- $this->clear();
- $this->smarty->assign('MGLANG', $this->lang);
- if (is_array($vars))
- {
- foreach ($vars as $key => $val)
- {
- $this->smarty->assign($key, $val);
- }
- }
- if (is_array($this->smarty->template_dir))
- {
- $file = $this->smarty->template_dir[0] . DS . $template . '.tpl';
- }
- else
- {
- $file = $this->smarty->template_dir . DS . $template . '.tpl';
- }
- if (!file_exists($file))
- {
- $errorManager = ServiceLocator::call('errorManager');
- $errorManager->addError(self::class, 'Unable to find Template: '.$file, ['file' => $file]);
- return (string)$errorManager;
- }
- if (isset($vars['isError']) && $vars['isError'] === false || !isset($vars['isError']) || ServiceLocator::$isDebug === false)
- {
- return $this->smarty->fetch($template . '.tpl', uniqid());
- }
- else
- {
- $template = ModuleConstants::getTemplateDir() . DS . ( isAdmin() ? "admin" : ("client" . DS . "default")) . DS . "ui" . DS . "core" . DS . "default" . DS;
- return $this->smarty->fetch($template . 'errorComponent.tpl', uniqid());
- }
-
- }
- protected function clear()
- {
- if (method_exists($this->smarty, 'clearAllAssign'))
- {
- $this->smarty->clearAllAssign();
- }
- elseif (method_exists($this->smarty, 'clear_all_assign'))
- {
- $this->smarty->clear_all_assign();
- }
- }
- }
|