Custom form action

Author Message

Pål J Didriksen

Friday 08 July 2005 2:31:50 pm

I have made an extension module that handles custom forms. This enables me to put action buttons in templates, that can trigger off any desired action in the php-script. Hope this can be useful for someone else!

The extension is based on the original form funconality in eZPublish' kernel. It must be used with caution, as it takes arguments that are embedded in the html-source, which can easily be manipulated by a hacker.

In my project, however, I use the form only for administrative tasks, so the module is only available for users with administrator privileges.

<b>Files:</b>
1. extension/myextension/modules/mymodule/process.php

<?php

include_once( "kernel/common/template.php" );
include_once( "lib/ezutils/classes/ezhttptool.php" );

$Module =& $Params['Module'];

$ini =& eZINI::instance();
$isEnabled = $ini->variable( 'FormProcessSettings', 'Module' ) == 'enabled';
if ( !$isEnabled )
{
    return $Module->handleError( EZ_ERROR_KERNEL_MODULE_DISABLED, 'kernel',
                                 array( 'check' => array( 'view_checked' => false,
                                                          'module' => 'form' ) ) );
}

$tpl =& templateInit();

// Parse HTTP POST variables
$formProcessed = false;

$http =& eZHTTPTool::instance();
$postVariables =& $http->attribute( 'post' );

$action = $postVariables['action'];
// additional input variables from the form are available in $postVariables
// $myVariable = $postVariables['my_variable']

// $redirectURL - address to go to after processing is finished
$redirectURL = $postVariables['redirect'];

switch ($action)
{
    case "my_action1":
    {
        // Add code to execute for forms identified as "my_action1"
    }break;

    case "my_action2":
    {
        // Add code to execute for forms identified as "my_action2"
    }break;

    default :
    {
        // No action submitted
    }break;
}

$Module->redirectTo( $redirectURL );

?>

2. extension/myextension/modules/mymodule/module.php

<?php
$Module = array( "name" => "MyModule" );

$ViewList = array();
$ViewList["process"] = array(
    "script" => "process.php" );

?>

3. extension/myextension/settings/module.ini.append

[ModuleSettings]
ExtensionRepositories[]=mymodule

4. Template code
To use the extension, you need this code in your templates:


<form method="post" action={"/mymodule/process"|ezurl}>
    <input name="action" type="hidden" value="my_action1" />

    {* Redirect to current node *}
    <input name="redirect" type="hidden" value="{$node.url}" />

    Enter value:
    <input name="my_variable" type="text" />

    <input class="button" type="submit" name="MyButton" value="Execute my_action1" />
</form>

Now, remember to activate your extension in the admin interface, Settings -> Extensions.

Hope I didn't forget anything... Comments and suggestions are appreciated!

Pål J

Steve P

Saturday 09 July 2005 2:29:07 am

Thank you - this looks interesting & I'll have a play...

You are obviously pretty experienced so wonder if you can suggest a way for me to update my users from a script? I would like to update a field "payment_status" automatically on completion of a shop transaction ie to "paid_o5" or similar.

Best wishes

Steve

David Eriksson

Monday 11 July 2005 7:14:44 am

Have a look at workflows Steve. They enable you to automatically run a php script every time something happens, for example at a shop checkout.

/David

Maxime Thomas

Friday 03 February 2006 2:28:44 am

I've got some Kernel (22) Errors : Module is disabled.

To fix that, put into your override site.ini.append.php :

[FormProcessSettings]
Module=enabled

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

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