Passed
Push — develop ( 1d7871...533904 )
by Felipe
06:14
created

TreeController::printTree()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 4
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.33
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\HelperTrait;
1 ignored issue
show
introduced by
The trait PHPPgAdmin\HelperTrait requires some properties which are not provided by PHPPgAdmin\XHtml\TreeController: $responseobj, $requestobj
Loading history...
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)
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
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
        //\PC::debug($this->controller_name, 'instanced controller');
45
    }
46
47
    /** Produce XML data for the browser tree
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Doc comment short description must be on the first line
Loading history...
Bug introduced by
The type PHPPgAdmin\XHtml\a was not found. Did you mean a? If so, make sure to prefix the type with \.
Loading history...
48
     * @param $treedata a set of records to populate the tree
49
     * @param $attrs Attributes for tree items
0 ignored issues
show
Bug introduced by
The type PHPPgAdmin\XHtml\Attributes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
     *        'text' - the text for the tree node
51
     *        'icon' - an icon for node
52
     *        'openIcon' - an alternative icon when the node is expanded
53
     *        'toolTip' - tool tip text for the node
54
     *        'action' - URL to visit when single clicking the node
55
     *        'iconAction' - URL to visit when single clicking the icon node
56
     *        'branch' - URL for child nodes (tree XML)
57
     *        'expand' - the action to return XML for the subtree
58
     *        'nodata' - message to display when node has no children
59
     * @param $section The section where the branch is linked in the tree
0 ignored issues
show
Bug introduced by
The type PHPPgAdmin\XHtml\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
     * @param mixed $print
61
     */
62
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
63
    {
64
        $plugin_manager = $this->plugin_manager;
65
66
        $treedata = [];
67
68
        if ($_treedata->recordCount() > 0) {
69
            while (!$_treedata->EOF) {
70
                $treedata[] = $_treedata->fields;
71
                $_treedata->moveNext();
72
            }
73
        }
74
75
        $tree_params = [
76
            'treedata' => &$treedata,
77
            'attrs'    => &$attrs,
78
            'section'  => $section,
79
        ];
80
81
        $plugin_manager->do_hook('tree', $tree_params);
82
83
        if (isset($_REQUEST['json'])) {
84
            return $this->printTreeJSON($treedata, $attrs, $print);
85
        }
86
87
        return $this->printTreeXML($treedata, $attrs, $print);
88
    }
89
90
    /** Produce XML data for the browser tree
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Doc comment short description must be on the first line
Loading history...
91
     * @param $treedata a set of records to populate the tree
92
     * @param $attrs Attributes for tree items
93
     *        'text' - the text for the tree node
94
     *        'icon' - an icon for node
95
     *        'openIcon' - an alternative icon when the node is expanded
96
     *        'toolTip' - tool tip text for the node
97
     *        'action' - URL to visit when single clicking the node
98
     *        'iconAction' - URL to visit when single clicking the icon node
99
     *        'branch' - URL for child nodes (tree XML)
100
     *        'expand' - the action to return XML for the subtree
101
     *        'nodata' - message to display when node has no children
102
     * @param mixed $print
103
     */
104
    private function printTreeXML(&$treedata, &$attrs, $print = true)
1 ignored issue
show
Coding Style introduced by
Private method name "TreeController::printTreeXML" must be prefixed with an underscore
Loading history...
105
    {
106
        $lang = $this->lang;
107
108
        $tree_xml = '<?xml version="1.0" encoding="UTF-8"?><tree>';
109
110
        if (count($treedata) > 0) {
111
            foreach ($treedata as $rec) {
112
                $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['icon'], $rec));
113
                if (!empty($attrs['openicon'])) {
114
                    $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['openIcon'], $rec));
115
                }
116
                $tree_xml .= '<tree>';
117
118
                $text_xml       = Decorator::value_xml_attr_tag('text', $attrs['text'], $rec);
119
                $action_xml     = Decorator::value_xml_attr_tag('action', $attrs['action'], $rec);
120
                $src_xml        = Decorator::value_xml_attr_tag('src', $attrs['branch'], $rec);
121
                $icon_xml       = Decorator::value_xml_attr_tag('icon', $icon, $rec);
122
                $iconaction_xml = Decorator::value_xml_attr_tag('iconaction', $attrs['iconAction'], $rec);
123
                $openicon_xml   = Decorator::value_xml_attr_tag('openicon', $icon, $rec);
124
                $tooltip_xml    = Decorator::value_xml_attr_tag('tooltip', $attrs['toolTip'], $rec);
125
126
                $tree_xml .= $text_xml;
127
                $tree_xml .= $action_xml;
128
                $tree_xml .= $src_xml;
129
                $tree_xml .= $icon_xml;
130
                $tree_xml .= $iconaction_xml;
131
                $tree_xml .= $openicon_xml;
