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 * rootDir, the base installation directory - can be used to find relative files.
102 * (set by page loader)
108 * the full request string used by the getCacheID().
109 * (set by page loader)
113 var $request; // clean page request for page
115 * overrides for elements.
120 var $elements = array(); // key=>HTML_Template_Flexy_Element
123 * errors for elements
128 var $errors = array(); // key(element name)=>error message
131 * The default page handler
132 * by default relays to get(), or post() methods depending on the request.
134 * Override this if you do not handle get or post differently.
137 * @param string request, the remainder of the request not handled by the object.
139 * @return none|string none = handled, string = redirect to another page = eg. data/list
143 function start($request,$isRedirect=false,$args=array())
145 $cli= HTML_Flexyframework::get()->cli;
146 if (!$cli && !$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
147 return $this->post($request,$args);
149 return $this->get($request,$args,$isRedirect);
153 * The get page handler
155 * Override this if you want to handle get requests
158 * @param string request, the remainder of the request not handled by the object.
160 * @return none|string none = handled, string = redirect to another page = eg. data/list
163 function get($request)
167 * The post page handler
169 * Override this if you want to handle get requests
172 * @param string request, the remainder of the request not handled by the object.
174 * @return none|string none = handled, string = redirect to another page = eg. data/list
177 function post($request)
181 * Authentication Check method
182 * Override this with a straight return for pages that do not require authentication
185 * a) redirects to login if authenticaiton fails
186 * b) checks to see if a isAdmin method exists on the auth object
187 * if it does see if the user is admin and let them in.
188 * otherwise access denied error is raised
193 * @return none or string redirection to another page.
208 * The master Output layer.
210 * compiles the template
211 * if no caching - just runs the template.
212 * otherwise stores it in the cache.
214 * you dont normally need to override this.
216 * called by the page loader.
224 /* output the body if no masterTemplate is set */
225 $options = PEAR::getStaticProperty('HTML_FlexyFramework','options');
227 $type = isset($this->contentType) ? $this->contentType : 'text/html';
228 header('Content-Type: '.$type.';charset='.( empty($options['charset']) ? 'UTF-8' : $options['charset'] ));
231 if (!$this->masterTemplate) {
232 return $this->outputBody();
234 /* master template */
237 $template_engine = new HTML_Template_Flexy();
238 $template_engine->compile($this->masterTemplate);
239 if (!$this->_cache || !$this->cacheMethod) {
\r
240 $template_engine->outputObject($this,$this->elements);
244 $id = $this->_cache->generateID($this->getID());
245 $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
246 echo $this->_cache->get($id);
250 * The body Output layer.
252 * compiles the template
253 * At present there is no caching in here.. - this may change latter..
255 * used by putting {outputBody} in the main template.
258 function outputBody() {
260 $template_engine = new HTML_Template_Flexy();
261 $template_engine->compile($this->template);
262 if ($this->elements) { /* BC crap! */
263 $this->elements = HTML_Template_Flexy_Factory::setErrors($this->elements,$this->errors);
265 $template_engine->elements = $this->elements;
266 $template_engine->outputObject($this,$this->elements);
272 * Do any Page Caching if $this->cacheMethod is set.
273 * You should also look at output caching by overriding the outputBody Method.
275 * Note that Caching is disabled in a number of situations.
276 * a) cacheMethod is empty
278 * c) if sess is set (Eg. if you are using sessions)
279 * d) useCache is not set in the [Cache] section of the config.
281 * utilizes $this->getCacheID() to
283 * @return none|boolean|string|int|object Description
284 * @access public|private
285 * @see see also methods.....
289 function getCache() {
290 if (!$this->cacheMethod) {
293 if ($_SERVER["REQUEST_METHOD"] == "POST") {
297 /* lets assume we can cache ourselves.. */
298 $coptions = PEAR::getStaticProperty('Cache','options');
302 if (empty($coptions['useCache'])) {
305 require_once 'Cache.php';
307 $this->_cache = new Cache($coptions['container'], $coptions);
308 $id = $this->_cache->generateID($this->getCacheID());
309 if ($_SERVER["REQUEST_METHOD"] == "POST") {
310 $this->_cache->flush($id);
313 if ($data = $this->_cache->get($id)) {
321 * Get a distinct Page Cache ID.
323 * By default this is the full request string
324 * override this to define a more precise string
326 * @return string distinct page id (eg. the request url)
330 function getCacheID() {
331 return $this->request;
337 * Utility method : get the Class name (used on templates)
339 * @return string class name
344 function getClass() {
345 return get_class($this);
348 * Utility method : get the Time taken to generate the page.
350 * @return string time in seconds..
356 $m = explode(' ',microtime());
357 $ff = HTML_FlexyFramework::get();
358 return sprintf('%0.2fs',($m[0] + $m[1]) - $ff->start)
359 . '/ Files ' . count(get_included_files());