Module/view redirect while preserving POST variables

Author Message

Piotrek Karaś

Monday 18 February 2008 10:10:41 pm

Let's say we have a standard form with NewButton and 'content/action' action, which is supposed to initiate an object:

  <form method="post" action="/index.php/pl/content/action">
    <input name="ClassIdentifier" value="some_class" type="hidden">
    <input name="NodeID" value="64" type="hidden">
    <input name="ContentLanguageCode" value="pol-PL" type="hidden">
    <input class="button" name="NewButton" value="Create some object" type="submit">
  </form>

Now, before we actually perform this action, we would like to perform some other operations in order to determine whether 'content/action' action should actually be performed. So, I create my own module and view, for example 'check/new', and modify the above form:

  <form method="post" action="/index.php/pl/check/new">
    <input name="ClassIdentifier" value="some_class" type="hidden">
    <input name="NodeID" value="64" type="hidden">
    <input name="ContentLanguageCode" value="pol-PL" type="hidden">
    <input class="button" name="NewButton" value="Create some object" type="submit">
  </form>

Now, in my own module I process the variables, and decide that new object can be created. <b>How do I redirect to 'content/action' with all those hidden post vars preserved? Is it possible with eZ API?</b>. Standard $Module->redirectTo(...) methods do not seem to pass the post variables.

I'd be grateful for any hints!

--
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

Bruce Morrison

Monday 18 February 2008 11:47:53 pm

Hi Piotrek

Without knowing the details it sounds as if you might be better off with a before_publish workflow.

If that doesn't work for your situation you can try doing a module redirect via the eZModule class:
http://pubsvn.ez.no/doxygen/trunk/html/classeZModule.html#6ddeaa1f5e2cfac8f5b56533c51d53df though that might still use redirects.

You might be able to do something list this:

$module = eZModule::exists( 'content' );
$module->run('action');

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Piotrek Karaś

Tuesday 19 February 2008 1:17:18 am

Bruce,

<i>You might be able to do something list this:</i>

Seems like something worked, the action.php view file was indeed executed (I was able to check that and even POST variables are there). The problem is that it didn't perform what I expected, it is supposed to do what it would have normally done - assign a new node and proceed to the edit view. Unfortunately, that didn't happen ;(

Thanks,

PS. Not sure if before_publish is what I need plus hoped to avoid that and carry out the task in my own module/view.

--
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

Gaetano Giunta

Tuesday 19 February 2008 1:29:45 am

As a side note: did anybody ever try to make use of content/search (or advanced search) functionality as an object picker instead of content/browse for any action involving choosing a location/object, such as move or copy?
It would be a better way to select content when eg you have one folder with 100.000 objects inside (think large user bases)

Principal Consultant International Business
Member of the Community Project Board

Bruce Morrison

Tuesday 19 February 2008 1:41:11 am

Hi Piotrek

Try this

$module = eZModule::exists( 'content' );
$module->setCurrentAction('NewObjectAddNodeAssignment','action');
$module->run('action');

@Gaetano
Your are right, though probably should be it's own thread?

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Piotrek Karaś

Tuesday 19 February 2008 2:02:41 am

$module = eZModule::exists( 'content');
$module->setCurrentAction('NewObjectAddNodeAssignment','action');
$module->run('action');

Hehe, yes, I tried that in the meantime, after having looked through the action.php file. But that doesn't seem to change anything, NewObjectAddNodeAssignment seems the default setting, because whether I use that additional line or not, I get inside that if-statement. It must fail further...

Thanks anyway!

--
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

Bruce Morrison

Tuesday 19 February 2008 2:52:00 am

Hi Piotrek

Try storing the POST variables in the session then redirecting to content/action...from user/login.php

    
    // Save array of previous post variables in session variable
    $post = $http->attribute( 'post' );
    $lastPostVars = array();
    foreach ( array_keys( $post ) as $postKey )
    {
        if ( substr( $postKey, 0, 5 ) == 'Last_' )
            $lastPostVars[ substr( $postKey, 5, strlen( $postKey ) )] = $post[ $postKey ];
    }
    if ( count( $lastPostVars ) > 0 )
    {
        $postData = $lastPostVars;
        $http->setSessionVariable( 'LastPostVars', $lastPostVars );
    }

content/action.php merges these with it's own POST variables. I think the is the mechanism that allows object creation to continue after a login is required.

I'll leave it for now but I'm positive it's possible and not difficult ;)

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Piotrek Karaś

Tuesday 19 February 2008 10:57:27 am

Hey Bruce,

You were right, that wasn't that hard, eventually ;) Just like you wrote, "Last_" as session variables + standard redirection did the trick. Funny, I have recently reported a bug in those very lines in action.php ;)

Thanks for your help!

--
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

Piotrek Karaś

Tuesday 19 February 2008 11:48:22 am

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

Maxime Thomas

Friday 22 February 2008 6:11:13 am

Hi,

You can also write a module that will translate your post variables in module params. By this way, you can redirect as many time as you want saving all your post data in the url.

For example, you've got two input : val1 and val2. You can post on a specific module redirect/redirect and this one will convert 1 and 2 to your desired module mymodule/myfunction/val1/val2.

It's another point to avoid putting data in the session.

Hope it helps.

Maxime Thomas
maxime.thomas@wascou.org | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

Piotrek Karaś

Saturday 23 February 2008 5:17:31 am

Hello Maxime,

Yes, that's a good idea, even though I try to avoid putting most of POST/SESSION values in the URL, unless they are ids or identifiers. I also didn't make that clear enough, but it was a user-dedicated operation that I was after, so sessions served me just fine and secure enough.

Thanks anyways!

--
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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 19 2025 03:29:19
Script start
Timing: Jan 19 2025 03:29:19
Module start 'layout'
Timing: Jan 19 2025 03:29:19
Module start 'content'
Timing: Jan 19 2025 03:29:21
Module end 'content'
Timing: Jan 19 2025 03:29:21
Script end

Main resources:

Total runtime1.4904 sec
Peak memory usage4,096.0000 KB
Database Queries85

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0049 590.4219152.6406
Module start 'layout' 0.00490.0027 743.062539.4766
Module start 'content' 0.00761.4813 782.5391756.7578
Module end 'content' 1.48890.0014 1,539.296936.1250
Script end 1.4903  1,575.4219 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00350.2338160.0002
Check MTime0.00160.1066160.0001
Mysql Total
Database connection0.00080.056210.0008
Mysqli_queries1.397893.7906850.0164
Looping result0.00090.0598830.0000
Template Total1.463098.220.7315
Template load0.00240.160520.0012
Template processing1.460698.000420.7303
Template load and register function0.00010.007010.0001
states
state_id_array0.00080.050610.0008
state_identifier_array0.00140.091220.0007
Override
Cache load0.00220.1451760.0000
Sytem overhead
Fetch class attribute can translate value0.00090.061640.0002
Fetch class attribute name0.00140.0918150.0001
XML
Image XML parsing0.00210.137740.0005
class_abstraction
Instantiating content class attribute0.00000.0027220.0000
General
dbfile0.00140.0912350.0000
String conversion0.00000.000440.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
11content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
11content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
20content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
7content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
6content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 57
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs