]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/PageBuilder.php
Merge pull request #828 from ArthurHoaro/project/master-version
[github/shaarli/Shaarli.git] / application / PageBuilder.php
CommitLineData
03eb19ac
A
1<?php
2
3/**
4 * This class is in charge of building the final page.
5 * (This is basically a wrapper around RainTPL which pre-fills some fields.)
6 * $p = new PageBuilder();
7 * $p->assign('myfield','myvalue');
8 * $p->renderPage('mytemplate');
9 */
10class PageBuilder
11{
12 /**
13 * @var RainTPL RainTPL instance.
14 */
15 private $tpl;
16
278d9ee2
A
17 /**
18 * @var ConfigManager $conf Configuration Manager instance.
19 */
20 protected $conf;
21
03eb19ac
A
22 /**
23 * PageBuilder constructor.
24 * $tpl is initialized at false for lazy loading.
278d9ee2
A
25 *
26 * @param ConfigManager $conf Configuration Manager instance (reference).
03eb19ac 27 */
93b1fe54 28 public function __construct(&$conf)
03eb19ac
A
29 {
30 $this->tpl = false;
278d9ee2 31 $this->conf = $conf;
03eb19ac
A
32 }
33
34 /**
35 * Initialize all default tpl tags.
36 */
37 private function initialize()
38 {
39 $this->tpl = new RainTPL();
40
41 try {
42 $version = ApplicationUtils::checkUpdate(
43 shaarli_version,
894a3c4b
A
44 $this->conf->get('resource.update_check'),
45 $this->conf->get('updates.check_updates_interval'),
46 $this->conf->get('updates.check_updates'),
03eb19ac 47 isLoggedIn(),
894a3c4b 48 $this->conf->get('updates.check_updates_branch')
03eb19ac
A
49 );
50 $this->tpl->assign('newVersion', escape($version));
51 $this->tpl->assign('versionError', '');
52
53 } catch (Exception $exc) {
894a3c4b 54 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
03eb19ac
A
55 $this->tpl->assign('newVersion', '');
56 $this->tpl->assign('versionError', escape($exc->getMessage()));
57 }
58
59 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
60 $searchcrits = ''; // Search criteria
61 if (!empty($_GET['searchtags'])) {
62 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
63 }
64 if (!empty($_GET['searchterm'])) {
65 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
66 }
67 $this->tpl->assign('searchcrits', $searchcrits);
68 $this->tpl->assign('source', index_url($_SERVER));
69 $this->tpl->assign('version', shaarli_version);
70 $this->tpl->assign('scripturl', index_url($_SERVER));
03eb19ac 71 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
97ef33bb 72 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
278d9ee2
A
73 if ($this->conf->exists('general.header_link')) {
74 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
03eb19ac 75 }
97ef33bb 76 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
894a3c4b 77 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
2ea89aba
A
78 $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
79 $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
894a3c4b 80 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
fd5ac47e 81 $this->tpl->assign('token', getToken($this->conf));
b302c77c
A
82 // To be removed with a proper theme configuration.
83 $this->tpl->assign('conf', $this->conf);
03eb19ac
A
84 }
85
86 /**
87 * The following assign() method is basically the same as RainTPL (except lazy loading)
88 *
89 * @param string $placeholder Template placeholder.
90 * @param mixed $value Value to assign.
91 */
92 public function assign($placeholder, $value)
93 {
03eb19ac
A
94 if ($this->tpl === false) {
95 $this->initialize();
96 }
97 $this->tpl->assign($placeholder, $value);
98 }
99
100 /**
101 * Assign an array of data to the template builder.
102 *
103 * @param array $data Data to assign.
104 *
105 * @return false if invalid data.
106 */
107 public function assignAll($data)
108 {
03eb19ac
A
109 if ($this->tpl === false) {
110 $this->initialize();
111 }
112
113 if (empty($data) || !is_array($data)){
114 return false;
115 }
116
117 foreach ($data as $key => $value) {
118 $this->assign($key, $value);
119 }
278d9ee2 120 return true;
03eb19ac
A
121 }
122
123 /**
124 * Render a specific page (using a template file).
125 * e.g. $pb->renderPage('picwall');
126 *
127 * @param string $page Template filename (without extension).
128 */
129 public function renderPage($page)
130 {
278d9ee2 131 if ($this->tpl === false) {
03eb19ac
A
132 $this->initialize();
133 }
278d9ee2 134
03eb19ac
A
135 $this->tpl->draw($page);
136 }
137
138 /**
139 * Render a 404 page (uses the template : tpl/404.tpl)
140 * usage : $PAGE->render404('The link was deleted')
141 *
142 * @param string $message A messate to display what is not found
143 */
144 public function render404($message = 'The page you are trying to reach does not exist or has been deleted.')
145 {
146 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
147 $this->tpl->assign('error_message', $message);
148 $this->renderPage('404');
149 }
150}