PageRenderTime 1555ms CodeModel.GetById 56ms RepoModel.GetById 5ms app.codeStats 0ms

/system/classes/template.php

https://gitlab.com/srueegger/1zu12bB
PHP | 290 lines | 200 code | 23 blank | 67 comment | 31 complexity | af81355876b06e8038f2d9e8c1369542 MD5 | raw file
  1. <?php
  2. class Template{
  3. private $template = "";
  4. private $loop_templates = array();
  5. private $template_path = "";
  6. private $loops = array();
  7. private $ifs = array();
  8. /**
  9. *
  10. * @param string $filename
  11. * @return string
  12. */
  13. private function read_file($filename){
  14. $code = "";
  15. if(file_exists($filename)){
  16. $templatefile = @fopen($filename, "r");
  17. if($templatefile){
  18. while(!feof($templatefile)){
  19. $code = $code.fgets($templatefile, 1024);
  20. }
  21. fclose($templatefile);
  22. }
  23. }
  24. return $code;
  25. }
  26. /**
  27. *
  28. * @param string $template
  29. * @return string
  30. */
  31. private function initialize_loops($template){
  32. preg_match_all("/{LOOP:([\w]+)[^}]*}((.|\s)*?){\/LOOP:(\s*?.*?)*}/", $template, $matches, PREG_SET_ORDER);
  33. foreach ($matches as $match) {
  34. $template = str_ireplace($match[0], "<!--LOOP(".$match[1].")-->", $template);
  35. $this->loop_templates[strtoupper($match[1])] = $match[2];
  36. if(!array_key_exists(strtoupper($match[1]), $this->loops)){
  37. $this->loops[strtoupper($match[1])] = array();
  38. }
  39. }
  40. return $template;
  41. }
  42. /**
  43. *
  44. * @param string $template
  45. */
  46. public function load($template){
  47. $path = $this->getTemplatePath($template);
  48. if(file_exists($path)){
  49. $template = $this->read_file($path);
  50. }
  51. elseif(file_exists($this->template_path."/".$template)){
  52. $template = $this->read_file($this->template_path."/".$template);
  53. }
  54. $template = $this->initialize_loops($template);
  55. $this->template = $template;
  56. }
  57. /**
  58. *
  59. * @param string $name
  60. * @return string
  61. */
  62. public function getTemplatePath($name){
  63. $namespaces = explode("_",strtolower($name));
  64. $res = Settings::getInstance()->get("root");
  65. $skin = SkinController::getCurrentSkinName();
  66. $imported = false;
  67. if(sizeOf($namespaces) == 3){
  68. if($namespaces[0] == "plugin"){
  69. if(file_exists(Settings::getInstance()->get("root")."/system/skins/".$skin."/templates/plugins/".$namespaces[1]."/".$namespaces[2].".html")){
  70. $res .= "/system/skins/".$skin."/templates/plugins/".$namespaces[1]."/".$namespaces[2].".html";
  71. }
  72. else{
  73. $res .= "/system/plugins/".$namespaces[1]."/templates/".$namespaces[2].".html";
  74. }
  75. $imported = true;
  76. }
  77. }
  78. else if(sizeOf($namespaces) == 2){
  79. if($namespaces[0] == "form"){
  80. if(file_exists(Settings::getInstance()->get("root")."/system/skins/".$skin."/templates/forms/".$namespaces[1].".html")){
  81. $res .= "/system/skins/".$skin."/templates/forms/".$namespaces[1].".html";
  82. }
  83. else{
  84. $res .= "/system/templates/forms/".$namespaces[1].".html";
  85. }
  86. $imported = true;
  87. }
  88. else if($namespaces[0] == "control"){
  89. if(file_exists(Settings::getInstance()->get("root")."/system/skins/".$skin."/templates/controls/".$namespaces[1].".html")){
  90. $res .= "/system/skins/".$skin."/templates/controls/".$namespaces[1].".html";
  91. }
  92. else{
  93. $res .= "/system/templates/controls/".$namespaces[1].".html";
  94. }
  95. $imported = true;
  96. }
  97. else if($namespaces[0] == "widget"){
  98. if(file_exists(Settings::getInstance()->get("root")."/system/skins/".$skin."/templates/widgets/".$namespaces[1].".html")){
  99. $res .= "/system/skins/".$skin."/templates/widgets/".$namespaces[1].".html";
  100. }
  101. else{
  102. $res .= "/system/templates/widgets/".$namespaces[1].".html";
  103. }
  104. $imported = true;
  105. }
  106. }
  107. if(!$imported){
  108. if(file_exists(Settings::getInstance()->get("root")."/system/skins/".$skin."/templates/".$name.".html")){
  109. $res .= "/system/skins/".$skin."/templates/".$name.".html";
  110. }
  111. else{
  112. $res .= "/system/templates/".$name.".html";
  113. }
  114. }
  115. return $res;
  116. }
  117. /**
  118. *
  119. * @param string $type
  120. * @param string $field
  121. * @param string $value
  122. */
  123. public function assign($type, $field, $value){
  124. $this->template = str_ireplace('{'.strtoupper($type).':'.$field.'}', $value, $this->template);
  125. }
  126. /**
  127. *
  128. * @param string $field
  129. * @param string $value
  130. */
  131. public function assign_var($field, $value){
  132. $this->template = str_ireplace('{VAR:'.$field.'}', $this->escape($value), $this->template);
  133. }
  134. /**
  135. *
  136. * @param string $name
  137. * @return int
  138. */
  139. public function add_loop_item($name){
  140. $res = 0;
  141. if(isset($this->loops[strtoupper($name)])){
  142. $this->loops[strtoupper($name)][] = $this->loop_templates[strtoupper($name)];
  143. $res = count($this->loops[strtoupper($name)]) - 1;
  144. }
  145. return $res;
  146. }
  147. /**
  148. *
  149. * @param string $loop_name
  150. * @param int $index
  151. * @param string $field
  152. * @param string $value
  153. */
  154. public function assign_loop_var($loop_name, $index, $field, $value){
  155. if(isset($this->loops[strtoupper($loop_name)])){
  156. $this->loops[strtoupper($loop_name)][$index] = str_ireplace('{VAR:'.$field.'}', $value, $this->loops[strtoupper($loop_name)][$index]);
  157. }
  158. }
  159. /**
  160. *
  161. * @param string $name
  162. * @param boolean $show
  163. */
  164. public function show_if($name, $show = true){
  165. $this->ifs[strtoupper($name)] = $show;
  166. $this->ifs["!".strtoupper($name)] = !$show;
  167. }
  168. /**
  169. *
  170. * @param string $area
  171. * @param string $template
  172. */
  173. public function import($area, $template){
  174. if(file_exists($template)){
  175. $template = $this->read_file($template);
  176. }
  177. elseif(file_exists($this->template_path."/".$template)){
  178. $template = $this->read_file($template_path."/".$template);
  179. }
  180. $template = $this->initialize_loops($template);
  181. $this->template = str_ireplace('{INCLUDE:'.$area.'}', $template, $this->template);
  182. }
  183. private function replaceLanguageTokens(){
  184. preg_match_all("/{LANG:(.+)}/", $this->template, $matches, PREG_SET_ORDER);
  185. foreach($matches as $match){
  186. $translation = htmlentities(Language::DirectTranslate($match[1]));
  187. $this->template = str_ireplace($match[0],$translation,$this->template);
  188. }
  189. }
  190. private function replaceIcons(){
  191. preg_match_all("/{ICON:(.+)}/", $this->template, $matches, PREG_SET_ORDER);
  192. foreach($matches as $match){
  193. $translation = htmlentities(Icons::getIcon($match[1]));
  194. $this->template = str_ireplace($match[0],$translation,$this->template);
  195. }
  196. }
  197. private function replaceForms(){
  198. preg_match_all("/{FORM:(.+)}/", $this->template, $matches, PREG_SET_ORDER);
  199. foreach($matches as $match){
  200. $form = new Form($match[1]);
  201. $translation = $form->getCode();
  202. $this->template = str_ireplace($match[0],$translation,$this->template);
  203. }
  204. }
  205. /**
  206. *
  207. * @param string $template
  208. * @return string
  209. */
  210. private function removeHiddenIfBlocks($template){
  211. foreach($this->ifs as $if=>$value){
  212. $if = strtoupper($if);
  213. if($value){
  214. $template = str_ireplace("{IF:".$if."}","",$template);
  215. $template = str_ireplace("{/IF:".$if."}","",$template);
  216. }
  217. else{
  218. $template = preg_replace("/{IF:".$if."}((.|\s)*?){\/IF:".$if."}/","",$template);
  219. }
  220. }
  221. return $template;
  222. }
  223. private function unescape(){
  224. $this->template = str_ireplace("{TAG:","{",$this->template);
  225. }
  226. /**
  227. *
  228. * @param string $var
  229. * @return string
  230. */
  231. private function escape($var){
  232. return str_ireplace("{","{TAG:",$var);
  233. }
  234. /**
  235. *
  236. * @param boolean $escape
  237. * @return string
  238. */
  239. public function getCode($escape = false){
  240. $this->replaceGetParams();
  241. EventManager::RaiseEvent("PARSE_CONTENT",array("template" => $this));
  242. $this->template = $this->removeHiddenIfBlocks($this->template);
  243. foreach($this->loops as $key => $array){
  244. $loop_template = implode("\n",$array);
  245. $this->template = str_ireplace('<!--LOOP('.strtoupper($key).')-->', $loop_template, $this->template);
  246. }
  247. $this->replaceLanguageTokens();
  248. $this->replaceIcons();
  249. $this->replaceForms();
  250. if(!$escape){
  251. $this->unescape();
  252. }
  253. return $this->template;
  254. }
  255. public function replaceGetParams(){
  256. preg_match_all("/{GET:(.*?)}/", $this->template, $matches, PREG_SET_ORDER);
  257. foreach($matches as $match){
  258. if(isset($_GET[$match[1]])){
  259. $this->template = str_ireplace($match[0],htmlentities($_GET[$match[1]]),$this->template);
  260. }
  261. }
  262. }
  263. public function output(){
  264. echo $this->getCode();
  265. }
  266. }
  267. ?>