/**
  * Replace vars like {object:field:type} or {var|session|request|page:name:type}. Called during definition compilation
  *
  * @param string $text : the text which need to be replaced
  * @param boolean reverse : reverse single and double quotes useage (default is false : double quotes)
  * @param array $optionalReplacement : optionnal replacement to do
  * @param boolean $cleanNotMatches : remove vars without matches
  * @param mixed $matchCallback : function name or array(object classname, object method) which represent a valid callback function to execute on matches
  * @return text : the text replaced
  * @access public
  */
 function preReplaceVars($text, $reverse = false, $cleanNotMatches = false, $matchCallback = array('CMS_polymod_definition_parsing', 'encloseString'), $returnMatchedVarsArray = false)
 {
     static $replacements;
     //if no text => return
     if (!$text || !trim($text)) {
         return $text;
     }
     //substitute simple replacement values
     $preReplaceCount = 0;
     $text = preg_replace("#{([a-zA-Z]+)}#", '@@@\\1@@@', $text, -1, $preReplaceCount);
     $count = 1;
     //loop on text for vars to replace if any
     while (preg_match_all("#{[^{}\n]+}#", $text, $matches) && $count) {
         $matches = array_unique($matches[0]);
         //get all tags handled by modules
         if (!$replacements) {
             //create replacement array
             $replacements = array();
             $modules = CMS_modulesCatalog::getAll("id");
             foreach ($modules as $codename => $aModule) {
                 $moduleReplacements = $aModule->getModuleReplacements();
                 if (is_array($moduleReplacements) && $moduleReplacements) {
                     foreach ($moduleReplacements as $pattern => $replacement) {
                         $replacements[$pattern] = $replacement;
                     }
                 }
             }
         }
         $replace = $replacements;
         //pr($matches);
         if ($reverse) {
             $reversedReplace = array();
             foreach ($replace as $key => $value) {
                 $reversedReplace[str_replace("'", "\\\\'", $key)] = $value;
             }
             $replace = $reversedReplace;
         }
         $count = 0;
         $matchesValues = preg_replace(array_keys($replace), $replace, $matches, -1, $count);
         //create vars conversion table
         $replace = array();
         if ($matchesValues) {
             if (isset($this->_parameters['module'])) {
                 $externalReferences = CMS_poly_object_catalog::getFieldsReferencesUsage($this->_parameters['module']);
             } else {
                 $externalReferences = CMS_poly_object_catalog::getFieldsReferencesUsage();
             }
             foreach ($matches as $key => $match) {
                 //record external references for cache reference
                 if ($externalReferences) {
                     foreach ($externalReferences as $id => $type) {
                         if (strpos($match, '[\'fields\'][' . $id . ']') !== false || strpos($match, '[\\\'fields\\\'][' . $id . ']') !== false) {
                             //CMS_grandFather::log(print_r($this->_elements, true));
                             $this->_elements = array_merge_recursive($type, (array) $this->_elements);
                             //CMS_grandFather::log(print_r($this->_elements, true));
                         }
                     }
                 }
                 //record used pages for cache reference
                 if (strpos($match, '{page:') !== false) {
                     $this->_elements['module'][] = MOD_STANDARD_CODENAME;
                 }
                 //record used users for cache reference
                 if (strpos($match, '{user:'******'resource'][] = 'users';
                 }
                 if ($match != $matchesValues[$key]) {
                     $matchValue = $matchesValues[$key];
                 } else {
                     $matchValue = null;
                 }
                 //apply callback if any to value
                 if (isset($matchValue)) {
                     if ($matchCallback !== false) {
                         if (is_callable($matchCallback)) {
                             $replace[$match] = call_user_func($matchCallback, $matchValue, $reverse);
                         } else {
                             CMS_grandFather::raiseError("Unknown callback function : " . $matchCallback);
                             return false;
                         }
                     } else {
                         $replace[$match] = $matchValue;
                     }
                 } elseif ($cleanNotMatches) {
                     $replace[$match] = '';
                 }
             }
         }
         //return matched vars if needed
         if ($returnMatchedVarsArray) {
             //substitute simple replacement values
             if ($preReplaceCount) {
                 $replace = preg_replace("#\\@\\@\\@([a-zA-Z]+)\\@\\@\\@#", '{\\1}', $replace);
             }
             return $replace;
         } else {
             //then replace variables in text and return it
             $text = str_replace(array_keys($replace), $replace, $text);
         }
     }
     //substitute simple replacement values
     if ($preReplaceCount) {
         $text = preg_replace("#\\@\\@\\@([a-zA-Z]+)\\@\\@\\@#", '{\\1}', $text);
     }
     return $text;
 }