diff options
Diffstat (limited to 'application/render/PageBuilder.php')
-rw-r--r-- | application/render/PageBuilder.php | 79 |
1 files changed, 42 insertions, 37 deletions
diff --git a/application/render/PageBuilder.php b/application/render/PageBuilder.php index f4fefda8..7a716673 100644 --- a/application/render/PageBuilder.php +++ b/application/render/PageBuilder.php | |||
@@ -3,10 +3,12 @@ | |||
3 | namespace Shaarli\Render; | 3 | namespace Shaarli\Render; |
4 | 4 | ||
5 | use Exception; | 5 | use Exception; |
6 | use exceptions\MissingBasePathException; | ||
6 | use RainTPL; | 7 | use RainTPL; |
7 | use Shaarli\ApplicationUtils; | 8 | use Shaarli\ApplicationUtils; |
8 | use Shaarli\Bookmark\BookmarkServiceInterface; | 9 | use Shaarli\Bookmark\BookmarkServiceInterface; |
9 | use Shaarli\Config\ConfigManager; | 10 | use Shaarli\Config\ConfigManager; |
11 | use Shaarli\Security\SessionManager; | ||
10 | use Shaarli\Thumbnailer; | 12 | use Shaarli\Thumbnailer; |
11 | 13 | ||
12 | /** | 14 | /** |
@@ -69,6 +71,15 @@ class PageBuilder | |||
69 | } | 71 | } |
70 | 72 | ||
71 | /** | 73 | /** |
74 | * Reset current state of template rendering. | ||
75 | * Mostly useful for error handling. We remove everything, and display the error template. | ||
76 | */ | ||
77 | public function reset(): void | ||
78 | { | ||
79 | $this->tpl = false; | ||
80 | } | ||
81 | |||
82 | /** | ||
72 | * Initialize all default tpl tags. | 83 | * Initialize all default tpl tags. |
73 | */ | 84 | */ |
74 | private function initialize() | 85 | private function initialize() |
@@ -136,11 +147,6 @@ class PageBuilder | |||
136 | $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); | 147 | $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); |
137 | $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); | 148 | $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); |
138 | 149 | ||
139 | if (!empty($_SESSION['warnings'])) { | ||
140 | $this->tpl->assign('global_warnings', $_SESSION['warnings']); | ||
141 | unset($_SESSION['warnings']); | ||
142 | } | ||
143 | |||
144 | $this->tpl->assign('formatter', $this->conf->get('formatter', 'default')); | 150 | $this->tpl->assign('formatter', $this->conf->get('formatter', 'default')); |
145 | 151 | ||
146 | // To be removed with a proper theme configuration. | 152 | // To be removed with a proper theme configuration. |
@@ -148,6 +154,34 @@ class PageBuilder | |||
148 | } | 154 | } |
149 | 155 | ||
150 | /** | 156 | /** |
157 | * Affect variable after controller processing. | ||
158 | * Used for alert messages. | ||
159 | */ | ||
160 | protected function finalize(string $basePath): void | ||
161 | { | ||
162 | // TODO: use the SessionManager | ||
163 | $messageKeys = [ | ||
164 | SessionManager::KEY_SUCCESS_MESSAGES, | ||
165 | SessionManager::KEY_WARNING_MESSAGES, | ||
166 | SessionManager::KEY_ERROR_MESSAGES | ||
167 | ]; | ||
168 | foreach ($messageKeys as $messageKey) { | ||
169 | if (!empty($_SESSION[$messageKey])) { | ||
170 | $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]); | ||
171 | unset($_SESSION[$messageKey]); | ||
172 | } | ||
173 | } | ||
174 | |||
175 | $this->assign('base_path', $basePath); | ||
176 | $this->assign( | ||
177 | 'asset_path', | ||
178 | $basePath . '/' . | ||
179 | rtrim($this->conf->get('resource.raintpl_tpl', 'tpl'), '/') . '/' . | ||
180 | $this->conf->get('resource.theme', 'default') | ||
181 | ); | ||
182 | } | ||
183 | |||
184 | /** | ||
151 | * The following assign() method is basically the same as RainTPL (except lazy loading) | 185 | * The following assign() method is basically the same as RainTPL (except lazy loading) |
152 | * | 186 | * |
153 | * @param string $placeholder Template placeholder. | 187 | * @param string $placeholder Template placeholder. |
@@ -185,21 +219,6 @@ class PageBuilder | |||
185 | } | 219 | } |
186 | 220 | ||
187 | /** | 221 | /** |
188 | * Render a specific page (using a template file). | ||
189 | * e.g. $pb->renderPage('picwall'); | ||
190 | * | ||
191 | * @param string $page Template filename (without extension). | ||
192 | */ | ||
193 | public function renderPage($page) | ||
194 | { | ||
195 | if ($this->tpl === false) { | ||
196 | $this->initialize(); | ||
197 | } | ||
198 | |||
199 | $this->tpl->draw($page); | ||
200 | } | ||
201 | |||
202 | /** | ||
203 | * Render a specific page as string (using a template file). | 222 | * Render a specific page as string (using a template file). |
204 | * e.g. $pb->render('picwall'); | 223 | * e.g. $pb->render('picwall'); |
205 | * | 224 | * |
@@ -207,28 +226,14 @@ class PageBuilder | |||
207 | * | 226 | * |
208 | * @return string Processed template content | 227 | * @return string Processed template content |
209 | */ | 228 | */ |
210 | public function render(string $page): string | 229 | public function render(string $page, string $basePath): string |
211 | { | 230 | { |
212 | if ($this->tpl === false) { | 231 | if ($this->tpl === false) { |
213 | $this->initialize(); | 232 | $this->initialize(); |
214 | } | 233 | } |
215 | 234 | ||
216 | return $this->tpl->draw($page, true); | 235 | $this->finalize($basePath); |
217 | } | ||
218 | 236 | ||
219 | /** | 237 | return $this->tpl->draw($page, true); |
220 | * Render a 404 page (uses the template : tpl/404.tpl) | ||
221 | * usage: $PAGE->render404('The link was deleted') | ||
222 | * | ||
223 | * @param string $message A message to display what is not found | ||
224 | */ | ||
225 | public function render404($message = '') | ||
226 | { | ||
227 | if (empty($message)) { | ||
228 | $message = t('The page you are trying to reach does not exist or has been deleted.'); | ||
229 | } | ||
230 | header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found')); | ||
231 | $this->tpl->assign('error_message', $message); | ||
232 | $this->renderPage('404'); | ||
233 | } | 238 | } |
234 | } | 239 | } |