]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/PageBuilder.php
3233d6b64562f78790f08513d8a519be1640e3e3
[github/shaarli/Shaarli.git] / application / PageBuilder.php
1 <?php
2
3 use Shaarli\Config\ConfigManager;
4
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 */
12 class PageBuilder
13 {
14 /**
15 * @var RainTPL RainTPL instance.
16 */
17 private $tpl;
18
19 /**
20 * @var ConfigManager $conf Configuration Manager instance.
21 */
22 protected $conf;
23
24 /**
25 * @var LinkDB $linkDB instance.
26 */
27 protected $linkDB;
28
29 /**
30 * PageBuilder constructor.
31 * $tpl is initialized at false for lazy loading.
32 *
33 * @param ConfigManager $conf Configuration Manager instance (reference).
34 * @param LinkDB $linkDB instance.
35 * @param string $token Session token
36 */
37 public function __construct(&$conf, $linkDB = null, $token = null)
38 {
39 $this->tpl = false;
40 $this->conf = $conf;
41 $this->linkDB = $linkDB;
42 $this->token = $token;
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(
54 SHAARLI_VERSION,
55 $this->conf->get('resource.update_check'),
56 $this->conf->get('updates.check_updates_interval'),
57 $this->conf->get('updates.check_updates'),
58 isLoggedIn(),
59 $this->conf->get('updates.check_updates_branch')
60 );
61 $this->tpl->assign('newVersion', escape($version));
62 $this->tpl->assign('versionError', '');
63
64 } catch (Exception $exc) {
65 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
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));
80 $this->tpl->assign('version', SHAARLI_VERSION);
81 $this->tpl->assign(
82 'version_hash',
83 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
84 );
85 $this->tpl->assign('scripturl', index_url($_SERVER));
86 $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
87 $this->tpl->assign('visibility', $visibility);
88 $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
89 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
90 if ($this->conf->exists('general.header_link')) {
91 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
92 }
93 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
94 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
95 $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
96 $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
97 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
98 $this->tpl->assign('token', $this->token);
99
100 if ($this->linkDB !== null) {
101 $this->tpl->assign('tags', $this->linkDB->linksCountPerTag());
102 }
103 // To be removed with a proper theme configuration.
104 $this->tpl->assign('conf', $this->conf);
105 }
106
107 /**
108 * The following assign() method is basically the same as RainTPL (except lazy loading)
109 *
110 * @param string $placeholder Template placeholder.
111 * @param mixed $value Value to assign.
112 */
113 public function assign($placeholder, $value)
114 {
115 if ($this->tpl === false) {
116 $this->initialize();
117 }
118 $this->tpl->assign($placeholder, $value);
119 }
120
121 /**
122 * Assign an array of data to the template builder.
123 *
124 * @param array $data Data to assign.
125 *
126 * @return false if invalid data.
127 */
128 public function assignAll($data)
129 {
130 if ($this->tpl === false) {
131 $this->initialize();
132 }
133
134 if (empty($data) || !is_array($data)){
135 return false;
136 }
137
138 foreach ($data as $key => $value) {
139 $this->assign($key, $value);
140 }
141 return true;
142 }
143
144 /**
145 * Render a specific page (using a template file).
146 * e.g. $pb->renderPage('picwall');
147 *
148 * @param string $page Template filename (without extension).
149 */
150 public function renderPage($page)
151 {
152 if ($this->tpl === false) {
153 $this->initialize();
154 }
155
156 $this->tpl->draw($page);
157 }
158
159 /**
160 * Render a 404 page (uses the template : tpl/404.tpl)
161 * usage : $PAGE->render404('The link was deleted')
162 *
163 * @param string $message A messate to display what is not found
164 */
165 public function render404($message = '')
166 {
167 if (empty($message)) {
168 $message = t('The page you are trying to reach does not exist or has been deleted.');
169 }
170 header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found'));
171 $this->tpl->assign('error_message', $message);
172 $this->renderPage('404');
173 }
174 }