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 ) );
    }
Пример #2
0
 static function elementDynamicArray($elements)
 {
     if (!eZTemplateNodeTool::isDynamicArrayElement($elements)) {
         return null;
     }
     return $elements[0][2];
 }