diff options
author | Nicolas Lœuillet <nicolas.loeuillet@smile.fr> | 2014-10-10 13:33:54 +0200 |
---|---|---|
committer | Nicolas Lœuillet <nicolas.loeuillet@smile.fr> | 2014-10-10 13:33:54 +0200 |
commit | 44d35257e805856b4913c63fcbed3c0acb64bae8 (patch) | |
tree | 11e9d276c34b1b287706cb61182bdc71729661e2 /inc/poche/Template.class.php | |
parent | af8292c1de1886cd975d79f0f42df40e0bd1c5bd (diff) | |
parent | cf8a5e1eedbed484dbcb1ddc9f7a13fc19b7a27b (diff) | |
download | wallabag-1.8.0.tar.gz wallabag-1.8.0.tar.zst wallabag-1.8.0.zip |
Merge branch 'dev'1.8.0
Diffstat (limited to 'inc/poche/Template.class.php')
-rw-r--r-- | inc/poche/Template.class.php | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/inc/poche/Template.class.php b/inc/poche/Template.class.php new file mode 100644 index 00000000..b686f2ec --- /dev/null +++ b/inc/poche/Template.class.php | |||
@@ -0,0 +1,235 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * wallabag, self hostable application allowing you to not miss any content anymore | ||
4 | * | ||
5 | * @category wallabag | ||
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | ||
7 | * @copyright 2013 | ||
8 | * @license http://opensource.org/licenses/MIT see COPYING file | ||
9 | */ | ||
10 | |||
11 | class Template extends Twig_Environment | ||
12 | { | ||
13 | protected $wallabag; | ||
14 | |||
15 | private $canRenderTemplates = TRUE; | ||
16 | private $currentTheme = ''; | ||
17 | |||
18 | public function __construct(Poche $wallabag) | ||
19 | { | ||
20 | $this->wallabag = $wallabag; | ||
21 | |||
22 | // Set up theme | ||
23 | $pocheUser = Session::getParam('poche_user'); | ||
24 | |||
25 | $themeDirectory = (is_null($pocheUser) ? DEFAULT_THEME : $pocheUser->getConfigValue('theme')); | ||
26 | |||
27 | if ($themeDirectory === false) { | ||
28 | $themeDirectory = DEFAULT_THEME; | ||
29 | } | ||
30 | |||
31 | $this->currentTheme = $themeDirectory; | ||
32 | |||
33 | if ($this->_themeIsInstalled() === array()) { | ||
34 | $this->_init(); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * Returns true if selected theme is installed | ||
40 | * | ||
41 | * @return bool | ||
42 | */ | ||
43 | private function _themeIsInstalled() | ||
44 | { | ||
45 | $errors = array(); | ||
46 | |||
47 | // Twig is an absolute requirement for wallabag to function. | ||
48 | // Abort immediately if the Composer installer hasn't been run yet | ||
49 | if (!$this->canRenderTemplates) { | ||
50 | $errors[] = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. You can also download <a href="http://wllbg.org/vendor">vendor.zip</a> and extract it in your wallabag folder.'; | ||
51 | } | ||
52 | |||
53 | // Check if the selected theme and its requirements are present | ||
54 | $theme = $this->getTheme(); | ||
55 | if ($theme != '' && !is_dir(THEME . '/' . $theme)) { | ||
56 | $errors[] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $theme . ')'; | ||
57 | $this->canRenderTemplates = FALSE; | ||
58 | } | ||
59 | |||
60 | $themeInfo = $this->getThemeInfo($theme); | ||
61 | if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) { | ||
62 | foreach ($themeInfo['requirements'] as $requiredTheme) { | ||
63 | if (! is_dir(THEME . '/' . $requiredTheme)) { | ||
64 | $errors[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')'; | ||
65 | $this->canRenderTemplates = FALSE; | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | $currentErrors = (is_null(Session::getParam('errors'))? array() : Session::getParam('errors')); | ||
71 | Session::setParam('errors', array_merge($errors, $currentErrors)); | ||
72 | |||
73 | return $errors; | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Initialization for templates | ||
78 | */ | ||
79 | private function _init() | ||
80 | { | ||
81 | $loaderChain = new Twig_Loader_Chain(); | ||
82 | $theme = $this->getTheme(); | ||
83 | |||
84 | // add the current theme as first to the loader chain | ||
85 | // so Twig will look there first for overridden template files | ||
86 | try { | ||
87 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $theme)); | ||
88 | } catch (Twig_Error_Loader $e) { | ||
89 | # @todo isInstalled() should catch this, inject Twig later | ||
90 | die('The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME . '/' . $theme .' is missing)'); | ||
91 | } | ||
92 | |||
93 | // add all required themes to the loader chain | ||
94 | $themeInfo = $this->getThemeInfo($theme); | ||
95 | if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) { | ||
96 | foreach ($themeInfo['requirements'] as $requiredTheme) { | ||
97 | try { | ||
98 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $requiredTheme)); | ||
99 | } catch (Twig_Error_Loader $e) { | ||
100 | # @todo isInstalled() should catch this, inject Twig later | ||
101 | die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')'); | ||
102 | } | ||
103 | } | ||
104 | } | ||
105 | |||
106 | if (DEBUG_POCHE) { | ||
107 | $twigParams = array(); | ||
108 | } else { | ||
109 | $twigParams = array('cache' => CACHE); | ||
110 | } | ||
111 | |||
112 | parent::__construct($loaderChain, $twigParams); | ||
113 | |||
114 | //$tpl = new Twig_Environment($loaderChain, $twigParams); | ||
115 | $this->addExtension(new Twig_Extensions_Extension_I18n()); | ||
116 | |||
117 | # filter to display domain name of an url | ||
118 | $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); | ||
119 | $this->addFilter($filter); | ||
120 | |||
121 | # filter for reading time | ||
122 | $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime'); | ||
123 | $this->addFilter($filter); | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * Returns current theme | ||
128 | * | ||
129 | * @return string | ||
130 | */ | ||
131 | public function getTheme() | ||
132 | { | ||
133 | return $this->currentTheme; | ||
134 | } | ||
135 | |||
136 | /** | ||
137 | * Provides theme information by parsing theme.ini file if present in the theme's root directory. | ||
138 | * In all cases, the following data will be returned: | ||
139 | * - name: theme's name, or key if the theme is unnamed, | ||
140 | * - current: boolean informing if the theme is the current user theme. | ||
141 | * | ||
142 | * @param string $theme Theme key (directory name) | ||
143 | * @return array|boolean Theme information, or false if the theme doesn't exist. | ||
144 | */ | ||
145 | public function getThemeInfo($theme) | ||
146 | { | ||
147 | if (!is_dir(THEME . '/' . $theme)) { | ||
148 | return false; | ||
149 | } | ||
150 | |||
151 | $themeIniFile = THEME . '/' . $theme . '/theme.ini'; | ||
152 | $themeInfo = array(); | ||
153 | |||
154 | if (is_file($themeIniFile) && is_readable($themeIniFile)) { | ||
155 | $themeInfo = parse_ini_file($themeIniFile); | ||
156 | } | ||
157 | |||
158 | if ($themeInfo === false) { | ||
159 | $themeInfo = array(); | ||
160 | } | ||
161 | |||
162 | if (!isset($themeInfo['name'])) { | ||
163 | $themeInfo['name'] = $theme; | ||
164 | } | ||
165 | |||
166 | $themeInfo['current'] = ($theme === $this->getTheme()); | ||
167 | |||
168 | return $themeInfo; | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * Returns an array with installed themes | ||
173 | * | ||
174 | * @return array | ||
175 | */ | ||
176 | public function getInstalledThemes() | ||
177 | { | ||
178 | $handle = opendir(THEME); | ||
179 | $themes = array(); | ||
180 | |||
181 | while (($theme = readdir($handle)) !== false) { | ||
182 | # Themes are stored in a directory, so all directory names are themes | ||
183 | # @todo move theme installation data to database | ||
184 | if (!is_dir(THEME . '/' . $theme) || in_array($theme, array('.', '..'))) { | ||
185 | continue; | ||
186 | } | ||
187 | |||
188 | $themes[$theme] = $this->getThemeInfo($theme); | ||
189 | } | ||
190 | |||
191 | ksort($themes); | ||
192 | |||
193 | return $themes; | ||
194 | } | ||
195 | |||
196 | /** | ||
197 | * Update theme for the current user | ||
198 | * | ||
199 | * @param $newTheme | ||
200 | */ | ||
201 | public function updateTheme($newTheme) | ||
202 | { | ||
203 | # we are not going to change it to the current theme... | ||
204 | if ($newTheme == $this->getTheme()) { | ||
205 | $this->wallabag->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); | ||
206 | Tools::redirect('?view=config'); | ||
207 | } | ||
208 | |||
209 | $themes = $this->getInstalledThemes(); | ||
210 | $actualTheme = false; | ||
211 | |||
212 | foreach (array_keys($themes) as $theme) { | ||
213 | if ($theme == $newTheme) { | ||
214 | $actualTheme = true; | ||
215 | break; | ||
216 | } | ||
217 | } | ||
218 | |||
219 | if (!$actualTheme) { | ||
220 | $this->wallabag->messages->add('e', _('that theme does not seem to be installed')); | ||
221 | Tools::redirect('?view=config'); | ||
222 | } | ||
223 | |||
224 | $this->wallabag->store->updateUserConfig($this->wallabag->user->getId(), 'theme', $newTheme); | ||
225 | $this->wallabag->messages->add('s', _('you have changed your theme preferences')); | ||
226 | |||
227 | $currentConfig = $_SESSION['poche_user']->config; | ||
228 | $currentConfig['theme'] = $newTheme; | ||
229 | |||
230 | $_SESSION['poche_user']->setConfig($currentConfig); | ||
231 | |||
232 | Tools::emptyCache(); | ||
233 | Tools::redirect('?view=config'); | ||
234 | } | ||
235 | } \ No newline at end of file | ||