Double->single converted
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index c40b183c021148b0cc3fba84b297083080e53bf7..19e06431c0600b5d99f2af2929ff7f451da4bb4f 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007 - 2009 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -27,7 +27,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * templates are stored. We will internally determine the language plus
         * "html" for web templates or "emails" for email templates
         */
-       private $basePath = '';
+       private $templateBasePath = '';
 
        /**
         * Template type
@@ -45,9 +45,14 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        private $codeExtension = '.ctp';
 
        /**
-        * Path relative to $basePath and language code for compiled code-templates
+        * Path relative to $templateBasePath and language code for compiled code-templates
         */
-       private $compileOutputPath = 'templates/_compiled';
+       private $compileOutputPath = 'templates/_compiled/';
+
+       /**
+        * The path name for all templates
+        */
+       private $genericBasePath = 'templates/';
 
        /**
         * The raw (maybe uncompiled) template
@@ -301,7 +306,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @param       $templateType   The current template's type
         * @return      void
         */
-       private final function setTemplateType ($templateType) {
+       protected final function setTemplateType ($templateType) {
                $this->templateType = (string) $templateType;
        }
 
@@ -327,22 +332,32 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * Setter for base path
         *
-        * @param               $basePath               The local base path for all templates
+        * @param               $templateBasePath               The relative base path for all templates
         * @return      void
         */
-       public final function setBasePath ($basePath) {
+       public final function setTemplateBasePath ($templateBasePath) {
                // And set it
-               $this->basePath = (string) $basePath;
+               $this->templateBasePath = (string) $templateBasePath;
        }
 
        /**
         * Getter for base path
         *
-        * @return      $basePath               The local base path for all templates
+        * @return      $templateBasePath               The relative base path for all templates
+        */
+       public final function getTemplateBasePath () {
+               // And set it
+               return $this->templateBasePath;
+       }
+
+       /**
+        * Getter for generic base path
+        *
+        * @return      $templateBasePath               The relative base path for all templates
         */
-       public final function getBasePath () {
+       public final function getGenericBasePath () {
                // And set it
-               return $this->basePath;
+               return $this->genericBasePath;
        }
 
        /**
@@ -510,29 +525,49 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * Private loader for all template types
         *
         * @param       $template       The template we shall load
+        * @param       $extOther       An other extension to use
         * @return      void
+        * @throws      FileNotFoundException   If the template was not found
         */
-       private function loadTemplate ($template) {
-               // Get extension for the template
-               $ext = $this->getRawTemplateExtension();
-
-               // If we shall load a code-template we need to switch the file extension
-               if ($this->getTemplateType() == $this->getConfigInstance()->readConfig('code_template_type')) {
-                       // Switch over to the code-template extension
-                       $ext = $this->getCodeTemplateExtension();
-               } // END - if
+       protected function loadTemplate ($template, $extOther = '') {
+               // Get extension for the template if empty
+               if (empty($extOther)) {
+                       // None provided, so get the raw one
+                       $ext = $this->getRawTemplateExtension();
+               } else {
+                       // Then use it!
+                       $ext = (string) $extOther;
+               }
 
                // Construct the FQFN for the template by honoring the current language
-               $fqfn = sprintf("%s%s/%s/%s%s",
-                       $this->getBasePath(),
+               $fqfn = sprintf("%s%s%s%s/%s/%s%s",
+                       $this->getConfigInstance()->getConfigEntry('base_path'),
+                       $this->getTemplateBasePath(),
+                       $this->getGenericBasePath(),
                        $this->getLanguageInstance()->getLanguageCode(),
                        $this->getTemplateType(),
                        (string) $template,
                        $ext
                );
 
-               // Load the raw template data
-               $this->loadRawTemplateData($fqfn);
+               // First try this
+               try {
+                       // Load the raw template data
+                       $this->loadRawTemplateData($fqfn);
+               } catch (FileNotFoundException $e) {
+                       // If we shall load a code-template we need to switch the file extension
+                       if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('web_template_type')) && (empty($extOther))) {
+                               // Switch over to the code-template extension and try it again
+                               $ext = $this->getCodeTemplateExtension();
+
+                               // Try it again...
+                               $this->loadTemplate($template, $ext);
+                       } else {
+                               // Throw it again
+                               throw new FileNotFoundException($fqfn, FrameworkFileInputPointer::EXCEPTION_FILE_NOT_FOUND);
+                       }
+               }
+
        }
 
        /**
@@ -895,7 +930,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public function loadWebTemplate ($template) {
                // Set template type
-               $this->setTemplateType($this->getConfigInstance()->readConfig('web_template_type'));
+               $this->setTemplateType($this->getConfigInstance()->getConfigEntry('web_template_type'));
 
                // Load the special template
                $this->loadTemplate($template);
@@ -910,7 +945,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        public function assignConfigVariable ($var) {
                // Sweet and simple...
                //* DEBUG: */ echo __METHOD__.':var={$var}<br />\n';
-               $this->varStack['config'][$var] = $this->getConfigInstance()->readConfig($var);
+               $this->varStack['config'][$var] = $this->getConfigInstance()->getConfigEntry($var);
        }
 
        /**
@@ -922,7 +957,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public function loadEmailTemplate ($template) {
                // Set template type
-               $this->setTemplateType($this->getConfigInstance()->readConfig('email_template_type'));
+               $this->setTemplateType($this->getConfigInstance()->getConfigEntry('email_template_type'));
 
                // Load the special template
                $this->loadTemplate($template);
@@ -937,7 +972,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public function loadCodeTemplate ($template) {
                // Set template type
-               $this->setTemplateType($this->getConfigInstance()->readConfig('code_template_type'));
+               $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_template_type'));
 
                // Load the special template
                $this->loadTemplate($template);
@@ -951,7 +986,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public final function compileVariables () {
                // Initialize the $content array
-               $validVar = $this->getConfigInstance()->readConfig('tpl_valid_var');
+               $validVar = $this->getConfigInstance()->getConfigEntry('tpl_valid_var');
                $dummy = array();
 
                // Iterate through all general variables
@@ -1015,10 +1050,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        );
 
                        // Run the constructed command. This will "compile" all variables in
-                       @eval($eval);
+                       eval($eval);
 
                        // Goes something wrong?
-                       if (!isset($result)) {
+                       if ((!isset($result)) || (empty($result))) {
                                // Output eval command
                                $this->debugOutput(sprintf("Failed eval() code: <pre>%s</pre>", $this->markupCode($eval, true)), true);
 
@@ -1049,9 +1084,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public function compileTemplate () {
                // We will only work with template type "code" from configuration
-               if ($this->getTemplateType() != $this->getConfigInstance()->readConfig('code_template_type')) {
+               if ($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('code_template_type')) {
                        // Abort here
-                       throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->readConfig('code_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
+                       throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
                } // END - if
 
                // Get the raw data.