Some variables are needed by all templates to make creation easier. Make sure they...
[squirrelmail.git] / class / template / template.class.php
index 15163375bbc803d9fce8a20be65cf5f5b5d1df16..238d0d1379ae8f05881985de5f44c0170f26d564 100755 (executable)
@@ -44,49 +44,53 @@ class Template
 
   /**
    * Template files provided by this template set
-   * 
+   *
    * @var array
    */
   var $templates_provided = array();
-  
+
   /**
    * Javascript files required by the template
-   * 
+   *
    * @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 
+   * not provided, the js file by the same name will be included from the
    * default template directory.
-   * 
+   *
    * @var array
    */
   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
-       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();
+  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();
+        }
   }
 
 
@@ -199,13 +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
-    
+
     $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
-    ob_start();
-    include($template);
-    ob_end_flush();
+    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();
+    }
   }
 
   /**
@@ -214,22 +233,25 @@ 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
 
     $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
-    include($template);
-    $contents = ob_get_contents();
-    ob_end_clean();
-    return $contents;
+    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 
+   * Return paths to the required javascript files.  Used when generating page
    * header.
-   * 
+   *
    * @return array $paths
    */
   function getJavascriptIncludes () {
@@ -239,14 +261,14 @@ class Template
             $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 () {
@@ -261,5 +283,3 @@ class Template
     return $paths;
   }
 }
-
-?>