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