Several changes to make edit.php a generic edit form

edit.php:
- use ?table= parameter to decide what will be edited
- generate (and validate) Handler classname based on ?table=
- read handler-specific configuration from $handler->webformConfig()
  and use it at various places
- add option to run $handler->init() early. Useful for $new in case
  of AliasdomainHandler which might fail if all domains are already
  aliased.
- always redirect to edit.php?table=$table after adding an item to
  ensure correct initialization for next item

templates/editform.tpl:
- add hidden field "table"


git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1274 a1433add-5e2c-0410-b055-b7f2511e0802
pull/2/head
Christian Boltz 13 years ago
parent bfd4981433
commit 7ed5f7e471

@ -12,13 +12,20 @@
* @version $Id$
* @license GNU GPL v2 or later.
*
* File: create-domain.php
* Allows administrators to create or edit domains.
* File: edit.php
* This file implements the handling of edit forms.
*/
require_once('common.php');
authentication_require_role('global-admin');
$username = authentication_get_username(); # enforce login
$table = safepost('table', safeget('table'));
$handlerclass = ucfirst($table) . 'Handler';
if ( !preg_match('/^[a-z]+$/', $table) || !file_exists("model/$handlerclass.php")) { # validate $table
die ("Invalid table name given!");
}
$error = 0;
$mode = 'create';
@ -27,26 +34,30 @@ $edit = safepost('edit', safeget('edit'));
$new = 0;
if ($edit == "") $new = 1;
$listview = 'list-domain.php';
$handler = new $handlerclass($new, $username);
$formconf = $handler->webformConfig();
authentication_require_role($formconf['required_role']);
$handler = new DomainHandler($new);
$form_fields = $handler->getStruct();
$id_field = $handler->getId_field();
if ($edit != "") {
$mode = 'edit';
if ($edit != "" || $formconf['early_init']) {
if (!$handler->init($edit)) {
flash_error(join("<br />", $handler->errormsg));
header ("Location: $listview");
header ("Location: " . $formconf['listview']);
exit;
}
}
if ($edit != "") {
$mode = 'edit';
if ($_SERVER['REQUEST_METHOD'] == "GET") { # read values from database
if (!$handler->view()) {
flash_error(join("<br />", $handler->errormsg));
header ("Location: $listview");
header ("Location: " . $formconf['listview']);
exit;
} else {
$values = $handler->return;
@ -85,7 +96,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (!$handler->store()) {
$errormsg = $handler->errormsg;
} else {
flash_info($PALANG['pAdminCreate_domain_result_success'] . " (" . $values[$id_field] . ")");
flash_info(Lang::read($formconf['successmessage']) . " (" . $values[$id_field] . ")");
# TODO: - use a sprintf string
# TODO: - get the success message from DomainHandler
# TODO: - use a different success message for create and edit
@ -95,7 +106,10 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
}
if ($edit != "") {
header ("Location: $listview");
header ("Location: " . $formconf['listview']);
exit;
} else {
header("Location: edit.php?table=$table");
exit;
}
}
@ -131,17 +145,17 @@ foreach($errormsg as $msg) { # output the remaining error messages (not related
}
if ($mode == 'edit') {
$smarty->assign('formtitle', Lang::read('pAdminEdit_domain_welcome'));
$smarty->assign('formtitle', Lang::read($formconf['formtitle_edit']));
$smarty->assign('submitbutton', Lang::read('save'));
} else {
$smarty->assign('formtitle', Lang::read('pAdminCreate_domain_welcome'));
$smarty->assign('submitbutton', Lang::read('pAdminCreate_domain_button'));
$smarty->assign('formtitle', Lang::read($formconf['formtitle_create']));
$smarty->assign('submitbutton', Lang::read($formconf['create_button']));
}
$smarty->assign ('struct', $form_fields);
$smarty->assign ('fielderror', $fielderror);
$smarty->assign ('mode', $mode);
$smarty->assign ('table', 'domain');
$smarty->assign ('table', $table);
$smarty->assign ('smarty_template', 'editform');
$smarty->display ('index.tpl');

@ -1,5 +1,6 @@
<div id="edit_form">
<form name="edit_{$table}" method="post" action="">
<input class="flat" type="hidden" name="table" value="{$table}" />
<table>
<tr>

Loading…
Cancel
Save