t12n of compose function. Paul, could you please check the plugin hook implementatio...
[squirrelmail.git] / class / template / template.class.php
index bf2dcc3fd51a35f21dad2b89dc475eb8dd82b180..f1c37c4ff30de5951dcaddc71699c465be5c68b0 100755 (executable)
@@ -267,20 +267,28 @@ return substr($this->template_dir, strlen(SM_PATH));
      *
      * Find the right template file.
      *
-     * Templates are expected to be found in the template set directory
-     * (for example SM_PATH/templates/<template name>/) or, in the case
-     * of plugin templates, in a plugin directory in the template set
-     * directory (for example, 
-     * SM_PATH/templates/<template name>/plugins/<plugin name>/) *OR* in 
-     * a template directory in the plugin as a fallback (for example,
-     * SM_PATH/plugins/<plugin name>/templates/<template name>/).  If 
-     * the correct file is not found for the current template set, a 
+     * Templates are expected to be found in the template set directory,
+     * for example:
+     *     SM_PATH/templates/<template name>/
+     * or, in the case of plugin templates, in a plugin directory in the 
+     * template set directory, for example:
+     *     SM_PATH/templates/<template name>/plugins/<plugin name>/
+     * *OR* in a template directory in the plugin as a fallback, for example:
+     *     SM_PATH/plugins/<plugin name>/templates/<template name>/
+     * If the correct file is not found for the current template set, a 
      * default template is loaded, which is expected to be found in the 
-     * default template directory (for example, SM_PATH/templates/default/)
-     * or for plugins, in a plugin directory in the default template set
-     * (for example, SM_PATH/templates/default/plugins/<plugin name>/),
-     * *OR* in a default template directory in the plugin as a fallback
-     * (for example, SM_PATH/plugins/<plugin name>/templates/default/).
+     * default template directory, for example:
+     *     SM_PATH/templates/default/
+     * or for plugins, in a plugin directory in the default template set,
+     * for example:
+     *     SM_PATH/templates/default/plugins/<plugin name>/
+     * *OR* in a default template directory in the plugin as a fallback,
+     * for example:
+     *     SM_PATH/plugins/<plugin name>/templates/default/
+     *
+     * Plugin authors must note that the $filename MUST be prefaced
+     * with "plugins/<plugin name>/" in order to correctly resolve the 
+     * template file.
      *
      * @param string $filename The name of the template file,
      *                         possibly prefaced with 
@@ -313,19 +321,29 @@ return substr($this->template_dir, strlen(SM_PATH));
                           . $this->get_template_file_directory() 
                           . substr($filename, strlen($plugin_name) + 9);
 
-                // no go, we have to get the default template
-                // from the plugin
+                // no go, we have to get the default template
+                // first try the default SM template
                 //
                 if (!file_exists($filepath)) {
 
-                    $filepath = SM_PATH . 'plugins/' . $plugin_name . '/'
+                    $filepath = SM_PATH 
                               . $this->get_default_template_file_directory() 
-                              . substr($filename, strlen($plugin_name) + 9);
+                              . $filename;
 
-                    // no dice whatsoever, return empty string
+                    // still no luck?  get default template from the plugin
                     //
                     if (!file_exists($filepath)) {
-                        $filepath = '';
+
+                        $filepath = SM_PATH . 'plugins/' . $plugin_name . '/'
+                                  . $this->get_default_template_file_directory() 
+                                  . substr($filename, strlen($plugin_name) + 9);
+
+                        // no dice whatsoever, return empty string
+                        //
+                        if (!file_exists($filepath)) {
+                            $filepath = '';
+                        }
+
                     }
 
                 }
@@ -360,17 +378,46 @@ return substr($this->template_dir, strlen(SM_PATH));
      */
     function display($file)
     {
-        // Pull in our config file
-        $t = &$this->values; // place values array directly in scope
 
-        // Get right template file
+        // get right template file
+        //
         $template = $this->get_template_file_path($file);
         if (empty($template)) {
-            trigger_error('The template "'.htmlspecialchars($file).'" could not be displayed!', E_USER_ERROR);
+
+            trigger_error('The template "' . htmlspecialchars($file) 
+                          . '" could not be displayed!', E_USER_ERROR);
+
         } else {
+
+            $aPluginOutput = array();
+            $aPluginOutput = concat_hook_function('template_construct_' . $file,
+                                                  array($aPluginOutput, $this));
+            $this->assign('plugin_output', $aPluginOutput);
+
+            // pull in our config file ($t?  let's try to be more verbose please :-) )
+            //
+            $t = &$this->values; // place values array directly in scope
+
             ob_start();
             include($template);
+
+            // CAUTION: USE OF THIS HOOK IS HIGHLY DISCOURAGED AND CAN
+            // RESULT IN NOTICABLE PERFORMANCE DEGREDATION.  Plugins
+            // using this hook will probably be rejected by the 
+            // SquirrelMail team.
+            //
+            // Anyone hooking in here that wants to manipulate the output
+            // buffer has to get the buffer, clean it and then echo the 
+            // new buffer like this:
+            // $buffer = ob_get_contents(); ob_clean(); .... echo $new_buffer;
+            //
+            // Don't need to pass buffer contents ourselves
+            // do_hook_function('template_output', array(ob_get_contents()));
+            //
+            do_hook('template_output');
+
             ob_end_flush();
+
         }
     }
 
@@ -381,18 +428,48 @@ return substr($this->template_dir, strlen(SM_PATH));
      * @return string A string of the results
      */
     function fetch($file) {
-        $t = &$this->values; // place values array directly in scope
 
-        // Get right template file
+        // get right template file
+        //
         $template = $this->get_template_file_path($file);
-        if (!file_exists($template)) {
-            trigger_error('The template "'.htmlspecialchars($file).'" could not be fetched!', E_USER_ERROR);
+        if (empty($template)) {
+
+            trigger_error('The template "' . htmlspecialchars($file) 
+                          . '" could not be fetched!', E_USER_ERROR);
+
         } else {
+
+            $aPluginOutput = array();
+            $aPluginOutput = concat_hook_function('template_construct_' . $file,
+                                                  array($aPluginOutput, $this));
+            $this->assign('plugin_output', $aPluginOutput);
+
+            // pull in our config file ($t?  let's try to be more verbose please :-) )
+            //
+            $t = &$this->values; // place values array directly in scope
+
             ob_start();
             include($template);
+
+            // CAUTION: USE OF THIS HOOK IS HIGHLY DISCOURAGED AND CAN
+            // RESULT IN NOTICABLE PERFORMANCE DEGREDATION.  Plugins
+            // using this hook will probably be rejected by the 
+            // SquirrelMail team.
+            //
+            // Anyone hooking in here that wants to manipulate the output
+            // buffer has to get the buffer, clean it and then echo the 
+            // new buffer like this:
+            // $buffer = ob_get_contents(); ob_clean(); .... echo $new_buffer;
+            //
+            // Don't need to pass buffer contents ourselves
+            // do_hook_function('template_output', array(ob_get_contents()));
+            //
+            do_hook('template_output');
+
             $contents = ob_get_contents();
             ob_end_clean();
             return $contents;
+
         }
     }