1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
<?php
# $Id$
/**
* class to handle add and edit in Cli
*
* extends the "Shell" class
*/
class CliEdit extends Shell
{
public $handler_to_use = "";
public $new = 0;
/**
* Execution method always used for tasks
*/
public function execute()
{
if (empty($this->args)) {
return $this->__interactive();
} else {
return $this->__handle_params();
}
}
/**
* non-interactive mode
* read, check and handle all --* parameters
* The list of allowed params is based on $handler->struct
*/
private function __handle_params()
{
$handler = new $this->handler_to_use($this->new);
$form_fields = $handler->getStruct();
$id_field = $handler->getId_field();
$values = array();
$param_error = 0;
foreach ($this->params as $key => $val) {
$key = preg_replace('/^-/', '', $key); # allow --param, not only -param
$key = str_replace('-', '_', $key); # allow --foo-bar even if field is named foo_bar
if (isset($form_fields[$key]) && $form_fields[$key]['editable'] && $form_fields[$key]['display_in_form'] && $key != $id_field) {
if ($form_fields[$key]['type'] == 'txtl') {
$values[$key] = explode(',', $val);
} elseif ($form_fields[$key]['type'] == 'bool') {
if (strtolower($val) == 'y') {
$val = 1;
} # convert y to 1
elseif (strtolower($val) == 'n') {
$val = 0;
} # convert n to 0
$values[$key] = $val; # don't modify any other value - *Handler will complain if it's invalid ;-)
} else {
$values[$key] = $val;
}
} elseif ($key == 'webroot') {
# always set, ignore
} else { # not editable, unknown field etc.
$param_error = 1;
$this->err("invalid parameter --$key => $val");
return 1;
}
}
return $this->__handle($this->args[0], $values);
}
/**
* Interactive mode
*/
private function __interactive()
{
$handler = new $this->handler_to_use($this->new);
$form_fields = $handler->getStruct();
$id_field = $handler->getId_field();
$values = array($id_field => '');
while ($form_fields[$id_field]['editable'] != 0) { # endlees loop - except if input is valid or id_field is not editable (like auto_increment)
$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 (true) { # 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";
$selected = (int) $this->in($question, $optionlist);
$values[$key] = $field['options'][$selected - 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
$this->err("*** value of $key is NULL - this should not happen! ***");
return 1;
}
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]);
return 1;
} else {
break;
}
} # end while
} # end if $field[editable] etc.
} # end foreach
return $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->err($handler->errormsg);
return 1;
}
if (!$handler->set($values)) {
$this->err($handler->errormsg);
return 1;
}
if (!$handler->save()) {
$this->err($handler->errormsg);
return 1;
}
$this->out("");
$this->out($handler->infomsg);
$this->hr();
return 0;
}
/**
* Displays help contents
*/
public function help()
{
if ($this->new) {
$cmd = 'add';
$cmdtext = 'Adds';
} else {
$cmd = 'update';
$cmdtext = 'Changes';
}
$module = preg_replace('/Handler$/', '', $this->handler_to_use);
$module = strtolower($module);
$this->out(
"Usage:
postfixadmin-cli $module $cmd
$cmdtext $module in interactive mode.
- or -
postfixadmin-cli $module $cmd <address> --option value --option2 value [...]
$cmdtext $module in non-interactive mode.
Available options are:
"
);
$handler = new $this->handler_to_use($this->new);
$form_fields = $handler->getStruct();
$id_field = $handler->getId_field();
foreach ($form_fields as $key => $field) {
if ($field['editable'] && $field['display_in_form'] && $key != $id_field) {
$optkey = str_replace('_', '-', $key);
$this->out(" --$optkey");
$this->out(" " . $field['label']);
if ($field['desc']) {
$this->out(" " . $field['desc']);
}
$this->out("");
}
}
$this->_stop(1);
}
}
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|