File: CliView.php

package info (click to toggle)
postfixadmin 4.0.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,888 kB
  • sloc: php: 12,256; perl: 1,156; sh: 717; python: 142; xml: 63; sql: 3; makefile: 2
file content (117 lines) | stat: -rw-r--r-- 2,907 bytes parent folder | download
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
<?php

# $Id$
/**
 * class to handle 'view' in Cli
 */

class CliView extends Shell
{
    /**
     * Execution method always used for tasks
     */
    public function execute()
    {
        if (empty($this->args)) {
            return $this->__interactive();
        }

        if (!empty($this->args[0])) {
            return $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);

        return $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 1;
        }

        if (!$handler->view()) {
            $this->err($handler->errormsg);
            return 1;
        }

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