Some variables are needed by all templates to make creation easier. Make sure they...
[squirrelmail.git] / class / template / template.class.php
index 5fb9d168671850b81521922b8a1e20585e5ac47d..238d0d1379ae8f05881985de5f44c0170f26d564 100755 (executable)
@@ -1,20 +1,18 @@
 <?php
+
 /**
  * Copyright 2003, Paul James
- * Copyright (c) 2005 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This file contains some methods from the Smarty templating engine version
  * 2.5.0 by Monte Ohrt <monte@ispi.net> and Andrei Zmievski <andrei@php.net>.
  *
- * @version $Id$
- */
-
-/**
  * The SquirrelMail (Foowd) template implementation.
  * Derived from the foowd template implementation and adapted
  * for squirrelmail
- * @package SquirrelMail
+ * @copyright &copy; 2005-2006 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @version $Id$
+ * @package squirrelmail
  */
 
 /**
@@ -24,9 +22,9 @@
  * This class uses a similar API to Smarty.
  *
  * @author Paul James
- * @author Monte Ohrt <monte@ispi.net>
- * @author Andrei Zmievski <andrei@php.net>
- * @package SquirrelMail
+ * @author Monte Ohrt <monte at ispi.net>
+ * @author Andrei Zmievski <andrei at php.net>
+ * @package squirrelmail
  */
 class Template
 {
@@ -42,15 +40,57 @@ class Template
    *
    * @var string
    */
-  var $template_dir = 'templates\default';
+  var $template_dir = '';
+
+  /**
+   * Template files provided by this template set
+   *
+   * @var array
+   */
+  var $templates_provided = array();
 
   /**
-   * Constructor
+   * Javascript files required by the template
    *
-   * @param string $sTplDir where the template set is located
+   * @var array
+   */
+  var $required_js_files = array();
+
+  /**
+   * Javascript files provided by the template.  If a JS file is required, but
+   * not provided, the js file by the same name will be included from the
+   * default template directory.
+   *
+   * @var array
    */
-  function Template($sTplDir = 'templates\default') {
-       $this->template_dir = $sTplDir;
+  var $provided_js_files = array();
+
+  /**
+   * Additional stylesheets provided by the template.  This allows template
+   * authors (namely me to begin with :p) to provide additional CSS sheets
+   * to templates while using the default template set stylesheet for other
+   * definitions.
+   */
+  var $additional_css_sheets = array();
+
+    /**
+     * Constructor
+     *
+     * @param string $sTplDir where the template set is located
+     */
+    function Template($sTplDir) {
+        $this->template_dir = $sTplDir;
+
+        // Pull in the tempalte config file
+        if (!file_exists($this->template_dir . 'template.php')) {
+             trigger_error('No template.php could be found in the requested template directory ("'.$this->template_dir.'")', E_USER_ERROR);
+        } else {
+            include ($this->template_dir . 'template.php');
+            $this->templates_provided = is_array($templates_provided) ? $templates_provided : array();
+            $this->required_js_files = is_array($required_js_files) ? $required_js_files : array();
+            $this->provided_js_files = is_array($provided_js_files) ? $provided_js_files: array();
+            $this->additional_css_sheets = is_array($additional_css_sheets) ? $additional_css_sheets : array();
+        }
   }
 
 
@@ -163,10 +203,28 @@ class Template
    */
   function display($file)
   {
+    /**
+     * We want to make sure that certain variables are always passed to the
+     * templates b/c they are critical to certain template functions.
+     */
+    $always_include = array('icon_theme_path', 'sTplDir');
+    foreach ($always_include as $var) {
+        if (!isset($this->values[$var]) && isset($GLOBALS[$var])) {
+            $this->assign($var, $GLOBALS[$var]);
+        }
+    }
+    
+    // Pull in our config file
     $t = &$this->values; // place values array directly in scope
-    ob_start();
-    include($this->template_dir.$file);
-    ob_end_flush();
+
+    $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
+    if (!file_exists($template)) {
+        trigger_error('The template "'.htmlspecialchars($file).'" could not be displayed!', E_USER_ERROR);
+    } else {
+        ob_start();
+        include($template);
+        ob_end_flush();
+    }
   }
 
   /**
@@ -175,16 +233,53 @@ class Template
    * @param string $file The template file to use
    * @return string A string of the results
    */
-  function fetch($file)
-  {
-    ob_start();
+  function fetch($file) {
     $t = &$this->values; // place values array directly in scope
-    include($this->template_dir.$file);
-    $contents = ob_get_contents();
-    ob_end_clean();
-    return $contents;
+
+    $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
+    if (!file_exists($template)) {
+        trigger_error('The template "'.htmlspecialchars($file).'" could not be fetched!', E_USER_ERROR);
+    } else {
+        ob_start();
+        include($template);
+        $contents = ob_get_contents();
+        ob_end_clean();
+        return $contents;
+    }
   }
 
-}
+  /**
+   * Return paths to the required javascript files.  Used when generating page
+   * header.
+   *
+   * @return array $paths
+   */
+  function getJavascriptIncludes () {
+    $paths = array();
+    foreach ($this->required_js_files as $file) {
+        if (in_array($file, $this->provided_js_files))
+            $paths[] = './'.$this->template_dir.'js/'.basename($file);
+        else $paths[] = SM_PATH .'templates/default/js/'.basename($file);
+    }
+
+    return $paths;
+  }
 
-?>
+  /**
+   * Return any additional stylsheets provided by the template.  Used when
+   * generating page headers.
+   *
+   * @return array $paths
+   */
+  function getAdditionalStyleSheets () {
+    $paths = array();
+    foreach ($this->additional_css_sheets as $css) {
+        $css = basename($css);
+        if (strtolower($css) == 'stylesheet.tpl') {
+            continue;
+        }
+        $paths[] = $css;
+    }
+    return $paths;
+  }
+}