Template.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits;
  3. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\ModuleConstants;
  4. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Helpers\TemplateConstants;
  5. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper;
  6. trait Template
  7. {
  8. protected $templateName = null;
  9. protected $templateDir = null;
  10. protected $defaultTemplateName = 'container';
  11. protected $templateSet = null;
  12. protected $templateScope = null;
  13. protected $templateMainDir = null;
  14. protected $customTplVars = [];
  15. public function getTemplateDir()
  16. {
  17. return $this->templateDir;
  18. }
  19. public function getDefaultTemplateDir()
  20. {
  21. return ModuleConstants::getTemplateDir() . DS . $this->templateScope . DS . $this->templateMainDir . DS;
  22. }
  23. public function changeTemplateSet($templateSet)
  24. {
  25. if (file_exists(ModuleConstants::getTemplateDir() . DS . $this->templateScope . DS . $this->templateMainDir . DS . $templateSet))
  26. {
  27. $this->templateSet = $templateSet;
  28. }
  29. return $this;
  30. }
  31. public function getTemplateName()
  32. {
  33. return $this->templateName;
  34. }
  35. protected function loadTemplateVars()
  36. {
  37. $this->setScopes();
  38. $this->loadTemplateName();
  39. $this->loadTemplateDir();
  40. $this->evaluateTemplatePath();
  41. return $this;
  42. }
  43. protected function setScopes()
  44. {
  45. $this->checkAdmin();
  46. $this->loadSet();
  47. $this->loadMainDir();
  48. return $this;
  49. }
  50. protected function checkAdmin()
  51. {
  52. $this->templateScope = Helper\isAdmin() ? TemplateConstants::ADMIN_PATH : TemplateConstants::CLIENT_PATH;
  53. return $this;
  54. }
  55. protected function loadSet()
  56. {
  57. $this->templateSet = TemplateConstants::DEFAULT_SET_DIR;
  58. return $this;
  59. }
  60. protected function loadMainDir()
  61. {
  62. $this->templateMainDir = TemplateConstants::MAIN_DIR;
  63. return $this;
  64. }
  65. protected function loadTemplateName()
  66. {
  67. $className = class_basename($this);
  68. $this->templateName = lcfirst($className);
  69. return $this;
  70. }
  71. protected function loadTemplateDir()
  72. {
  73. $class = get_class($this);
  74. $base = $isCore = null;
  75. if (str_contains($class, 'ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\\'))
  76. {
  77. $base = str_replace('ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\\', '', trim($class, '\\'));
  78. $isCore = true;
  79. }
  80. if (str_contains($class, 'ModulesGarden\Servers\ProxmoxCloudVps\App\UI\\'))
  81. {
  82. $base = str_replace('ModulesGarden\Servers\ProxmoxCloudVps\App\UI\\', '', trim($class, '\\'));
  83. }
  84. if (!$base)
  85. {
  86. $this->templateDir = $this->getDefaultTemplateDir() . $this->templateSet . DS;
  87. return $this;
  88. }
  89. $pathParts = explode('\\', $base);
  90. $lastPart = end($pathParts);
  91. if ($lastPart === '')
  92. {
  93. unset($pathParts[key($pathParts)]);
  94. $lastPart = end($pathParts);
  95. }
  96. if ($lastPart === class_basename($this))
  97. {
  98. unset($pathParts[key($pathParts)]);
  99. }
  100. $this->getBasePatch($pathParts, $isCore);
  101. return $this;
  102. }
  103. protected function getBasePatch($pathParts, $isCore = false)
  104. {
  105. if ($isCore === true)
  106. {
  107. $this->getBaseCorePatch($pathParts);
  108. return $this;
  109. }
  110. $this->getBaseAppPatch($pathParts);
  111. return $this;
  112. }
  113. protected function getBaseCorePatch($pathParts)
  114. {
  115. $basePath = implode(DS, array_map('lcfirst', $pathParts));
  116. $this->templateDir = $this->getDefaultTemplateDir() . 'core' . DS . $this->templateSet . ($basePath ? DS . $basePath : '') . DS;
  117. }
  118. protected function getBaseAppPatch($pathParts)
  119. {
  120. $controler = $pathParts[0];
  121. unset($pathParts[0]);
  122. $controlerDir = ModuleConstants::getModuleRootDir() . DS . 'app' . DS . 'UI' . DS . $controler . DS . 'Templates';
  123. $basePath = implode(DS, array_map('lcfirst', $pathParts));
  124. $this->templateDir = $controlerDir . ($basePath ? DS . $basePath : '') . DS;
  125. }
  126. protected function evaluateTemplatePath()
  127. {
  128. if (!file_exists($this->templateDir . $this->templateName . '.tpl'))
  129. {
  130. $parent = get_parent_class($this);
  131. if ($parent)
  132. {
  133. $new = \ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\di($parent, null, true);
  134. if ($new)
  135. {
  136. $this->templateName = $new->getTemplateName();
  137. $this->templateDir = $new->getTemplateDir();
  138. return;
  139. }
  140. }
  141. $this->templateName = $this->defaultTemplateName;
  142. $this->templateDir = $this->getTemplateDir();
  143. }
  144. }
  145. public function getCustomTplVars()
  146. {
  147. return $this->customTplVars;
  148. }
  149. public function getCustomTplVarsValue($varName)
  150. {
  151. return $this->customTplVars[$varName];
  152. }
  153. public function setTemplate($patch = null, $tpl = null)
  154. {
  155. if ($patch && file_exists($patch) && is_readable($patch)
  156. && file_exists($patch . DIRECTORY_SEPARATOR . $tpl . '.tpl') && is_readable($patch . DIRECTORY_SEPARATOR . $tpl . '.tpl'))
  157. {
  158. $this->templateDir = $patch;
  159. $this->templateName = $tpl;
  160. }
  161. }
  162. }