function preferencesTransformation($operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters) { if (count($parameters[0]) == 0) { return false; } $values = array(); if (eZTemplateNodeTool::isConstantElement($parameters[0])) { $name = eZTemplateNodeTool::elementConstantValue($parameters[0]); $nameText = eZPHPCreator::variableText($name, 0, 0, false); } else { $nameText = '%1%'; $values[] = $parameters[0]; } return array(eZTemplateNodeTool::createCodePieceElement("%output% = eZPreferences::value( {$nameText} );\n", $values)); }
function templateNodeCaseTransformation($tpl, &$newNodes, &$caseNodes, &$caseCounter, &$node, $privateData) { if ($node[2] == 'case') { if (is_array($node[3]) && count($node[3])) { if (isset($node[3]['match'])) { $match = $node[3]['match']; $match = eZTemplateCompiler::processElementTransformationList($tpl, $node, $match, $privateData); $dynamicCase = false; if (eZTemplateNodeTool::isConstantElement($match)) { $matchValue = eZTemplateNodeTool::elementConstantValue($match); $caseText = eZPHPCreator::variableText($matchValue, 0, 0, false); } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $match, false, array(), 'case' . $caseCounter); $caseText = "\$case" . $caseCounter; ++$caseCounter; $dynamicCase = true; } $caseNodes[] = eZTemplateNodeTool::createCodePieceNode(" case {$caseText}:\n {"); if ($dynamicCase) { $caseNodes[] = eZTemplateNodeTool::createCodePieceNode(" unset( {$caseText} );"); } } else { if (isset($node[3]['in'])) { return false; } } } else { $caseNodes[] = eZTemplateNodeTool::createCodePieceNode(" default:\n {"); } $children = eZTemplateNodeTool::extractFunctionNodeChildren($node); if ($children === false) { $children = array(); } else { $children = eZTemplateCompiler::processNodeTransformationNodes($tpl, $node, $children, $privateData); } $caseNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(8); $caseNodes = array_merge($caseNodes, $children); $caseNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(8); $caseNodes[] = eZTemplateNodeTool::createCodePieceNode(" } break;"); } }
function mergeTrans($operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters) { $code = ''; $stringCode = ''; $paramCount = 0; $values = array(); $staticArray = array(); for ($i = 1; $i < count($parameters); ++$i) { if ($i != 1) { $code .= ', '; $stringCode .= ', '; } if (!eZTemplateNodeTool::isStaticElement($parameters[$i])) { $values[] = $parameters[$i]; ++$paramCount; if ($operatorName == $this->MergeName or $operatorName == $this->ArrayMergeName) { $code .= '%' . $paramCount . '%'; } else { $code .= 'array( %' . $paramCount . '% )'; } $stringCode .= '%' . $paramCount . '%'; } else { if ($paramCount == 0) { $staticArray[] = eZTemplateNodeTool::elementStaticValue($parameters[$i]); } if ($operatorName == $this->MergeName or $operatorName == $this->ArrayMergeName) { $code .= '' . eZPHPCreator::variableText(eZTemplateNodeTool::elementStaticValue($parameters[$i]), 0, 0, false) . ''; } else { $tmp_check = eZPHPCreator::variableText(eZTemplateNodeTool::elementStaticValue($parameters[$i]), 0, 0, false); // hiding "%1%", "%output%" etc. in static input string to avoid replacing it on "$var" inside compiler. $tmp_check = str_replace("%", '"."%"."', $tmp_check); $code .= 'array( ' . $tmp_check . ' )'; } $stringCode .= eZPHPCreator::variableText(eZTemplateNodeTool::elementStaticValue($parameters[$i]), 0, 0, false); } } $isString = false; $isArray = false; $code2 = false; if ($parameters[0]) { if (!eZTemplateNodeTool::isStaticElement($parameters[0])) { $values[] = $parameters[0]; ++$paramCount; $code2 = '%' . $paramCount . '%'; } else { $isString = is_string(eZTemplateNodeTool::elementStaticValue($parameters[0])); $isArray = is_array(eZTemplateNodeTool::elementStaticValue($parameters[0])); if ($paramCount == 0) { // $staticArray[] = eZTemplateNodeTool::elementStaticValue( $parameters[0] ); } else { $code2 = eZPHPCreator::variableText(eZTemplateNodeTool::elementStaticValue($parameters[0]), 0, 0, false); } } } if ($paramCount == 0) { if ($operatorName == $this->AppendName or $operatorName == $this->ArrayAppendName or $operatorName == $this->MergeName or $operatorName == $this->ArrayMergeName) { if ($isString) { $str = eZTemplateNodeTool::elementStaticValue($parameters[0]); for ($i = 0; $i < count($staticArray); ++$i) { $str .= $staticArray[$i]; } return array(eZTemplateNodeTool::createStringElement($str)); } else { if ($isArray) { $returnArray = eZTemplateNodeTool::elementStaticValue($parameters[0]); for ($i = 0; $i < count($staticArray); ++$i) { $returnArray = array_merge($returnArray, $staticArray[$i]); } return array(eZTemplateNodeTool::createArrayElement($returnArray)); } } } else { if ($operatorName == $this->PrependName or $operatorName == $this->ArrayPrependName) { if ($isString) { return array(eZTemplateNodeTool::createStringElement(eZTemplateNodeTool::elementStaticValue($parameters[1]) . eZTemplateNodeTool::elementStaticValue($parameters[0]))); } else { if ($isArray) { return array(eZTemplateNodeTool::createArrayElement(array_merge($staticArray, eZTemplateNodeTool::elementStaticValue($parameters[0])))); } } } } } if ($code2) { if ($operatorName == $this->AppendName) { $code = 'if ( is_string( ' . $code2 . ' ) )' . "\n" . ' %output% = ' . $code2 . ' . implode( \'\', array( ' . $stringCode . ' ) );' . "\n" . 'else if( is_array( ' . $code2 . ' ) )' . "\n" . ' %output% = array_merge( ' . $code2 . ', ' . $code . ' );'; } else { if ($operatorName == $this->ArrayAppendName) { $code = '%output% = array_merge( ' . $code2 . ', ' . $code . ' );'; } else { if ($operatorName == $this->MergeName) { $code = '%output% = array_merge( ' . $code2 . ', ' . $code . ' );'; } else { if ($operatorName == $this->ArrayMergeName) { $code = '%output% = array_merge( ' . $code2 . ', ' . $code . ' );'; } else { if ($operatorName == $this->PrependName) { $code = 'if ( is_string( ' . $code2 . ' ) )' . "\n" . ' %output% = implode( \'\', array( ' . $stringCode . ' ) ) . ' . $code2 . ';' . "\n" . 'else if( is_array( ' . $code2 . ' ) )' . "\n" . ' %output% = array_merge( ' . $code . ', ' . $code2 . ' );'; } else { if ($operatorName == $this->ArrayPrependName) { $code = '%output% = array_merge( ' . $code . ', ' . $code2 . ' );'; } } } } } } } else { if ($operatorName == $this->MergeName) { $code = '%output% = array_merge( ' . $code . ' );'; } else { $code = '%output% = array(' . $code . ');'; } } return array(eZTemplateNodeTool::createCodePieceElement($code . "\n", $values)); }
function templateNodeTransformation($functionName, &$node, $tpl, $parameters, $privateData) { $ini = eZINI::instance(); $children = eZTemplateNodeTool::extractFunctionNodeChildren($node); if ($ini->variable('TemplateSettings', 'TemplateCache') != 'enabled') { return $children; } $functionPlacement = eZTemplateNodeTool::extractFunctionNodePlacement($node); $placementKeyString = eZTemplateCacheBlock::placementString($functionPlacement); $newNodes = array(); $ignoreContentExpiry = false; if (isset($parameters['expiry'])) { if (eZTemplateNodeTool::isConstantElement($parameters['expiry'])) { $expiryValue = eZTemplateNodeTool::elementConstantValue($parameters['expiry']); $ttlCode = $expiryValue > 0 ? eZPHPCreator::variableText($expiryValue, 0, 0, false) : 'null'; } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['expiry'], false, array(), 'localExpiry'); $ttlCode = "( \$localExpiry > 0 ? \$localExpiry : null )"; } } else { $ttlCode = eZPHPCreator::variableText(self::DEFAULT_TTL, 0, 0, false); } if (isset($parameters['ignore_content_expiry'])) { $ignoreContentExpiry = eZTemplateNodeTool::elementConstantValue($parameters['ignore_content_expiry']); } $keysData = false; $hasKeys = false; $subtreeExpiryData = null; $subtreeValue = null; if (isset($parameters['keys'])) { $keysData = $parameters['keys']; $hasKeys = true; } if (isset($parameters['subtree_expiry'])) { $subtreeExpiryData = $parameters['subtree_expiry']; if (!eZTemplateNodeTool::isConstantElement($subtreeExpiryData)) { $hasKeys = true; } else { $subtreeValue = eZTemplateNodeTool::elementConstantValue($subtreeExpiryData); } $ignoreContentExpiry = true; } $accessName = false; if (isset($GLOBALS['eZCurrentAccess']['name'])) { $accessName = $GLOBALS['eZCurrentAccess']['name']; } if ($hasKeys) { $placementKeyStringText = eZPHPCreator::variableText($placementKeyString, 0, 0, false); $accessNameText = eZPHPCreator::variableText($accessName, 0, 0, false); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $keysData, false, array(), 'cacheKeys'); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $subtreeExpiryData, false, array(), 'subtreeExpiry'); $code = "\$cacheKeys = array( \$cacheKeys, {$placementKeyStringText}, {$accessNameText} );\n"; $cachePathText = "\$cachePath"; } else { $nodeID = $subtreeValue ? eZTemplateCacheBlock::decodeNodeID($subtreeValue) : false; $cachePath = eZTemplateCacheBlock::cachePath(eZTemplateCacheBlock::keyString(array($placementKeyString, $accessName)), $nodeID); $code = ""; $cachePathText = eZPHPCreator::variableText($cachePath, 0, 0, false); } $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code); $code = ''; $codePlacementHash = md5($placementKeyString); if ($hasKeys) { $code .= "list(\$cacheHandler_{$codePlacementHash}, \$contentData) =\n eZTemplateCacheBlock::retrieve( \$cacheKeys, \$subtreeExpiry, {$ttlCode}, " . ($ignoreContentExpiry ? "false" : "true") . " );\n"; } else { $nodeIDText = var_export($nodeID, true); $code .= "list(\$cacheHandler_{$codePlacementHash}, \$contentData) =\n eZTemplateCacheBlock::handle( {$cachePathText}, {$nodeIDText}, {$ttlCode}, " . ($ignoreContentExpiry ? "false" : "true") . " );\n"; } $code .= "if ( !( \$contentData instanceof eZClusterFileFailure ) )\n" . "{\n"; $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code, array('spacing' => 0)); $newNodes[] = eZTemplateNodeTool::createWriteToOutputVariableNode('contentData', array('spacing' => 4)); $newNodes[] = eZTemplateNodeTool::createCodePieceNode(" unset( \$contentData );\n" . "}\n" . "else\n" . "{\n" . " unset( \$contentData );"); $newNodes[] = eZTemplateNodeTool::createOutputVariableIncreaseNode(array('spacing' => 4)); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(4); $newNodes = array_merge($newNodes, $children); $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(4); $newNodes[] = eZTemplateNodeTool::createAssignFromOutputVariableNode('cachedText', array('spacing' => 4)); $code = "\$cacheHandler_{$codePlacementHash}->storeCache( array( 'scope' => 'template-block', 'binarydata' => \$cachedText ) );\n"; $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code, array('spacing' => 4)); $newNodes[] = eZTemplateNodeTool::createOutputVariableDecreaseNode(array('spacing' => 4)); $newNodes[] = eZTemplateNodeTool::createWriteToOutputVariableNode('cachedText', array('spacing' => 4)); $newNodes[] = eZTemplateNodeTool::createCodePieceNode(" unset( \$cachedText, \$cacheHandler_{$codePlacementHash} );\n}\n"); return $newNodes; }
function templateNodeTransformation($functionName, &$node, $tpl, &$resourceData, $parameters, $namespaceValue) { if ($this->Name != 'design' and $this->Name != 'standard') { return false; } $file = $resourceData['template-name']; $matchFileArray = eZTemplateDesignResource::overrideArray($this->OverrideSiteAccess); $matchList = array(); foreach ($matchFileArray as $matchFile) { if (!isset($matchFile['template'])) { continue; } if ($matchFile['template'] == '/' . $file) { $matchList[] = $matchFile; } } $resourceName = $resourceData['resource']; $resourceNameText = eZPHPCreator::variableText($resourceName); $designKeysName = 'dKeys'; if ($resourceName == 'standard') { $designKeysName = 'rKeys'; } $newNodes = array(); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if " . ($resourceData['use-comments'] ? "/*TDR:" . __LINE__ . "*/" : "") . "( !isset( \${$designKeysName} ) )\n" . "{\n" . " \$resH = \$tpl->resourceHandler( {$resourceNameText} );\n" . " \${$designKeysName} = \$resH->keys();" . "\n" . "}\n"); foreach ($matchList as $match) { $basedir = $match['base_dir']; $template = $match['template']; $file = $basedir . $template; $spacing = 0; $addFileResource = true; if (isset($match['custom_match'])) { $spacing = 4; $customMatchList = $match['custom_match']; $matchCount = 0; foreach ($customMatchList as $customMatch) { $matchConditionCount = count($customMatch['conditions']); $code = ''; if ($matchCount > 0) { $code = "else " . ($resourceData['use-comments'] ? "/*TDR:" . __LINE__ . "*/" : "") . ""; } if ($matchConditionCount > 0) { if ($matchCount > 0) { $code .= " "; } $code .= "if " . ($resourceData['use-comments'] ? "/*TDR:" . __LINE__ . "*/" : "") . "( "; } $ifLength = strlen($code); $conditionCount = 0; if (is_array($customMatch['conditions'])) { foreach ($customMatch['conditions'] as $conditionName => $conditionValue) { if ($conditionCount > 0) { $code .= " and\n" . str_repeat(' ', $ifLength); } $conditionNameText = eZPHPCreator::variableText($conditionName, 0); $conditionValueText = eZPHPCreator::variableText($conditionValue, 0); $code .= "isset( \$" . $designKeysName . "[{$conditionNameText}] ) and "; if ($conditionName == 'url_alias') { $code .= "(strpos( \$" . $designKeysName . "[{$conditionNameText}], {$conditionValueText} ) === 0 )"; } else { $code .= "( is_array( \$" . $designKeysName . "[{$conditionNameText}] ) ? " . "in_array( {$conditionValueText}, \$" . $designKeysName . "[{$conditionNameText}] ) : " . "\$" . $designKeysName . "[{$conditionNameText}] == {$conditionValueText} )"; } ++$conditionCount; } } if ($matchConditionCount > 0) { $code .= " )\n"; } if ($matchConditionCount > 0 or $matchCount > 0) { $code .= "{"; } $matchFile = $customMatch['match_file']; $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code); $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode('', $matchFile, $matchFile, eZTemplate::RESOURCE_FETCH, false, $node[4], array('spacing' => $spacing), $namespaceValue); if ($matchConditionCount > 0 or $matchCount > 0) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}"); } ++$matchCount; if ($matchConditionCount == 0) { $addFileResource = false; break; } } if ($addFileResource) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("else" . ($resourceData['use-comments'] ? "/*TDR:" . __LINE__ . "*/" : "") . "\n{"); } } if ($addFileResource) { $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode('', $file, $file, eZTemplate::RESOURCE_FETCH, false, $node[4], array('spacing' => $spacing), $namespaceValue); } if (isset($match['custom_match']) and $addFileResource) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}"); } } return $newNodes; }
static function generateVariableDataCode($php, $tpl, $variableData, &$knownTypes, $dataInspection, &$persistence, $parameters, &$resourceData) { $staticTypeMap = array(eZTemplate::TYPE_STRING => 'string', eZTemplate::TYPE_NUMERIC => 'numeric', eZTemplate::TYPE_IDENTIFIER => 'string', eZTemplate::TYPE_ARRAY => 'array', eZTemplate::TYPE_BOOLEAN => 'boolean'); $variableAssignmentName = $parameters['variable']; $variableAssignmentCounter = $parameters['counter']; $spacing = 0; $optimizeNode = false; if (isset($parameters['spacing'])) { $spacing = $parameters['spacing']; } if ($variableAssignmentCounter > 0) { $variableAssignmentName .= $variableAssignmentCounter; } // We need to unset the assignment variable before any elements are processed // This ensures that we don't work on existing variables $php->addCodePiece("unset( \${$variableAssignmentName} );\n", array('spacing' => $spacing)); if (is_array($variableData)) { foreach ($variableData as $index => $variableDataItem) { $variableDataType = $variableDataItem[0]; if ($variableDataType == eZTemplate::TYPE_STRING or $variableDataType == eZTemplate::TYPE_NUMERIC or $variableDataType == eZTemplate::TYPE_IDENTIFIER or $variableDataType == eZTemplate::TYPE_ARRAY or $variableDataType == eZTemplate::TYPE_BOOLEAN) { $knownTypes = array_unique(array_merge($knownTypes, array($staticTypeMap[$variableDataType]))); $dataValue = $variableDataItem[1]; $dataText = $php->thisVariableText($dataValue, 0, 0, false); $php->addCodePiece("\${$variableAssignmentName} = {$dataText};\n", array('spacing' => $spacing)); } else { if ($variableDataType == eZTemplate::TYPE_OPTIMIZED_NODE) { $optimizeNode = true; if (!isset($resourceData['node-object-cached'])) { $tpl->error("eZTemplateCompiler" . ($resourceData['use-comments'] ? ":" . __LINE__ : ""), "Attribute node-object-cached of variable \$resourceData was not found but variable node eZTemplate::TYPE_OPTIMIZED_NODE is still present. This should not happen"); } $php->addCodePiece("\${$variableAssignmentName} = \$nod_{$resourceData['uniqid']};\n"); // If optimized node is not set, use unoptimized code. $php->addCodePiece("if ( !\${$variableAssignmentName} )\n{\n"); } else { if ($variableDataType == eZTemplate::TYPE_OPTIMIZED_ARRAY_LOOKUP) { $code = $resourceData['use-comments'] ? "/*TC:" . __LINE__ . "*/\n" : ""; // This code is used a lot so we create a variable for it $phpVar = "\${$variableAssignmentName}"; $indexName = "'{$variableDataItem[1][0][1]}'"; // Add sanity checking $code .= "if ( !isset( {$phpVar}[{$indexName}] ) )\n" . "{\n" . " \$tpl->error( 'eZTemplateCompiler" . ($resourceData['use-comments'] ? ":" . __LINE__ : "") . "', \"PHP variable \\{$phpVar}" . "[{$indexName}] does not exist, cannot fetch the value.\" );\n" . " {$phpVar} = null;\n" . "}\n" . "else\n "; // Add the actual code $code .= "{$phpVar} = {$phpVar}[{$indexName}];\n"; $php->addCodePiece($code); } else { if ($variableDataType == eZTemplate::TYPE_OPTIMIZED_ATTRIBUTE_LOOKUP) { $code = $resourceData['use-comments'] ? "/*TC:" . __LINE__ . "*/\n" : ""; $code .= <<<END if ( !is_object( \${$variableAssignmentName} ) ) { \${$variableAssignmentName} = null; } else if ( \${$variableAssignmentName}->hasAttribute( "{$variableDataItem[1][0][1]}" ) ) { \${$variableAssignmentName} = \${$variableAssignmentName}->attribute( "{$variableDataItem[1][0][1]}" ); } END; $php->addCodePiece($code); } else { if ($variableDataType == eZTemplate::TYPE_OPTIMIZED_CONTENT_CALL) { // Line number comment $code = $resourceData['use-comments'] ? "/*TC:" . __LINE__ . "*/\n" : ""; // This code is used a lot so we create a variable for it $phpVar = "\${$variableAssignmentName}"; // Add sanity checking $code .= "if ( !is_object( {$phpVar} ) )\n" . "{\n" . " \$tpl->error( 'eZTemplateCompiler" . ($resourceData['use-comments'] ? ":" . __LINE__ : "") . "', \"PHP variable \\{$phpVar} is not an object, cannot fetch content()\" );\n" . " {$phpVar} = null;\n" . "}\n" . "else\n" . "{\n"; // Add the actual code $code .= " {$phpVar}Tmp = {$phpVar}->content();\n" . " unset( {$phpVar} );\n" . " {$phpVar} = {$phpVar}Tmp;\n" . " unset( {$phpVar}Tmp );\n}\n"; $php->addCodePiece($code); } else { if ($variableDataType == eZTemplate::TYPE_PHP_VARIABLE) { $knownTypes = array(); $phpVariableName = $variableDataItem[1]; $php->addCodePiece("\${$variableAssignmentName} = \${$phpVariableName};\n", array('spacing' => $spacing)); } else { if ($variableDataType == eZTemplate::TYPE_VARIABLE) { $knownTypes = array(); $namespace = $variableDataItem[1][0]; $namespaceScope = $variableDataItem[1][1]; $variableName = $variableDataItem[1][2]; $namespaceText = eZTemplateCompiler::generateMergeNamespaceCode($php, $tpl, $namespace, $namespaceScope, array('spacing' => $spacing), true); if (!is_string($namespaceText)) { $namespaceText = "\$namespace"; } $variableNameText = $php->thisVariableText($variableName, 0, 0, false); $code = "unset( \${$variableAssignmentName} );\n"; $code .= "\${$variableAssignmentName} = ( array_key_exists( {$namespaceText}, \$vars ) and array_key_exists( {$variableNameText}, \$vars[{$namespaceText}] ) ) ? \$vars[{$namespaceText}][{$variableNameText}] : null;\n"; $php->addCodePiece($code, array('spacing' => $spacing)); } else { if ($variableDataType == eZTemplate::TYPE_ATTRIBUTE) { $knownTypes = array(); $newParameters = $parameters; $newParameters['counter'] += 1; $tmpVariableAssignmentName = $newParameters['variable']; $tmpVariableAssignmentCounter = $newParameters['counter']; if ($tmpVariableAssignmentCounter > 0) { $tmpVariableAssignmentName .= $tmpVariableAssignmentCounter; } if (eZTemplateNodeTool::isStaticElement($variableDataItem[1])) { $attributeStaticValue = eZTemplateNodeTool::elementStaticValue($variableDataItem[1]); $attributeText = $php->thisVariableText($attributeStaticValue, 0, 0, false); } else { $newParameters['counter'] += 1; $tmpKnownTypes = array(); eZTemplateCompiler::generateVariableDataCode($php, $tpl, $variableDataItem[1], $tmpKnownTypes, $dataInspection, $persistence, $newParameters, $resourceData); $newVariableAssignmentName = $newParameters['variable']; $newVariableAssignmentCounter = $newParameters['counter']; if ($newVariableAssignmentCounter > 0) { $newVariableAssignmentName .= $newVariableAssignmentCounter; } $attributeText = "\${$newVariableAssignmentName}"; } $php->addCodePiece("\${$tmpVariableAssignmentName} = compiledFetchAttribute( \${$variableAssignmentName}, {$attributeText} );\n" . "unset( \${$variableAssignmentName} );\n" . "\${$variableAssignmentName} = \${$tmpVariableAssignmentName};\n", array('spacing' => $spacing)); // End if optimized node object is null/false. See also eZTemplateOptimizer::optimizeVariable() if ($optimizeNode && $index == 3) { $php->addCodePiece("}\n"); } } else { if ($variableDataType == eZTemplate::TYPE_OPERATOR) { $knownTypes = array(); $operatorParameters = $variableDataItem[1]; $operatorName = $operatorParameters[0]; $operatorParameters = array_splice($operatorParameters, 1); $operatorNameText = $php->thisVariableText($operatorName, 0, 0, false); $operatorParametersText = $php->thisVariableText($operatorParameters, 23, 0, false); $operatorHint = eZTemplateCompiler::operatorHint($tpl, $operatorName); if (isset($operatorHint['output']) and $operatorHint['output']) { if (isset($operatorHint['output-type'])) { $knownType = $operatorHint['output-type']; if (is_array($knownType)) { $knownTypes = array_merge($knownTypes, $knownType); } else { $knownTypes[] = $knownType; } $knownTypes = array_unique($knownTypes); } else { $knownTypes[] = 'static'; } } $php->addCodePiece("if (! isset( \${$variableAssignmentName} ) ) \${$variableAssignmentName} = NULL;\n", array('spacing' => $spacing)); $php->addCodePiece("while " . ($resourceData['use-comments'] ? "/*TC:" . __LINE__ . "*/" : "") . "( is_object( \${$variableAssignmentName} ) and method_exists( \${$variableAssignmentName}, 'templateValue' ) )\n" . " \${$variableAssignmentName} = \${$variableAssignmentName}" . "->templateValue();\n"); $php->addCodePiece("\$" . $variableAssignmentName . "Data = array( 'value' => \${$variableAssignmentName} );\n\$tpl->processOperator( {$operatorNameText},\n {$operatorParametersText},\n \$rootNamespace, \$currentNamespace, \$" . $variableAssignmentName . "Data, false, false );\n\${$variableAssignmentName} = \$" . $variableAssignmentName . "Data['value'];\nunset( \$" . $variableAssignmentName . "Data );\n", array('spacing' => $spacing)); } else { if ($variableDataType == eZTemplate::TYPE_VOID) { } else { if ($variableDataType == eZTemplate::TYPE_DYNAMIC_ARRAY) { $knownTypes = array_unique(array_merge($knownTypes, array('array'))); $code = '%output% = array( '; $matchMap = array('%input%', '%output%'); $replaceMap = array('$' . $variableAssignmentName, '$' . $variableAssignmentName); $unsetList = array(); $counter = 1; $paramCount = 0; $values = $variableDataItem[2]; $newParameters = $parameters; foreach ($values as $key => $value) { if ($paramCount != 0) { $code .= ', '; } ++$paramCount; $code .= '\'' . $key . '\' => '; if (eZTemplateNodeTool::isStaticElement($value)) { $code .= eZPHPCreator::variableText(eZTemplateNodeTool::elementStaticValue($value), 0, 0, false); continue; } $code .= '%' . $counter . '%'; $newParameters['counter'] += 1; $newVariableAssignmentName = $newParameters['variable']; $newVariableAssignmentCounter = $newParameters['counter']; if ($newVariableAssignmentCounter > 0) { $newVariableAssignmentName .= $newVariableAssignmentCounter; } $matchMap[] = '%' . $counter . '%'; $replaceMap[] = '$' . $newVariableAssignmentName; $unsetList[] = $newVariableAssignmentName; $tmpKnownTypes = array(); eZTemplateCompiler::generateVariableDataCode($php, $tpl, $value, $tmpKnownTypes, $dataInspection, $persistence, $newParameters, $resourceData); ++$counter; } $code .= ' );'; $code = str_replace($matchMap, $replaceMap, $code); $php->addCodePiece($code, array('spacing' => $spacing)); $php->addVariableUnsetList($unsetList, array('spacing' => $spacing)); } else { if ($variableDataType == eZTemplate::TYPE_INTERNAL_CODE_PIECE) { $code = $variableDataItem[1]; $values = false; $matchMap = array('%input%', '%output%'); $replaceMap = array('$' . $variableAssignmentName, '$' . $variableAssignmentName); $unsetList = array(); $counter = 1; if (isset($variableDataItem[3]) && is_array($variableDataItem[3])) { $newParameters = $parameters; $values = $variableDataItem[3]; foreach ($values as $value) { $newParameters['counter'] += 1; $newVariableAssignmentName = $newParameters['variable']; $newVariableAssignmentCounter = $newParameters['counter']; if ($newVariableAssignmentCounter > 0) { $newVariableAssignmentName .= $newVariableAssignmentCounter; } if (eZTemplateNodeTool::isStaticElement($value)) { $staticValue = eZTemplateNodeTool::elementStaticValue($value); $staticValueText = $php->thisVariableText($staticValue, 0, 0, false); if (preg_match("/%code{$counter}%/", $code)) { $matchMap[] = '%code' . $counter . '%'; $replaceMap[] = ''; } $matchMap[] = '%' . $counter . '%'; $replaceMap[] = $staticValueText; } else { $matchMap[] = '%' . $counter . '%'; $replaceMap[] = '$' . $newVariableAssignmentName; $unsetList[] = $newVariableAssignmentName; if (preg_match("/%code{$counter}%/", $code)) { $tmpPHP = new eZPHPCreator('', '', eZTemplateCompiler::TemplatePrefix()); $tmpKnownTypes = array(); eZTemplateCompiler::generateVariableDataCode($tmpPHP, $tpl, $value, $tmpKnownTypes, $dataInspection, $persistence, $newParameters, $resourceData); $newCode = $tmpPHP->fetch(false); if (count($tmpKnownTypes) == 0 or in_array('objectproxy', $tmpKnownTypes)) { $newCode .= "while " . ($resourceData['use-comments'] ? "/*TC:" . __LINE__ . "*/" : "") . "( is_object( \${$newVariableAssignmentName} ) and method_exists( \${$newVariableAssignmentName}, 'templateValue' ) )\n" . " \${$newVariableAssignmentName} = \${$newVariableAssignmentName}" . "->templateValue();\n"; } $matchMap[] = '%code' . $counter . '%'; $replaceMap[] = $newCode; } else { $tmpKnownTypes = array(); eZTemplateCompiler::generateVariableDataCode($php, $tpl, $value, $tmpKnownTypes, $dataInspection, $persistence, $newParameters, $resourceData); if (!$parameters['treat-value-as-non-object'] and (count($tmpKnownTypes) == 0 or in_array('objectproxy', $tmpKnownTypes))) { $php->addCodePiece("while " . ($resourceData['use-comments'] ? "/*TC:" . __LINE__ . "*/" : "") . "( is_object( \${$newVariableAssignmentName} ) and method_exists( \${$newVariableAssignmentName}, 'templateValue' ) )\n" . " \${$newVariableAssignmentName} = \${$newVariableAssignmentName}" . "->templateValue();\n", array('spacing' => $spacing)); } } } ++$counter; } } if (isset($variableDataItem[4]) && $variableDataItem[4] !== false) { $values = $variableDataItem[4]; for ($i = 0; $i < $values; $i++) { $newParameters['counter'] += 1; $newVariableAssignmentName = $newParameters['variable']; $newVariableAssignmentCounter = $newParameters['counter']; if ($newVariableAssignmentCounter > 0) { $newVariableAssignmentName .= $newVariableAssignmentCounter; } $matchMap[] = '%tmp' . ($i + 1) . '%'; $replaceMap[] = '$' . $newVariableAssignmentName; $unsetList[] = $newVariableAssignmentName; } } if (isset($variableDataItem[5]) and $variableDataItem[5]) { if (is_array($variableDataItem[5])) { $knownTypes = array_unique(array_merge($knownTypes, $variableDataItem[5])); } else { if (is_string($variableDataItem[5])) { $knownTypes = array_unique(array_merge($knownTypes, array($variableDataItem[5]))); } else { $knownTypes = array_unique(array_merge($knownTypes, array('static'))); } } } $code = str_replace($matchMap, $replaceMap, $code); $php->addCodePiece($code, array('spacing' => $spacing)); $php->addVariableUnsetList($unsetList, array('spacing' => $spacing)); } } } } } } } } } } } } } } // After the entire expression line is done we try to extract the actual value if proxies are used $php->addCodePiece("if (! isset( \${$variableAssignmentName} ) ) \${$variableAssignmentName} = NULL;\n"); $php->addCodePiece("while " . ($resourceData['use-comments'] ? "/*TC:" . __LINE__ . "*/" : "") . "( is_object( \${$variableAssignmentName} ) and method_exists( \${$variableAssignmentName}, 'templateValue' ) )\n" . " \${$variableAssignmentName} = \${$variableAssignmentName}" . "->templateValue();\n"); }
function fetchTransform( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters ) { $parameterTranslation = false; $constParameters = array(); if ( $operatorName == $this->Fetch ) { if ( !eZTemplateNodeTool::isConstantElement( $parameters[0] ) || !eZTemplateNodeTool::isConstantElement( $parameters[1] ) ) { return false; } $moduleName = eZTemplateNodeTool::elementConstantValue( $parameters[0] ); $functionName = eZTemplateNodeTool::elementConstantValue( $parameters[1] ); $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $moduleName ); if ( !$moduleFunctionInfo->isValid() ) { eZDebug::writeError( "Cannot execute module '$moduleName', no module found", __METHOD__ ); return array(); } $fetchParameters = array(); if ( isset( $parameters[2] ) ) $fetchParameters = $parameters[2]; } else if ( $operatorName == $this->FetchAlias ) { if ( !eZTemplateNodeTool::isConstantElement( $parameters[0] ) ) { return false; } $aliasFunctionName = eZTemplateNodeTool::elementConstantValue( $parameters[0] ); $aliasSettings = eZINI::instance( 'fetchalias.ini' ); if ( $aliasSettings->hasSection( $aliasFunctionName ) ) { $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $aliasSettings->variable( $aliasFunctionName, 'Module' ) ); if ( !$moduleFunctionInfo->isValid() ) { eZDebug::writeError( "Cannot execute function '$aliasFunctionName' in module '$moduleName', no valid data", __METHOD__ ); return array(); } $functionName = $aliasSettings->variable( $aliasFunctionName, 'FunctionName' ); $functionArray = array(); if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Parameter' ) ) { $parameterTranslation = $aliasSettings->variable( $aliasFunctionName, 'Parameter' ); } if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Constant' ) ) { $constantParameterArray = $aliasSettings->variable( $aliasFunctionName, 'Constant' ); if ( is_array( $constantParameterArray ) ) { foreach ( array_keys( $constantParameterArray ) as $constKey ) { if ( $moduleFunctionInfo->isParameterArray( $functionName, $constKey ) ) $constParameters[$constKey] = explode( ';', $constantParameterArray[$constKey] ); else $constParameters[$constKey] = $constantParameterArray[$constKey]; } } } } else { $placement = eZTemplateNodeTool::extractFunctionNodePlacement( $node ); $tpl->warning( 'fetch_alias', "Fetch alias '$aliasFunctionName' is not defined in fetchalias.ini", $placement ); return array(); } $fetchParameters = array(); if ( isset( $parameters[1] ) ) $fetchParameters = $parameters[1]; } else { return false; } $functionDefinition = $moduleFunctionInfo->preExecute( $functionName ); if ( $functionDefinition === false ) { return false; } $isDynamic = false; $isVariable = false; if ( eZTemplateNodeTool::isConstantElement( $fetchParameters ) ) { $staticParameters = eZTemplateNodeTool::elementConstantValue( $fetchParameters ); $functionKeys = array_keys( $staticParameters ); } else if ( eZTemplateNodeTool::isDynamicArrayElement( $fetchParameters ) ) { $isDynamic = true; $dynamicParameters = eZTemplateNodeTool::elementDynamicArray( $fetchParameters ); $functionKeys = eZTemplateNodeTool::elementDynamicArrayKeys( $fetchParameters ); } else if ( eZTemplateNodeTool::isVariableElement( $fetchParameters ) or eZTemplateNodeTool::isInternalCodePiece( $fetchParameters ) ) { $isVariable = true; } else { $functionKeys = array(); } $paramCount = 0; $values = array(); if ( $isVariable ) { $values[] = $fetchParameters; $parametersCode = 'array( '; foreach( $functionDefinition['parameters'] as $parameterDefinition ) { if ( $paramCount != 0 ) { $parametersCode .= ',' . "\n"; } ++$paramCount; $parameterName = $parameterDefinition['name']; if ( $parameterTranslation ) { if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) ) { $parameterName = $parameterTranslation[$parameterName]; } } $defaultValue = 'null'; if ( !$parameterDefinition['required'] ) $defaultValue = eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false ); $parametersSelection = '%1%[\'' . $parameterName . '\']'; $parametersCode .= '( isset( ' . $parametersSelection . ' ) ? ' . $parametersSelection . " : $defaultValue )"; } $parametersCode .= ' )'; } else { $parametersCode = 'array( '; if ( count( $functionDefinition['parameters'] ) ) { foreach( $functionDefinition['parameters'] as $parameterDefinition ) { if ( $paramCount != 0 ) { $parametersCode .= ',' . "\n"; } ++$paramCount; $parameterName = $parameterDefinition['name']; if ( $parameterTranslation ) { if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) ) { $parameterName = $parameterTranslation[$parameterName]; } } $parametersCode .= ' \'' . $parameterName . '\' => '; if ( in_array( $parameterName, $functionKeys ) ) { if ( $isDynamic ) { if ( eZTemplateNodeTool::isConstantElement( $dynamicParameters[$parameterName] ) ) { $parametersCode .= eZPHPCreator::variableText( eZTemplateNodeTool::elementConstantValue( $dynamicParameters[$parameterName] ), 0, 0, false ); } else { $values[] = $dynamicParameters[$parameterName]; $parametersCode .= '%' . count( $values ) . '%'; } } else { $parametersCode .= eZPHPCreator::variableText( $staticParameters[$parameterName], 0, 0, false ); } } else if( $constParameters && isset( $constParameters[$parameterName] ) ) { $parametersCode .= eZPHPCreator::variableText( $constParameters[$parameterName], 0, 0, false ); } else { if ( $parameterDefinition['required'] ) { return false; } else if ( isset( $parameterDefinition['default'] ) ) { $parametersCode .= eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false ); } else { $parametersCode .= 'null'; } } } } $parametersCode .= ' )'; } $code = '%output% = call_user_func_array( array( new ' . $functionDefinition['call_method']['class'] . '(), \'' . $functionDefinition['call_method']['method'] . '\' ),' . "\n" . ' ' . $parametersCode . ' );'; $code .= "\n"; $code .= '%output% = isset( %output%[\'result\'] ) ? %output%[\'result\'] : null;' . "\n"; return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) ); }
function resourceAcquisitionTransformation( $functionName, &$node, $rule, $inputData, $outputName, $namespaceValue, $templateRoot, $viewDir, $viewValue, $matchFileArray, $acquisitionSpacing, &$resourceData ) { $startRoot = '/' . $templateRoot . $viewDir; $viewFileMatchName = '/' . $templateRoot . '/' . $viewValue . '.tpl'; $startRootLength = strlen( $startRoot ); $matchList = array(); $viewFileMatch = null; foreach ( $matchFileArray as $matchFile ) { if ( !isset( $matchFile['template'] ) ) continue; $path = $matchFile['template']; if ( substr( $path, 0, $startRootLength ) == $startRoot and $path[$startRootLength] == '/' ) { $matchFile['match_part'] = substr( $path, $startRootLength + 1 ); $matchList[] = $matchFile; } if ( $path == $viewFileMatchName ) $viewFileMatch = $matchFile; } $designKeysName = 'dKeys'; $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( !isset( \$$designKeysName ) )\n" . "{\n" . " \$resH = \$tpl->resourceHandler( 'design' );\n" . " \$$designKeysName = \$resH->keys();\n" . "}", array( 'spacing' => $acquisitionSpacing ) ); if ( isset( $rule["attribute_keys"] ) ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( !isset( \$" . $designKeysName . "Stack ) )\n" . "{\n" . " \$" . $designKeysName . "Stack = array();\n" . "}\n" . "\$" . $designKeysName . "Stack[] = \$$designKeysName;", array( 'spacing' => $acquisitionSpacing ) ); foreach ( $rule["attribute_keys"] as $designKey => $attributeKeyArray ) { $attributeAccessData = array(); $attributeAccessData[] = eZTemplateNodeTool::createVariableElement( $outputName, $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE ); foreach ( $attributeKeyArray as $attributeKey ) { $attributeAccessData[] = eZTemplateNodeTool::createAttributeLookupElement( $attributeKey ); } $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $attributeAccessData, false, array( 'spacing' => 0 ), 'dKey' ); $designKeyText = eZPHPCreator::variableText( $designKey, 0, 0, false ); $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$" . $designKeysName . "[$designKeyText] = \$dKey;", array( 'spacing' => $acquisitionSpacing ) ); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'dKey' ); } } $attributeAccess = $rule["attribute_access"]; $hasAttributeAccess = false; if ( is_array( $attributeAccess ) ) { $hasAttributeAccess = count( $attributeAccess ) > 0; $attributeAccessCount = 0; foreach ( $attributeAccess as $attributeAccessEntries ) { $attributeAccessData = $inputData; $spacing = $acquisitionSpacing; if ( $attributeAccessCount > 1 ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . " if ( !\$resourceFound )\n{\n", array( 'spacing' => $acquisitionSpacing ) ); $spacing += 4; } else if ( $attributeAccessCount > 0 ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( !\$resourceFound )\n{\n", array( 'spacing' => $acquisitionSpacing ) ); $spacing += 4; } foreach ( $attributeAccessEntries as $attributeAccessName ) { // $attributeAccessData[] = eZTemplateNodeTool::createCodePieceNode( "" . ( $resourceData['use-comments'] ? ( "/*TC:" . __LINE__ . "*/" ) : "" ) . "" ); $attributeAccessData[] = eZTemplateNodeTool::createAttributeLookupElement( $attributeAccessName ); } $accessNodes = array(); $accessNodes[] = eZTemplateNodeTool::createVariableNode( false, $attributeAccessData, false, array( 'spacing' => $spacing ), 'attributeAccess' ); $acquisitionNodes = array(); $templateCounter = 0; $hasAcquisitionNodes = false; $matchLookupArray = array(); foreach ( $matchList as $matchItem ) { $tmpAcquisitionNodes = array(); $matchPart = $matchItem['match_part']; if ( preg_match( "/^(.+)\.tpl$/", $matchPart, $matches ) ) $matchPart = $matches[1]; $code = "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( \$attributeAccess == '$matchPart' )\n{\n"; if ( $templateCounter > 0 ) $code = "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "" . $code; $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => $spacing ) ); $defaultMatchSpacing = $spacing; $useArrayLookup = false; $addFileResource = true; if ( isset( $matchItem['custom_match'] ) ) { $customSpacing = $spacing + 4; $defaultMatchSpacing = $spacing + 4; $matchCount = 0; foreach ( $matchItem['custom_match'] as $customMatch ) { $matchConditionCount = count( $customMatch['conditions'] ); $code = ''; if ( $matchCount > 0 ) { $code = "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . ""; } if ( $matchConditionCount > 0 ) { if ( $matchCount > 0 ) $code .= " "; $code .= "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( "; } $ifLength = strlen( $code ); $conditionCount = 0; if ( isset( $customMatch['conditions'] ) ) { foreach ( $customMatch['conditions'] as $conditionName => $conditionValue ) { if ( $conditionCount > 0 ) $code .= " and\n" . str_repeat( ' ', $ifLength ); $conditionNameText = eZPHPCreator::variableText( $conditionName, 0 ); $conditionValueText = eZPHPCreator::variableText( $conditionValue, 0 ); $code .= "isset( \$" . $designKeysName . "[$conditionNameText] ) and "; if ( $conditionNameText == '"url_alias"' ) { $code .= "( strpos(\$" . $designKeysName . "[$conditionNameText], $conditionValueText ) === 0 )"; } else { $code .= "( is_array( \$" . $designKeysName . "[$conditionNameText] ) ? " . "in_array( $conditionValueText, \$" . $designKeysName . "[$conditionNameText] ) : " . "\$" . $designKeysName . "[$conditionNameText] == $conditionValueText )"; } ++$conditionCount; } } if ( $matchConditionCount > 0 ) { $code .= " )\n"; } if ( $matchConditionCount > 0 or $matchCount > 0 ) { $code .= "{"; } $matchFile = $customMatch['match_file']; $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => $customSpacing ) ); $hasAcquisitionNodes = true; // If $matchFile is an array we cannot create a transformation for this entry if ( is_array( $matchFile ) ) return false; $tmpAcquisitionNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '', $matchFile, $matchFile, eZTemplate::RESOURCE_FETCH, false, $node[4], array( 'spacing' => $customSpacing + 4 ), $rule['namespace'] ); if ( $matchConditionCount > 0 or $matchCount > 0 ) { $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $customSpacing ) ); } ++$matchCount; if ( $matchConditionCount == 0 ) { $addFileResource = false; break; } } if ( $addFileResource ) $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . " \n{", array( 'spacing' => $customSpacing ) ); } else { $matchFile = $matchItem['base_dir'] . $matchItem['template']; $matchLookupArray[$matchPart] = $matchFile; $useArrayLookup = true; } if ( !$useArrayLookup ) { if ( $addFileResource ) { $matchFile = $matchItem['base_dir'] . $matchItem['template']; $tmpAcquisitionNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '', $matchFile, $matchFile, eZTemplate::RESOURCE_FETCH, false, $node[4], array( 'spacing' => $defaultMatchSpacing + 4 ), $rule['namespace'] ); $hasAcquisitionNodes = true; if ( isset( $matchItem['custom_match'] ) ) $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $customSpacing ) ); } ++$templateCounter; $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $spacing ) ); $acquisitionNodes = array_merge( $acquisitionNodes, $tmpAcquisitionNodes ); } } if ( count( $matchLookupArray ) > 0 ) { $newNodes = array_merge( $newNodes, $accessNodes ); $accessNodes = array(); // If $matchFile is an array we cannot create a transformation for this entry if ( is_array( $matchLookupArray ) ) return false; $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '', $matchLookupArray, false, eZTemplate::RESOURCE_FETCH, false, $node[4], array( 'spacing' => $spacing ), $rule['namespace'], 'attributeAccess' ); if ( $hasAcquisitionNodes ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "\n{", array( 'spacing' => $spacing ) ); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(); } } if ( $hasAcquisitionNodes ) { $newNodes = array_merge( $newNodes, $accessNodes, $acquisitionNodes ); if ( $attributeAccessCount > 0 ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $acquisitionSpacing ) ); } ++$attributeAccessCount; } else if ( count( $matchLookupArray ) == 0 ) { $newNodes[] = eZTemplateNodeTool::createErrorNode( "Failed to load template", $functionName, eZTemplateNodeTool::extractFunctionNodePlacement( $node ), array( 'spacing' => $acquisitionSpacing ) ); } if ( count( $matchLookupArray ) > 0 and $hasAcquisitionNodes ) { $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(); $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $spacing ) ); } } } if ( $viewFileMatch !== null ) { $mainSpacing = 0; if ( $hasAttributeAccess ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "\n{\n", array( 'spacing' => $acquisitionSpacing ) ); $mainSpacing = 4; } $templateCounter = 0; $addFileResource = true; if ( isset( $viewFileMatch['custom_match'] ) ) { $spacing = $mainSpacing + 4; $matchCount = 0; foreach ( $viewFileMatch['custom_match'] as $customMatch ) { $matchConditionCount = count( $customMatch['conditions'] ); $code = ''; if ( $matchCount > 0 ) { $code = "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . ""; } if ( $matchConditionCount > 0 ) { if ( $matchCount > 0 ) $code .= " "; $code .= "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( "; $ifLength = strlen( $code ); $conditionCount = 0; if ( is_array( $customMatch['conditions'] ) ) { foreach ( $customMatch['conditions'] as $conditionName => $conditionValue ) { if ( $conditionCount > 0 ) $code .= " and\n" . str_repeat( ' ', $ifLength ); $conditionNameText = eZPHPCreator::variableText( $conditionName, 0 ); $conditionValueText = eZPHPCreator::variableText( $conditionValue, 0 ); $code .= "isset( \$" . $designKeysName . "[$conditionNameText] ) and "; if ( $conditionNameText == '"url_alias"' ) { $code .= "( strpos(\$" . $designKeysName . "[$conditionNameText], $conditionValueText ) === 0 )"; } else { $code .= "( is_array( \$" . $designKeysName . "[$conditionNameText] ) ? " . "in_array( $conditionValueText, \$" . $designKeysName . "[$conditionNameText] ) : " . "\$" . $designKeysName . "[$conditionNameText] == $conditionValueText )"; } ++$conditionCount; } } $code .= " )\n"; } if ( $matchConditionCount > 0 or $matchCount > 0 ) { $code .= "{"; } $matchFile = $customMatch['match_file']; $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => $acquisitionSpacing ) ); // If $matchFile is an array we cannot create a transformation for this entry if ( is_array( $matchFile ) ) return false; $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '', $matchFile, $matchFile, eZTemplate::RESOURCE_FETCH, false, $node[4], array( 'spacing' => $spacing ), $rule['namespace'] ); if ( $matchConditionCount > 0 or $matchCount > 0 ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $acquisitionSpacing ) ); } ++$matchCount; if ( $matchConditionCount == 0 ) { if ( $matchCount > 0 ) $addFileResource = false; break; } } if ( $addFileResource ) $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "\n{", array( 'spacing' => $acquisitionSpacing ) ); } if ( $addFileResource ) { $file = $viewFileMatch['base_dir'] . $viewFileMatch['template']; $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '', $file, $file, eZTemplate::RESOURCE_FETCH, false, $node[4], array( 'spacing' => $mainSpacing ), $rule['namespace'] ); } if ( isset( $viewFileMatch['custom_match'] ) and $addFileResource ) $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $acquisitionSpacing ) ); if ( $hasAttributeAccess ) $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\n", array( 'spacing' => $acquisitionSpacing ) ); } if ( isset( $rule["attribute_keys"] ) ) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$$designKeysName = array_pop( \$" . $designKeysName . "Stack );", array( 'spacing' => $acquisitionSpacing ) ); } return $newNodes; }
function operatorTransform($operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters) { if (!eZTemplateNodeTool::isConstantElement($parameters[1]) || count($parameters) > 2 && !eZTemplateNodeTool::isConstantElement($parameters[2])) { return false; } // We do not support non-static values for decimal_count, decimal_symbol and thousands_separator if (count($parameters) > 3 and !eZTemplateNodeTool::isConstantElement($parameters[3])) { return false; } if (count($parameters) > 4 and !eZTemplateNodeTool::isConstantElement($parameters[4])) { return false; } if (count($parameters) > 5 and !eZTemplateNodeTool::isConstantElement($parameters[5])) { return false; } $locale = eZLocale::instance(); $decimalCount = $locale->decimalCount(); $decimalSymbol = $locale->decimalSymbol(); $decimalThousandsSeparator = $locale->thousandsSeparator(); if (count($parameters) > 2) { $prefix = eZTemplateNodeTool::elementConstantValue($parameters[2]); } else { $prefix = 'auto'; } if (count($parameters) > 3) { $decimalCount = eZTemplateNodeTool::elementConstantValue($parameters[3]); } elseif ($prefix == 'none') { $decimalCount = 0; } if (count($parameters) > 4) { $decimalSymbol = eZTemplateNodeTool::elementConstantValue($parameters[4]); } if (count($parameters) > 5) { $decimalThousandsSeparator = eZTemplateNodeTool::elementConstantValue($parameters[5]); } $decimalSymbolText = eZPHPCreator::variableText($decimalSymbol, 0, 0, false); $decimalThousandsSeparatorText = eZPHPCreator::variableText($decimalThousandsSeparator, 0, 0, false); $unit = eZTemplateNodeTool::elementConstantValue($parameters[1]); $ini = eZINI::instance(); if ($prefix == "auto") { $prefixes = $ini->variableArray("UnitSettings", "BinaryUnits"); if (in_array($unit, $prefixes)) { $prefix = "binary"; } else { $prefix = "decimal"; } } $unit_ini = eZINI::instance("units.ini"); $use_si = $ini->variable("UnitSettings", "UseSIUnits") == "true"; $fake = $use_si ? "" : "Fake"; if ($unit_ini->hasVariable("Base", $unit)) { $base = $unit_ini->variable("Base", $unit); } $hasInput = false; $output = false; if (eZTemplateNodeTool::isConstantElement($parameters[0])) { $output = eZTemplateNodeTool::elementConstantValue($parameters[0]); $hasInput = true; } $prefix_var = ""; if ($prefix == "decimal") { $prefixes = $unit_ini->group("DecimalPrefixes"); $prefix_group = $unit_ini->group("DecimalPrefixes"); $prefixes = array(); foreach ($prefix_group as $prefix_item) { $prefixes[] = explode(";", $prefix_item); } usort($prefixes, "eZTemplateUnitCompareFactor"); $prefix_var = ""; if ($hasInput) { if ($output >= 0 and $output < 10) { $prefix_var = ''; } else { foreach ($prefixes as $prefix) { $val = pow(10, (int) $prefix[0]); if ($val <= $output) { $prefix_var = $prefix[1]; $output = number_format($output / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator); break; } } } } else { $values = array(); $values[] = $parameters[0]; $values[] = array(eZTemplateNodeTool::createArrayElement($prefixes)); $values[] = array(eZTemplateNodeTool::createStringElement($base)); $code = 'if ( %1% >= 0 and %1% < 10 )' . "\n" . '{' . "\n" . ' %tmp3% = \'\';' . "\n" . '}' . "\n" . 'else' . "\n" . '{' . "\n" . ' %tmp3% = "";' . "\n" . ' foreach ( %2% as %tmp1% )' . "\n" . ' {' . "\n" . ' %tmp2% = pow( 10, (int)%tmp1%[0] );' . "\n" . ' if ( %tmp2% <= %1% )' . "\n" . ' {' . "\n" . ' %tmp3% = %tmp1%[1];' . "\n" . ' %1% = number_format( %1% / %tmp2%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' );' . "\n" . ' break;' . "\n" . ' }' . "\n" . ' }' . "\n" . '}' . "\n" . '%output% = %1% . \' \' . %tmp3% . %3%;'; return array(eZTemplateNodeTool::createCodePieceElement($code, $values, false, 3)); } } else { if ($prefix == "binary") { $prefix_group = $unit_ini->group($fake . "BinaryPrefixes"); $prefixes = array(); foreach ($prefix_group as $prefix_item) { $prefixes[] = explode(";", $prefix_item); } usort($prefixes, "eZTemplateUnitCompareFactor"); $prefix_var = ""; if ($hasInput) { if ($output >= 0 and $output < 10) { $prefix_var = ''; } foreach ($prefixes as $prefix) { $val = pow(2, (int) $prefix[0]); if ($val <= $output) { $prefix_var = $prefix[1]; $output = number_format($output / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator); break; } } } else { $values = array(); $values[] = $parameters[0]; $values[] = array(eZTemplateNodeTool::createArrayElement($prefixes)); $values[] = array(eZTemplateNodeTool::createStringElement($base)); $code = 'if ( %1% >= 0 and %1% < 10 )' . "\n" . '{' . "\n" . ' %tmp3% = \'\';' . "\n" . '}' . "\n" . 'else' . "\n" . '{' . "\n" . ' %tmp3% = "";' . "\n" . ' foreach ( %2% as %tmp1% )' . "\n" . ' {' . "\n" . ' %tmp2% = pow( 2, (int)%tmp1%[0] );' . "\n" . ' if ( %tmp2% <= %1% )' . "\n" . ' {' . "\n" . ' %tmp3% = %tmp1%[1];' . "\n" . ' %1% = number_format( %1% / %tmp2%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' );' . "\n" . ' break;' . "\n" . ' }' . "\n" . ' }' . "\n" . '}' . "\n" . '%output% = %1% . \' \' . %tmp3% . %3%;'; return array(eZTemplateNodeTool::createCodePieceElement($code, $values, false, 3)); } } else { if ($unit_ini->hasVariable("BinaryPrefixes", $prefix)) { $prefix_base = 2; $prefix_var = $unit_ini->variableArray("BinaryPrefixes", $prefix); } else { if ($unit_ini->hasVariable("DecimalPrefixes", $prefix)) { $prefix_base = 10; $prefix_var = $unit_ini->variableArray("DecimalPrefixes", $prefix); } else { if ($prefix == "none") { $prefix_var = ''; if ($hasInput) { $output = number_format($output, $decimalCount, $decimalSymbol, $decimalThousandsSeparator); } else { $values = array(); $values[] = $parameters[0]; $values[] = array(eZTemplateNodeTool::createStringElement('')); $values[] = array(eZTemplateNodeTool::createStringElement($base)); $code = '%output% = number_format( %1%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' ) . \' \' . %2% . %3%;'; return array(eZTemplateNodeTool::createCodePieceElement($code, $values)); } } } } if (is_array($prefix_var)) { if ($hasInput) { $val = pow($prefix_base, (int) $prefix_var[0]); $output = number_format($output / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator); $prefix_var = $prefix_var[1]; } else { $values = array(); $values[] = $parameters[0]; $values[] = array(eZTemplateNodeTool::createNumericElement(pow($prefix_base, (int) $prefix_var[0]))); $values[] = array(eZTemplateNodeTool::createStringElement($prefix_var[1])); $values[] = array(eZTemplateNodeTool::createStringElement($base)); $code = '%output% = number_format( %1% / %2%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' ) . \' \' . %3% . %4%;'; return array(eZTemplateNodeTool::createCodePieceElement($code, $values)); } } } } if ($hasInput) { return array(eZTemplateNodeTool::createStringElement($output . ' ' . $prefix_var . $base)); } $values = array(); $values[] = $parameters[0]; $values[] = array(eZTemplateNodeTool::createStringElement($prefix_var)); $values[] = array(eZTemplateNodeTool::createStringElement($base)); $code = '%output% = %1% . \' \' . %2% . %3%;'; return array(eZTemplateNodeTool::createCodePieceElement($code, $values)); }
function templateNodeTransformation($functionName, &$node, $tpl, $parameters, $privateData) { if ($functionName == $this->BlockName or $functionName == $this->AppendBlockName) { if (!isset($parameters['variable'])) { return false; } $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE; if (isset($parameters['scope'])) { if (!eZTemplateNodeTool::isConstantElement($parameters['scope'])) { return false; } $scopeText = eZTemplateNodeTool::elementConstantValue($parameters['scope']); if ($scopeText == 'relative') { $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE; } else { if ($scopeText == 'root') { $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL; } else { if ($scopeText == 'global') { $scope = eZTemplate::NAMESPACE_SCOPE_GLOBAL; } } } } $name = ''; if (isset($parameters['name'])) { if (!eZTemplateNodeTool::isConstantElement($parameters['name'])) { return false; } $name = eZTemplateNodeTool::elementConstantValue($parameters['name']); } $variableName = eZTemplateNodeTool::elementConstantValue($parameters['variable']); $newNodes = array(); $children = eZTemplateNodeTool::extractFunctionNodeChildren($node); $newNodes[] = eZTemplateNodeTool::createOutputVariableIncreaseNode(); $newNodes = array_merge($newNodes, $children); $newNodes[] = eZTemplateNodeTool::createAssignFromOutputVariableNode('blockText'); if ($functionName == $this->AppendBlockName) { $data = array(eZTemplateNodeTool::createVariableElement($variableName, $name, $scope)); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $data, false, array(), 'blockData'); // This block checks whether the append-block variable is an array or not. // TODO: This is a temporary solution and should also check whether the template variable exists. // This new solution requires probably writing the createVariableElement and createVariableNode your self. $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( is_null ( \$blockData ) ) \$blockData = array();"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( is_array ( \$blockData ) ) \$blockData[] = \$blockText;"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("else eZDebug::writeError( \"Variable '{$variableName}' is already in use.\" );"); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'blockData', false, array(), array($name, $scope, $variableName), false, true, true); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode('blockData'); } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'blockText', false, array(), array($name, $scope, $variableName), false, true, true); } $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode('blockText'); $newNodes[] = eZTemplateNodeTool::createOutputVariableDecreaseNode(); return $newNodes; } else { if ($functionName == $this->OnceName) { $functionPlacement = eZTemplateNodeTool::extractFunctionNodePlacement($node); $key = $this->placementKey($functionPlacement); $newNodes = array(); if ($key !== false) { $keyText = eZPHPCreator::variableText($key, 0, 0, false); $placementText = eZPHPCreator::variableText($functionPlacement, 0, 0, false); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( !isset( \$GLOBALS['eZTemplateRunOnceKeys'][{$keyText}] ) )\n" . "{\n" . " \$GLOBALS['eZTemplateRunOnceKeys'][{$keyText}] = {$placementText};"); $children = eZTemplateNodeTool::extractFunctionNodeChildren($node); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(4); $newNodes = array_merge($newNodes, $children); $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(4); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}"); } return $newNodes; } } return false; }
static function variableText($value, $column = 0, $iteration = 0, $maxIterations = false) { // the last parameter will always be ignored $maxIterations = false; if ($value === true) { $text = 'true'; } else { if ($value === false) { $text = 'false'; } else { if ($value === null) { $text = 'null'; } else { if (is_string($value)) { $valueText = str_replace(array("\\", "\"", "\$", "\n"), array("\\\\", "\\\"", "\\\$", "\\n"), $value); $text = "\"{$valueText}\""; } else { if (is_numeric($value)) { $text = $value; } else { if (is_object($value)) { $text = ''; if (method_exists($value, 'serializedata')) { $serializeData = $value->serializeData(); $className = $serializeData['class_name']; $text = "new {$className}("; $column += strlen($text); $parameters = $serializeData['parameters']; $variables = $serializeData['variables']; $i = 0; foreach ($parameters as $parameter) { if ($i > 0) { $text .= ",\n" . str_repeat(' ', $column); } $variableName = $variables[$parameter]; $variableValue = $value->{$variableName}; $keyText = " "; $text .= $keyText . eZPHPCreator::variableText($variableValue, $column + strlen($keyText), $iteration + 1); ++$i; } if ($i > 0) { $text .= ' '; } $text .= ')'; } } else { if (is_array($value)) { $text = 'array('; $column += strlen($text); $valueKeys = array_keys($value); $isIndexed = true; for ($i = 0; $i < count($valueKeys); ++$i) { if ($i !== $valueKeys[$i]) { $isIndexed = false; break; } } $i = 0; foreach ($valueKeys as $key) { if ($i > 0) { $text .= ",\n" . str_repeat(' ', $column); } $element = $value[$key]; $keyText = ' '; if (!$isIndexed) { if (is_int($key)) { $keyText = $key; } else { $keyText = "\"" . str_replace(array("\\", "\"", "\n"), array("\\\\", "\\\"", "\\n"), $key) . "\""; } $keyText = " {$keyText} => "; } $text .= $keyText . eZPHPCreator::variableText($element, $column + strlen($keyText), $iteration + 1); ++$i; } if ($i > 0) { $text .= ' '; } $text .= ')'; } else { $text = 'null'; } } } } } } } return $text; }
function chooseTransformation( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters ) { $values = array(); $function = $operatorName; if ( ( count( $parameters ) < 2) ) { return false; } $tmpValues = false; $newElements = array(); if ( eZTemplateNodeTool::isConstantElement( $parameters[0] ) ) { $selected = eZTemplateNodeTool::elementConstantValue( $parameters[0] ); if ( $selected < 0 or $selected > ( count( $parameters ) - 1 ) ) { return false; } return $parameters[$selected + 1]; } else { $values[] = $parameters[0]; $array = $parameters; unset( $array[0] ); $count = count( $parameters ) - 1; $operatorNameText = eZPHPCreator::variableText( $operatorName ); if ( count( $parameters ) == ( 2 + 1 ) ) { $code = "%output% = %1% ? %3% : %2%;\n"; $values[] = $parameters[1]; $values[] = $parameters[2]; } else { $code = ( "if ( %1% < 0 and\n" . " %1% >= $count )\n" . "{\n" . " \$tpl->error( $operatorNameText, \"Index \" . %1% . \" out of range\" );\n" . " %output% = false;\n" . "}\n" ); $code .= "else switch ( %1% )\n{\n"; $valueNumber = 2; for ( $i = 0; $i < $count; ++$i ) { $parameterNumber = $i + 1; $code .= " case $i:"; if ( eZTemplateNodeTool::isConstantElement( $parameters[$parameterNumber] ) ) { $value = eZTemplateNodeTool::elementConstantValue( $parameters[$parameterNumber] ); $valueText = eZPHPCreator::variableText( $value, 0, 0, false ); $code .= " %output% = $valueText; break;\n"; } else { $code .= "\n {\n"; $code .= "%code$valueNumber%\n"; $code .= "%output% = %$valueNumber%;\n"; $code .= " } break;\n"; $values[] = $parameters[$parameterNumber]; ++$valueNumber; } } $code .= "}\n"; } } $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, eZTemplateNodeTool::extractVariableNodePlacement( $node ), false ); return $newElements; }
function templateNodeTransformation($functionName, &$node, $tpl, $parameters, $privateData) { $useLastValue = false; if (isset($parameters['last-value']) and !eZTemplateNodeTool::isConstantElement($parameters['last-value'])) { return false; } if (isset($parameters['name']) and !eZTemplateNodeTool::isConstantElement($parameters['name'])) { return false; } if (isset($parameters['var']) and !eZTemplateNodeTool::isConstantElement($parameters['var'])) { return false; } if (isset($parameters['reverse']) and !eZTemplateNodeTool::isConstantElement($parameters['reverse'])) { return false; } $varName = false; if (isset($parameters['var'])) { $varName = eZTemplateNodeTool::elementConstantValue($parameters['var']); } if (isset($parameters['last-value'])) { $useLastValue = (bool) eZTemplateNodeTool::elementConstantValue($parameters['last-value']); } if (!$varName) { $useLastValue = false; } $reverseLoop = false; if (isset($parameters['reverse'])) { $reverseLoop = eZTemplateNodeTool::elementConstantValue($parameters['reverse']); } $useLoop = isset($parameters['loop']); $allowLoop = true; $newNodes = array(); $maxText = "false"; $useMax = false; $maxPopText = false; if (isset($parameters['max'])) { if (eZTemplateNodeTool::isConstantElement($parameters['max'])) { $maxValue = eZTemplateNodeTool::elementConstantValue($parameters['max']); if ($maxValue > 0) { $maxText = eZPHPCreator::variableText($maxValue); $useMax = true; } else { return array(eZTemplateNodeTool::createTextNode('')); } } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['max'], eZTemplateNodeTool::extractFunctionNodePlacement($node), array(), 'max'); $maxText = "\$max"; $maxPopText = ", \$max"; $useMax = true; } } // Controls whether the 'if' statement with brackets is added $useShow = false; // Controls whether main nodes are handled, also controls delimiter and filters $useMain = true; // Controls wether else nodes are handled $useElse = false; $spacing = 0; if (isset($parameters['show'])) { if (eZTemplateNodeTool::isConstantElement($parameters['show'])) { $showValue = eZTemplateNodeTool::elementConstantValue($parameters['show']); if ($showValue) { $useMain = true; $useElse = false; $useShow = false; } else { $useMain = false; $useElse = true; $useShow = false; } $newNodes[] = eZTemplateNodeTool::createTextNode(''); } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['show'], eZTemplateNodeTool::extractFunctionNodePlacement($node), array(), 'show'); $spacing = 4; $useElse = true; $useShow = true; } } $children = eZTemplateNodeTool::extractFunctionNodeChildren($node); if ($useShow) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \$show )\n{\n"); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode($spacing); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode('show'); } if (isset($parameters['name']) and !$useLoop) { $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode($parameters['name']); } $mainNodes = eZTemplateNodeTool::extractNodes($children, array('match' => array('type' => 'before', 'matches' => array(array('match-keys' => array(0), 'match-with' => eZTemplate::NODE_FUNCTION), array('match-keys' => array(2), 'match-with' => 'section-else')), 'filter' => array(array(array('match-keys' => array(0), 'match-with' => eZTemplate::NODE_FUNCTION), array('match-keys' => array(2), 'match-with' => array('delimiter', 'section-exclude', 'section-include'))))))); $delimiterNodes = eZTemplateNodeTool::extractNodes($children, array('match' => array('type' => 'equal', 'matches' => array(array('match-keys' => array(0), 'match-with' => eZTemplate::NODE_FUNCTION), array('match-keys' => array(2), 'match-with' => 'delimiter'))))); $filterNodes = eZTemplateNodeTool::extractNodes($children, array('match' => array('type' => 'equal', 'matches' => array(array('match-keys' => array(0), 'match-with' => eZTemplate::NODE_FUNCTION), array('match-keys' => array(2), 'match-with' => array('section-exclude', 'section-include')))))); $delimiterNode = false; if (count($delimiterNodes) > 0) { $delimiterNode = $delimiterNodes[0]; } if ($useMain) { // Avoid transformation if the nodes will not be used, saves time $mainNodes = eZTemplateCompiler::processNodeTransformationNodes($tpl, $node, $mainNodes, $privateData); } if ($useLoop and $useMain) { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['loop'], eZTemplateNodeTool::extractFunctionNodePlacement($node), array(), 'loopItem'); $hasSequence = false; if (isset($parameters['sequence'])) { $sequenceParameter = $parameters['sequence']; $hasSequence = true; $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $sequenceParameter, eZTemplateNodeTool::extractFunctionNodePlacement($node), array(), 'sequence'); } if (isset($parameters['name'])) { $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode($parameters['name']); } $code = "if ( !isset( \$sectionStack ) )\n" . " \$sectionStack = array();\n"; $variableValuePushText = ''; $variableValuePopText = ''; if ($varName) { $code .= "\$variableValue = new eZTemplateSectionIterator();\n" . "\$lastVariableValue = false;\n"; $variableValuePushText = "&\$variableValue, "; $variableValuePopText = "\$variableValue, "; } $code .= "\$index = 0;\n" . "\$currentIndex = 1;\n"; $arrayCode = ''; $numericCode = ''; $stringCode = ''; $offsetText = '0'; if (isset($parameters['offset'])) { $offsetParameter = $parameters['offset']; if (eZTemplateNodeTool::isConstantElement($offsetParameter)) { $iterationValue = (int) eZTemplateNodeTool::elementConstantValue($offsetParameter); if ($iterationValue > 0) { $arrayCode = " \$loopKeys = array_splice( \$loopKeys, {$iterationValue} );\n"; } $offsetText = $iterationValue; } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $offsetParameter, eZTemplateNodeTool::extractFunctionNodePlacement($node), array(), 'offset'); $arrayCode = " if ( \$offset > 0 )\n" . " \$loopKeys = array_splice( \$loopKeys, \$offset );\n"; $offsetText = "\$offset"; } } // Initialization for array $code .= "if ( is_array( \$loopItem ) )\n{\n" . " \$loopKeys = array_keys( \$loopItem );\n"; if ($reverseLoop) { $code .= " \$loopKeys = array_reverse( \$loopKeys );\n"; } $code .= $arrayCode; $code .= " \$loopCount = count( \$loopKeys );\n"; $code .= "}\n"; // Initialization for numeric $code .= "else if ( is_numeric( \$loopItem ) )\n{\n" . " \$loopKeys = false;\n" . $numericCode . " if ( \$loopItem < 0 )\n" . " \$loopCountValue = -\$loopItem;\n" . " else\n" . " \$loopCountValue = \$loopItem;\n" . " \$loopCount = \$loopCountValue - {$offsetText};\n" . "}\n"; // Initialization for string $code .= "else if ( is_string( \$loopItem ) )\n{\n" . " \$loopKeys = false;\n" . $stringCode . " \$loopCount = strlen( \$loopItem ) - {$offsetText};\n" . "}\n"; // Fallback for no item $code .= "else\n{\n" . " \$loopKeys = false;\n" . " \$loopCount = 0;\n" . "}"; // Initialization end $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code); $code = "while ( \$index < \$loopCount )\n" . "{\n"; if ($useMax) { $code .= " if ( \$currentIndex > {$maxText} )\n" . " break;\n" . " unset( \$item );\n"; } // Iterator check for array $code .= " if ( is_array( \$loopItem ) )\n" . " {\n" . " \$loopKey = \$loopKeys[\$index];\n" . " unset( \$item );\n" . " \$item = \$loopItem[\$loopKey];\n" . " }\n"; // Iterator check for numeric $code .= " else if ( is_numeric( \$loopItem ) )\n" . " {\n" . " unset( \$item );\n"; if ($reverseLoop) { $code .= " \$item = \$loopCountValue - \$index - {$offsetText};\n"; } else { $code .= " \$item = \$index + {$offsetText} + 1;\n"; } $code .= " if ( \$loopItem < 0 )\n" . " \$item = -\$item;\n"; if ($reverseLoop) { $code .= " \$loopKey = \$loopCountValue - \$index - {$offsetText} - 1;\n"; } else { $code .= " \$loopKey = \$index + {$offsetText};\n"; } $code .= " }\n"; // Iterator check for string $code .= " else if ( is_string( \$loopItem ) )\n" . " {\n" . " unset( \$item );\n"; if ($reverseLoop) { $code .= " \$loopKey = \$loopCount - \$index - {$offsetText} + 1;\n"; } else { $code .= " \$loopKey = \$index + {$offsetText};\n"; } $code .= " \$item = \$loopItem[\$loopKey];\n" . " }\n"; // Iterator check end $code .= " unset( \$last );\n" . " \$last = false;\n"; $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code); $code = ''; if ($useLastValue) { $code .= " if ( \$currentIndex > 1 )\n" . " {\n" . " \$last = \$lastVariableValue;\n" . " \$variableValue = new eZTemplateSectionIterator();\n" . " }\n"; } if ($varName) { $code .= " \$variableValue->setIteratorValues( \$item, \$loopKey, \$currentIndex - 1, \$currentIndex, false, \$last );"; $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'variableValue', eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 4), array('', eZTemplate::NAMESPACE_SCOPE_RELATIVE, $varName), false, true, true); } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'loopKey', eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 4), array('', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'key'), false, true, true); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'item', eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 4), array('', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'item'), false, true, true); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\$currentIndexInc = \$currentIndex - 1;\n"); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'currentIndexInc', eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 4), array('', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'index'), false, true, true); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'currentIndex', eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 4), array('', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'number'), false, true, true); } $mainSpacing = 0; $hasFilter = false; if (count($filterNodes) > 0) { $newFilterNodes = array(); $matchValue = true; $hasDynamicFilter = false; foreach ($filterNodes as $filterNode) { $filterParameters = eZTemplateNodeTool::extractFunctionNodeParameters($filterNode); if (!isset($filterParameters['match'])) { continue; } $hasFilter = true; $filterParameterMatch = $filterParameters['match']; $filterParameterMatch = eZTemplateCompiler::processElementTransformationList($tpl, $filterNode, $filterParameterMatch, $privateData); if (eZTemplateNodeTool::isConstantElement($filterParameterMatch)) { $matchValue = eZTemplateNodeTool::elementConstantValue($filterParameterMatch); if (eZTemplateNodeTool::extractFunctionNodeName($filterNode) == 'section-exclude') { if ($matchValue) { $matchValue = false; } } else { if ($matchValue) { $matchValue = true; } } $newFilterNodes = array(); $hasDynamicFilter = false; } else { $newFilterNodes[] = eZTemplateNodeTool::createVariableNode(false, $filterParameterMatch, eZTemplateNodeTool::extractFunctionNodePlacement($filterNode), array('spacing' => 4), 'tmpMatchValue'); if (eZTemplateNodeTool::extractFunctionNodeName($filterNode) == 'section-exclude') { $newFilterNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \$tmpMatchValue )\n \$matchValue = false;", array('spacing' => 4)); } else { $newFilterNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \$tmpMatchValue )\n \$matchValue = true;", array('spacing' => 4)); } $hasDynamicFilter = true; } } if ($hasFilter) { $mainSpacing += 4; $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $matchValue, eZTemplateNodeTool::extractFunctionNodePlacement($filterNode), array('spacing' => 4), 'matchValue'); if ($hasDynamicFilter) { $newNodes = array_merge($newNodes, $newFilterNodes); } $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \$matchValue )\n{\n", array('spacing' => 4)); } } $sequencePopText = ''; if ($hasSequence) { $sequencePopText = ", \$sequence"; if ($varName) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( is_array( \$sequence ) )\n" . "{\n" . " \$sequenceValue = array_shift( \$sequence );\n" . " \$variableValue->setSequence( \$sequenceValue );\n" . " \$sequence[] = \$sequenceValue;\n" . " unset( \$sequenceValue );\n" . "}", array('spacing' => $mainSpacing + 4)); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'variableValue', eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 4), array('', eZTemplate::NAMESPACE_SCOPE_RELATIVE, $varName), false, true, true); } else { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( is_array( \$sequence ) )\n" . "{\n" . " \$sequenceValue = array_shift( \$sequence );\n", array('spacing' => $mainSpacing + 4)); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'sequenceValue', eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => $mainSpacing + 4), array('', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'sequence'), false, true, true); $newNodes[] = eZTemplateNodeTool::createCodePieceNode(" \$sequence[] = \$sequenceValue;\n" . " unset( \$sequenceValue );\n" . "}", array('spacing' => $mainSpacing + 4)); } } $code = "\$sectionStack[] = array( " . $variableValuePushText . "&\$loopItem, \$loopKeys, \$loopCount, \$currentIndex, \$index" . $sequencePopText . $maxPopText . " );\n" . "unset( \$loopItem, \$loopKeys );\n"; $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code, array('spacing' => $mainSpacing + 4)); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode($mainSpacing + 4); if ($delimiterNode) { $delimiterChildren = eZTemplateNodeTool::extractFunctionNodeChildren($delimiterNode); $delimiterParameters = eZTemplateNodeTool::extractFunctionNodeParameters($delimiterNode); $delimiterChildren = eZTemplateCompiler::processNodeTransformationNodes($tpl, $node, $delimiterChildren, $privateData); $delimiterModulo = false; $matchCode = false; $useModulo = true; if (isset($delimiterParameters['match'])) { $delimiterMatch = $delimiterParameters['match']; $delimiterMatch = eZTemplateCompiler::processElementTransformationList($tpl, $delimiterNode, $delimiterMatch, $privateData); if (eZTemplateNodeTool::isConstantElement($delimiterMatch)) { $moduloValue = eZTemplateNodeTool::elementConstantValue($delimiterMatch); $useModulo = false; } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $delimiterMatch, eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 0), 'matchValue'); $matchCode = " and \$matchValue"; } } else { if (isset($delimiterParameters['modulo'])) { $delimiterModulo = $delimiterParameters['modulo']; $delimiterModulo = eZTemplateCompiler::processElementTransformationList($tpl, $delimiterModulo, $delimiterModulo, $privateData); if (eZTemplateNodeTool::isConstantElement($delimiterModulo)) { $moduloValue = (int) eZTemplateNodeTool::elementConstantValue($delimiterModulo); $matchCode = " and ( ( \$currentIndex - 1 ) % {$moduloValue} ) == 0"; } else { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $delimiterModulo, eZTemplateNodeTool::extractFunctionNodePlacement($node), array('spacing' => 0), 'moduloValue'); $matchCode = " and ( ( \$currentIndex - 1 ) % \$moduloValue ) == 0"; } } } if ($useModulo) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \$currentIndex > 1{$matchCode} )\n{"); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(4); $newNodes = array_merge($newNodes, $delimiterChildren); $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(4); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}\n"); } } $newNodes = array_merge($newNodes, $mainNodes); $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode($mainSpacing + 4); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("list( " . $variableValuePopText . "\$loopItem, \$loopKeys, \$loopCount, \$currentIndex, \$index" . $sequencePopText . $maxPopText . " ) = array_pop( \$sectionStack );", array('spacing' => $mainSpacing + 4)); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("++\$currentIndex;\n", array('spacing' => $mainSpacing + 4)); if ($varName) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\$lastVariableValue = \$variableValue;", array('spacing' => $mainSpacing + 4)); } if ($hasFilter) { $newNodes[] = eZTemplateNodeTool::createCodePieceNode(" }"); $mainSpacing -= 4; } $newNodes[] = eZTemplateNodeTool::createCodePieceNode("++\$index;\n", array('spacing' => $mainSpacing + 4)); $code = "}\n" . "unset( \$loopKeys, \$loopCount, \$index, \$last, \$loopIndex, \$loopItem"; if ($hasSequence) { $code .= ", \$sequence"; } $code .= " );"; $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code); } else { if ($useMain) { $newNodes = array_merge($newNodes, $mainNodes); } } if (isset($parameters['name'])) { $newNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode(); } if ($useShow) { $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode($spacing); } if ($useElse) { $elseNodes = eZTemplateNodeTool::extractNodes($children, array('match' => array('type' => 'after', 'matches' => array(array('match-keys' => array(0), 'match-with' => eZTemplate::NODE_FUNCTION), array('match-keys' => array(2), 'match-with' => 'section-else'))))); $elseNodes = eZTemplateCompiler::processNodeTransformationNodes($tpl, $node, $elseNodes, $privateData); if (count($elseNodes) > 0) { if ($useShow) { // This is needed if a 'if ( $show )' was used earlier $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}\nelse\n{\n"); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode($spacing); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode('show'); } if (isset($parameters['name'])) { $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode($parameters['name']); } $newNodes = array_merge($newNodes, $elseNodes); if (isset($parameters['name'])) { $newNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode(); } if ($useShow) { // This is needed if a 'if ( $show )' was used earlier $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode($spacing); } } if ($useShow) { // This is needed if a 'if ( $show )' was used earlier $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}\n"); } } return $newNodes; }
function condTransform( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters ) { switch( $operatorName ) { case $this->CondName: { $paramCount = count( $parameters ); $clauseCount = floor( $paramCount / 2 ); $hasDefaultClause = ( $paramCount % 2 ) != 0; if ( $paramCount == 1 ) { return $parameters[0]; } $values = array(); $code = ''; $spacing = 0; $spacingCode = ''; for ( $i = 0; $i < $clauseCount; ++$i ) { $prevSpacingCode = $spacingCode; $spacingCode = str_repeat( " ", $spacing*4 ); if ( $i > 0 ) { $code .= $prevSpacingCode . "else\n" . $prevSpacingCode . "{\n"; } $values[] = $parameters[$i*2]; if ( $i > 0 ) { $code .= $spacingCode . "%code" . count( $values ) . "%\n"; } $code .= $spacingCode. 'if ( %' . count( $values ) . "% )\n" . $spacingCode . "{\n"; if ( !eZTemplateNodeTool::isConstantElement( $parameters[$i*2 + 1] ) ) { $values[] = $parameters[$i*2 + 1]; $code .= ( $spacingCode . " %code" . count( $values ) . "%\n" . $spacingCode . " %output% = %" . count( $values ) . "%;\n" ); } else { $code .= $spacingCode . ' %output% = ' . eZPHPCreator::variableText( eZTemplateNodeTool::elementConstantValue( $parameters[$i*2 + 1] ), 0, 0, false ) . ';' . "\n"; } $code .= $spacingCode . "}\n"; ++$spacing; } $bracketCount = $clauseCount - 1; if ( $hasDefaultClause ) { ++$bracketCount; $values[] = $parameters[$paramCount - 1]; if ( $clauseCount > 0 ) { $code .= $spacingCode . "else\n" . $spacingCode . "{\n" . $spacingCode . " %code" . count( $values ) . "%\n "; } $code .= $spacingCode . '%output% = %' . count( $values ) . "%;\n"; } for ( $clauseIndex = 0; $clauseIndex < $bracketCount; ++$clauseIndex ) { $spacingCode = str_repeat( " ", ( $bracketCount - $clauseIndex - 1 ) *4 ); $code .= $spacingCode . "}\n"; } return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) ); } break; case $this->FirstSetName: { $values = array(); $code = ''; $spacing = 0; $spacingCode = ''; $nestCount = 0; for( $i = 0; $i < count( $parameters ); ++$i ) { if ( $i != 0 ) { $code .= "$spacingCode}\n" . $spacingCode . "else\n$spacingCode{\n"; } $spacingCode = str_repeat( ' ', $spacing*4 ); ++$spacing; if ( eZTemplateNodeTool::isConstantElement( $parameters[$i] ) ) { $code .= "$spacingCode%output% = " . eZPHPCreator::variableText( eZTemplateNodeTool::elementConstantValue( $parameters[$i] ), 0, 0, false ) . ";\n"; break; } ++$nestCount; $values[] = $parameters[$i]; $code .= ( $spacingCode . "%code" . count( $values ) . "%\n" . $spacingCode . 'if ( isset( %' . count( $values ) . "% ) )\n" . $spacingCode . "{\n" . $spacingCode . " %output% = %" . count( $values ) . '%;' . "\n" ); } for ( $i = 0; $i < $nestCount; ++$i ) { $spacing = $nestCount - $i - 1; $spacingCode = str_repeat( ' ', $spacing*4 ); $code .= $spacingCode . "}\n"; } return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) ); } break; } }