Passed
Push — develop ( c6d385...331c63 )
by Felipe
05:01
created

TreeController::printTree()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 4
b 0
f 0
nc 2
nop 4
dl 0
loc 22
rs 9.8666
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-RC1.
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 array $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 JSON)
58
     *                        'expand' - the action to return JSON for the subtree
59
     *                        'nodata' - message to display when node has no children
60
     * @param string  $section section of the tree
61
     *
62
     * @return \Slim\Http\Response|string the json rendered tree
63
     */
64
    public function printTreeJSON(&$treedata, &$attrs, $section = '')
65
    {
66
        $tree_params = [
67
            'treedata' => &$treedata,
68
            'attrs'    => &$attrs,
69
            'section'  => $section,
70
        ];
71
        $plugin_manager = $this->plugin_manager;
72
        $plugin_manager->doHook('tree', $tree_params);
73
74
        $parent = [];
75
76
        if (isset($attrs['is_root'])) {
77
            $parent = [
78
                'id'       => 'root',
79
                'children' => true,
80
                'icon'     => \SUBFOLDER . '/assets/images/themes/default/Servers.png',
81
                'state'    => ['opened' => true],
82
                'a_attr'   => ['href' => str_replace('//', '/', \SUBFOLDER . '/src/views/servers')],
83
                'url'      => str_replace('//', '/', \SUBFOLDER . '/src/views/servers?action=tree'),
84
                'text'     => 'Servers',
85
            ];
86
        } elseif (count($treedata) > 0) {
87
            foreach ($treedata as $rec) {
88
                $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['icon'], $rec));
89
                if (!empty($attrs['openicon'])) {
90
                    $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['openIcon'], $rec));
91
                }
92
93
                $tree = [
94
                    'text'       => Decorator::get_sanitized_value($attrs['text'], $rec),
95
                    'id'         => sha1(Decorator::get_sanitized_value($attrs['action'], $rec)),
96
                    'icon'       => Decorator::get_sanitized_value($icon, $rec),
97
                    'iconaction' => Decorator::get_sanitized_value($attrs['iconAction'], $rec),
98
                    'openicon'   => Decorator::get_sanitized_value($icon, $rec),
99
                    'tooltip'    => Decorator::get_sanitized_value($attrs['toolTip'], $rec),
100
                    'a_attr'     => ['href' => Decorator::get_sanitized_value($attrs['action'], $rec)],
101
                    'children'   => false,
102
                ];
103
                $url = Decorator::get_sanitized_value($attrs['branch'], $rec);
104
                if ($url && strpos($url, '/src/views') === false) {
105
                    $url = str_replace('//', '/', \SUBFOLDER . '/src/views/' . $url);
106
                }
107
                if ($url) {
108
                    $tree['url']      = $url;
109
                    $tree['children'] = true;
110
                }
111
112
                //$tree['text'] = '<a href="' . $tree['id'] . '" target="detail">' . $tree['text'] . '</a>';
113
114
                $parent[] = $tree;
115
            }
116
        } else {
117
            $parent = ['children' => false];
118
        }
119
120
        if (isset($_REQUEST['children'])) {
121
            $children = $parent;
122
            $parent   = ['children' => $children];
123
        }
124
125
        return $this
126
            ->container
127
            ->responseobj
128
            ->withStatus(200)
129
            ->withJson($parent);
130
131
    }
132
133
    /**
134
     * Hides or show tree tabs according to their properties.
135
     *
136
     * @param array $tabs The tabs
137
     *
138
     * @return \PHPPgAdmin\ArrayRecordSet filtered tabs in the form of an ArrayRecordSet
139
     */
140
    public function adjustTabsForTree(&$tabs)
141
    {
142
        foreach ($tabs as $i => $tab) {
143
            if ((isset($tab['hide']) && true === $tab['hide']) || (isset($tab['tree']) && false === $tab['tree'])) {
144
                unset($tabs[$i]);
145
            }
146
        }
147
148
        return new \PHPPgAdmin\ArrayRecordSet($tabs);
149
    }
150
}
151