<?php

function smarty_function_xcl_block( $params , &$smarty )
{
    $render = ! empty( $params['render'] ) ;
    $assign_name = @$params['item'] . @$params['assign'] ;

    $dirname = @$params['dirname'] . @$params['mydirname'] ;
    $show_func = empty( $params['class'] ) ? @$params['func'] : 'cl::'.$params['class'] ;
    $options = empty( $params['opt'] ) ? @$params['options'] : str_replace( ',' , '|' , @$params['opt'] ) ;

    $blockHandler =& xoops_gethandler('block');
    $criteria = new CriteriaCompo() ;
    $criteria->add( new Criteria( 'show_func' , addslashes( $show_func ) ) ) ;
    if( ! empty( $dirname ) ) $criteria->add( new Criteria( 'dirname' , addslashes( $dirname ) ) ) ;
    $blockObjects = $blockHandler->getObjectsDirectly($criteria);

    if( is_object( $blockObjects[0] ) ) {
        $blockObject = $blockObjects[0] ;
        $blockObject->set( 'options' , $options ) ;
        $blockProcedure = Legacy_Utils::createBlockProcedure( $blockObject );

        if( $blockProcedure->prepare() !== false ) {
            $blockProcedure->execute();
            $renderer =& $blockProcedure->getRenderTarget();
            if( empty( $assign_name ) ) {
                echo $renderer->getResult() ;
            } else if( $render ) {
                $smarty->assign( $assign_name , $renderer->getResult() ) ;
            } else {
                $smarty->assign( $assign_name , $renderer->getAttributes() ) ;
            }
        }
    } else {
        echo 'invalid "func" or "class" or "dirname" in <{xcl_block}>' ;
        return ;
    }
}