Passed
Pull Request — develop (#262)
by Felipe
04:12
created

TreeController::printTree()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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