Author Topic

Piotrek Karaś

Tuesday 19 February 2008 11:48:22 am

Comment49108

It got tricky again when loggedin/anonymous user redirections came to play, but managed to shape it like this:

$http = eZHTTPTool::instance();

$currentUser = eZUser::instance();
if( !$currentUser->isLoggedIn() )
{
    $http->setSessionVariable( '$_POST_BeforeLogin', $http->attribute( 'post' ) );
    return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
}

// Merge post variables and variables that were used before login
if ( $http->hasSessionVariable( 'LastPostVars' ) )
{
    $post = $http->attribute( 'post' );
    $currentPostVarNames = array_keys( $post );
    foreach ( $http->sessionVariable( 'LastPostVars' ) as $var => $value )
    {
        if ( !in_array( $var, $currentPostVarNames ) )
        {
            $http->setPostVariable( $var, $value );
        }
    }
    $http->removeSessionVariable( 'LastPostVars' );
}


$hasValidCreateAction = false;
$canCreate = false;
if( $http->hasPostVariable( 'NewButton' ) )
{
    if( $http->hasPostVariable( 'ClassIdentifier' ) )
    {
        $hasValidCreateAction = true;
        $newObjectClassIdentifier = $http->postVariable( 'ClassIdentifier' );
        $newObjectClass = eZContentClass::fetchByIdentifier( $newObjectClassIdentifier );
        $newObjectClassID = $newObjectClass->ID;

        $canCreate = true;
        if( !eZUserCreateLimitTools::canCurrentUserCreate( $newObjectClassID ) )
        {
            $canCreate = false;
        }
    }
    elseif( $http->hasPostVariable( 'ClassID' ) )
    {
        $hasValidCreateAction = true;
        $newObjectClassID = $http->hasPostVariable( 'ClassID' );
        $newObjectClass = eZContentClass::fetch( $newObjectClassID );

        $canCreate = true;
        if( !eZUserCreateLimitTools::canCurrentUserCreate( $newObjectClassID ) )
        {
            $canCreate = false;
        }
    }
}

if( $hasValidCreateAction )
{
    if( $canCreate )
    {
        $post = $http->attribute( 'post' );
        $lastPostVars = array();
        foreach ( array_keys( $post ) as $postKey )
        {
            $lastPostVars[ $postKey ] = $post[ $postKey ];
        }
        if ( count( $lastPostVars ) > 0 )
        {
            $postData = $lastPostVars;
            $http->setSessionVariable( 'LastPostVars', $lastPostVars );
        }
        $Module->redirectTo( 'content/action' );
    }
    else
    {
        $Result['content'] = $tpl->fetch( "design:createlimit/cannot_create.tpl" );
    }
}
else
{
    $Result['content'] = $tpl->fetch( "design:createlimit/lost_info.tpl" );
}

;)

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu