2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Alan Knowles <alan@akbkhome.com> |
17 // +----------------------------------------------------------------------+
19 // $Id: Page.php,v 1.5 2003/02/22 01:52:50 alan Exp $
21 // A Base Page Class for use with HTML_Template_Flexy
22 // You could write one of these which used another template engine.
26 require_once 'HTML/Template/Flexy.php' ;
27 require_once 'HTML/Template/Flexy/Factory.php' ;
30 * The Base Page class - extend and override the methods to implement your own pages.
35 class HTML_FlexyFramework_Page {
39 * the main Template name (which can include a body template)
41 * @var string template name
44 var $masterTemplate = "master.html";
46 * the body Template name
48 * @var string template name
51 var $template = "error.html";
58 * can be 'output', or 'data'
59 * used to set a default caching method
65 var $cacheMethod = '';
71 * cache store (PEAR's Cache Object Instance
72 * initialized at start
73 * set at output stage.
82 /* ---- Variables set by Page loader -------- */
87 * baseURL, that can be prefixed to URL's to ensure that they correctly relate to application
88 * (set by page loader)
94 * rootURL, the base installation directory - can be used to get images directories.
95 * (set by page loader)
101 * the full request string used by the getCacheID().
102 * (set by page loader)
106 var $request; // clean page request for page
108 * overrides for elements.
113 var $elements = array(); // key=>HTML_Template_Flexy_Element
116 * errors for elements
121 var $errors = array(); // key(element name)=>error message
124 * The default page handler
125 * by default relays to get(), or post() methods depending on the request.
127 * Override this if you do not handle get or post differently.
130 * @param string request, the remainder of the request not handled by the object.
132 * @return none|string none = handled, string = redirect to another page = eg. data/list
136 function start($request,$isRedirect=false,$args=array())
138 if (!$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
139 return $this->post($request,$args);
141 return $this->get($request,$args);
145 * The get page handler
147 * Override this if you want to handle get requests
150 * @param string request, the remainder of the request not handled by the object.
152 * @return none|string none = handled, string = redirect to another page = eg. data/list
155 function get($request)
159 * The post page handler
161 * Override this if you want to handle get requests
164 * @param string request, the remainder of the request not handled by the object.
166 * @return none|string none = handled, string = redirect to another page = eg. data/list
169 function post($request)
173 * Authentication Check method
174 * Override this with a straight return for pages that do not require authentication
177 * a) redirects to login if authenticaiton fails
178 * b) checks to see if a isAdmin method exists on the auth object
179 * if it does see if the user is admin and let them in.
180 * otherwise access denied error is raised
185 * @return none or string redirection to another page.
200 * The master Output layer.
202 * compiles the template
203 * if no caching - just runs the template.
204 * otherwise stores it in the cache.
206 * you dont normally need to override this.
208 * called by the page loader.
216 /* output the body if no masterTemplate is set */
217 $options = PEAR::getStaticProperty('HTML_FlexyFramework','options');
220 header('Content-Type: text/html;charset='.( empty($options['charset']) ? 'UTF-8' : $options['charset'] ));
223 if (!$this->masterTemplate) {
224 return $this->outputBody();
226 /* master template */
229 $template_engine = new HTML_Template_Flexy();
230 $template_engine->compile($this->masterTemplate);
231 if (!$this->_cache || !$this->cacheMethod) {
\r
232 $template_engine->outputObject($this,$this->elements);
236 $id = $this->_cache->generateID($this->getID());
237 $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
238 echo $this->_cache->get($id);
242 * The body Output layer.
244 * compiles the template
245 * At present there is no caching in here.. - this may change latter..
247 * used by putting {outputBody} in the main template.
250 function outputBody() {
252 $template_engine = new HTML_Template_Flexy();
253 $template_engine->compile($this->template);
254 if ($this->elements) { /* BC crap! */
255 $this->elements = HTML_Template_Flexy_Factory::setErrors($this->elements,$this->errors);
257 $template_engine->elements = $this->elements;
258 $template_engine->outputObject($this,$this->elements);
264 * Do any Page Caching if $this->cacheMethod is set.
265 * You should also look at output caching by overriding the outputBody Method.
267 * Note that Caching is disabled in a number of situations.
268 * a) cacheMethod is empty
270 * c) if sess is set (Eg. if you are using sessions)
271 * d) useCache is not set in the [Cache] section of the config.
273 * utilizes $this->getCacheID() to
275 * @return none|boolean|string|int|object Description
276 * @access public|private
277 * @see see also methods.....
281 function getCache() {
282 if (!$this->cacheMethod) {
285 if ($_SERVER["REQUEST_METHOD"] == "POST") {
289 /* lets assume we can cache ourselves.. */
290 $coptions = PEAR::getStaticProperty('Cache','options');
294 if (empty($coptions['useCache'])) {
297 require_once 'Cache.php';
299 $this->_cache = new Cache($coptions['container'], $coptions);
300 $id = $this->_cache->generateID($this->getCacheID());
301 if ($_SERVER["REQUEST_METHOD"] == "POST") {
302 $this->_cache->flush($id);
305 if ($data = $this->_cache->get($id)) {
313 * Get a distinct Page Cache ID.
315 * By default this is the full request string
316 * override this to define a more precise string
318 * @return string distinct page id (eg. the request url)
322 function getCacheID() {
323 return $this->request;
329 * Utility method : get the Class name (used on templates)
331 * @return string class name
336 function getClass() {
337 return get_class($this);
340 * Utility method : get the Time taken to generate the page.
342 * @return string time in seconds..
348 $m = explode(' ',microtime());
349 return sprintf('%0.2fs',($m[0] + $m[1]) - HTML_FlexyFramework::get()->start)
350 . '/ Files ' . count(get_included_files());