Passed
Push — develop ( 2969a7...df24bb )
by Felipe
05:41 queued 11s
created

TreeController::printTree()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 3
b 0
f 0
nc 2
nop 4
dl 0
loc 22
rs 9.8666
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-RC6
5
 */
6
7
namespace PHPPgAdmin\XHtml;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base TreeController controller class.
13
 */
14
class TreeController
15
{
16
    use \PHPPgAdmin\Traits\HelperTrait;
17
18
    protected $container;
19
    public $form             = '';
20
    public $href             = '';
21
    public $lang             = [];
22
    public $action           = '';
23
    public $controller_name  = 'TreeController';
24
    public $controller_title = 'base';
25
26
    // Constructor
27
    public function __construct(\Slim\Container $container, $controller_name = null)
28
    {
29
        $this->container = $container;
30
        $this->lang      = $container->get('lang');
31
        //$this->conf           = $container->get('conf');
32
        $this->view           = $container->get('view');
33
        $this->plugin_manager = $container->get('plugin_manager');
34
        $this->appName        = $container->get('settings')['appName'];
35
        $this->appVersion     = $container->get('settings')['appVersion'];
36
        $this->appLangFiles   = $container->get('appLangFiles');
37
        $this->misc           = $container->get('misc');
38
        $this->conf           = $this->misc->getConf();
39
        $this->appThemes      = $container->get('appThemes');
40
        $this->action         = $container->get('action');
41
        if (null !== $controller_name) {
42
            $this->controller_name = $controller_name;
43
        }
44
    }
45
46
    /**
47
     * Produce JSON data for the browser tree.
48
     *
49
     * @param \PHPPgAdmin\ArrayRecordSet $_treedata a set of records to populate the tree
50
     * @param array                      $attrs     Attributes for tree items
51
     *                                              'text' - the text for the tree node
52
     *                                              'icon' - an icon for node
53
     *                                              'openIcon' - an alternative icon when the node is expanded
54
     *                                              'toolTip' - tool tip text for the node
55
     *                                              'action' - URL to visit when single clicking the node
56
     *                                              'iconAction' - URL to visit when single clicking the icon node
57
     *                                              'branch' - URL for child nodes (tree XML)
58
     *                                              'expand' - the action to return XML for the subtree
59
     *                                              'nodata' - message to display when node has no children
60
     * @param string                     $section   The section where the branch is linked in the tree
61
     * @param bool                       $print     either to return or echo the result
62
     *
63
     * @return \Slim\Http\Response|string the json rendered tree
64
     */
65
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
66
    {
67
        $plugin_manager = $this->plugin_manager;
68
69
        $treedata = [];
70
71
        if ($_treedata->recordCount() > 0) {
72
            while (!$_treedata->EOF) {
73
                $treedata[] = $_treedata->fields;
74
                $_treedata->moveNext();
75
            }
76
        }
77
78
        $tree_params = [
79
            'treedata' => &$treedata,
80
            'attrs'    => &$attrs,
81
            'section'  => $section,
82
        ];
83
84
        $plugin_manager->doHook('tree', $tree_params);
85
86
        return $this->printTreeJSON($treedata, $attrs, $print);
87
    }
88
89
    /**
90
     * Produce JSON data for the browser tree.
91
     *
92
     * @param array $treedata a set of records to populate the tree
93
     * @param array $attrs    Attributes for tree items
94
     *                        'text' - the text for the tree node
95
     *                        'icon' - an icon for node
96
     *                        'openIcon' - an alternative icon when the node is expanded
97
     *                        'toolTip' - tool tip text for the node
98
     *                        'action' - URL to visit when single clicking the node
99
     *                        'iconAction' - URL to visit when single clicking the icon node
100
     *                        'branch' - URL for child nodes (tree JSON)
101
     *                        'expand' - the action to return JSON for the subtree
102
     *                        'nodata' - message to display when node has no children
103
     * @param bool  $print    either to return or echo the result
104
     *
105
     * @return \Slim\Http\Response|string the json rendered tree
106
     */
107
    private function printTreeJSON(&$treedata, &$attrs, $print = true)
108
    {
109
        $parent = [];
110
111
        if (isset($attrs['is_root'])) {
112
            $parent = [
113
                'id'       => 'root',
114
                'children' => true,
115
                'icon'     => \SUBFOLDER.'/assets/images/themes/default/Servers.png',
116
                'state'    => ['opened' => true],
117
                'a_attr'   => ['href' => str_replace('//', '/', \SUBFOLDER.'/src/views/servers')],
118
                'url'      => str_replace('//', '/', \SUBFOLDER.'/src/views/servers?action=tree'),
119
                'text'     => 'Servers',
120
            ];
121
        } elseif (count($treedata) > 0) {
122
            foreach ($treedata as $rec) {
123
                $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['icon'], $rec));
124
                if (!empty($attrs['openicon'])) {
125
                    $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['openIcon'], $rec));
126
                }
127
128
                $tree = [
129
                    'text'       => Decorator::get_sanitized_value($attrs['text'], $rec),
130
                    'id'         => sha1(Decorator::get_sanitized_value($attrs['action'], $rec)),
131
                    'icon'       => Decorator::get_sanitized_value($icon, $rec),
132
                    'iconaction' => Decorator::get_sanitized_value($attrs['iconAction'], $rec),
133
                    'openicon'   => Decorator::get_sanitized_value($icon, $rec),
134
                    'tooltip'    => Decorator::get_sanitized_value($attrs['toolTip'], $rec),
135
                    'a_attr'     => ['href' => Decorator::get_sanitized_value($attrs['action'], $rec)],
136
                    'children'   => false,
137
                ];
138
                $url = Decorator::get_sanitized_value($attrs['branch'], $rec);
139
                if ($url && strpos($url, '/src/views') === false) {
140
                    $url = str_replace('//', '/', \SUBFOLDER.'/src/views/'.$url);
141
                }
142
                if ($url) {
143
                    $tree['url']      = $url;
144
                    $tree['children'] = true;
145
                }
146
147
                //$tree['text'] = '<a href="' . $tree['id'] . '" target="detail">' . $tree['text'] . '</a>';
148
149
                $parent[] = $tree;
150
            }
151
        } else {
152
            $parent = ['children' => false];
153
        }
154
155
        if (true === $print) {
156
            if (isset($_REQUEST['children'])) {
157
                $children = $parent;
158
                $parent   = ['children' => $children];
159
            }
160
161
            return $this
162
                ->container
163
                ->responseobj
164
                ->withStatus(200)
165
                ->withJson($parent);
166
        }
167
168
        return $parent;
169
    }
170
171
    /**
172
     * Hides or show tree tabs according to their properties.
173
     *
174
     * @param array $tabs The tabs
175
     *
176
     * @return \PHPPgAdmin\ArrayRecordSet filtered tabs in the form of an ArrayRecordSet
177
     */
178
    public function adjustTabsForTree(&$tabs)
179
    {
180
        foreach ($tabs as $i => $tab) {
181
            if ((isset($tab['hide']) && true === $tab['hide']) || (isset($tab['tree']) && false === $tab['tree'])) {
182
                unset($tabs[$i]);
183
            }
184
        }
185
186
        return new \PHPPgAdmin\ArrayRecordSet($tabs);
187
    }
188
}
189