From db8c2df6bca566acd14ee1c95e0066d5fd66804e Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Mon, 9 Apr 2012 20:01:40 +0000 Subject: [PATCH] PFAHandler: - set(): call $this->_missing_$fieldname() if a field is not set on $new - new function set_default_value() - typically called from _missing_$fieldname to set the default from $struct AliasHandler: - set default values on $new: - on_vacation and active from $struct default - localpart and domain based on address scripts/shells/alias.php: - convert AddTask to *Handler syntax. It was broken before, see https://sourceforge.net/tracker/?func=detail&aid=3232719&group_id=191583&atid=937964 git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1364 a1433add-5e2c-0410-b055-b7f2511e0802 --- model/AliasHandler.php | 17 +++++++++++++++++ model/PFAHandler.php | 25 +++++++++++++++++++++++++ scripts/shells/alias.php | 8 ++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/model/AliasHandler.php b/model/AliasHandler.php index 9d78697a..3d80b598 100644 --- a/model/AliasHandler.php +++ b/model/AliasHandler.php @@ -286,6 +286,23 @@ class AliasHandler extends PFAHandler { } } + protected function _missing_on_vacation($field) { return $this->set_default_value($field); } + protected function _missing_active ($field) { return $this->set_default_value($field); } + + protected function _missing_localpart ($field) { + if (isset($this->RAWvalues['address'])) { + $parts = explode('@', $this->RAWvalues['address']); + if (count($parts) == 2) $this->RAWvalues['localpart'] = $parts[0]; + } + } + + protected function _missing_domain ($field) { + if (isset($this->RAWvalues['address'])) { + $parts = explode('@', $this->RAWvalues['address']); + if (count($parts) == 2) $this->RAWvalues['domain'] = $parts[1]; + } + } + /** * Returns the vacation alias for this user. diff --git a/model/PFAHandler.php b/model/PFAHandler.php index 856adae9..ff094631 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -98,6 +98,19 @@ class PFAHandler { $this->RAWvalues = $values; # allows comparison of two fields before the second field is checked # Warning: $this->RAWvalues contains unchecked input data - use it carefully! + if ($this->new) { + foreach($this->struct as $key=>$row) { + if ($row['editable'] && !isset($values[$key]) ) { + $func="_missing_".$key; # call $this->_missing_$fieldname() + if (method_exists($this, $func) ) { + $this->{$func}($key); # function can set $this->RAWvalues[$key] (or do nothing if it can't set a useful value) + } + } + } + $values = $this->RAWvalues; + } + + # base validation $this->values = array(); $this->values_valid = false; @@ -362,6 +375,18 @@ class PFAHandler { return false; } + /** + * set field to default value + * typically called from _missing_$fieldname() + * @param string $field - fieldname + */ + protected function set_default_value($field) { + if (isset($this->struct[$field]['default'])) { + $this->RAWvalues[$field] = $this->struct[$field]['default']; + } + } + + /************************************************************************** * functions for basic input validation */ diff --git a/scripts/shells/alias.php b/scripts/shells/alias.php index 46aed6bd..3c0bfea5 100644 --- a/scripts/shells/alias.php +++ b/scripts/shells/alias.php @@ -105,9 +105,13 @@ class AddTask extends Shell { $handler = new AliasHandler(1); $handler->init($address); - $return = $handler->add($goto); - if($return == 1) { + $values = array( + 'goto' => explode(',', $goto), + ); + if (!$handler->set($values)) { + $this->error("Error:", join("\n", $handler->errormsg)); + } elseif (!$handler->store()) { $this->error("Error:", join("\n", $handler->errormsg)); } else { $this->out("");