]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/render/PageBuilder.php
Process configure page 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
72 /**
73 * Initialize all default tpl tags.
74 */
75 private function initialize()
76 {
77 $this->tpl = new RainTPL();
78
79 try {
80 $version = ApplicationUtils::checkUpdate(
b3e1f92e 81 SHAARLI_VERSION,
894a3c4b
A
82 $this->conf->get('resource.update_check'),
83 $this->conf->get('updates.check_updates_interval'),
84 $this->conf->get('updates.check_updates'),
89ccc83b 85 $this->isLoggedIn,
894a3c4b 86 $this->conf->get('updates.check_updates_branch')
03eb19ac
A
87 );
88 $this->tpl->assign('newVersion', escape($version));
89 $this->tpl->assign('versionError', '');
03eb19ac 90 } catch (Exception $exc) {
894a3c4b 91 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
03eb19ac
A
92 $this->tpl->assign('newVersion', '');
93 $this->tpl->assign('versionError', escape($exc->getMessage()));
94 }
95
89ccc83b 96 $this->tpl->assign('is_logged_in', $this->isLoggedIn);
03eb19ac
A
97 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
98 $searchcrits = ''; // Search criteria
99 if (!empty($_GET['searchtags'])) {
100 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
101 }
102 if (!empty($_GET['searchterm'])) {
103 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
104 }
105 $this->tpl->assign('searchcrits', $searchcrits);
106 $this->tpl->assign('source', index_url($_SERVER));
b3e1f92e 107 $this->tpl->assign('version', SHAARLI_VERSION);
bfe4f536
A
108 $this->tpl->assign(
109 'version_hash',
110 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
111 );
a120fb29 112 $this->tpl->assign('index_url', index_url($_SERVER));
8c0f19c7 113 $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
9d4736a3 114 $this->tpl->assign('visibility', $visibility);
f210d94f 115 $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
97ef33bb 116 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
278d9ee2
A
117 if ($this->conf->exists('general.header_link')) {
118 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
03eb19ac 119 }
97ef33bb 120 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
894a3c4b 121 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
2ea89aba
A
122 $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
123 $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
894a3c4b 124 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
ebd650c0 125 $this->tpl->assign('token', $this->token);
bfe4f536 126
cb974e47
A
127 $this->tpl->assign('language', $this->conf->get('translation.language'));
128
cf92b4dd
A
129 if ($this->bookmarkService !== null) {
130 $this->tpl->assign('tags', $this->bookmarkService->bookmarksCountPerTag());
73c89626 131 }
1b93137e 132
b302b3c5
A
133 $this->tpl->assign(
134 'thumbnails_enabled',
135 $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
136 );
1b93137e
A
137 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
138 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
139
cf92b4dd
A
140 $this->tpl->assign('formatter', $this->conf->get('formatter', 'default'));
141
b302c77c
A
142 // To be removed with a proper theme configuration.
143 $this->tpl->assign('conf', $this->conf);
03eb19ac
A
144 }
145
66063ed1
A
146 /**
147 * Affect variable after controller processing.
148 * Used for alert messages.
149 */
ef00f9d2
A
150 protected function finalize(): void
151 {
152 // TODO: use the SessionManager
153 $messageKeys = [
154 SessionManager::KEY_SUCCESS_MESSAGES,
155 SessionManager::KEY_WARNING_MESSAGES,
156 SessionManager::KEY_ERROR_MESSAGES
157 ];
158 foreach ($messageKeys as $messageKey) {
159 if (!empty($_SESSION[$messageKey])) {
160 $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]);
161 unset($_SESSION[$messageKey]);
162 }
163 }
164 }
165
03eb19ac
A
166 /**
167 * The following assign() method is basically the same as RainTPL (except lazy loading)
168 *
169 * @param string $placeholder Template placeholder.
170 * @param mixed $value Value to assign.
171 */
172 public function assign($placeholder, $value)
173 {
03eb19ac
A
174 if ($this->tpl === false) {
175 $this->initialize();
176 }
177 $this->tpl->assign($placeholder, $value);
178 }
179
180 /**
181 * Assign an array of data to the template builder.
182 *
183 * @param array $data Data to assign.
184 *
185 * @return false if invalid data.
186 */
187 public function assignAll($data)
188 {
03eb19ac
A
189 if ($this->tpl === false) {
190 $this->initialize();
191 }
192
f211e417 193 if (empty($data) || !is_array($data)) {
03eb19ac
A
194 return false;
195 }
196
197 foreach ($data as $key => $value) {
198 $this->assign($key, $value);
199 }
278d9ee2 200 return true;
03eb19ac
A
201 }
202
203 /**
204 * Render a specific page (using a template file).
205 * e.g. $pb->renderPage('picwall');
206 *
207 * @param string $page Template filename (without extension).
208 */
209 public function renderPage($page)
210 {
278d9ee2 211 if ($this->tpl === false) {
03eb19ac
A
212 $this->initialize();
213 }
278d9ee2 214
ef00f9d2
A
215 $this->finalize();
216
03eb19ac
A
217 $this->tpl->draw($page);
218 }
219
6c50a6cc
A
220 /**
221 * Render a specific page as string (using a template file).
222 * e.g. $pb->render('picwall');
223 *
224 * @param string $page Template filename (without extension).
225 *
226 * @return string Processed template content
227 */
228 public function render(string $page): string
229 {
230 if ($this->tpl === false) {
231 $this->initialize();
232 }
233
ef00f9d2
A
234 $this->finalize();
235
6c50a6cc
A
236 return $this->tpl->draw($page, true);
237 }
238
03eb19ac
A
239 /**
240 * Render a 404 page (uses the template : tpl/404.tpl)
8c0f19c7 241 * usage: $PAGE->render404('The link was deleted')
03eb19ac 242 *
8c0f19c7 243 * @param string $message A message to display what is not found
03eb19ac 244 */
12266213 245 public function render404($message = '')
03eb19ac 246 {
12266213
A
247 if (empty($message)) {
248 $message = t('The page you are trying to reach does not exist or has been deleted.');
249 }
8c0f19c7 250 header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found'));
03eb19ac
A
251 $this->tpl->assign('error_message', $message);
252 $this->renderPage('404');
253 }
254}