Forums / Developer / Custom form action

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
[email protected] | 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

eZ debug

Timing: Jan 15 2025 04:36:25
Script start
Timing: Jan 15 2025 04:36:25
Module start 'content'
Timing: Jan 15 2025 04:36:27
Module end 'content'
Timing: Jan 15 2025 04:36:27
Script end

Main resources:

Total runtime2.0033 sec
Peak memory usage6,144.0000 KB
Database Queries194

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0095 685.3984214.8047
Module start 'content' 0.00951.6923 900.20311,912.4141
Module end 'content' 1.70180.3015 2,812.6172635.8125
Script end 2.0033  3,448.4297 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00770.3855210.0004
Check MTime0.00160.0820210.0001
Mysql Total
Database connection0.00050.024910.0005
Mysqli_queries1.884894.08311940.0097
Looping result0.00250.12481910.0000
Template Total1.963798.020.9818
Template load0.00480.241820.0024
Template processing1.958897.776220.9794
Template load and register function0.00130.066510.0013
states
state_id_array0.00160.081310.0016
state_identifier_array0.00450.225620.0023
Override
Cache load0.00440.2194330.0001
Sytem overhead
Fetch class attribute can translate value0.00200.101650.0004
Fetch class attribute name0.00200.097960.0003
XML
Image XML parsing0.00380.188650.0008
class_abstraction
Instantiating content class attribute0.00000.000560.0000
General
dbfile0.00570.2861270.0002
String conversion0.00000.000430.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 25
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs