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