Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TreeController.php « controllers « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32ee9434632e0a4e917b070add774485378da815 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php

/**
 * PHPPgAdmin 6.1.3
 */

namespace PHPPgAdmin\Controller;

use PHPPgAdmin\ArrayRecordSet;
use PHPPgAdmin\ContainerUtils;
use PHPPgAdmin\Decorators\Decorator;
use PHPPgAdmin\Traits\HelperTrait;
use Slim\Http\Response;

/**
 * Base TreeController controller class.
 */
class TreeController extends BaseController
{
    use HelperTrait;

    public $form = '';

    public $href = '';

    public $lang = [];

    public $action = '';

    public $controller_name = 'TreeController';

    public $controller_title = 'base';

    public $misc;

    public $conf;

    public $appThemes;

    public $view;

    public $appVersion;

    public $appLangFiles;

    protected $container;

    // Constructor
    public function __construct(ContainerUtils $container, $controller_name = null)
    {
        $this->container = $container;
        $this->lang = $container->get('lang');
        //$this->conf           = $container->get('conf');
        $this->view = $container->get('view');

        $this->appName = $container->get('settings')['appName'];
        $this->appVersion = $container->get('settings')['appVersion'];
        $this->appLangFiles = $container->get('appLangFiles');
        $this->misc = $container->get('misc');
        $this->conf = $this->misc->getConf();
        $this->appThemes = $container->get('appThemes');
        $this->action = $container->get('action');

        if (null !== $controller_name) {
            $this->controller_name = $controller_name;
        }
    }

    /**
     * Produce JSON data for the browser tree.
     *
     * @param ArrayRecordSet $_treedata a set of records to populate the tree
     * @param array          $attrs     Attributes for tree items
     *                                  'text' - the text for the tree node
     *                                  'icon' - an icon for node
     *                                  'openIcon' - an alternative icon when the node is expanded
     *                                  'toolTip' - tool tip text for the node
     *                                  'action' - URL to visit when single clicking the node
     *                                  'iconAction' - URL to visit when single clicking the icon node
     *                                  'branch' - URL for child nodes (tree XML)
     *                                  'expand' - the action to return XML for the subtree
     *                                  'nodata' - message to display when node has no children
     * @param string         $section   The section where the branch is linked in the tree
     * @param bool           $print     either to return or echo the result
     *
     * @return (array|bool|string)[] the json rendered tree
     *
     * @psalm-return array<int|string, array<string, mixed>|bool|string>
     */
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
    {
        $treedata = [];

        if (0 < $_treedata->recordCount()) {
            while (!$_treedata->EOF) {
                $treedata[] = $_treedata->fields;
                $_treedata->moveNext();
            }
        }

        $tree_params = [
            'treedata' => &$treedata,
            'attrs' => &$attrs,
            'section' => $section,
        ];

        return $this->printTreeJSON($treedata, $attrs, $print);
    }

    /**
     * Hides or show tree tabs according to their properties.
     *
     * @param array $tabs The tabs
     *
     * @return ArrayRecordSet filtered tabs in the form of an ArrayRecordSet
     */
    public function adjustTabsForTree(&$tabs)
    {
        foreach ($tabs as $i => $tab) {
            if ((isset($tab['hide']) && true === $tab['hide']) || (isset($tab['tree']) && false === $tab['tree'])) {
                unset($tabs[$i]);
            }
        }

        return new ArrayRecordSet($tabs);
    }

    /**
     * Produce JSON data for the browser tree.
     *
     * @param array $treedata a set of records to populate the tree
     * @param array $attrs    Attributes for tree items
     *                        'text' - the text for the tree node
     *                        'icon' - an icon for node
     *                        'openIcon' - an alternative icon when the node is expanded
     *                        'toolTip' - tool tip text for the node
     *                        'action' - URL to visit when single clicking the node
     *                        'iconAction' - URL to visit when single clicking the icon node
     *                        'branch' - URL for child nodes (tree JSON)
     *                        'expand' - the action to return JSON for the subtree
     *                        'nodata' - message to display when node has no children
     * @param bool  $print    either to return or echo the result
     *
     * @return array<int|string, array<string, mixed>|bool|string> the json rendered tree
     */
    private function printTreeJSON(&$treedata, &$attrs, $print = true)
    {
        $parent = [];

        if (isset($attrs['is_root'])) {
            $parent = [
                'id' => 'root',
                'children' => true,
                'icon' => \containerInstance()->subFolder . '/assets/images/themes/default/Servers.png',
                'state' => ['opened' => true],
                'a_attr' => ['href' => \str_replace('//', '/', \containerInstance()->subFolder . '/src/views/servers')],
                'url' => \str_replace('//', '/', \containerInstance()->subFolder . '/src/views/servers?action=tree'),
                'text' => 'Servers',
            ];
        } elseif (0 < \count($treedata)) {
            foreach ($treedata as $rec) {
                $icon = $this->view->icon(Decorator::get_sanitized_value($attrs['icon'], $rec));

                if (!empty($attrs['openicon'])) {
                    $icon = $this->view->icon(Decorator::get_sanitized_value($attrs['openIcon'], $rec));
                }

                $tree = [
                    'text' => Decorator::get_sanitized_value($attrs['text'], $rec),
                    'id' => \sha1(Decorator::get_sanitized_value($attrs['action'], $rec)),
                    'icon' => Decorator::get_sanitized_value($icon, $rec),
                    'iconaction' => Decorator::get_sanitized_value($attrs['iconAction'], $rec),
                    'openicon' => Decorator::get_sanitized_value($icon, $rec),
                    'tooltip' => Decorator::get_sanitized_value($attrs['toolTip'], $rec),
                    'a_attr' => ['href' => Decorator::get_sanitized_value($attrs['action'], $rec)],
                    'children' => false,
                ];
                $url = Decorator::get_sanitized_value($attrs['branch'], $rec);

                if ($url && false === \mb_strpos($url, '/src/views')) {
                    $url = \str_replace('//', '/', \containerInstance()->subFolder . '/src/views/' . $url);
                }

                if ($url) {
                    $tree['url'] = $url;
                    $tree['children'] = true;
                }

                //$tree['text'] = '<a href="' . $tree['id'] . '" target="detail">' . $tree['text'] . '</a>';

                $parent[] = $tree;
            }
        } else {
            $parent = ['children' => false];
        }

        if (true === $print) {
            if (isset($_REQUEST['children'])) {
                $children = $parent;
                $parent = ['children' => $children];
            }

            return $this
                ->container
                ->response
                ->withStatus(200)
                ->withJson($parent);
        }

        return $parent;
    }
}