|
| 1 | +<?php |
| 2 | +/* |
| 3 | +########################################################################## |
| 4 | +# # |
| 5 | +# Version 4 / / / # |
| 6 | +# -----------__---/__---__------__----__---/---/- # |
| 7 | +# | /| / /___) / ) (_ ` / ) /___) / / # |
| 8 | +# _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___ # |
| 9 | +# Free Content / Management System # |
| 10 | +# / # |
| 11 | +# # |
| 12 | +# # |
| 13 | +# Copyright 2005-2015 by webspell.org # |
| 14 | +# # |
| 15 | +# visit webSPELL.org, webspell.info to get webSPELL for free # |
| 16 | +# - Script runs under the GNU GENERAL PUBLIC LICENSE # |
| 17 | +# - It's NOT allowed to remove this copyright-tag # |
| 18 | +# -- http://www.fsf.org/licensing/licenses/gpl.html # |
| 19 | +# # |
| 20 | +# Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at), # |
| 21 | +# Far Development by Development Team - webspell.org # |
| 22 | +# # |
| 23 | +# visit webspell.org # |
| 24 | +# # |
| 25 | +########################################################################## |
| 26 | +*/ |
| 27 | + |
| 28 | +namespace webspell_ng; |
| 29 | + |
| 30 | +use webspell_ng\Language; |
| 31 | + |
| 32 | +class Template |
| 33 | +{ |
| 34 | + |
| 35 | + /** |
| 36 | + * @var string $moduleName |
| 37 | + */ |
| 38 | + private $moduleName; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var bool $moduleName |
| 42 | + */ |
| 43 | + private $isAdminLanguage; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string $defaultFolder |
| 47 | + */ |
| 48 | + private $defaultFolder = __DIR__ . '/../templates'; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string $rootFolder |
| 52 | + */ |
| 53 | + private $rootFolder; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var Language $language |
| 57 | + */ |
| 58 | + private $language; |
| 59 | + |
| 60 | + /** |
| 61 | + * @param string $rootFolder base folder where the template files are located |
| 62 | + */ |
| 63 | + public function __construct(string $module_name, bool $is_admin_language=false, string $language="en", $rootFolder = "templates") |
| 64 | + { |
| 65 | + |
| 66 | + $this->moduleName = $module_name; |
| 67 | + $this->isAdminLanguage = $is_admin_language; |
| 68 | + $this->rootFolder = $rootFolder; |
| 69 | + |
| 70 | + $this->language = new Language(); |
| 71 | + $this->language->setLanguage($language); |
| 72 | + $this->language->readModule($this->moduleName, false, $this->isAdminLanguage); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * returns the content of a template file |
| 78 | + * |
| 79 | + * @param string $template name of the template |
| 80 | + * |
| 81 | + * @return string content of the template |
| 82 | + */ |
| 83 | + private function loadFile($template): string |
| 84 | + { |
| 85 | + |
| 86 | + $file = $this->rootFolder . "/" . $template . ".html"; |
| 87 | + if (!file_exists($file)) { |
| 88 | + $file = $this->defaultFolder . "/error.html"; |
| 89 | + } |
| 90 | + |
| 91 | + return file_get_contents($file); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Replace all keys of data with its values in the string |
| 97 | + * Longer keys are replaced first (users before user) |
| 98 | + * |
| 99 | + * @param string $template |
| 100 | + * @param array $data |
| 101 | + * |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + private function replace($template, $data = array()): string |
| 105 | + { |
| 106 | + return strtr($template, $data); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Replace a single template with one set of data and translate all language keys |
| 111 | + * |
| 112 | + * @param string $template name of a template |
| 113 | + * @param array $data data which gets replaced |
| 114 | + * |
| 115 | + * @return string |
| 116 | + */ |
| 117 | + public function replaceTemplate($template, $data = array()): string |
| 118 | + { |
| 119 | + $templateString = $this->loadFile($template); |
| 120 | + $templateTranslated = $this->replaceLanguage($templateString); |
| 121 | + return $this->replace($templateTranslated, $data); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Replaces all language variables which are available |
| 126 | + * |
| 127 | + * @param string $template content of a template |
| 128 | + * |
| 129 | + * @return string |
| 130 | + */ |
| 131 | + private function replaceLanguage(string $template): string |
| 132 | + { |
| 133 | + return $this->replace( |
| 134 | + $template, |
| 135 | + $this->language->getTranslationTable() |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments