]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/render/PageBuilder.php
Process main page (linklist) through Slim controller
[github/shaarli/Shaarli.git] / application / render / PageBuilder.php
CommitLineData
03eb19ac
A
1<?php
2
8c0f19c7
V
3namespace Shaarli\Render;
4
8c0f19c7 5use Exception;
8c0f19c7 6use RainTPL;
dea72c71 7use Shaarli\ApplicationUtils;
cf92b4dd 8use Shaarli\Bookmark\BookmarkServiceInterface;
ae3aa968 9use Shaarli\Config\ConfigManager;
ef00f9d2 10use Shaarli\Security\SessionManager;
b302b3c5 11use Shaarli\Thumbnailer;
ae3aa968 12
03eb19ac
A
13/**
14 * This class is in charge of building the final page.
15 * (This is basically a wrapper around RainTPL which pre-fills some fields.)
16 * $p = new PageBuilder();
17 * $p->assign('myfield','myvalue');
18 * $p->renderPage('mytemplate');
19 */
20class PageBuilder
21{
22 /**
23 * @var RainTPL RainTPL instance.
24 */
25 private $tpl;
26
278d9ee2
A
27 /**
28 * @var ConfigManager $conf Configuration Manager instance.
29 */
30 protected $conf;
31
28f26524
A
32 /**
33 * @var array $_SESSION
34 */
35 protected $session;
36
73c89626 37 /**
cf92b4dd 38 * @var BookmarkServiceInterface $bookmarkService instance.
73c89626 39 */
cf92b4dd 40 protected $bookmarkService;
28f26524
A
41
42 /**
43 * @var null|string XSRF token
44 */
45 protected $token;
46
8c0f19c7
V
47 /**
48 * @var bool $isLoggedIn Whether the user is logged in
49 */
89ccc83b 50 protected $isLoggedIn = false;
73c89626 51
03eb19ac
A
52 /**
53 * PageBuilder constructor.
54 * $tpl is initialized at false for lazy loading.
278d9ee2 55 *
cf92b4dd
A
56 * @param ConfigManager $conf Configuration Manager instance (reference).
57 * @param array $session $_SESSION array
58 * @param BookmarkServiceInterface $linkDB instance.
59 * @param string $token Session token
60 * @param bool $isLoggedIn
03eb19ac 61 */
28f26524 62 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
03eb19ac
A
63 {
64 $this->tpl = false;
278d9ee2 65 $this->conf = $conf;
28f26524 66 $this->session = $session;
cf92b4dd 67 $this->bookmarkService = $linkDB;
ebd650c0 68 $this->token = $token;
89ccc83b 69 $this->isLoggedIn = $isLoggedIn;
03eb19ac
A
70 }
71
1a8ac737
A
72 /**
73 * Reset current state of template rendering.
74 * Mostly useful for error handling. We remove everything, and display the error template.
75 */
76 public function reset(): void
77 {
78 $this->tpl = false;
79 }
80
03eb19ac
A
81 /**
82 * Initialize all default tpl tags.
83 */
84 private function initialize()
85 {
86 $this->tpl = new RainTPL();
87
88 try {
89 $version = ApplicationUtils::checkUpdate(
b3e1f92e 90 SHAARLI_VERSION,
894a3c4b
A
91 $this->conf->get('resource.update_check'),
92 $this->conf->get('updates.check_updates_interval'),
93 $this->conf->get('updates.check_updates'),
89ccc83b 94 $this->isLoggedIn,
894a3c4b 95 $this->conf->get('updates.check_updates_branch')
03eb19ac
A
96 );
97 $this->tpl->assign('newVersion', escape($version));
98 $this->tpl->assign('versionError', '');
03eb19ac 99 } catch (Exception $exc) {
894a3c4b 100 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
03eb19ac
A
101 $this->tpl->assign('newVersion', '');
102 $this->tpl->assign('versionError', escape($exc->getMessage()));
103 }
104
89ccc83b 105 $this->tpl->assign('is_logged_in', $this->isLoggedIn);
03eb19ac
A
106 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
107 $searchcrits = ''; // Search criteria
108 if (!empty($_GET['searchtags'])) {
109 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
110 }
111 if (!empty($_GET['searchterm'])) {
112 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
113 }
114 $this->tpl->assign('searchcrits', $searchcrits);
115 $this->tpl->assign('source', index_url($_SERVER));
b3e1f92e 116 $this->tpl->assign('version', SHAARLI_VERSION);
bfe4f536
A
117 $this->tpl->assign(
118 'version_hash',
119 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
120 );
a120fb29 121 $this->tpl->assign('index_url', index_url($_SERVER));
8c0f19c7 122 $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
9d4736a3 123 $this->tpl->assign('visibility', $visibility);
f210d94f 124 $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
97ef33bb 125 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
278d9ee2
A
126 if ($this->conf->exists('general.header_link')) {
127 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
03eb19ac 128 }
97ef33bb 129 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
894a3c4b 130 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
2ea89aba
A
131 $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
132 $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
894a3c4b 133 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
ebd650c0 134 $this->tpl->assign('token', $this->token);
bfe4f536 135
cb974e47
A
136 $this->tpl->assign('language', $this->conf->get('translation.language'));
137
cf92b4dd
A
138 if ($this->bookmarkService !== null) {
139 $this->tpl->assign('tags', $this->bookmarkService->bookmarksCountPerTag());
73c89626 140 }
1b93137e 141
b302b3c5
A
142 $this->tpl->assign(
143 'thumbnails_enabled',
144 $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
145 );
1b93137e
A
146 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
147 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
148
cf92b4dd
A
149 $this->tpl->assign('formatter', $this->conf->get('formatter', 'default'));
150
b302c77c
A
151 // To be removed with a proper theme configuration.
152 $this->tpl->assign('conf', $this->conf);
03eb19ac
A
153 }
154
66063ed1
A
155 /**
156 * Affect variable after controller processing.
157 * Used for alert messages.
158 */
ef00f9d2
A
159 protected function finalize(): void
160 {
818b3193
A
161 //FIXME - DEV _ REMOVE ME
162 $this->assign('base_path', '/Shaarli');
163 $this->assign('asset_path', '/Shaarli/tpl/default');
164
ef00f9d2
A
165 // TODO: use the SessionManager
166 $messageKeys = [
167 SessionManager::KEY_SUCCESS_MESSAGES,
168 SessionManager::KEY_WARNING_MESSAGES,
169 SessionManager::KEY_ERROR_MESSAGES
170 ];
171 foreach ($messageKeys as $messageKey) {
172 if (!empty($_SESSION[$messageKey])) {
173 $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]);
174 unset($_SESSION[$messageKey]);
175 }
176 }
177 }
178
03eb19ac
A
179 /**
180 * The following assign() method is basically the same as RainTPL (except lazy loading)
181 *
182 * @param string $placeholder Template placeholder.
183 * @param mixed $value Value to assign.
184 */
185 public function assign($placeholder, $value)
186 {
03eb19ac
A
187 if ($this->tpl === false) {
188 $this->initialize();
189 }
190 $this->tpl->assign($placeholder, $value);
191 }
192
193 /**
194 * Assign an array of data to the template builder.
195 *
196 * @param array $data Data to assign.
197 *
198 * @return false if invalid data.
199 */
200 public function assignAll($data)
201 {
03eb19ac
A
202 if ($this->tpl === false) {
203 $this->initialize();
204 }
205
f211e417 206 if (empty($data) || !is_array($data)) {
03eb19ac
A
207 return false;
208 }
209
210 foreach ($data as $key => $value) {
211 $this->assign($key, $value);
212 }
278d9ee2 213 return true;
03eb19ac
A
214 }
215
216 /**
217 * Render a specific page (using a template file).
218 * e.g. $pb->renderPage('picwall');
219 *
220 * @param string $page Template filename (without extension).
221 */
222 public function renderPage($page)
223 {
278d9ee2 224 if ($this->tpl === false) {
03eb19ac
A
225 $this->initialize();
226 }
278d9ee2 227
ef00f9d2
A
228 $this->finalize();
229
03eb19ac
A
230 $this->tpl->draw($page);
231 }
232
6c50a6cc
A
233 /**
234 * Render a specific page as string (using a template file).
235 * e.g. $pb->render('picwall');
236 *
237 * @param string $page Template filename (without extension).
238 *
239 * @return string Processed template content
240 */
241 public function render(string $page): string
242 {
243 if ($this->tpl === false) {
244 $this->initialize();
245 }
246
ef00f9d2
A
247 $this->finalize();
248
6c50a6cc
A
249 return $this->tpl->draw($page, true);
250 }
251
03eb19ac
A
252 /**
253 * Render a 404 page (uses the template : tpl/404.tpl)
8c0f19c7 254 * usage: $PAGE->render404('The link was deleted')
03eb19ac 255 *
8c0f19c7 256 * @param string $message A message to display what is not found
03eb19ac 257 */
12266213 258 public function render404($message = '')
03eb19ac 259 {
12266213
A
260 if (empty($message)) {
261 $message = t('The page you are trying to reach does not exist or has been deleted.');
262 }
8c0f19c7 263 header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found'));
03eb19ac
A
264 $this->tpl->assign('error_message', $message);
265 $this->renderPage('404');
266 }
267}