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