]> git.mxchange.org Git - mailer.git/blobdiff - inc/xml-functions.php
More fixes, debug lines commented out
[mailer.git] / inc / xml-functions.php
index a623e70181d6dac2f41c4fa413b7821fc6adb3a0..ff62e48d5d128204f01abb83df4a700338e9fc99 100644 (file)
@@ -40,12 +40,30 @@ if (!defined('__SECURITY')) {
        die();
 } // END - if
 
+// Init XML system
+function initXml () {
+       // All conditions
+       $GLOBALS['__XML_CONDITIONS'] = array(
+               // Equals not
+               'NOT-EQUALS' => ' != ',
+               // Is not
+               'IS-NOT'     => ' IS NOT ',
+               // Is
+               'IS'         => ' IS ',
+               // Equals
+               'EQUALS'     => ' = ',
+       );
+}
+
 // Calls back a function based on given XML template data
-function showEntriesByXmlCallback ($template) {
+function doGenericXmlTemplateCallback ($template, $content = array(), $compileCode = TRUE) {
+       // Init XML system as sch calls will be only used once per run
+       initXml();
+
        // Generate FQFN for with special path
        $FQFN = sprintf("%stemplates/xml/%s%s.xml",
                getPath(),
-               detectExtraTemplatePath($template),
+               detectExtraTemplatePath('xml', $template),
                $template
        );
 
@@ -60,8 +78,20 @@ function showEntriesByXmlCallback ($template) {
 
        // Is it again readable?
        if (isFileReadable($FQFN)) {
-               // Read it
-               $templateContent = readFromFile($FQFN);
+               // Is there cache?
+               if ((!isDebuggingTemplateCache()) && (isTemplateCached('xml', $template))) {
+                       // Evaluate the cache
+                       eval(readTemplateCache('xml', $template));
+               } else {
+                       // Read it
+                       $templateContent = readFromFile($FQFN);
+
+                       // Prepare it for finaly eval() command
+                       $GLOBALS['template_eval']['xml'][$template] = '$templateContent = decodeEntities("' . compileRawCode(escapeJavaScriptQuotes($templateContent), FALSE, TRUE, TRUE, $compileCode) . '");';
+
+                       // Eval the code, this does insert any array elements from $content
+                       eval($GLOBALS['template_eval']['xml'][$template]);
+               }
 
                // Init main arrays
                $GLOBALS['__XML_CALLBACKS'] = array(
@@ -70,6 +100,7 @@ function showEntriesByXmlCallback ($template) {
                );
                $GLOBALS['__XML_ARGUMENTS'] = array();
                $GLOBALS['__COLUMN_INDEX']  = array();
+               $GLOBALS['__XML_CONTENT']   = $content;
 
                // Handle it over to the parser
                parseXmlData($templateContent);
@@ -78,7 +109,7 @@ function showEntriesByXmlCallback ($template) {
                addXmlSpecialElements($template);
 
                // Call the call-back function
-               doCallXmlCallbackFunction();
+               doCallXmlCallbackFunction($content);
        } else {
                // Template not found
                displayMessage('{%message,XML_TEMPLATE_404=' . $template . '%}');
@@ -87,14 +118,14 @@ function showEntriesByXmlCallback ($template) {
 
 // Adds special elements by calling back another template-depending function
 function addXmlSpecialElements ($template) {
-       // Generate the FQCN (Full-Qualified CallbackName)
+       // Generate the FQCN (Full-Qualified Callback Name)
        $FQCN = 'addXmlSpecial' . capitalizeUnderscoreString($template);
 
        // Is it there?
        if (function_exists($FQCN)) {
                // Call it
                call_user_func($FQCN);
-       } else {
+       } elseif (isDebugModeEnabled()) {
                // This callback function is only optional
                logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $FQCN . ' for template ' . $template . ' does not exist.');
        }
@@ -102,7 +133,7 @@ function addXmlSpecialElements ($template) {
 
 // Parses the XML content
 function parseXmlData ($content) {
-       // Do we have recode?
+       // Is there recode?
        if (!function_exists('recode')) {
                // No fallback ATM
                reportBug('PHP extension recode is missing. Please install it.');
@@ -115,7 +146,7 @@ function parseXmlData ($content) {
        $xmlParser = xml_parser_create();
 
        // Force case-folding to on
-       xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
+       xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
 
        // Set UTF-8
        xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
@@ -139,7 +170,7 @@ function parseXmlData ($content) {
 function doCallXmlCallbackFunction () {
        // Loop through all added entries
        foreach ($GLOBALS['__XML_CALLBACKS']['callbacks'] as $callback) {
-               // Do we have the entry?
+               // Is there the entry?
                if ((isset($GLOBALS['__XML_CALLBACKS']['functions'][$callback])) && (isset($GLOBALS['__XML_ARGUMENTS'][$callback]))) {
                        // Run all function callbacks
                        foreach ($GLOBALS['__XML_CALLBACKS']['functions'][$callback] as $function) {
@@ -157,11 +188,12 @@ function doCallXmlCallbackFunction () {
 
                                // Is it there?
                                if (!function_exists($callbackName)) {
-                                       reportBug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . 'arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ' - execute call-back does not exist.');
+                                       // No, then please add it
+                                       reportBug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . ',arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ',content()=' . count($GLOBALS['__XML_CONTENT']) . ' - execute call-back does not exist.');
                                } // END - if
 
                                // Call it
-                               call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback], $GLOBALS['__COLUMN_INDEX'][$callback]));
+                               call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback], $GLOBALS['__COLUMN_INDEX'][$callback], $GLOBALS['__XML_CONTENT']));
                        } // END - foreach
                } else {
                        // Not found
@@ -199,7 +231,7 @@ function xmlCharacterHandler ($resource, $characters) {
        // Trim spaces away
        $characters = trim($characters);
 
-       // Do we have some to handle?
+       // Are there some to handle?
        if (strlen($characters) == 0) {
                // Nothing to handle
                return;
@@ -244,22 +276,13 @@ function isXmlValueValid ($type, $value) {
 
 // Converts given condition into a symbol
 function convertXmlContion ($condition) {
-       // Default is an invalid one
-       $return = '???';
-
        // Detect the condition again
-       switch ($condition) {
-               case 'EQUALS': // Equals
-                       $return = '=';
-                       break;
-
-               default: // Unknown condition
-                       reportBug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
-                       break;
-       } // END - switch
+       if (!isset($GLOBALS['__XML_CONDITIONS'][$condition])) {
+               reportBug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
+       } // END - if
 
        // Return it
-       return $return;
+       return $GLOBALS['__XML_CONDITIONS'][$condition];
 }
 
 // "Getter" for sql part back from given array
@@ -272,7 +295,7 @@ function getSqlPartFromXmlArray ($columns) {
                // Init SQL part
                $sqlPart = '';
 
-               // Do we have a table/alias
+               // Is there a table/alias
                if (!empty($columnArray['table'])) {
                        // Pre-add it
                        $sqlPart .= $columnArray['table'] . '.';
@@ -298,7 +321,7 @@ function getSqlPartFromXmlArray ($columns) {
 // Searches in given XML array for value and returns the parent index
 function searchXmlArray ($value, $columns, $childKey) {
        // Default is not found
-       $return = false;
+       $return = FALSE;
 
        // Walk through whole array
        foreach ($columns as $key => $columnArray) {
@@ -306,7 +329,8 @@ function searchXmlArray ($value, $columns, $childKey) {
                assert(isset($columnArray[$childKey]));
 
                // Now is it what we are looking for?
-               if ($columnArray[$childKey] == $value) {
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $value . ',key=' . $key . ',childKey=' . $childKey . ',columnArray=' . $columnArray[$childKey]);
+               if ($columnArray[$childKey] === $value) {
                        // Remember this match
                        $return = $key;