]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/PageBuilder.php
Merge pull request #560 from ArthurHoaro/nb-private-shaare
[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
17 /**
18 * PageBuilder constructor.
19 * $tpl is initialized at false for lazy loading.
20 */
21 function __construct()
22 {
23 $this->tpl = false;
24 }
25
26 /**
27 * Initialize all default tpl tags.
28 */
29 private function initialize()
30 {
31 $this->tpl = new RainTPL();
32
33 try {
34 $version = ApplicationUtils::checkUpdate(
35 shaarli_version,
36 $GLOBALS['config']['UPDATECHECK_FILENAME'],
37 $GLOBALS['config']['UPDATECHECK_INTERVAL'],
38 $GLOBALS['config']['ENABLE_UPDATECHECK'],
39 isLoggedIn(),
40 $GLOBALS['config']['UPDATECHECK_BRANCH']
41 );
42 $this->tpl->assign('newVersion', escape($version));
43 $this->tpl->assign('versionError', '');
44
45 } catch (Exception $exc) {
46 logm($GLOBALS['config']['LOG_FILE'], $_SERVER['REMOTE_ADDR'], $exc->getMessage());
47 $this->tpl->assign('newVersion', '');
48 $this->tpl->assign('versionError', escape($exc->getMessage()));
49 }
50
51 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
52 $searchcrits = ''; // Search criteria
53 if (!empty($_GET['searchtags'])) {
54 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
55 }
56 if (!empty($_GET['searchterm'])) {
57 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
58 }
59 $this->tpl->assign('searchcrits', $searchcrits);
60 $this->tpl->assign('source', index_url($_SERVER));
61 $this->tpl->assign('version', shaarli_version);
62 $this->tpl->assign('scripturl', index_url($_SERVER));
63 $this->tpl->assign('pagetitle', 'Shaarli');
64 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
65 if (!empty($GLOBALS['title'])) {
66 $this->tpl->assign('pagetitle', $GLOBALS['title']);
67 }
68 if (!empty($GLOBALS['titleLink'])) {
69 $this->tpl->assign('titleLink', $GLOBALS['titleLink']);
70 }
71 if (!empty($GLOBALS['pagetitle'])) {
72 $this->tpl->assign('pagetitle', $GLOBALS['pagetitle']);
73 }
74 $this->tpl->assign('shaarlititle', empty($GLOBALS['title']) ? 'Shaarli': $GLOBALS['title']);
75 if (!empty($GLOBALS['plugin_errors'])) {
76 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']);
77 }
78 }
79
80 /**
81 * The following assign() method is basically the same as RainTPL (except lazy loading)
82 *
83 * @param string $placeholder Template placeholder.
84 * @param mixed $value Value to assign.
85 */
86 public function assign($placeholder, $value)
87 {
88 // Lazy initialization
89 if ($this->tpl === false) {
90 $this->initialize();
91 }
92 $this->tpl->assign($placeholder, $value);
93 }
94
95 /**
96 * Assign an array of data to the template builder.
97 *
98 * @param array $data Data to assign.
99 *
100 * @return false if invalid data.
101 */
102 public function assignAll($data)
103 {
104 // Lazy initialization
105 if ($this->tpl === false) {
106 $this->initialize();
107 }
108
109 if (empty($data) || !is_array($data)){
110 return false;
111 }
112
113 foreach ($data as $key => $value) {
114 $this->assign($key, $value);
115 }
116 }
117
118 /**
119 * Render a specific page (using a template file).
120 * e.g. $pb->renderPage('picwall');
121 *
122 * @param string $page Template filename (without extension).
123 */
124 public function renderPage($page)
125 {
126 // Lazy initialization
127 if ($this->tpl===false) {
128 $this->initialize();
129 }
130 $this->tpl->draw($page);
131 }
132
133 /**
134 * Render a 404 page (uses the template : tpl/404.tpl)
135 * usage : $PAGE->render404('The link was deleted')
136 *
137 * @param string $message A messate to display what is not found
138 */
139 public function render404($message = 'The page you are trying to reach does not exist or has been deleted.')
140 {
141 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
142 $this->tpl->assign('error_message', $message);
143 $this->renderPage('404');
144 }
145}