]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/PageBuilder.php
Use web-thumbnailer to retrieve thumbnails
[github/shaarli/Shaarli.git] / application / PageBuilder.php
CommitLineData
03eb19ac
A
1<?php
2
ae3aa968
A
3use Shaarli\Config\ConfigManager;
4
03eb19ac
A
5/**
6 * This class is in charge of building the final page.
7 * (This is basically a wrapper around RainTPL which pre-fills some fields.)
8 * $p = new PageBuilder();
9 * $p->assign('myfield','myvalue');
10 * $p->renderPage('mytemplate');
11 */
12class PageBuilder
13{
14 /**
15 * @var RainTPL RainTPL instance.
16 */
17 private $tpl;
18
278d9ee2
A
19 /**
20 * @var ConfigManager $conf Configuration Manager instance.
21 */
22 protected $conf;
23
73c89626
A
24 /**
25 * @var LinkDB $linkDB instance.
26 */
27 protected $linkDB;
89ccc83b
V
28
29 /** @var bool $isLoggedIn Whether the user is logged in **/
30 protected $isLoggedIn = false;
73c89626 31
03eb19ac
A
32 /**
33 * PageBuilder constructor.
34 * $tpl is initialized at false for lazy loading.
278d9ee2 35 *
73c89626
A
36 * @param ConfigManager $conf Configuration Manager instance (reference).
37 * @param LinkDB $linkDB instance.
ebd650c0 38 * @param string $token Session token
03eb19ac 39 */
89ccc83b 40 public function __construct(&$conf, $linkDB = null, $token = null, $isLoggedIn = false)
03eb19ac
A
41 {
42 $this->tpl = false;
278d9ee2 43 $this->conf = $conf;
73c89626 44 $this->linkDB = $linkDB;
ebd650c0 45 $this->token = $token;
89ccc83b 46 $this->isLoggedIn = $isLoggedIn;
03eb19ac
A
47 }
48
49 /**
50 * Initialize all default tpl tags.
51 */
52 private function initialize()
53 {
54 $this->tpl = new RainTPL();
55
56 try {
57 $version = ApplicationUtils::checkUpdate(
b3e1f92e 58 SHAARLI_VERSION,
894a3c4b
A
59 $this->conf->get('resource.update_check'),
60 $this->conf->get('updates.check_updates_interval'),
61 $this->conf->get('updates.check_updates'),
89ccc83b 62 $this->isLoggedIn,
894a3c4b 63 $this->conf->get('updates.check_updates_branch')
03eb19ac
A
64 );
65 $this->tpl->assign('newVersion', escape($version));
66 $this->tpl->assign('versionError', '');
67
68 } catch (Exception $exc) {
894a3c4b 69 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
03eb19ac
A
70 $this->tpl->assign('newVersion', '');
71 $this->tpl->assign('versionError', escape($exc->getMessage()));
72 }
73
89ccc83b 74 $this->tpl->assign('is_logged_in', $this->isLoggedIn);
03eb19ac
A
75 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
76 $searchcrits = ''; // Search criteria
77 if (!empty($_GET['searchtags'])) {
78 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
79 }
80 if (!empty($_GET['searchterm'])) {
81 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
82 }
83 $this->tpl->assign('searchcrits', $searchcrits);
84 $this->tpl->assign('source', index_url($_SERVER));
b3e1f92e 85 $this->tpl->assign('version', SHAARLI_VERSION);
bfe4f536
A
86 $this->tpl->assign(
87 'version_hash',
88 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
89 );
03eb19ac 90 $this->tpl->assign('scripturl', index_url($_SERVER));
9d4736a3
A
91 $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
92 $this->tpl->assign('visibility', $visibility);
f210d94f 93 $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
97ef33bb 94 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
278d9ee2
A
95 if ($this->conf->exists('general.header_link')) {
96 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
03eb19ac 97 }
97ef33bb 98 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
894a3c4b 99 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
2ea89aba
A
100 $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
101 $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
894a3c4b 102 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
ebd650c0 103 $this->tpl->assign('token', $this->token);
bfe4f536 104
73c89626 105 if ($this->linkDB !== null) {
6ccd0b21 106 $this->tpl->assign('tags', $this->linkDB->linksCountPerTag());
73c89626 107 }
1b93137e
A
108
109 $this->tpl->assign('thumbnails_enabled', $this->conf->get('thumbnails.enabled'));
110 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
111 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
112
b302c77c
A
113 // To be removed with a proper theme configuration.
114 $this->tpl->assign('conf', $this->conf);
03eb19ac
A
115 }
116
117 /**
118 * The following assign() method is basically the same as RainTPL (except lazy loading)
119 *
120 * @param string $placeholder Template placeholder.
121 * @param mixed $value Value to assign.
122 */
123 public function assign($placeholder, $value)
124 {
03eb19ac
A
125 if ($this->tpl === false) {
126 $this->initialize();
127 }
128 $this->tpl->assign($placeholder, $value);
129 }
130
131 /**
132 * Assign an array of data to the template builder.
133 *
134 * @param array $data Data to assign.
135 *
136 * @return false if invalid data.
137 */
138 public function assignAll($data)
139 {
03eb19ac
A
140 if ($this->tpl === false) {
141 $this->initialize();
142 }
143
144 if (empty($data) || !is_array($data)){
145 return false;
146 }
147
148 foreach ($data as $key => $value) {
149 $this->assign($key, $value);
150 }
278d9ee2 151 return true;
03eb19ac
A
152 }
153
154 /**
155 * Render a specific page (using a template file).
156 * e.g. $pb->renderPage('picwall');
157 *
158 * @param string $page Template filename (without extension).
159 */
160 public function renderPage($page)
161 {
278d9ee2 162 if ($this->tpl === false) {
03eb19ac
A
163 $this->initialize();
164 }
278d9ee2 165
03eb19ac
A
166 $this->tpl->draw($page);
167 }
168
169 /**
170 * Render a 404 page (uses the template : tpl/404.tpl)
171 * usage : $PAGE->render404('The link was deleted')
172 *
173 * @param string $message A messate to display what is not found
174 */
12266213 175 public function render404($message = '')
03eb19ac 176 {
12266213
A
177 if (empty($message)) {
178 $message = t('The page you are trying to reach does not exist or has been deleted.');
179 }
180 header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found'));
03eb19ac
A
181 $this->tpl->assign('error_message', $message);
182 $this->renderPage('404');
183 }
184}