Rewrote 'we' word a little, rewrote mail order to use SQL_INSERTID() instead of anoth...
[mailer.git] / inc / xml-functions.php
index d09e8e11fae32792dc250b08d242ef77ec27cd19..c3cfa51b0be8109eeb207bdbfd813cd1462c698b 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -69,10 +69,14 @@ function showEntriesByXmlCallback ($template) {
                        'functions' => array()
                );
                $GLOBALS['__XML_ARGUMENTS'] = array();
+               $GLOBALS['__COLUMN_INDEX']  = array();
 
                // Handle it over to the parser
                parseXmlData($templateContent);
 
+               // Add special elements, e.g. column index
+               addXmlSpecialElements($template);
+
                // Call the call-back function
                doCallXmlCallbackFunction();
        } else {
@@ -81,16 +85,31 @@ function showEntriesByXmlCallback ($template) {
        }
 }
 
+// Adds special elements by calling back another template-depending function
+function addXmlSpecialElements ($template) {
+       // Generate the FQCN (Full-Qualified CallbackName)
+       $FQCN = 'addXmlSpecial' . capitalizeUnderscoreString($template);
+
+       // Is it there?
+       if (function_exists($FQCN)) {
+               // Call it
+               call_user_func($FQCN);
+       } elseif (isDebugModeEnabled()) {
+               // This callback function is only optional
+               logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $FQCN . ' for template ' . $template . ' does not exist.');
+       }
+}
+
 // Parses the XML content
 function parseXmlData ($content) {
-       // Do we have recode?
-       if (function_exists('recode')) {
-               // Convert 'HTML' to UTF-8
-               $content = recode('html..utf8', $content);
-       } else {
+       // Is there recode?
+       if (!function_exists('recode')) {
                // No fallback ATM
-               debug_report_bug('PHP extension recode is missing. Please install it.');
-       }
+               reportBug('PHP extension recode is missing. Please install it.');
+       } // END - if
+
+       // Convert HTML entities to UTF-8
+       $content = recode('html..utf8', $content);
 
        // Create a new XML parser
        $xmlParser = xml_parser_create();
@@ -109,7 +128,7 @@ function parseXmlData ($content) {
        if (!xml_parse($xmlParser, $content)) {
                // Error found in XML!
                //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
-               debug_report_bug(__FUNCTION__, __LINE__, 'Error found in XML. errorMessage=' . xml_error_string(xml_get_error_code($xmlParser)) . ', line=' . xml_get_current_line_number($xmlParser));
+               reportBug(__FUNCTION__, __LINE__, 'Error found in XML. errorMessage=' . xml_error_string(xml_get_error_code($xmlParser)) . ', line=' . xml_get_current_line_number($xmlParser));
        } // END - if
 
        // Free the parser
@@ -120,7 +139,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) {
@@ -138,15 +157,16 @@ function doCallXmlCallbackFunction () {
 
                                // Is it there?
                                if (!function_exists($callbackName)) {
-                                       debug_report_bug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . 'arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ' - execute call-back not found.');
+                                       // No, then please add it
+                                       reportBug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . 'arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ' - execute call-back does not exist.');
                                } // END - if
 
                                // Call it
-                               call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback]));
+                               call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback], $GLOBALS['__COLUMN_INDEX'][$callback]));
                        } // END - foreach
                } else {
                        // Not found
-                       debug_report_bug(__FUNCTION__, __LINE__, 'Entry in callbacks does exist, but not in functions, callback= ' . $callback);
+                       reportBug(__FUNCTION__, __LINE__, 'Entry in callbacks does exist, but not in functions, callback= ' . $callback);
                }
        } // END - foreach
 }
@@ -163,7 +183,7 @@ function startXmlElement ($resource, $element, $attributes) {
        // Is the call-back function there?
        if (!function_exists($elementCallback)) {
                // Not there
-               debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $elementCallback . ', please add it.');
+               reportBug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $elementCallback . ', please add it.');
        } // END - if
 
        // Call the call-back function
@@ -180,7 +200,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;
@@ -211,16 +231,16 @@ function isXmlConditionValid ($condition) {
 // Checks if given value is valid/verifyable
 function isXmlValueValid ($type, $value) {
        // Depends on type, so build a call-back
-       $callbackFunction = 'isXmlType' . trim(capitalizeUnderscoreString($type));
+       $callbackName = 'isXmlType' . trim(capitalizeUnderscoreString($type));
 
        // Is the call-back function there?
-       if (!function_exists($callbackFunction)) {
+       if (!function_exists($callbackName)) {
                // Not there
-               debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $callbackFunction . ', please add it.');
+               reportBug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $callbackName . ', please add it.');
        } // END - if
 
        // Call and return it
-       return call_user_func_array($callbackFunction, array($value));
+       return call_user_func_array($callbackName, array($value));
 }
 
 // Converts given condition into a symbol
@@ -235,7 +255,7 @@ function convertXmlContion ($condition) {
                        break;
 
                default: // Unknown condition
-                       debug_report_bug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
+                       reportBug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
                        break;
        } // END - switch
 
@@ -253,7 +273,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'] . '.';
@@ -293,7 +313,7 @@ function searchXmlArray ($value, $columns, $childKey) {
 
                        // And abort any further searches
                        break;
-               } // END - foreach
+               } // END - if
        } // END - foreach
 
        // Return key/false