diff --git a/model/CliEdit.php b/model/CliEdit.php new file mode 100644 index 00000000..b5a91911 --- /dev/null +++ b/model/CliEdit.php @@ -0,0 +1,179 @@ +args)) { + $this->__interactive(); + } + + if (!empty($this->args[0])) { + # print_r($this->args); print_r($this->params); die; + # TODO: handle the various -* parameters (see help) + $this->__handle($this->args[0], $this->args[1]); + } + } + + /** + * Interactive mode + */ + private function __interactive() { + + $handler = new $this->handler_to_use($this->new); + + $form_fields = $handler->getStruct(); + $id_field = $handler->getId_field(); + + while(0==0) { # endlees loop - except if input is valid + $question = $form_fields[$id_field]['label'] . ":"; + if ( $form_fields[$id_field]['desc'] != '') { + $question .= "\n(" . $form_fields[$id_field]['desc'] . ')'; + } + + $values[$id_field] = $this->in($form_fields[$id_field]['label'] . ':'); + + if ($handler->init($values[$id_field])) { + break; + } else { + $this->err($handler->errormsg); + # always use a fresh handler to avoid problems with previous error messages + $handler = new $this->handler_to_use($this->new); + } + } + + # update $form_fields (needed for example to display the correct allowed quota) + # TODO: doesn't (always?) work - wrong time for the refresh? +# $handler->set(array()); + $form_fields = $handler->getStruct(); + + foreach($form_fields as $key => $field) { + + if ($field['editable'] && $field['display_in_form'] && $key != $id_field) { + + while(0==0) { # endlees loop - except if input is valid + $question = $field['label'] . ':'; + if ($field['desc'] != '') { + $question .= "\n(" . $field['desc'] . ')'; + } + + if ($field['type'] == 'bool') { + $values[$key] = $this->in($question, array ('y', 'n') ); + + if ($values[$key] == 'y') { + $values[$key] = 1; + } else { + $values[$key] = 0; + } + + } elseif ($field['type'] == 'enum') { + $optiontxt = array(); + $optionlist = array(); + + foreach ($field['options'] AS $optionkey => $optionval) { + // $this->in hates number 0 + $optionkey = $optionkey + 1; + $optiontxt[] = '['.$optionkey.'] - '.$optionval; + $optionlist[] = $optionkey; + } + + $question .= "\n" . join("\n", $optiontxt) . "\n"; + + $values[$key] = $this->in($question, $optionlist); + + $values[$key] = $field['options'][$values[$key]-1]; # convert int to option name + + } elseif ($field['type'] == 'txtl') { + $values[$key] = array(); + $nextval = $this->in($question); + while ($nextval != '') { + if ($nextval != '') { + $values[$key][] = $nextval; + } + $nextval = $this->in(""); + } + + } else { + $values[$key] = $this->in($question); + } + + if (is_null($values[$key]) ) { # TODO: insull() is probably obsoleted by change in Shell class +echo "*** value of $key is NULL - this should not happen! ***"; + } + + if ($values[$key] == '' && (!$this->new) ) { # edit mode + unset ($values[$key]); # empty input - don't change + } + + # always use a fresh handler to avoid problems with previous error messages + $handler = new $this->handler_to_use($this->new); + $handler->init($values[$id_field]); + + $handler->set($values); + + if ( isset($handler->errormsg[$key]) ) { # only check the errormessage for this field + $this->err($handler->errormsg[$key]); + } else { + break; + } + } # end while + + } # end if $field[editable] etc. + } # end foreach + + $this->__handle($values[$id_field], $values); + } + + /** + * (try to) store values + */ + private function __handle($id, $values) { + + $handler = new $this->handler_to_use($this->new); + if (!$handler->init($id)) { + $this->error("Error:",join("\n", $handler->errormsg)); + return; + } + + if (!$handler->set($values)) { + $this->error("Error:", join("\n", $handler->errormsg)); + } + + if (!$handler->store()) { + $this->error("Error:", join("\n", $handler->errormsg)); + } else { + $this->out(""); + $this->out($handler->infomsg); + $this->hr(); + } + return; + } + + /** + * Displays help contents + */ + public function help() { +# TODO: generate from $struct + $this->hr(); + $this->out("Usage: postfixadmin-cli user add
[] [-g]"); + $this->hr(); + $this->out('Commands:'); + $this->out("\n\tadd\n\t\tAdds mailbox in interactive mode."); + $this->out("\n\tadd
[] [-g] \n\t\tAdds mailbox for
with password of if -g with rand pw. in MB."); + $this->out(""); + $this->_stop(); + } + +} +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/scripts/shells/shell.php b/scripts/shells/shell.php index 859ae8b8..891663f5 100644 --- a/scripts/shells/shell.php +++ b/scripts/shells/shell.php @@ -207,7 +207,10 @@ if ( empty($this->params['q'] ) ) { $taskClass = Inflector::camelize($taskName.'Task'); $taskKey = Inflector::underscore($taskClass); - if (!class_exists($taskClass)) { + if ($taskName == 'Add') { + $taskClass = 'CliEdit'; + + } elseif (!class_exists($taskClass)) { foreach ($this->Dispatch->shellPaths as $path) { $taskPath = $path . 'tasks' . DS . $task.'.php'; if (file_exists($taskPath)) { @@ -223,7 +226,11 @@ if ( empty($this->params['q'] ) ) { } else { $this->{$taskName} = new $taskClass($this->Dispatch); } - + + if ($taskName == 'Add') { + $this->{$taskName}->handler_to_use = ucfirst($this->shell) . 'Handler'; + $this->{$taskName}->new = 1; + } if (!isset($this->{$taskName})) { $this->err("Task '".$taskName."' could not be loaded"); @@ -247,6 +254,7 @@ if ( empty($this->params['q'] ) ) { if (!$this->interactive) { return $default; } + if ($prompt != '') $this->out(""); $in = $this->Dispatch->getInput($prompt, $options, $default); if ($options && is_string($options)) { @@ -260,12 +268,12 @@ if ( empty($this->params['q'] ) ) { } if (is_array($options)) { while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) { + $this->err("Invalid input"); # TODO: make translateable $in = $this->Dispatch->getInput($prompt, $options, $default); } } - if ($in) { - return $in; - } + + return $in; } /** * Outputs to the stdout filehandle.