diff --git a/model/CliView.php b/model/CliView.php new file mode 100644 index 00000000..31b8f963 --- /dev/null +++ b/model/CliView.php @@ -0,0 +1,113 @@ +args)) { + $this->__interactive(); + } + + if (!empty($this->args[0])) { + $this->__handle($this->args[0]); + } + } + + /** + * Interactive mode + */ + protected function __interactive() { + $module = preg_replace('/Handler$/', '', $this->handler_to_use); + $module = strtolower($module); + + $question = "Which $module do you want to view?"; + $address = $this->in($question); + + $this->__handle($address); + } + + /** + * actually view something + * + * @param string address to view + */ + protected function __handle($address) { + $handler = new $this->handler_to_use($this->new); + + if (!$handler->init($address)) { + $this->err($handler->errormsg); + return; + } + + if (!$handler->view()) { + $this->err($handler->errormsg); + return; + } + + $result = $handler->result(); + $struct = $handler->getStruct(); + + foreach(array_keys($struct) as $field) { + if (isset($struct[$field]) && empty($struct[$field]['label'])) { + # $struct[$field]['label'] = "--- $field ---"; + $struct[$field]['display_in_list'] = 0; + } + + if ($struct[$field]['display_in_list'] == 0) { + # do nothing + } else { + $value = $result[$field]; + + $func="_formatted_".$field; + if (method_exists($handler, $func) ) { + $value = $handler->{$func}($result); # call _formatted_$fieldname() + } + + + if ($struct[$field]['type'] == 'txtl') { + # $value = join("\n" . str_repeat(" ", 20 + 2), $value); # multiline, one item per line + $value = join(", ", $value); # one line, comma-separated + } elseif ($struct[$field]['type'] == 'bool') { + $value = Config::Lang($value ? 'YES' : 'NO'); + } + + $this->out(sprintf("%20s: %s", $struct[$field]['label'], $value)); + } + } + + } + + /** + * Display help contents + * + * @access public + */ + public function help() { + $module = preg_replace('/Handler$/', '', $this->handler_to_use); + $module = strtolower($module); + + $this->out( +"Usage: + + postfixadmin-cli $module view + + View $module in interactive mode. + +- or - + + postfixadmin-cli $module view
+ + View $module
in non-interactive mode. +"); + $this->_stop(); + } + +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/scripts/shells/alias.php b/scripts/shells/alias.php index e47c4f1e..bca603e8 100644 --- a/scripts/shells/alias.php +++ b/scripts/shells/alias.php @@ -51,90 +51,4 @@ class PostfixAdminAlias extends Shell { } -#TODO: implement -class ViewTask extends Shell { -/** - * Execution method always used for tasks - * - * @access public - */ - function execute() { - - if (empty($this->args)) { - $this->__interactive(); - } - - if (!empty($this->args[0])) { - $this->__handle($this->args[0]); - - } - } -/** - * Interactive - * - * @access private - */ - function __interactive() { - $question[] = "Which Alias do you want to view?"; - - $address = $this->in(join("\n", $question)); - - $this->__handle($address); - - - - } - /** - * Interactive - * - * @access private - */ - function __handle($address) { - - - $handler = new AliasHandler(); - $handler->init($address); - if ( ! $handler->view() ) { - $this->error("Error: Not Found", "The requested alias was not found!"); - } else { - $result = $handler->result; - - $this->out(sprintf("Entries for: %s\n", $address)); - $this->out("Goto: \t"); - foreach($result['goto'] AS $goto) { - $this->out("\t -> ".$goto); - } - if( $result['is_mailbox'] ) { - $this->out("A mailbox was set for this alias!\n"); - } - if( $result['goto_mailbox'] ) { - $this->out("The alias delivers to the mailbox!\n"); - } - if( $result['on_vacation'] ) { - $this->out("This alias is a vacation address!"); - } - $this->out("Active: " . $result['active']); - } - return; - - } -/** - * Displays help contents - * - * @access public - */ - function help() { - $this->out(""); - $this->hr(); - $this->out("Usage: postfixadmin-cli user view
"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tview\n\t\tView user. Select address in interactive mode."); - $this->out("\n\tview
\n\t\tView user with address
"); - $this->out(""); - $this->_stop(); - } - -} - /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/scripts/shells/domain.php b/scripts/shells/domain.php index 793d1cc9..e3f439b9 100644 --- a/scripts/shells/domain.php +++ b/scripts/shells/domain.php @@ -63,101 +63,4 @@ class PostfixAdminDomain extends Shell { } -class ViewTask extends Shell { -/** - * Execution method always used for tasks - * - * @access public - */ - function execute() { - - if (empty($this->args)) { - $this->__interactive(); - } - - if (!empty($this->args[0])) { - $output = $this->__handle($this->args[0]); - $this->out($output); - - } - } -/** - * Interactive - * - * @access private - */ - function __interactive() { - $question[] = "Which Domain do you want to view?"; - - $domain = $this->in(join("\n", $question)); - - $this->__handle($domain); - - - - } - /** - * Interactive - * - * @access private - */ - function __handle($domain) { - - - $handler = new DomainHandler(); - if (!$handler->init($domain)) { - $this->error("Error:",join("\n", $handler->errormsg)); - return; - } - - $status = $handler->view(); - if (!$status) { - $this->error("Error:",join("\n", $handler->errormsg)); - } else { - $result = $handler->result(); - $struct = $handler->getStruct(); - - # TODO: $totalfield should be in DomainHandler (in $struct or as separate array) - $totalfield = array( - 'aliases' => 'alias_count', - 'mailboxes' => 'mailbox_count', - 'quota' => 'total_quota', - ); - - foreach($struct as $key => $field) { - if ($field['display_in_list']) { - if (isset($totalfield[$key])) { - # TODO: format based on $field['type'] (also in the else section) - $this->out($field['label'] . ": \t" . $result[$totalfield[$key]] . " / " . $result[$key] ); - } else { - if (!in_array($key, $totalfield)) { # skip if we already displayed the field as part of "x/y" - $this->out($field['label'] . ": \t" . $result[$key]); - } - } - } - } - - return; - } - - } -/** - * Displays help contents - * - * @access public - */ - function help() { -# TODO: this is the DOMAIN shell... - $this->out(""); - $this->hr(); - $this->out("Usage: postfixadmin-cli user view
"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tview\n\t\tView user. Select address in interactive mode."); - $this->out("\n\tview
\n\t\tView user with address
"); - $this->out(""); - $this->_stop(); - } - -} /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/scripts/shells/mailbox.php b/scripts/shells/mailbox.php index 455bcd01..0cc431e5 100644 --- a/scripts/shells/mailbox.php +++ b/scripts/shells/mailbox.php @@ -178,84 +178,3 @@ class PasswordTask extends Shell { } } -class ViewTask extends Shell { -/** - * Execution method always used for tasks - * - * @access public - */ - function execute() { - - if (empty($this->args)) { - $this->__interactive(); - } - - if (!empty($this->args[0])) { - $output = $this->__handle($this->args[0]); - $this->out($output); - - } - } -/** - * Interactive - * - * @access private - */ - function __interactive() { - $question[] = "Which Address do you want to view?"; - - $address = $this->in(join("\n", $question)); - - $this->__handle($address); - - - - } - /** - * Interactive - * - * @access private - */ - function __handle($address) { - - - $handler = new MailboxHandler(); - if ( ! $handler->init($address)) { - $this->error("Not found!", "The mailbox you have searched could not be found."); - } - if ( ! $handler->view() ) { - $this->error("Not Found!", "The mailbox you have searched could not be found."); - } -# TODO: offer alternative output formats (based on parameter) -# TODO: whitespace fix - 8 lines below - $result = $handler->result; - $this->out(sprintf("Entries for: %s\n", $address)); - $this->out(""); - $this->out(sprintf("+%'-25s+%'-15s+%'-10s+%'-20s+%'-8s+%'-8s+%'-6s+",'','','','','','','')); - $this->out(sprintf('|%25s|%15s|%10s|%20s|%8s|%8s|%6s|', 'Address', 'Name', 'Quota', 'Dir', 'Created', 'Modified', 'Active')); - $this->out(sprintf("+%'-25s+%'-15s+%'-10s+%'-20s+%'-8s+%'-8s+%'-6s+",'','','','','','','')); - $result['maildir'] = '--- skipped ---'; # TODO: include in view() result - or don't (try to) display it - $this->out(sprintf('|%25s|%15s|%10s|%20s|%8s|%8s|%6s|', $result['username'], $result['name'], $result['quota'], $result['maildir'], $result['created'], $result['modified'], $result['active'])); - $this->out(sprintf("+%'-25s+%'-15s+%'-10s+%'-20s+%'-8s+%'-8s+%'-6s+",'','','','','','','')); - - return; - - } -/** - * Displays help contents - * - * @access public - */ - function help() { - $this->out(""); - $this->hr(); - $this->out("Usage: postfixadmin-cli mailbox view
"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tview\n\t\tView mailbox. Select address in interactive mode."); - $this->out("\n\tview
\n\t\tView mailbox with address
"); - $this->out(""); - $this->_stop(); - } - -} diff --git a/scripts/shells/shell.php b/scripts/shells/shell.php index 8bbfc714..3c7caaf7 100644 --- a/scripts/shells/shell.php +++ b/scripts/shells/shell.php @@ -211,6 +211,8 @@ if ( empty($this->params['q'] ) ) { $taskClass = 'CliEdit'; } elseif ($taskName == 'Delete') { $taskClass = 'CliDelete'; + } elseif ($taskName == 'View') { + $taskClass = 'CliView'; } # elseif (!class_exists($taskClass)) { @@ -232,7 +234,7 @@ if ( empty($this->params['q'] ) ) { } elseif ($taskName == 'Update') { $this->{$taskName}->handler_to_use = ucfirst($this->shell) . 'Handler'; $this->{$taskName}->new = 0; - } elseif ($taskName == 'Delete') { + } elseif ($taskName == 'Delete' || $taskName == 'View') { $this->{$taskName}->handler_to_use = ucfirst($this->shell) . 'Handler'; $this->{$taskName}->new = 0; }