You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CliView.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. # $Id: CliView.php 1709 2014-11-01 18:05:39Z christian_boltz $
  3. /**
  4. * class to handle 'view' in Cli
  5. */
  6. class CliView extends Shell {
  7. /**
  8. * Execution method always used for tasks
  9. */
  10. public function execute() {
  11. if (empty($this->args)) {
  12. $this->__interactive();
  13. }
  14. if (!empty($this->args[0])) {
  15. $this->__handle($this->args[0]);
  16. }
  17. }
  18. /**
  19. * Interactive mode
  20. */
  21. protected function __interactive() {
  22. $module = preg_replace('/Handler$/', '', $this->handler_to_use);
  23. $module = strtolower($module);
  24. $question = "Which $module do you want to view?";
  25. $address = $this->in($question);
  26. $this->__handle($address);
  27. }
  28. /**
  29. * actually view something
  30. *
  31. * @param string address to view
  32. */
  33. protected function __handle($address) {
  34. $handler = new $this->handler_to_use($this->new);
  35. if (!$handler->init($address)) {
  36. $this->err($handler->errormsg);
  37. return;
  38. }
  39. if (!$handler->view()) {
  40. $this->err($handler->errormsg);
  41. return;
  42. }
  43. $result = $handler->result();
  44. $struct = $handler->getStruct();
  45. foreach(array_keys($struct) as $field) {
  46. if (isset($struct[$field]) && empty($struct[$field]['label'])) {
  47. # $struct[$field]['label'] = "--- $field ---";
  48. $struct[$field]['display_in_list'] = 0;
  49. }
  50. if ($struct[$field]['display_in_list'] == 0) {
  51. # do nothing
  52. } else {
  53. $value = $result[$field];
  54. $func="_formatted_".$field;
  55. if (method_exists($handler, $func) ) {
  56. $value = $handler->{$func}($result); # call _formatted_$fieldname()
  57. }
  58. if ($struct[$field]['type'] == 'txtl') {
  59. # $value = join("\n" . str_repeat(" ", 20 + 2), $value); # multiline, one item per line
  60. $value = join(", ", $value); # one line, comma-separated
  61. } elseif ($struct[$field]['type'] == 'bool') {
  62. $value = Config::Lang($value ? 'YES' : 'NO');
  63. }
  64. $this->out(sprintf("%20s: %s", $struct[$field]['label'], $value));
  65. }
  66. }
  67. }
  68. /**
  69. * Display help contents
  70. *
  71. * @access public
  72. */
  73. public function help() {
  74. $module = preg_replace('/Handler$/', '', $this->handler_to_use);
  75. $module = strtolower($module);
  76. $this->out(
  77. "Usage:
  78. postfixadmin-cli $module view
  79. View $module in interactive mode.
  80. - or -
  81. postfixadmin-cli $module view <address>
  82. View $module <address> in non-interactive mode.
  83. ");
  84. $this->_stop();
  85. }
  86. }
  87. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */