Smarty.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Http\View;
  3. use ThurData\Servers\KerioEmail\Core\ServiceLocator;
  4. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  5. use function ThurData\Servers\KerioEmail\Core\Helper\isAdmin;
  6. /**
  7. * Smarty Wrapper
  8. *
  9. * @autor ThurData <info@thurdata.ch>
  10. * @SuppressWarnings(PHPMD)
  11. */
  12. class Smarty
  13. {
  14. private static $instance = null;
  15. private $smarty;
  16. private $templateDIR;
  17. private $lang;
  18. private function __construct()
  19. {
  20. $this->smarty = new \Smarty();
  21. }
  22. private function __clone()
  23. {
  24. }
  25. public static function get()
  26. {
  27. if (empty(self::$instance))
  28. {
  29. self::$instance = new self;
  30. }
  31. return self::$instance;
  32. }
  33. public function setLang($land)
  34. {
  35. $this->lang = $land;
  36. return $this;
  37. }
  38. /**
  39. * Set Tempalte Dir
  40. *
  41. * @autor ThurData <info@thurdata.ch>
  42. * @param string $dir
  43. */
  44. public function setTemplateDir($dir)
  45. {
  46. if (is_array($dir))
  47. {
  48. ServiceLocator::call('errorManager')->addError(self::class, 'Wrong Template Path : ' . $dir, ['dir' => $dir]);
  49. }
  50. $this->templateDIR = $dir;
  51. return $this;
  52. }
  53. /**
  54. * Parse Template File
  55. *
  56. * @autor ThurData <info@thurdata.ch>
  57. * @global string $templates_compiledir
  58. * @param string $template
  59. * @param array $vars
  60. * @param string $customDir
  61. * @return string
  62. * @throws exceptions\System
  63. */
  64. public function view($template, $vars = [], $customDir = false)
  65. {
  66. if (is_array($customDir))
  67. {
  68. ServiceLocator::call('errorManager')->addError(self::class, 'Wrong Template Path : ' . $customDir, ['dir' => $customDir]);
  69. return '';
  70. }
  71. global $templates_compiledir;
  72. if ($customDir)
  73. {
  74. $this->smarty->template_dir = $customDir;
  75. }
  76. else
  77. {
  78. $this->smarty->template_dir = $this->templateDIR;
  79. }
  80. $this->smarty->compile_dir = $templates_compiledir;
  81. $this->smarty->force_compile = 1;
  82. $this->smarty->caching = 0;
  83. $this->clear();
  84. $this->smarty->assign('MGLANG', $this->lang);
  85. if (is_array($vars))
  86. {
  87. foreach ($vars as $key => $val)
  88. {
  89. $this->smarty->assign($key, $val);
  90. }
  91. }
  92. if (is_array($this->smarty->template_dir))
  93. {
  94. $file = $this->smarty->template_dir[0] . DS . $template . '.tpl';
  95. }
  96. else
  97. {
  98. $file = $this->smarty->template_dir . DS . $template . '.tpl';
  99. }
  100. if (!file_exists($file))
  101. {
  102. $errorManager = ServiceLocator::call('errorManager');
  103. $errorManager->addError(self::class, 'Unable to find Template: '.$file, ['file' => $file]);
  104. return (string)$errorManager;
  105. }
  106. if (isset($vars['isError']) && $vars['isError'] === false || !isset($vars['isError']) || ServiceLocator::$isDebug === false)
  107. {
  108. return $this->smarty->fetch($template . '.tpl', uniqid());
  109. }
  110. else
  111. {
  112. $template = ModuleConstants::getTemplateDir() . DS . ( isAdmin() ? "admin" : ("client" . DS . "default")) . DS . "ui" . DS . "core" . DS . "default" . DS;
  113. return $this->smarty->fetch($template . 'errorComponent.tpl', uniqid());
  114. }
  115. }
  116. protected function clear()
  117. {
  118. if (method_exists($this->smarty, 'clearAllAssign'))
  119. {
  120. $this->smarty->clearAllAssign();
  121. }
  122. elseif (method_exists($this->smarty, 'clear_all_assign'))
  123. {
  124. $this->smarty->clear_all_assign();
  125. }
  126. }
  127. }