132
                $tree_xml .= $tooltip_xml;
133
134
                $tree_xml .= '</tree>';
135
136
            }
137
        } else {
138
            $msg = isset($attrs['nodata']) ? $attrs['nodata'] : $lang['strnoobjects'];
139
            $tree_xml .= "<tree text=\"{$msg}\" onaction=\"tree.getSelected().getParent().reload()\" icon=\"" . $this->misc->icon('ObjectNotFound') . '" />' . "\n";
140
        }
141
142
        $tree_xml .= '</tree>';
143
        if (true === $print) {
144
145
            if (null === $this->container->requestobj->getAttribute('route')) {
146
                header('Content-Type: text/xml; charset=UTF-8');
147
                header('Cache-Control: no-cache');
148
                echo $tree_xml;
149
            } else {
150
                return $this
151
                    ->container
152
                    ->responseobj
153
                    ->withStatus(200)
154
                    ->withHeader('Content-Type', 'text/xml; charset=UTF-8')
155
                    ->write($tree_xml);
156
            }
157
        } else {
158
            return $tree_xml;
159
        }
160
    }
161
162
    /** Produce XML data for the browser tree
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Doc comment short description must be on the first line
Loading history...
163
     * @param $treedata a set of records to populate the tree
164
     * @param $attrs Attributes for tree items
165
     *        'text' - the text for the tree node
166
     *        'icon' - an icon for node
167
     *        'openIcon' - an alternative icon when the node is expanded
168
     *        'toolTip' - tool tip text for the node
169
     *        'action' - URL to visit when single clicking the node
170
     *        'iconAction' - URL to visit when single clicking the icon node
171
     *        'branch' - URL for child nodes (tree XML)
172
     *        'expand' - the action to return XML for the subtree
173
     *        'nodata' - message to display when node has no children
174
     * @param mixed $print
175
     */
176
    private function printTreeJSON(&$treedata, &$attrs, $print = true)
1 ignored issue
show
Coding Style introduced by
Private method name "TreeController::printTreeJSON" must be prefixed with an underscore
Loading history...
177
    {
178
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
179
180
        $parent = [];
181
182
        if (count($treedata) > 0) {
183
            foreach ($treedata as $rec) {
184
                $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['icon'], $rec));
185
                if (!empty($attrs['openicon'])) {
186
                    $icon = $this->misc->icon(Decorator::get_sanitized_value($attrs['openIcon'], $rec));
187
                }
188
189
                $tree = [
190
                    'text'           => Decorator::get_sanitized_value($attrs['text'], $rec),
191
                    'action_xml'     => Decorator::get_sanitized_value($attrs['action'], $rec),
192
193
                    'icon'           => Decorator::get_sanitized_value($icon, $rec),
194
                    'iconaction_xml' => Decorator::get_sanitized_value($attrs['iconAction'], $rec),
195
                    'openicon_xml'   => Decorator::get_sanitized_value($icon, $rec),
196
                    'tooltip_xml'    => Decorator::get_sanitized_value($attrs['toolTip'], $rec),
197
                    'children'       => false,
198
                ];
199
                $url = Decorator::get_sanitized_value($attrs['branch'], $rec);
200
                if (strpos($url, '/src/views') === false) {
201
                    $url = '/src/views/' . $url;
202
                }
203
                if ($url) {
204
                    $tree['url']      = $url;
205
                    $tree['children'] = true;
206
                    $tree['id']       = $url; //str_replace(' ', '_', $tree['text']);
207
                }
208
                $tree['text'] = '<a href="' . $tree['action_xml'] . '" target="detail">' . $tree['text'] . '</a>';
209
210
                $parent[] = $tree;
211
            }
212
        } else {
213
            $parent = ['children' => false];
214
        }
215
216
        if (true === $print) {
217
218
            if (null === $this->container->requestobj->getAttribute('route')) {
219
                header('Content-Type: text/xml; charset=UTF-8');
220
                header('Cache-Control: no-cache');
221
                echo $tree_xml;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tree_xml seems to be never defined.
Loading history...
222
            } else {
223
                if (isset($_REQUEST['children'])) {
224
                    $children = $parent;
225
                    $parent   = ['children' => $children];
226
                }
227
                return $this
228
                    ->container
229
                    ->responseobj
230
                    ->withStatus(200)
231
                    ->withJson($parent);
232
            }
233
        } else {
234
            return $tree_xml;
235
        }
236
    }
237
238
    public function adjustTabsForTree(&$tabs)
239
    {
240
        foreach ($tabs as $i => $tab) {
241
            if ((isset($tab['hide']) && true === $tab['hide']) || (isset($tab['tree']) && false === $tab['tree'])) {
242
                unset($tabs[$i]);
243
            }
244
        }
245
246
        return new \PHPPgAdmin\ArrayRecordSet($tabs);
247
    }
248
}
249