3 use Shaarli\Config\ConfigManager
;
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');
15 * @var RainTPL RainTPL instance.
20 * @var ConfigManager $conf Configuration Manager instance.
25 * @var LinkDB $linkDB instance.
30 * PageBuilder constructor.
31 * $tpl is initialized at false for lazy loading.
33 * @param ConfigManager $conf Configuration Manager instance (reference).
34 * @param LinkDB $linkDB instance.
35 * @param string $token Session token
37 public function __construct(&$conf, $linkDB = null, $token = null)
41 $this->linkDB
= $linkDB;
42 $this->token
= $token;
46 * Initialize all default tpl tags.
48 private function initialize()
50 $this->tpl
= new RainTPL();
53 $version = ApplicationUtils
::checkUpdate(
55 $this->conf
->get('resource.update_check'),
56 $this->conf
->get('updates.check_updates_interval'),
57 $this->conf
->get('updates.check_updates'),
59 $this->conf
->get('updates.check_updates_branch')
61 $this->tpl
->assign('newVersion', escape($version));
62 $this->tpl
->assign('versionError', '');
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()));
70 $this->tpl
->assign('feedurl', escape(index_url($_SERVER)));
71 $searchcrits = ''; // Search criteria
72 if (!empty($_GET['searchtags'])) {
73 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
75 if (!empty($_GET['searchterm'])) {
76 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
78 $this->tpl
->assign('searchcrits', $searchcrits);
79 $this->tpl
->assign('source', index_url($_SERVER));
80 $this->tpl
->assign('version', SHAARLI_VERSION
);
83 ApplicationUtils
::getVersionHash(SHAARLI_VERSION
, $this->conf
->get('credentials.salt'))
85 $this->tpl
->assign('scripturl', index_url($_SERVER));
86 $this->tpl
->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
87 $this->tpl
->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
88 $this->tpl
->assign('pagetitle', $this->conf
->get('general.title', 'Shaarli'));
89 if ($this->conf
->exists('general.header_link')) {
90 $this->tpl
->assign('titleLink', $this->conf
->get('general.header_link'));
92 $this->tpl
->assign('shaarlititle', $this->conf
->get('general.title', 'Shaarli'));
93 $this->tpl
->assign('openshaarli', $this->conf
->get('security.open_shaarli', false));
94 $this->tpl
->assign('showatom', $this->conf
->get('feed.show_atom', true));
95 $this->tpl
->assign('feed_type', $this->conf
->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
96 $this->tpl
->assign('hide_timestamps', $this->conf
->get('privacy.hide_timestamps', false));
97 $this->tpl
->assign('token', $this->token
);
99 if ($this->linkDB
!== null) {
100 $this->tpl
->assign('tags', $this->linkDB
->linksCountPerTag());
102 // To be removed with a proper theme configuration.
103 $this->tpl
->assign('conf', $this->conf
);
107 * The following assign() method is basically the same as RainTPL (except lazy loading)
109 * @param string $placeholder Template placeholder.
110 * @param mixed $value Value to assign.
112 public function assign($placeholder, $value)
114 if ($this->tpl
=== false) {
117 $this->tpl
->assign($placeholder, $value);
121 * Assign an array of data to the template builder.
123 * @param array $data Data to assign.
125 * @return false if invalid data.
127 public function assignAll($data)
129 if ($this->tpl
=== false) {
133 if (empty($data) || !is_array($data)){
137 foreach ($data as $key => $value) {
138 $this->assign($key, $value);
144 * Render a specific page (using a template file).
145 * e.g. $pb->renderPage('picwall');
147 * @param string $page Template filename (without extension).
149 public function renderPage($page)
151 if ($this->tpl
=== false) {
155 $this->tpl
->draw($page);
159 * Render a 404 page (uses the template : tpl/404.tpl)
160 * usage : $PAGE->render404('The link was deleted')
162 * @param string $message A messate to display what is not found
164 public function render404($message = '')
166 if (empty($message)) {
167 $message = t('The page you are trying to reach does not exist or has been deleted.');
169 header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found'));
170 $this->tpl
->assign('error_message', $message);
171 $this->renderPage('404');