You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postfixadmin/edit.php

205 lines
6.7 KiB
PHTML

<?php
/**
* Postfix Admin
*
* LICENSE
* This source file is subject to the GPL license that is bundled with
* this package in the file LICENSE.TXT.
*
* Further details on the project are available at http://postfixadmin.sf.net
*
* @version $Id$
* @license GNU GPL v2 or later.
*
* File: edit.php
* This file implements the handling of edit forms.
* The form layout is retrieved from the *Handler classes, which also do
* the actual work of verifying and storing the values.
*
* GET parameters:
* table what to edit (*Handler)
* edit item to edit (if net given: a new item will be created)
* additional parameters will be accepted if specified in *Handler->webformConfig()[prefill] when creating a new item
*/
require_once('common.php');
$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;
$edit = safepost('edit', safeget('edit'));
$new = 0;
if ($edit == "") $new = 1;
$handler = new $handlerclass($new, $username);
$formconf = $handler->webformConfig();
authentication_require_role($formconf['required_role']);
if ($edit != '' || $formconf['early_init']) {
if (!$handler->init($edit)) {
flash_error($handler->errormsg);
header ("Location: " . $formconf['listview']);
exit;
}
}
$form_fields = $handler->getStruct();
$id_field = $handler->getId_field();
if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($edit == '') { # new - prefill fields from URL parameters if allowed in $formconf['prefill']
if ( isset($formconf['prefill']) ) {
foreach ($formconf['prefill'] as $field) {
$prefillvalue = safeget($field, safesession("prefill:$table:$field"));
if ($prefillvalue != '') {
$form_fields[$field]['default'] = $prefillvalue;
$handler->prefill($field, $prefillvalue);
}
}
}
$form_fields = $handler->getStruct(); # refresh $form_fields - a prefill field might have changed something
} else { # edit mode - read values from database
if (!$handler->view()) {
flash_error($handler->errormsg);
header ("Location: " . $formconf['listview']);
exit;
} else {
$values = $handler->result;
$values[$id_field] = $edit;
}
}
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach($form_fields as $key => $field) {
if ($field['editable'] && $field['display_in_form']) {
if($field['type'] == 'bool') {
$values[$key] = safepost($key, 0); # isset() for unchecked checkboxes is always false
AliasHandler now works with edit.php in many cases (TODO: catchall handling, mailbox and vacation aliases) AliasHandler.php - drop unused $username - set $domain_field - initStruct(): - use correct labels - set 'domain' field options to allowed domains - add (virtual) 'localpart' field - add comments for more virtual fields - add webformConfig() (note: modifies $struct on $new - otherwise we couldn't use the domain dropdown in the web interface) - add mergeId to merge localpart and domain to address (called by edit.php _before_ ->init) - add validate_new_id() (doesn't work for catchall yet) - add setmore() to - fill 'domain' based on 'address' - convert $values[goto] from array to comma-separated string - add read_from_db_postprocess to split goto to an array (TODO: handling of mailbox and vacation aliases) - add _field_goto() validator - add empty, commented dummy delete() that will replace the "old" delete function one day - make hasAliasRecord() private (only used internally) - mark all "old" functions as obsolete edit.php: - add handling of txtl field (convert textarea to array) - call $handler->mergeId if $id_field is editable, but not displayed in form (usecase: merge localpart + domain to address) editform.tpl: - add handling of txtl fields (textarea, filled by array) PFAHandler.php: - add setmore() hook function - runs at the end of set() AdminHandler.php: - add a comment for 'txtl' (array of one line texts, like alias goto) git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1311 a1433add-5e2c-0410-b055-b7f2511e0802
13 years ago
} elseif($field['type'] == 'txtl') {
$values[$key] = safepost($key);
$values[$key] = preg_replace ('/\\\r\\\n/', ',', $values[$key]);
$values[$key] = preg_replace ('/\r\n/', ',', $values[$key]);
$values[$key] = preg_replace ('/,[\s]+/i', ',', $values[$key]);
$values[$key] = preg_replace ('/[\s]+,/i', ',', $values[$key]);
$values[$key] = preg_replace ('/,,*/', ',', $values[$key]);
$values[$key] = preg_replace ('/,*$|^,*/', '', $values[$key]);
if ($values[$key] == '') {
$values[$key] = array();
} else {
$values[$key] = explode(",", $values[$key]);
}
} else {
$values[$key] = safepost($key);
}
}
}
if (isset($formconf['hardcoded_edit']) && $formconf['hardcoded_edit']) {
$values[$id_field] = $form_fields[$id_field]['default'];
} elseif ($edit != "") {
$values[$id_field] = $edit;
}
AliasHandler now works with edit.php in many cases (TODO: catchall handling, mailbox and vacation aliases) AliasHandler.php - drop unused $username - set $domain_field - initStruct(): - use correct labels - set 'domain' field options to allowed domains - add (virtual) 'localpart' field - add comments for more virtual fields - add webformConfig() (note: modifies $struct on $new - otherwise we couldn't use the domain dropdown in the web interface) - add mergeId to merge localpart and domain to address (called by edit.php _before_ ->init) - add validate_new_id() (doesn't work for catchall yet) - add setmore() to - fill 'domain' based on 'address' - convert $values[goto] from array to comma-separated string - add read_from_db_postprocess to split goto to an array (TODO: handling of mailbox and vacation aliases) - add _field_goto() validator - add empty, commented dummy delete() that will replace the "old" delete function one day - make hasAliasRecord() private (only used internally) - mark all "old" functions as obsolete edit.php: - add handling of txtl field (convert textarea to array) - call $handler->mergeId if $id_field is editable, but not displayed in form (usecase: merge localpart + domain to address) editform.tpl: - add handling of txtl fields (textarea, filled by array) PFAHandler.php: - add setmore() hook function - runs at the end of set() AdminHandler.php: - add a comment for 'txtl' (array of one line texts, like alias goto) git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1311 a1433add-5e2c-0410-b055-b7f2511e0802
13 years ago
if ($new && ($form_fields[$id_field]['display_in_form'] == 0) && ($form_fields[$id_field]['editable'] == 1) ) { # address split to localpart and domain?
$values[$id_field] = $handler->mergeId($values);
}
if (!$handler->init($values[$id_field])) {
$error = 1;
$errormsg = $handler->errormsg;
}
if (!$handler->set($values)) {
$error = 1;
$errormsg = $handler->errormsg;
}
$form_fields = $handler->getStruct(); # refresh $form_fields - set() might have changed something
if ($error != 1) {
if (!$handler->store()) {
$errormsg = $handler->errormsg;
} else {
flash_info($handler->infomsg);
if (count($handler->errormsg)) { # might happen if domain_postcreation fails
flash_error($handler->errormsg);
}
# remember prefill values for next usage of the form
if ( isset($formconf['prefill']) ) {
foreach ($formconf['prefill'] as $field) {
if (isset($values[$field])) {
$_SESSION["prefill:$table:$field"] = $values[$field];
}
}
}
if ($edit != "") {
header ("Location: " . $formconf['listview']);
exit;
} else {
header("Location: edit.php?table=$table");
exit;
}
}
}
}
if ($error != 1 && $new) { # no error and not in edit mode - reset fields to default for new item
$values = array();
foreach (array_keys($form_fields) as $key) {
$values[$key] = $form_fields[$key]['default'];
}
}
$errormsg = $handler->errormsg;
$fielderror = array();
foreach($form_fields as $key => $field) {
if($form_fields[$key]['display_in_form']) {
if (isset($errormsg[$key])) {
$fielderror[$key] = $errormsg[$key];
unset ($errormsg[$key]);
} else {
$fielderror[$key] = '';
}
$smarty->assign ("value_$key", $values[$key]);
}
}
if (count($errormsg)) flash_error($errormsg); # display the remaining error messages (not related to a field) with flash_error
if ($new) {
$smarty->assign ('mode', 'create');
$smarty->assign('formtitle', Config::lang($formconf['formtitle_create']));
$smarty->assign('submitbutton', Config::lang($formconf['create_button']));
} else {
$smarty->assign ('mode', 'edit');
$smarty->assign('formtitle', Config::lang($formconf['formtitle_edit']));
$smarty->assign('submitbutton', Config::lang('save'));
}
$smarty->assign ('struct', $form_fields);
$smarty->assign ('fielderror', $fielderror);
$smarty->assign ('table', $table);
$smarty->assign ('smarty_template', 'editform');
$smarty->display ('index.tpl');
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
?>