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
|