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



git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1572 a1433add-5e2c-0410-b055-b7f2511e0802
pull/2/head
Christian Boltz 11 years ago
parent f1831975c0
commit 04cd5c5aa4

@ -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: */

@ -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 <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: */

@ -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 <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: */

@ -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 <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();
}
}

@ -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;
}

Loading…
Cancel
Save