]>
Commit | Line | Data |
---|---|---|
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('path.update_check'), | |
38 | $conf->get('general.check_updates_interval'), | |
39 | $conf->get('general.check_updates'), | |
40 | isLoggedIn(), | |
41 | $conf->get('general.check_updates_branch') | |
42 | ); | |
43 | $this->tpl->assign('newVersion', escape($version)); | |
44 | $this->tpl->assign('versionError', ''); | |
45 | ||
46 | } catch (Exception $exc) { | |
47 | logm($conf->get('path.log'), $_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('general.title')) { | |
67 | $this->tpl->assign('pagetitle', $conf->get('general.title')); | |
68 | } | |
69 | if ($conf->exists('general.header_link')) { | |
70 | $this->tpl->assign('titleLink', $conf->get('general.header_link')); | |
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('extras.open_shaarli', false)); | |
77 | $this->tpl->assign('showatom', $conf->get('extras.show_atom', false)); | |
78 | $this->tpl->assign('hide_timestamps', $conf->get('extras.hide_timestamps', false)); | |
79 | if (!empty($GLOBALS['plugin_errors'])) { | |
80 | $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']); | |
81 | } | |
82 | } | |
83 | ||
84 | /** | |
85 | * The following assign() method is basically the same as RainTPL (except lazy loading) | |
86 | * | |
87 | * @param string $placeholder Template placeholder. | |
88 | * @param mixed $value Value to assign. | |
89 | */ | |
90 | public function assign($placeholder, $value) | |
91 | { | |
92 | // Lazy initialization | |
93 | if ($this->tpl === false) { | |
94 | $this->initialize(); | |
95 | } | |
96 | $this->tpl->assign($placeholder, $value); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Assign an array of data to the template builder. | |
101 | * | |
102 | * @param array $data Data to assign. | |
103 | * | |
104 | * @return false if invalid data. | |
105 | */ | |
106 | public function assignAll($data) | |
107 | { | |
108 | // Lazy initialization | |
109 | if ($this->tpl === false) { | |
110 | $this->initialize(); | |
111 | } | |
112 | ||
113 | if (empty($data) || !is_array($data)){ | |
114 | return false; | |
115 | } | |
116 | ||
117 | foreach ($data as $key => $value) { | |
118 | $this->assign($key, $value); | |
119 | } | |
120 | } | |
121 | ||
122 | /** | |
123 | * Render a specific page (using a template file). | |
124 | * e.g. $pb->renderPage('picwall'); | |
125 | * | |
126 | * @param string $page Template filename (without extension). | |
127 | */ | |
128 | public function renderPage($page) | |
129 | { | |
130 | // Lazy initialization | |
131 | if ($this->tpl===false) { | |
132 | $this->initialize(); | |
133 | } | |
134 | $this->tpl->draw($page); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Render a 404 page (uses the template : tpl/404.tpl) | |
139 | * usage : $PAGE->render404('The link was deleted') | |
140 | * | |
141 | * @param string $message A messate to display what is not found | |
142 | */ | |
143 | public function render404($message = 'The page you are trying to reach does not exist or has been deleted.') | |
144 | { | |
145 | header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); | |
146 | $this->tpl->assign('error_message', $message); | |
147 | $this->renderPage('404'); | |
148 | } | |
149 | } |