]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/template/class_BaseTemplateEngine.php
Image generator added, first CAPTCHA added with missing controller (partly work)
[shipsimu.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index 7188fda70d6f9e2241ce30355554c5b71660f3ca..00691efa515ccc45bcfee424e748deda9738a4ef 100644 (file)
@@ -65,10 +65,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        private $lastTemplate = "";
 
        /**
-        * The variable stack for the templates. This must be initialized and
-        * shall become an instance of FrameworkArrayObject.
+        * The variable stack for the templates
         */
-       private $varStack = null;
+       private $varStack = array();
 
        /**
         * Configuration variables in a simple array
@@ -112,20 +111,20 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        private $helpers = array();
 
-       // Exception codes for the template engine
-       const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0x020;
-       const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x021;
-       const EXCEPTION_INVALID_VIEW_HELPER           = 0x022;
+       /**
+        * Current variable group
+        */
+       private $currGroup = "general";
 
        /**
-        * Initialize the variable stack. This holds all variables for later
-        * compilation.
-        *
-        * @return      void
+        * All template groups except "general"
         */
-       public final function initVariableStack () {
-               $this->varStack = new FrameworkArrayObject("FakedVariableStack");
-       }
+       private $varGroups = array();
+
+       // Exception codes for the template engine
+       const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0x200;
+       const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x201;
+       const EXCEPTION_INVALID_VIEW_HELPER           = 0x202;
 
        /**
         * Protected constructor
@@ -152,18 +151,18 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // First everything is not found
                $found = false;
 
-               // Now search for it
-               for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) {
-                       // Get current item
-                       $currEntry = $idx->current();
-
-                       // Is the entry found?
-                       if ($currEntry['name'] == $var) {
-                               // Found!
-                               $found = $idx->key();
-                               break;
-                       }
-               }
+               // Is the group there?
+               if (isset($this->varStack[$this->currGroup])) {
+                       // Now search for it
+                       foreach ($this->varStack[$this->currGroup] as $idx=>$currEntry) {
+                               // Is the entry found?
+                               if ($currEntry['name'] == $var) {
+                                       // Found!
+                                       $found = $idx;
+                                       break;
+                               } // END - if
+                       } // END - foreach
+               } // END - if
 
                // Return the current position
                return $found;
@@ -179,21 +178,19 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // First everything is not found
                $content = null;
 
-               // Now search for it
-               for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) {
-                       // Get current item
-                       $currEntry = $idx->current();
+               // Get variable index
+               $found = $this->isVariableAlreadySet($var);
 
-                       // Is the entry found?
-                       if ($currEntry['name'] == $var) {
-                               // Found!
-                               $content = $currEntry['value'];
-                               break;
-                       }
-               }
+               // Is the variable found?
+               if ($found !== false) {
+                       // Read it
+                       $found = $this->varStack[$this->currGroup][$found]['value'];
+               } // END - if
+
+               //* DEBUG: */ echo __METHOD__.": group=".$this->currGroup.",var=".$var.", found=".$found."<br />\n";
 
                // Return the current position
-               return $content;
+               return $found;
        }
 
        /**
@@ -204,11 +201,72 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        private function addVariable ($var, $value) {
+               // Set general variable group
+               $this->setVariableGroup("general");
+
                // Add it to the stack
-               $this->varStack->append(array(
-                       'name'  => trim($var),
-                       'value' => trim($value)
-               ));
+               $this->addGroupVariable($var, $value);
+       }
+
+       /**
+        * Returns all variables of current group or empty array
+        *
+        * @return      $result         Wether array of found variables or empty array
+        */
+       private function readCurrentGroup () {
+               // Default is not found
+               $result = array();
+
+               // Is the group there?
+               if (isset($this->varStack[$this->currGroup])) {
+                       // Then use it
+                       $result = $this->varStack[$this->currGroup];
+               } // END - if
+
+               // Return result
+               return $result;
+       }
+
+       /**
+        * Settter for variable group
+        *
+        * @param       $groupName      Name of variable group
+        * @param       $add            Wether add this group
+        * @return      void
+        */
+       public function setVariableGroup ($groupName, $add = true) {
+               // Set group name
+               //* DEBIG: */ echo __METHOD__.": currGroup=".$groupName."<br />\n";
+               $this->currGroup = $groupName;
+
+               // Skip group "general"
+               if (($groupName != "general") && ($add === true)) {
+                       $this->varGroups[$groupName] = "OK";
+               } // END - if
+       }
+
+
+       /**
+        * Adds a variable to current group
+        *
+        * @param       $var    Variable to set
+        * @param       $value  Value to store in variable
+        * @return      void
+        */
+       public function addGroupVariable ($var, $value) {
+               //* DEBUG: */ echo __METHOD__.": group=".$this->currGroup.", var=".$var.", value=".$value."<br />\n";
+
+               // Get current variables in group
+               $currVars = $this->readCurrentGroup();
+
+               // Append our variable
+               $currVars[] = array(
+                       'name'  => $var,
+                       'value' => $value
+               );
+
+               // Add it to the stack
+               $this->varStack[$this->currGroup] = $currVars;
        }
 
        /**
@@ -219,20 +277,14 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        private function modifyVariable ($var, $value) {
-               // It should be there so let's look again...
-               for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) {
-                       // Get current entry
-                       $currEntry = $idx->current();
-
-                       // Is this the requested variable?
-                       if ($currEntry['name'] == $var) {
-                               // Change it to the other value
-                               $this->varStack->offsetSet($idx->key(), array(
-                                       'name'  => $var,
-                                       'value' => $value
-                               ));
-                       }
-               }
+               // Get index for variable
+               $idx = $this->isVariableAlreadySet($var);
+
+               // Is the variable set?
+               if ($idx !== false) {
+                       // Then modify it
+                       $this->varStack[$this->currGroup][$idx]['value'] = $value;
+               } // END - if
        }
 
        /**
@@ -494,7 +546,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        throw new MissingMethodException(array($ioInstance, 'loadFileContents'), self::EXCEPTION_MISSING_METHOD);
                }
 
-               /* DEBUG: */ echo __METHOD__.": FQFN=".$fqfn."<br />\n";
+               // Some debug code to look on the file which is being loaded
+               //* DEBUG: */ echo __METHOD__.": FQFN=".$fqfn."<br />\n";
 
                // Load the raw template
                $rawTemplateData = $ioInstance->loadFileContents($fqfn);
