SF.net SVN: postfixadmin:[1572] trunk
Brought to you by:
christian_boltz,
gingerdog
From: <chr...@us...> - 2013-11-12 21:32:52
|
Revision: 1572 http://sourceforge.net/p/postfixadmin/code/1572 Author: christian_boltz Date: 2013-11-12 21:32:49 +0000 (Tue, 12 Nov 2013) Log Message: ----------- model/CliView.php: - new file for CLI 'view' - based on the ViewTask classes in scripts/shells/*.php - introduces usage of *Handler->_formatted_$field() to get "pretty" output for a field (not sure if this is the final solution, but it works ;-) scripts/shells/*.php - remove ViewTask, obsoleted by model/CliView.php scripts/shells/shell.php: - use CliView instead of ViewTask Modified Paths: -------------- trunk/scripts/shells/alias.php trunk/scripts/shells/domain.php trunk/scripts/shells/mailbox.php trunk/scripts/shells/shell.php Added Paths: ----------- trunk/model/CliView.php Added: trunk/model/CliView.php =================================================================== --- trunk/model/CliView.php (rev 0) +++ trunk/model/CliView.php 2013-11-12 21:32:49 UTC (rev 1572) @@ -0,0 +1,113 @@ +<?php +/** + * class to handle 'view' in Cli + */ + +class CliView extends Shell { + + /** + * Execution method always used for tasks + */ + public function execute() { + + if (empty($this->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 <address> + + View $module <address> in non-interactive mode. +"); + $this->_stop(); + } + +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ Property changes on: trunk/model/CliView.php ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Id \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/scripts/shells/alias.php =================================================================== --- trunk/scripts/shells/alias.php 2013-11-12 21:19:54 UTC (rev 1571) +++ trunk/scripts/shells/alias.php 2013-11-12 21:32:49 UTC (rev 1572) @@ -51,90 +51,4 @@ } -#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 <address>"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tview\n\t\tView user. Select address in interactive mode."); - $this->out("\n\tview <address>\n\t\tView user with address <address>"); - $this->out(""); - $this->_stop(); - } - -} - /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ Modified: trunk/scripts/shells/domain.php =================================================================== --- trunk/scripts/shells/domain.php 2013-11-12 21:19:54 UTC (rev 1571) +++ trunk/scripts/shells/domain.php 2013-11-12 21:32:49 UTC (rev 1572) @@ -63,101 +63,4 @@ } -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 <address>"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tview\n\t\tView user. Select address in interactive mode."); - $this->out("\n\tview <address>\n\t\tView user with address <address>"); - $this->out(""); - $this->_stop(); - } - -} /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ Modified: trunk/scripts/shells/mailbox.php =================================================================== --- trunk/scripts/shells/mailbox.php 2013-11-12 21:19:54 UTC (rev 1571) +++ trunk/scripts/shells/mailbox.php 2013-11-12 21:32:49 UTC (rev 1572) @@ -178,84 +178,3 @@ } } -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 <address>"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tview\n\t\tView mailbox. Select address in interactive mode."); - $this->out("\n\tview <address>\n\t\tView mailbox with address <address>"); - $this->out(""); - $this->_stop(); - } - -} Modified: trunk/scripts/shells/shell.php =================================================================== --- trunk/scripts/shells/shell.php 2013-11-12 21:19:54 UTC (rev 1571) +++ trunk/scripts/shells/shell.php 2013-11-12 21:32:49 UTC (rev 1572) @@ -211,6 +211,8 @@ $taskClass = 'CliEdit'; } elseif ($taskName == 'Delete') { $taskClass = 'CliDelete'; + } elseif ($taskName == 'View') { + $taskClass = 'CliView'; } # elseif (!class_exists($taskClass)) { @@ -232,7 +234,7 @@ } 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; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |