HTML/FlexyFramework/Page.php
[pear] / HTML / FlexyFramework / Page.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
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 // +----------------------------------------------------------------------+
18 //
19 // $Id: Page.php,v 1.5 2003/02/22 01:52:50 alan Exp $
20 //
21 // A Base Page Class for use with HTML_Template_Flexy 
22 // You could write one of these which used another template engine.
23 //
24
25
26 require_once 'HTML/Template/Flexy.php' ;
27 require_once 'HTML/Template/Flexy/Factory.php' ;
28
29 /**
30 * The Base Page class - extend and override the methods to implement your own pages.
31 *
32 *
33 */
34
35 class HTML_FlexyFramework_Page  {
36     
37     
38     /**
39     * the main Template name (which can include a body template)
40     *
41     * @var string template name
42     * @access public
43     */
44     var $masterTemplate = "master.html"; 
45     /**
46     * the body Template name
47     *
48     * @var string template name
49     * @access public
50     */
51     var $template = "error.html";
52         
53    
54      
55     
56     /**
57     * cache method - 
58     *   can be 'output', or 'data'
59     * used to set a default caching method
60     *
61     * @var string
62     * @access public
63     * @see getCache()
64     */
65     var $cacheMethod = '';
66     
67      
68     
69     
70    /**
71     * cache store (PEAR's Cache Object Instance
72     *   initialized at start
73     *   set at output stage.
74     *
75     * @var object
76     * @access private
77     * @see getCache()
78     */
79    
80     var $_cache = NULL; 
81     
82     /* ---- Variables set by Page loader -------- */
83     
84    
85         
86     /**
87     * baseURL, that can be prefixed to URL's to ensure that they correctly relate to application
88     * (set by page loader)
89     * @var string
90     * @access public
91     */
92     var $baseURL; 
93     /**
94     * rootURL, the base installation directory - can be used to get images directories.
95     * (set by page loader)
96     * @var string
97     * @access public
98     */
99     var $rootURL; 
100     /**
101     * rootDir, the base installation directory - can be used to find relative files.
102     * (set by page loader)
103     * @var string
104     * @access public
105     */
106     var $rootDir; 
107     /**
108     * the full request string used by the getCacheID().
109     * (set by page loader)
110     * @var string
111     * @access public
112     */
113     var $request; // clean page request for page
114     /**
115     * overrides for elements.
116     *  
117     * @var array
118     * @access public
119     */
120     var $elements = array(); // key=>HTML_Template_Flexy_Element
121    
122      /**
123     * errors for elements
124     *  
125     * @var array
126     * @access public
127     */
128     var $errors = array(); // key(element name)=>error message
129    
130     /**
131     * The default page handler
132     * by default relays to get(), or post() methods depending on the request.
133     *
134     * Override this if you do not handle get or post differently.
135     * 
136     * 
137     * @param   string  request, the remainder of the request not handled by the object.
138     *
139     * @return   none|string none = handled, string = redirect to another page = eg. data/list
140     * @access   public
141     */
142   
143     function start($request,$isRedirect=false,$args=array()) 
144     { 
145         if (!$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
146             return $this->post($request,$args);
147         } else {
148             return $this->get($request,$args);
149         }
150     }
151     /**
152     * The get page handler
153     *
154     * Override this if you want to handle get requests 
155     * 
156     * 
157     * @param   string  request, the remainder of the request not handled by the object.
158     *
159     * @return   none|string none = handled, string = redirect to another page = eg. data/list
160     * @access   public
161     */
162     function get($request) 
163     {
164     }
165      /**
166     * The post page handler
167     *
168     * Override this if you want to handle get requests 
169     * 
170     * 
171     * @param   string  request, the remainder of the request not handled by the object.
172     *
173     * @return   none|string none = handled, string = redirect to another page = eg. data/list
174     * @access   public
175     */
176    function post($request) 
177    {
178    }
179     /**
180     * Authentication Check method
181     * Override this with a straight return for pages that do not require authentication
182     *
183     * By default 
184     *   a) redirects to login if authenticaiton fails
185     *   b) checks to see if a isAdmin method exists on the auth object
186     *       if it does see if the user is admin and let them in.
187     *       otherwise access denied error is raised
188     *   c) lets them in.
189     * 
190     * 
191     *
192     * @return   none or string redirection to another page.
193     * @access   public
194     */
195  
196     function getAuth() {
197          
198         
199         return false;
200         
201     }
202     
203   
204     
205      
206     /**
207     * The master Output layer.
208     * 
209     * compiles the template
210     * if no caching - just runs the template.
211     * otherwise stores it in the cache.
212     * 
213     * you dont normally need to override this.
214     * 
215     * called by the page loader.
216     * @access   public
217     */
218     
219     
220     
221     function output() 
222     {
223         /* output the body if no masterTemplate is set */
224         $options = PEAR::getStaticProperty('HTML_FlexyFramework','options');
225         
226         
227         header('Content-Type: text/html;charset='.( empty($options['charset']) ? 'UTF-8' : $options['charset'] ));
228         
229          
230         if (!$this->masterTemplate) {
231             return $this->outputBody();
232         }
233         /* master template */
234         
235        
236         $template_engine = new HTML_Template_Flexy();
237         $template_engine->compile($this->masterTemplate);
238         if (!$this->_cache || !$this->cacheMethod) {\r
239             $template_engine->outputObject($this,$this->elements);
240             return;
241         }
242         
243         $id = $this->_cache->generateID($this->getID());
244         $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
245         echo $this->_cache->get($id);
246         
247     }
248     /**
249     * The body Output layer.
250     * 
251     * compiles the template
252     * At present there is no caching in here..  - this may change latter..
253     * 
254     * used by putting {outputBody} in the main template.
255     * @access   public
256     */    
257     function outputBody() {
258
259         $template_engine = new HTML_Template_Flexy();
260         $template_engine->compile($this->template);
261         if ($this->elements) { /* BC crap! */
262             $this->elements = HTML_Template_Flexy_Factory::setErrors($this->elements,$this->errors);
263         }
264         $template_engine->elements = $this->elements;
265         $template_engine->outputObject($this,$this->elements);
266         
267     }
268     
269    
270      /**
271     * Do any Page Caching if $this->cacheMethod is set.
272     * You should also look at output caching by overriding the outputBody Method.
273     *
274     * Note that Caching is disabled in a number of situations.
275     *   a) cacheMethod is empty
276     *   b) POST requests
277     *   c) if sess is set (Eg. if you are using sessions)
278     *   d) useCache is not set in the [Cache] section of the config.
279     * 
280     * utilizes $this->getCacheID() to  
281     *
282     * @return   none|boolean|string|int|object    Description
283     * @access   public|private
284     * @see      see also methods.....
285     */
286   
287     
288     function getCache() {
289         if (!$this->cacheMethod) {
290             return;
291         }
292         if ($_SERVER["REQUEST_METHOD"] == "POST") {
293             return;
294         } 
295          
296         /* lets assume we can cache ourselves.. */
297         $coptions = PEAR::getStaticProperty('Cache','options');
298         if (!$coptions) {
299             return;
300         }
301         if (empty($coptions['useCache'])) {
302             return;
303         }
304         require_once 'Cache.php';
305         
306         $this->_cache = new Cache($coptions['container'], $coptions);
307         $id = $this->_cache->generateID($this->getCacheID());
308         if ($_SERVER["REQUEST_METHOD"] == "POST") {
309             $this->_cache->flush($id);
310             return;
311         }
312         if ($data = $this->_cache->get($id)) {
313             echo $data;
314             return TRUE;
315         }
316     
317     }
318     
319     /**
320     * Get a distinct Page Cache ID. 
321     *
322     * By default this is the full request string
323     * override this to define a more precise string 
324     *
325     * @return   string   distinct page id (eg. the request url)
326     * @access   private
327     */
328   
329     function getCacheID() {
330         return  $this->request;
331         
332     }
333     
334     
335     /**
336     * Utility method : get the Class name (used on templates)
337     *
338     * @return   string   class name
339     * @access   public
340     */
341     
342     
343     function getClass() {
344         return get_class($this);
345     }
346     /**
347     * Utility method : get the Time taken to generate the page.
348     *
349     * @return   string   time in seconds..
350     * @access   public
351     */
352     
353     function getTime() {
354          
355         $m = explode(' ',microtime());
356         return sprintf('%0.2fs',($m[0] + $m[1]) -  HTML_FlexyFramework::get()->start)
357                 . '/ Files ' . count(get_included_files());
358     
359     
360     }
361     
362 }
363
364