]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/PageBuilder.php
c86621a254d4b3f262811e6d2a2588cac0ad7c2b
[github/shaarli/Shaarli.git] / application / PageBuilder.php
1 <?php
2
3 use Shaarli\Config\ConfigManager;
4
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
19 /**
20 * @var ConfigManager $conf Configuration Manager instance.
21 */
22 protected $conf;
23
24 /**
25 * @var LinkDB $linkDB instance.
26 */
27 protected $linkDB;
28
29 /**
30 * PageBuilder constructor.
31 * $tpl is initialized at false for lazy loading.
32 *
33 * @param ConfigManager $conf Configuration Manager instance (reference).
34 * @param LinkDB $linkDB instance.
35 */
36 public function __construct(&$conf, $linkDB = null)
37 {
38 $this->tpl = false;
39 $this->conf = $conf;
40 $this->linkDB = $linkDB;
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(
52 shaarli_version,
53 $this->conf->get('resource.update_check'),
54 $this->conf->get('updates.check_updates_interval'),
55 $this->conf->get('updates.check_updates'),
56 isLoggedIn(),
57 $this->conf->get('updates.check_updates_branch')
58 );
59 $this->tpl->assign('newVersion', escape($version));
60 $this->tpl->assign('versionError', '');
61
62 } catch (Exception $exc) {
63 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
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));
78 $this->tpl->assign('version', shaarli_version);
79 $this->tpl->assign('scripturl', index_url($_SERVER));
80 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
81 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
82 if ($this->conf->exists('general.header_link')) {
83 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
84 }
85 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
86 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
87 $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
88 $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
89 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
90 $this->tpl->assign('token', getToken($this->conf));
91 if ($this->linkDB !== null) {
92 $this->tpl->assign('tags', $this->linkDB->linksCountPerTag());
93 }
94 // To be removed with a proper theme configuration.
95 $this->tpl->assign('conf', $this->conf);
96 }
97
98 /**
99 * The following assign() method is basically the same as RainTPL (except lazy loading)
100 *
101 * @param string $placeholder Template placeholder.
102 * @param mixed $value Value to assign.
103 */
104 public function assign($placeholder, $value)
105 {
106 if ($this->tpl === false) {
107 $this->initialize();
108 }
109 $this->tpl->assign($placeholder, $value);
110 }
111
112 /**
113 * Assign an array of data to the template builder.
114 *
115 * @param array $data Data to assign.
116 *
117 * @return false if invalid data.
118 */
119 public function assignAll($data)
120 {
121 if ($this->tpl === false) {
122 $this->initialize();
123 }
124
125 if (empty($data) || !is_array($data)){
126 return false;
127 }
128
129 foreach ($data as $key => $value) {
130 $this->assign($key, $value);
131 }
132 return true;
133 }
134
135 /**
136 * Render a specific page (using a template file).
137 * e.g. $pb->renderPage('picwall');
138 *
139 * @param string $page Template filename (without extension).
140 */
141 public function renderPage($page)
142 {
143 if ($this->tpl === false) {
144 $this->initialize();
145 }
146
147 $this->tpl->draw($page);
148 }
149
150 /**
151 * Render a 404 page (uses the template : tpl/404.tpl)
152 * usage : $PAGE->render404('The link was deleted')
153 *
154 * @param string $message A messate to display what is not found
155 */
156 public function render404($message = 'The page you are trying to reach does not exist or has been deleted.')
157 {
158 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
159 $this->tpl->assign('error_message', $message);
160 $this->renderPage('404');
161 }
162 }