]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/PageBuilder.php
ConfigManager no longer uses singleton pattern
[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 */
278d9ee2 28 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,
278d9ee2
A
44 $this->conf->get('path.update_check'),
45 $this->conf->get('general.check_updates_interval'),
46 $this->conf->get('general.check_updates'),
03eb19ac 47 isLoggedIn(),
278d9ee2 48 $this->conf->get('general.check_updates_branch')
03eb19ac
A
49 );
50 $this->tpl->assign('newVersion', escape($version));
51 $this->tpl->assign('versionError', '');
52
53 } catch (Exception $exc) {
278d9ee2 54 logm($this->conf->get('path.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));
71 $this->tpl->assign('pagetitle', 'Shaarli');
72 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
278d9ee2
A
73 if ($this->conf->exists('general.title')) {
74 $this->tpl->assign('pagetitle', $this->conf->get('general.title'));
03eb19ac 75 }
278d9ee2
A
76 if ($this->conf->exists('general.header_link')) {
77 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
03eb19ac 78 }
278d9ee2
A
79 if ($this->conf->exists('pagetitle')) {
80 $this->tpl->assign('pagetitle', $this->conf->get('pagetitle'));
03eb19ac 81 }
278d9ee2
A
82 $this->tpl->assign('shaarlititle', $this->conf->get('title', 'Shaarli'));
83 $this->tpl->assign('openshaarli', $this->conf->get('extras.open_shaarli', false));
84 $this->tpl->assign('showatom', $this->conf->get('extras.show_atom', false));
85 $this->tpl->assign('hide_timestamps', $this->conf->get('extras.hide_timestamps', false));
03eb19ac
A
86 if (!empty($GLOBALS['plugin_errors'])) {
87 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']);
88 }
89 }
90
91 /**
92 * The following assign() method is basically the same as RainTPL (except lazy loading)
93 *
94 * @param string $placeholder Template placeholder.
95 * @param mixed $value Value to assign.
96 */
97 public function assign($placeholder, $value)
98 {
03eb19ac
A
99 if ($this->tpl === false) {
100 $this->initialize();
101 }
102 $this->tpl->assign($placeholder, $value);
103 }
104
105 /**
106 * Assign an array of data to the template builder.
107 *
108 * @param array $data Data to assign.
109 *
110 * @return false if invalid data.
111 */
112 public function assignAll($data)
113 {
03eb19ac
A
114 if ($this->tpl === false) {
115 $this->initialize();
116 }
117
118 if (empty($data) || !is_array($data)){
119 return false;
120 }
121
122 foreach ($data as $key => $value) {
123 $this->assign($key, $value);
124 }
278d9ee2 125 return true;
03eb19ac
A
126 }
127
128 /**
129 * Render a specific page (using a template file).
130 * e.g. $pb->renderPage('picwall');
131 *
132 * @param string $page Template filename (without extension).
133 */
134 public function renderPage($page)
135 {
278d9ee2 136 if ($this->tpl === false) {
03eb19ac
A
137 $this->initialize();
138 }
278d9ee2 139
03eb19ac
A
140 $this->tpl->draw($page);
141 }
142
143 /**
144 * Render a 404 page (uses the template : tpl/404.tpl)
145 * usage : $PAGE->render404('The link was deleted')
146 *
147 * @param string $message A messate to display what is not found
148 */
149 public function render404($message = 'The page you are trying to reach does not exist or has been deleted.')
150 {
151 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
152 $this->tpl->assign('error_message', $message);
153 $this->renderPage('404');
154 }
155}