Update version.php to 3.3.0
[isso.git] / Template.php
index 1d90c7246a26d857ee61e73585ae2ec3e816fae5..dfe30c12ae088949b8201e41d30c9c52b1d0d790 100644 (file)
@@ -2,7 +2,7 @@
 /*=====================================================================*\
 || ###################################################################
 || # Blue Static ISSO Framework
-|| # Copyright (c)2005-2008 Blue Static
+|| # Copyright (c)2005-2009 Blue Static
 || #
 || # This program is free software; you can redistribute it and/or modify
 || # it under the terms of the GNU General Public License as published by
@@ -39,7 +39,7 @@ require_once(ISSO . '/Functions.php');
  * CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
  *
  * @author             Blue Static
- * @copyright  Copyright (c)2005 - 2008, Blue Static
+ * @copyright  Copyright (c)2005 - 2009, Blue Static
  * @package            ISSO
  * 
  */
@@ -94,10 +94,16 @@ class BSTemplate
        public static $globalVars = array();
        
        /**
-        * The file name of the template
+        * The path of the template file
         * @var string
         */
-       protected $filename;
+       protected $path;
+       
+       /**
+        * The name of the template
+        * @var string
+        */
+       protected $name;
        
        /**
         * Template contents
@@ -118,6 +124,7 @@ class BSTemplate
                        return; // there's no point in pre-caching file templates
                }
                
+               $namearray = array_map(array('self', '_path'), $namearray);
                $cache = BSApp::$db->query("SELECT * FROM " . self::$dbCacheTable . " WHERE filename IN ('" . implode("', '", $namearray) . "')");
                while ($tpl = $cache->fetchArray())
                {
@@ -140,37 +147,48 @@ class BSTemplate
         * 
         * @param       string  File name
         */
-       public function __construct($path)
+       public function __construct($name)
        {
-               $this->file = $path;
+               $this->name = $name;
+               $this->path = self::_path($name);
                
                // checks to see if the template has been cached
-               if (isset(self::$cache[$this->file]))
+               if (isset(self::$cache[$this->path]))
                {
-                       if (!self::$dbCacheTable || filemtime(sprintf(self::$templatePath, $this->file)) <= self::$cache[$this->file]['timestamp'])
+                       if (!self::$dbCacheTable || filemtime($this->path) <= self::$cache[$this->path]['timestamp'])
                        {
-                               $this->template = self::$cache[$this->file]['template'];
+                               $this->template = self::$cache[$this->path]['template'];
                                return;
                        }
                }
                
                // it hasn't been cached
-               $path = sprintf(self::$templatePath, $this->file);
-               if (!is_file($path) || !is_readable($path))
+               if (!is_file($this->path) || !is_readable($this->path))
                {
-                       throw new Exception("Could not load the template $path");
+                       throw new Exception("Could not load the template {$this->path}");
                }
-               $this->template = $this->_parseTemplate(file_get_contents($path));
-               self::$cache[$this->file]['template'] = $this->template;
+               $this->template = $this->_parseTemplate(file_get_contents($this->path));
+               self::$cache[$this->path]['template'] = $this->template;
 
                // store the template in the database
                if (self::$dbCacheTable)
                {
-                       BSApp::$db->query("REPLACE INTO " . self::$dbCacheTable . " SET template = '" . BSApp::$db->escapeString($this->template) . "', timestamp = " . TIMENOW . ", filename = '" . $this->file . "'");
-                       self::$cache[$this->file]['time'] = TIMENOW;
+                       BSApp::$db->query("REPLACE INTO " . self::$dbCacheTable . " SET template = '" . BSApp::$db->escapeString($this->template) . "', timestamp = " . TIMENOW . ", filename = '" . $this->path . "'");
+                       self::$cache[$this->path]['time'] = TIMENOW;
                }
        }
        
+       /**
+        * Evaluates and returns the template. This is equivalent to calling:
+        * $tpl->evaluate()->getTemplate()
+        *
+        * @return      string
+        */
+       public function __toString()
+       {
+               return $this->evaluate()->getTemplate();
+       }
+       
        /**
         * Returns the template data
         *
@@ -221,34 +239,41 @@ class BSTemplate
                        throw new Exception('There is no output to print');
                }
                
-               $template = $this->template;
+               echo $this->template;
+       }
+       
+       /**
+        * Returns the debug block
+        *
+        * @return      string
+        */
+       public static function get_debug_block()
+       {
+               if (!BSApp::get_debug())
+               {
+                       return;
+               }
                
-               $debugBlock = '';
-               if (BSApp::get_debug() && strpos($template, '</body>') !== false)
+               $debugBlock = "\n<div align=\"center\">Executed in " . round(BSFunctions::fetch_microtime_diff('0 ' . $_SERVER['REQUEST_TIME']), 10) . ' seconds</div>';
+               $debugBlock .= "\n<br /><div align=\"center\">" . BSApp::get_debug_list() . "</div>";
+                                       
+               if (BSApp::$db)
                {
-                       $debugBlock .= "\n<div align=\"center\">Executed in " . round(BSFunctions::fetch_microtime_diff('0 ' . $_SERVER['REQUEST_TIME']), 10) . ' seconds</div>';
-                       $debugBlock .= "\n<br /><div align=\"center\">" . BSApp::get_debug_list() . "</div>";
-                                               
-                       if (BSApp::$db)
+                       $queries = BSApp::$db->getHistory();
+                       
+                       $debugBlock .= "<br />\n" . '<table cellpadding="4" cellspacing="1" border="0" align="center" width="30%" style="background-color: rgb(60, 60, 60); color: white">' . "\n\t" . '<tr><td><strong>Query Debug</strong></td></tr>';
+                       
+                       foreach ($queries as $query)
                        {
-                               $queries = BSApp::$db->getHistory();
-                               
-                               $debugBlock .= "<br />\n" . '<table cellpadding="4" cellspacing="1" border="0" align="center" width="30%" style="background-color: rgb(60, 60, 60); color: white">' . "\n\t" . '<tr><td><strong>Query Debug</strong></td></tr>';
-                               
-                               foreach ($queries as $query)
-                               {
-                                       $debugBlock .= "\n\t<tr style=\"background-color: rgb(230, 230, 230); color: black\">";
-                                       $debugBlock .= "\n\t\t<td>";
-                                       $debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t<div style=\"font-size: 9px;\">($query[time])</div>\n<!--\n$query[trace]\n-->\n\t\t</td>\n\t</tr>";
-                               }
-                               
-                               $debugBlock .= "\n</table>\n\n\n";
+                               $debugBlock .= "\n\t<tr style=\"background-color: rgb(230, 230, 230); color: black\">";
+                               $debugBlock .= "\n\t\t<td>";
+                               $debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t<div style=\"font-size: 9px;\">($query[time])</div>\n<!--\n$query[trace]\n-->\n\t\t</td>\n\t</tr>";
                        }
                        
-                       $template = str_replace('</body>', $debugBlock . '</body>', $template);
+                       $debugBlock .= "\n</table>\n\n\n";
                }
-
-               print($template);
+               
+               return $debugBlock;
        }
        
        /**
@@ -314,6 +339,18 @@ class BSTemplate
                
                return $template;
        }
+       
+       /**
+        * Returns the full path of a template given a name
+        *
+        * @param       string  Template name
+        *
+        * @return      string  Template path
+        */
+       protected static function _path($name)
+       {
+               return sprintf(self::$templatePath, $name);
+       }
 }
 
 ?>
\ No newline at end of file