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