Make create-domain.php shorter (164 -> 109 lines) ;-)

create-domain.php:
- rename "fField" form fields to just "field" to match the column names 
  in the database
- remove list of template and POST variables in the header - the code is
  self-documenting on this, one useless comment block less to maintain ;-)
- rename $default to $field - matches the usage better
- use $values[$key] instead $$key (this also avoids the need to fill
  $values before calling $handler->set)
- remove some validation that is already done in DomainHandler
- use $handler->set even if creating $handler results in an error to
  make error messages for all fields visible
- set $values to defaults at the end of the file if $error == 0 (and
  use a foreach loop) instead of doing it for GET at the beginning and
  again after successful POST
- remove some unused variables
- various other changes

edit-domain.php, templates/admin_edit-domain.tpl:
- rename "fField" form fields to just "field" to match the column names 
  in the database



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

@ -15,24 +15,6 @@
* File: create-domain.php
* Allows administrators to create new domains.
* Template File: admin_edit-domain.tpl
*
* Template Variables:
*
* tDomain
* tDescription
* tAliases
* tMailboxes
* tMaxquota
* tDefaultaliases
*
* Form POST \ GET Variables:
*
* fDomain
* fDescription
* fAliases
* fMailboxes
* fMaxquota
* fDefaultaliases
*/
require_once('common.php');
@ -40,123 +22,86 @@ require_once('common.php');
authentication_require_role('global-admin');
$error = 0;
$form_fields = array(
'fDomain' => array('type' => 'str', 'default' => null),
'fDescription' => array('type' => 'str', 'default' =>''),
'fAliases' => array('type' => 'int', 'default' => $CONF['aliases']),
'fMailboxes' => array('type' => 'int', 'default' => $CONF['mailboxes']),
'fMaxquota' => array('type' => 'int', 'default' => $CONF['maxquota']),
'fDomainquota' => array('type' => 'int', 'default' => $CONF['domain_quota_default']),
'fTransport' => array('type' => 'str', 'default' => $CONF['transport_default'], 'options' => $CONF['transport_options']),
'fDefaultaliases' => array('type' => 'bool', 'default' => '1', 'options' => array(1, 0)),
'fBackupmx' => array('type' => 'bool', 'default' => '0', 'options' => array(1, 0))
'domain' => array('type' => 'str', 'default' => null),
'description' => array('type' => 'str', 'default' =>''),
'aliases' => array('type' => 'int', 'default' => $CONF['aliases']),
'mailboxes' => array('type' => 'int', 'default' => $CONF['mailboxes']),
'maxquota' => array('type' => 'int', 'default' => $CONF['maxquota']),
'quota' => array('type' => 'int', 'default' => $CONF['domain_quota_default']),
'transport' => array('type' => 'str', 'default' => $CONF['transport_default'], 'options' => $CONF['transport_options']),
'default_aliases'=> array('type' => 'bool', 'default' => '1', 'options' => array(1, 0)),
'backupmx' => array('type' => 'bool', 'default' => '0', 'options' => array(1, 0))
);
$fDefaultaliases = "";
$tDefaultaliases = "";
# TODO: this foreach block should only be executed for POST
foreach($form_fields as $key => $default) {
if($default['type'] == 'bool' && $_SERVER['REQUEST_METHOD'] == "POST") {
$$key = escape_string(safepost($key, 0)); # isset for unchecked checkboxes is always false
foreach($form_fields as $key => $field) {
if($field['type'] == 'bool' && $_SERVER['REQUEST_METHOD'] == "POST") {
$values[$key] = safepost($key, 0); # isset for unchecked checkboxes is always false
}
elseif (isset($_POST[$key]) && (strlen($_POST[$key]) > 0)) {
$$key = escape_string($_POST[$key]);
$values[$key] = safepost($key);
}
else {
$$key = $default['default'];
}
if($default['type'] == 'int') {
$$key = intval($$key);
}
if($default['type'] == 'str') {
$$key = strip_tags($$key); /* should we even bother? */
$values[$key] = $field['default'];
}
if(isset($default['options'])) {
if(!in_array($$key, $default['options'])) {
# TODO: check via _inp_enum in *Handler
if(isset($field['options'])) {
if(!in_array($values[$key], $field['options'])) {
die("Invalid parameter given for $key");
}
}
}
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
/* default values as set above */
$tTransport = $fTransport;
$tAliases = $fAliases;
$tMaxquota = $fMaxquota;
$tDomainquota = $fDomainquota;
$tMailboxes = $fMailboxes;
$tDefaultaliases = $fDefaultaliases;
$tBackupmx = $fBackupmx;
}
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$tBackupmx = "";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$handler = new DomainHandler($fDomain, 1);
$handler = new DomainHandler($values['domain'], 1);
if (!$handler->result()) {
$error = 1;
$tDomain = $fDomain;
$tDescription = $fDescription;
$tAliases = $fAliases;
$tMailboxes = $fMailboxes;
if (isset ($_POST['fMaxquota'])) $tMaxquota = $fMaxquota;
if (isset ($_POST['fDomainquota'])) $tDomainquota = $fDomainquota;
if (isset ($_POST['fTransport'])) $tTransport = $fTransport;
if (isset ($_POST['fDefaultaliases'])) $tDefaultaliases = $fDefaultaliases;
if (isset ($_POST['fBackupmx'])) $tBackupmx = $fBackupmx;
$pAdminCreate_domain_domain_text_error = join("<br />", $handler->errormsg);
}
if ($error != 1)
{
$tAliases = $CONF['aliases'];
$tMailboxes = $CONF['mailboxes'];
$tMaxquota = $CONF['maxquota'];
$tDomainquota = $CONF['domain_quota_default'];
$values = array(
'description' => $fDescription,
'aliases' => $fAliases,
'mailboxes' => $fMailboxes,
'maxquota' => $fMaxquota,
'quota' => $fDomainquota,
'transport' => $fTransport,
'backupmx' => $fBackupmx,
'active' => 1, # hardcoded for now - TODO: change this ;-)
'default_aliases' => $fDefaultaliases,
);
if (!$handler->set($values)) {
$values['active'] = 1; # hardcoded for now - TODO: change this ;-)
if (!$handler->set($values)) {
$error = 1;
$pAdminCreate_domain_domain_text_error = join("<br />", $handler->errormsg);
}
if ($error != 1) {
if (!$handler->store()) {
$pAdminCreate_domain_domain_text_error = join("\n", $handler->errormsg);
} else {
if (!$handler->store()) {
$pAdminCreate_domain_domain_text_error = join("\n", $handler->errormsg);
} else {
flash_info($PALANG['pAdminCreate_domain_result_success'] . " ($fDomain)"); # TODO: use a sprintf string
if (count($handler->errormsg)) { # might happen if domain_postcreation fails
flash_error(join("<br />", $handler->errormsg));
}
flash_info($PALANG['pAdminCreate_domain_result_success'] . " (" . $values['domain'] . ")"); # TODO: use a sprintf string
if (count($handler->errormsg)) { # might happen if domain_postcreation fails
flash_error(join("<br />", $handler->errormsg));
}
}
}
}
if ($error != 1) {
$values = array();
foreach (array_keys($form_fields) as $key) {
$values[$key] = $form_fields[$key]['default'];
}
}
$smarty->assign ('mode', 'create');
$smarty->assign ('tDomain', $tDomain);
$smarty->assign ('pAdminCreate_domain_domain_text', $pAdminCreate_domain_domain_text, false);
$smarty->assign ('pAdminCreate_domain_domain_text_error', $pAdminCreate_domain_domain_text_error, false);
$smarty->assign ('tDescription', $tDescription, false);
$smarty->assign ('tAliases', $tAliases);
$smarty->assign ('tMailboxes', $tMailboxes);
$smarty->assign ('tDomainquota', $tDomainquota);
$smarty->assign ('tMaxquota', $tMaxquota,false); # TODO: why is sanitize disabled? Should be just integer...
$smarty->assign ('select_options', select_options ($CONF ['transport_options'], array ($tTransport)),false);
$smarty->assign ('tDefaultaliases', ($tDefaultaliases == '1') ? ' checked="checked"' : '');
$smarty->assign ('tBackupmx', ($tBackupmx == '1') ? ' checked="checked"' : '');
$smarty->assign ('tDomain', $values['domain']);
$smarty->assign ('tDescription', $values['description']);
$smarty->assign ('tAliases', $values['aliases']);
$smarty->assign ('tMailboxes', $values['mailboxes']);
$smarty->assign ('tDomainquota', $values['quota']);
$smarty->assign ('tMaxquota', $values['maxquota']);
$smarty->assign ('select_options', select_options ($form_fields['transport']['options'], array ($values['transport'])),false);
$smarty->assign ('tDefaultaliases', ($values['default_aliases'] == '1') ? ' checked="checked"' : '');
$smarty->assign ('tBackupmx', ($values['backupmx'] == '1') ? ' checked="checked"' : '');
$smarty->assign ('smarty_template', 'admin_edit-domain');
$smarty->display ('index.tpl');

@ -59,23 +59,23 @@ if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (isset ($_GET['domain'])) $domain = escape_string ($_GET['domain']);
$fDescription = safepost('fDescription');
$fAliases = (int) safepost('fAliases');
$fMailboxes = (int) safepost('fMailboxes');
$fMaxquota = (int) safepost('fMaxquota', 0);
$fDomainquota = (int) safepost('fDomainquota', $CONF['domain_quota_default']);
$fDescription = safepost('description');
$fAliases = (int) safepost('aliases');
$fMailboxes = (int) safepost('mailboxes');
$fMaxquota = (int) safepost('maxquota', 0);
$fQuota = (int) safepost('quota', $CONF['domain_quota_default']);
# TODO: check for / error out on values < -1
$fTransport = $CONF['transport_default'];
if($CONF['transport'] != 'NO' && isset ($_POST['fTransport'])) {
$fTransport = escape_string($_POST['fTransport']);
if($CONF['transport'] != 'NO' && isset ($_POST['transport'])) {
$fTransport = escape_string($_POST['transport']);
if(!in_array($fTransport, $CONF['transport_options'])) {
die("Invalid transport option given; check config.inc.php");
}
}
if (isset ($_POST['fBackupmx'])) $fBackupmx = (int) escape_string ($_POST['fBackupmx']);
if (isset ($_POST['fActive'])) $fActive = (int) escape_string ($_POST['fActive']);
if (isset ($_POST['backupmx'])) $fBackupmx = (int) escape_string ($_POST['backupmx']);
if (isset ($_POST['active'])) $fActive = (int) escape_string ($_POST['active']);
if ($fBackupmx == 1)
{
@ -100,7 +100,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST")
'aliases' => $fAliases,
'mailboxes' => $fMailboxes,
'maxquota' => $fMaxquota,
'quota' => $fDomainquota,
'quota' => $fQuota,
'backupmx' => $sqlBackupmx,
'active' => $sqlActive,
);

@ -16,7 +16,7 @@
{if $mode == 'edit'}
<em>{$domain}</em>
{else}
<input class="flat" type="text" name="fDomain" value="{$tDomain}" />
<input class="flat" type="text" name="domain" value="{$tDomain}" />
{/if}
</td>
<td>&nbsp;</td>
@ -24,25 +24,25 @@
</tr>
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_description}:</label></td>
<td><input class="flat" type="text" name="fDescription" value="{$tDescription}" /></td>
<td><input class="flat" type="text" name="description" value="{$tDescription}" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_aliases}:</label></td>
<td><input class="flat" type="text" name="fAliases" value="{$tAliases}" /></td>
<td><input class="flat" type="text" name="aliases" value="{$tAliases}" /></td>
<td>{$PALANG.pAdminEdit_domain_aliases_text}</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_mailboxes}:</label></td>
<td><input class="flat" type="text" name="fMailboxes" value="{$tMailboxes}" /></td>
<td><input class="flat" type="text" name="mailboxes" value="{$tMailboxes}" /></td>
<td>{$PALANG.pAdminEdit_domain_mailboxes_text}</td>
<td>&nbsp;</td>
</tr>
{if $CONF.domain_quota===YES}
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_quota}:</label></td>
<td><input class="flat" type="text" name="fDomainquota" value="{$tDomainquota}" /></td>
<td><input class="flat" type="text" name="quota" value="{$tDomainquota}" /></td>
<td>{$PALANG.pAdminEdit_domain_maxquota_text}</td>
<td>&nbsp;</td>
</tr>
@ -50,7 +50,7 @@
{if $CONF.quota===YES}
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_maxquota}:</label></td>
<td><input class="flat" type="text" name="fMaxquota" value="{$tMaxquota}" /></td>
<td><input class="flat" type="text" name="maxquota" value="{$tMaxquota}" /></td>
<td>{$PALANG.pAdminEdit_domain_maxquota_text}</td>
<td>&nbsp;</td>
</tr>
@ -58,7 +58,7 @@
{if $CONF.transport===YES}
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_transport}:</label></td>
<td><select class="flat" name="fTransport">{$select_options}</select></td>
<td><select class="flat" name="transport">{$select_options}</select></td>
<td>{$PALANG.pAdminEdit_domain_transport_text}</td>
<td>&nbsp;</td>
</tr>
@ -66,14 +66,14 @@
{if $mode == 'create'}
<tr>
<td class="label"><label>{$PALANG.pAdminCreate_domain_defaultaliases}:</label></td>
<td><input class="flat" type="checkbox" value='1' name="fDefaultaliases"{$tDefaultaliases}/></td>
<td><input class="flat" type="checkbox" value='1' name="default_aliases"{$tDefaultaliases}/></td>
<td>{$PALANG.pAdminCreate_domain_defaultaliases_text}</td>
<td>&nbsp;</td>
</tr>
{/if}
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_backupmx}:</label></td>
<td><input class="flat" type="checkbox" value='1' name="fBackupmx"{$tBackupmx}/></td>
<td><input class="flat" type="checkbox" value='1' name="backupmx"{$tBackupmx}/></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
@ -81,7 +81,7 @@
<!-- TODO: create should also offer the 'active' option -->
<tr>
<td class="label"><label>{$PALANG.pAdminEdit_domain_active}:</label></td>
<td><input class="flat" type="checkbox" value='1' name="fActive"{$tActive}/></td>
<td><input class="flat" type="checkbox" value='1' name="active"{$tActive}/></td>
<td colspan="2">&nbsp;</td>
</tr>
{/if}

Loading…
Cancel
Save