<?php

  /*

      templates.php

      Template path functions can be found in this file.

      ----

      Copyright (C) 2006  Central Manchester CLC
      Copyright (C) 2006  David Thorne (davethorne@gmail.com)

      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 the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   */

  // Pull in required config and preferences
  require_once 'config/global.conf.php';
  require_once 'system/lang.php';
  require_once 'system/user_preferences.php';

  /*
   * function get_template_path()
   *
   * Parameter $template_file is the filename (relative to the template path) of
   *   the template required.
   * Parameter $die tells this function what to do in the event no matching
   *   template could be found. Either it will die (default) or return a null
   *   string.
   *
   * In normal operation, this function returns the full path of the required
   * template file. The resultant template is dependant on language settings.
   */
  function get_template_path($template_file, $die = true)
  {
    // Ensure access to language and configuration
    global $_lang, $conf;

    // First try your chosen language
    $template = $conf['template_dir']."/".$_lang."/".$template_file;
    if (!file_exists($template)) {
      // Then try the system wide fallback languae
      $template = $conf['template_dir']."/".$conf['fallback_template_lang']."/".$template_file;
      if (!file_exists($template)) {
        // Then decide whether to die or not
        if ($die) {
          die(
            "<strong>Fatal Error</strong>: No template found!!!<br /><br />".
            "An unsuccessful attempt was made to access the template <em>".$template_file."</em><br />".
            "&nbsp;&nbsp;&nbsp;&nbsp;search path: ".$conf['template_dir'].'/'.
            $_lang.($_lang != $conf['fallback_template_lang'] ? ':'.
            $conf['template_dir'].'/'.$conf['fallback_template_lang'] : '')."<br />"
          );
        } else {
          return "";
        }
      }
    }
    return $template;
  }

?>
