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