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.

eZ debug

Timing: Jan 19 2025 00:10:11
Script start
Timing: Jan 19 2025 00:10:11
Module start 'layout'
Timing: Jan 19 2025 00:10:11
Module start 'content'
Timing: Jan 19 2025 00:10:12
Module end 'content'
Timing: Jan 19 2025 00:10:12
Script end

Main resources:

Total runtime1.0077 sec
Peak memory usage4,096.0000 KB
Database Queries64

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0062 588.9141152.6094
Module start 'layout' 0.00620.0039 741.523439.4297
Module start 'content' 0.01010.9961 780.9531638.2734
Module end 'content' 1.00620.0014 1,419.226616.1875
Script end 1.0076  1,435.4141 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00350.3512160.0002
Check MTime0.00140.1381160.0001
Mysql Total
Database connection0.00100.097310.0010
Mysqli_queries0.944893.7577640.0148
Looping result0.00100.1019620.0000
Template Total0.963795.620.4819
Template load0.00240.239020.0012
Template processing0.961395.399920.4807
Template load and register function0.00010.010810.0001
states
state_id_array0.00310.304110.0031
state_identifier_array0.00100.101520.0005
Override
Cache load0.00210.2048330.0001
Sytem overhead
Fetch class attribute can translate value0.00110.112040.0003
Fetch class attribute name0.00110.110060.0002
XML
Image XML parsing0.00160.156740.0004
class_abstraction
Instantiating content class attribute0.00000.000960.0000
General
dbfile0.00110.1128220.0001
String conversion0.00000.001340.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
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
10content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 25
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs