/** * compile a template * * @access public * @param string name of the template */ function compileTemplate($template) { $name = strtolower($template); if (!isset($this->_templates[$template])) { return patErrorManager::raiseWarning(PATTEMPLATE_WARNING_NO_TEMPLATE, "Template '{$name}' does not exist."); } /** * check, if the template has been loaded * and load it if necessary. */ if ($this->_templates[$template]['loaded'] !== true) { if ($this->_templates[$template]['attributes']['parse'] == 'on') { $result = $this->readTemplatesFromInput($this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template); } else { $result = $this->loadTemplateFromInput($this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], $template); } if (patErrorManager::isError($result)) { return $result; } } $this->_addToCode(''); $this->_addToCode('/**'); $this->_addToCode(' * Compiled version of ' . $template); $this->_addToCode(' *'); $this->_addToCode(' * Template type is ' . $this->_templates[$template]['attributes']['type']); $this->_addToCode(' */'); /** * start the output */ $this->_addToCode('function ' . $template . '()'); $this->_addToCode('{'); $this->_addToCode('$this->_prepareCompiledTemplate( "' . $template . '" );', 1); $this->_addToCode('$this->prepareTemplate( "' . $template . '" );', 1); /** * attributes */ $this->_addToCode('$this->_templates["' . $template . '"]["attributes"] = unserialize( \'' . serialize($this->_templates[$template]['attributes']) . '\' );', 1, 'Read the attributes'); /** * copyVars */ $this->_addToCode('$this->_templates["' . $template . '"]["copyVars"] = unserialize( \'' . serialize($this->_templates[$template]['copyVars']) . '\' );', 1, 'Read the copyVars'); /** * check visibility */ $this->_addToCode('if( $this->_templates["' . $template . '"]["attributes"]["visibility"] != "hidden" ) {', 1, 'Check, whether template is hidden'); /** * autoloop the template */ $this->_addToCode('$this->_templates["' . $template . '"]["iteration"] = 0;', 2, 'Reset the iteration'); $this->_addToCode('$loop = count( $this->_vars["' . $template . '"]["rows"] );', 2, 'Get the amount of loops'); $this->_addToCode('$loop = max( $loop, 1 );', 2); $this->_addToCode('$this->_templates["' . $template . '"]["loop"] = $loop;', 2); $this->_addToCode('for( $i = 0; $i < $loop; $i++ ) {', 2, 'Traverse all variables.'); /** * fetch the variables */ $this->_addToCode('unset( $this->_templates["' . $template . '"]["vars"] );', 3); $this->_addToCode('$this->_fetchVariables("' . $template . '");', 3); /** * different templates have to be compiled differently */ switch ($this->_templates[$template]['attributes']['type']) { /** * modulo template */ case 'modulo': $this->_compileModuloTemplate($template); break; /** * simple condition template */ /** * simple condition template */ case 'simplecondition': $this->_compileSimpleConditionTemplate($template); break; /** * condition template */ /** * condition template */ case 'condition': $this->_compileConditionTemplate($template); break; /** * standard template */ /** * standard template */ default: $this->_compileStandardTemplate($template); break; } $this->_addToCode('$this->_templates["' . $template . '"]["iteration"]++;', 3); $this->_addToCode('}', 2); $this->_addToCode('}', 1); $this->_addToCode('}'); /** * remember this template */ array_push($this->_compiledTemplates, $template); }
/** * load the translation file * * @access private * @param string current input that is used by patTemplate * @return boolean true on success */ function _loadTranslationFile($input) { foreach ($this->_globalconfig['lang'] as $lang) { $translationFile = sprintf($this->_config[$input]['langFile'], $lang); if (!file_exists($translationFile)) { if ($this->_tmpl->getOption('translationAutoCreate') && file_exists($this->_config[$input]['sentenceFile'])) { if (!@copy($this->_config[$input]['sentenceFile'], $translationFile)) { patErrorManager::raiseWarning(PATTTEMPLATE_FUNCTION_TRANSLATE_WARNING_LANGFILE_NOT_CREATABLE, 'A language file could not be created', 'Tried to create the language file [' . $translationFile . ']. Please check that I have write access to the parent folder, or turn off the translateAutoCreate option and create it manually.'); } } continue; } $tmp = @parse_ini_file($translationFile); if (is_array($tmp)) { $tmp = array_map(array($this, '_unescape'), $tmp); $this->_translation[$input] = $tmp; return true; } } return false; }
/** * frees a template * * All memory consumed by the template * will be freed. * * @access public * @param string name of the template * @param boolean clear dependencies of the template * @see freeAllTemplates() */ function freeTemplate($name, $recursive = false) { $name = strtolower($name); $key = array_search($name, $this->_templateList); if ($key === false) { return patErrorManager::raiseWarning(PATTEMPLATE_WARNING_NO_TEMPLATE, "Template '{$name}' does not exist."); } unset($this->_templateList[$key]); $this->_templateList = array_values($this->_templateList); /** * free child templates as well */ if ($recursive === true) { $deps = $this->_getDependencies($name); foreach ($deps as $dep) { $this->freeTemplate($dep, true); } } unset($this->_templates[$name]); unset($this->_vars[$name]); if (isset($this->_discoveredPlaceholders[$name])) { unset($this->_discoveredPlaceholders[$name]); } return true; }
/** * define a new namespace * * @access private * @param string $namespace */ function _defineNamespace($namespace) { array_push($this->defineStack, 'ns'); if (isset($this->definedTags[$namespace])) { $line = $this->_getCurrentLine(); $file = $this->_getCurrentFile(); $this->currentNamespace = false; return patErrorManager::raiseWarning(PATCONFIGURATION_ERROR_CONFIG_INVALID, 'Cannot redefine namespace ' . $namespace . ' on line ' . $line . ' in ' . $file); } $this->definedTags[$namespace] = array(); $this->currentNamespace = $namespace; return true; }
/** * Get a static property. * * Static properties are stored in an array in a global variable, * until PHP5 is ready to use. * * @static * @param string property name * @return mixed property value * @see setStaticProperty() */ function &getStaticProperty($property) { if (isset($GLOBALS["_patForms"][$property])) { return $GLOBALS["_patForms"][$property]; } return patErrorManager::raiseWarning(PATFORMS_ERROR_NO_STATIC_PROPERTY, 'Static property "' . $property . '" could not be retreived, it does not exist.'); }
/** * create an xml representation of the current config * * @access private * @param array $config config to serialize * @param array $options options for the serialization * @return string $content xml representation */ function serializeConfig($config, $options) { if (!isset($options["mode"])) { $options["mode"] = "plain"; } $this->openTags = array(); ksort($config); reset($config); if ($options["mode"] == "pretty") { $options["nl"] = "\n"; } else { $options["nl"] = ""; } $options["depth"] = 0; $xml = "<?xml version=\"1.0\" encoding=\"{$this->configObj->encoding}\"?>\n"; $xml .= "<configuration>" . $options["nl"]; // add comment in pretty mode if ($options["mode"] == "pretty") { $xml .= "\t<!--\n"; $xml .= "\t\tConfiguration generated by patConfiguration v" . $this->configObj->systemVars["appVersion"] . "\n"; $xml .= "\t\t(c) " . implode(",", $this->configObj->systemVars["author"]) . "\n"; $xml .= "\t\tdownload at http://www.php-tools.net\n"; $xml .= "\t\tgenerated on " . date("Y-m-d H:i:s", time()) . "\n"; $xml .= "\t-->\n"; } ++$options["depth"]; foreach ($config as $key => $value) { $path = explode(".", $key); switch (count($path)) { case 0: patErrorManager::raiseWarning(PATCONFIGURATION_WARNING_CONFIGVALUE_WITHOUT_NAME, "configValue without name found!"); default: $openNew = array(); $tag = array_pop($path); $start = max(count($path), count($this->openTags)); for ($i = $start - 1; $i >= 0; $i--) { if (!isset($this->openTags[$i]) || $path[$i] != $this->openTags[$i]) { if (count($this->openTags) > 0) { array_pop($this->openTags); $options["depth"]--; if ($options["mode"] == "pretty") { $xml .= str_repeat("\t", $options["depth"]); } $xml .= "</path>" . $options["nl"]; } if (isset($path[$i])) { array_push($openNew, $path[$i]); } } } while ($path = array_pop($openNew)) { array_push($this->openTags, $path); $xml .= str_repeat("\t", $options["depth"]); $xml .= "<path name=\"" . $path . "\">" . $options["nl"]; $options['depth']++; } $xml .= $this->createTag($tag, $value, $options); break; } } // close all open tags while ($open = array_pop($this->openTags)) { --$options["depth"]; $xml .= str_repeat("\t", $options["depth"]); $xml .= "</path>" . $options["nl"]; } $xml .= "</configuration>"; return $xml; }
/** * addValidationError * * * @access public * @param integer $code * @param array $vars fill named placeholder with values * @return boolean $result true on success */ function addValidationError($code, $vars = array()) { $error = false; $lang = $this->locale; $element = $this->getElementName(); // find error message for selected language while (true) { // error message matches language code if (isset($this->validatorErrorCodes[$lang][$code])) { $error = array("element" => $element, "code" => $code, "message" => $this->validatorErrorCodes[$lang][$code]); break; } else { if ($lang == "C") { break; } } $lang_old = $lang; // look for other languages if (strlen($lang) > 5) { list($lang, $trash) = explode(".", $lang); } else { if (strlen($lang) > 2) { list($lang, $trash) = explode("_", $lang); } else { $lang = "C"; } } // inform developer about missing language patErrorManager::raiseNotice(PATFORMS_ELEMENT_ERROR_VALIDATOR_ERROR_LOCALE_UNDEFINED, "Required Validation Error-Code for language: {$lang_old} not available. Now trying language: {$lang}", "Add language definition in used element or choose other language"); } // get default Error! if (!$error) { patErrorManager::raiseWarning(PATFORMS_ELEMENT_ERROR_VALIDATOR_ERROR_UNDEFINED, "No Error Message for this validation Error was defined", "Review the error-definition for validation-errors in your element '{$element}'."); $error = array("element" => $element, "code" => 0, "message" => "Unknown validation Error"); } // insert values to placeholders if (!empty($vars)) { foreach ($vars as $key => $value) { $error["message"] = str_replace("[" . strtoupper($key) . "]", $value, $error["message"]); } } array_push($this->validationErrors, $error); $this->valid = false; return true; }