@@ -803,10 +856,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $content = $this->getRawTemplateData();
 
                // Walk through all variables
-               for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) {
-                       // Get current entry
-                       $currEntry = $idx->current();
-
+               foreach ($this->varStack['general'] as $currEntry) {
                        // Replace all [$var] or {?$var?} with the content
                        //* DEBUG: */ echo "name=".$currEntry['name'].", value=<pre>".htmlentities($currEntry['value'])."</pre>\n";
                        $content = str_replace("\$content[".$currEntry['name']."]", $currEntry['value'], $content);
@@ -901,12 +951,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $validVar = $this->getConfigInstance()->readConfig('tpl_valid_var');
                $dummy = array();
 
-               // Iterate through all variables
-               for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) {
-
-                       // Get current variable from the stack
-                       $currVariable = $idx->current();
-
+               // Iterate through all general variables
+               foreach ($this->varStack['general'] as $currVariable) {
                        // Transfer it's name/value combination to the $content array
                        //* DEBUG: */ echo $currVariable['name']."=<pre>".htmlentities($currVariable['value'])."</pre>\n";
                        $dummy[$currVariable['name']] = $currVariable['value'];
@@ -1054,7 +1100,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         *
         * @return      void
         */
-       public final function output () {
+       public function output () {
                // Check which type of template we have
                switch ($this->getTemplateType()) {
                case "html": // Raw HTML templates can be send to the output buffer
@@ -1079,7 +1125,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * Loads a given view helper (by name)
         *
-        * @param       $helperName     The helper's name
+        * @param       $helperName             The helper's name
         * @return      void
         * @throws      ViewHelperNotFoundException     If the given view helper was not found
         */
@@ -1165,19 +1211,37 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @param       $rawCode        Raw code to compile
         * @return      $rawCode        Compile code with inserted variable value
         */
-       protected function compileRawCode ($rawCode) {
+       public function compileRawCode ($rawCode) {
                // Find the variables
+               //* DEBUG: */ echo "rawCode=<pre>".htmlentities($rawCode)."</pre>\n";
                preg_match_all($this->regExpVarValue, $rawCode, $varMatches);
 
                // Compile all variables
+               //* DEBUG: */ echo "<pre>".print_r($varMatches, true)."</pre>";
                foreach ($varMatches[0] as $match) {
-                       // Replace the variable with it's value, if found
-                       $rawCode = str_replace("{?".$match."?}", $this->readVariable($match), $rawCode);
+                       // Add variable tags around it
+                       $varCode = "{?".$match."?}";
+
+                       // Is the variable found in code? (safes some calls)
+                       if (strpos($rawCode, $varCode) !== false) {
+                               // Replace the variable with it's value, if found
+                               //* DEBUG: */ echo __METHOD__.": match=".$match."<br />\n";
+                               $rawCode = str_replace($varCode, $this->readVariable($match), $rawCode);
+                       } // END - if
                } // END - foreach
 
                // Return the compiled data
                return $rawCode;
        }
+
+       /**
+        * Getter for variable group array
+        *
+        * @return      $vargroups      All variable groups
+        */
+       public final function getVariableGroups () {
+               return $this->varGroups;
+       }
 }
 
 // [EOF]