Passed
Push — main ( b09a59...ef0562 )
by Thierry
25:19 queued 18:47
created

Package::getHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin;
4
5
use Jaxon\Plugin\Package as JaxonPackage;
6
use Lagdo\DbAdmin\App\Ajax\Server;
7
8
use function array_key_exists;
9
use function array_walk;
10
use function jaxon;
11
use function pm;
12
13
/**
14
 * Adminer package
15
 */
16
class Package extends JaxonPackage
17
{
18
    /**
19
     * The constructor
20
     */
21
    public function __construct()
22
    {
23
        jaxon()->callback()->boot(function() {
24
            $template = $this->getConfig()->getOption('template', 'bootstrap3');
25
            jaxon()->template()->pagination(__DIR__ . "/../templates/views/$template/pagination/");
26
        });
27
    }
28
29
    /**
30
     * Get the div id of the HTML element
31
     *
32
     * @return string
33
     */
34
    public function getContainerId()
35
    {
36
        return 'adminer';
37
    }
38
39
    /**
40
     * Get the div id of the HTML element
41
     *
42
     * @return string
43
     */
44
    public function getUserInfoId()
45
    {
46
        return 'adminer-user-info';
47
    }
48
49
    /**
50
     * Get the div id of the HTML element
51
     *
52
     * @return string
53
     */
54
    public function getServerInfoId()
55
    {
56
        return 'adminer-server-info';
57
    }
58
59
    /**
60
     * Get the div id of the HTML element
61
     *
62
     * @return string
63
     */
64
    public function getBreadcrumbsId()
65
    {
66
        return 'adminer-breadcrumbs';
67
    }
68
69
    /**
70
     * Get the div id of the HTML element
71
     *
72
     * @return string
73
     */
74
    public function getMainActionsId()
75
    {
76
        return 'adminer-main-actions';
77
    }
78
79
    /**
80
     * Get the div id of the HTML element
81
     *
82
     * @return string
83
     */
84
    public function getServerActionsId()
85
    {
86
        return 'adminer-server-actions';
87
    }
88
89
    /**
90
     * Get the div id of the HTML element
91
     *
92
     * @return string
93
     */
94
    public function getDbListId()
95
    {
96
        return 'adminer-database-list';
97
    }
98
99
    /**
100
     * Get the div id of the HTML element
101
     *
102
     * @return string
103
     */
104
    public function getSchemaListId()
105
    {
106
        return 'adminer-schema-list';
107
    }
108
109
    /**
110
     * Get the div id of the HTML element
111
     *
112
     * @return string
113
     */
114
    public function getDbMenuId()
115
    {
116
        return 'adminer-database-menu';
117
    }
118
119
    /**
120
     * Get the div id of the HTML element
121
     *
122
     * @return string
123
     */
124
    public function getDbActionsId()
125
    {
126
        return 'adminer-database-actions';
127
    }
128
129
    /**
130
     * Get the div id of the HTML element
131
     *
132
     * @return string
133
     */
134
    public function getDbContentId()
135
    {
136
        return 'adminer-database-content';
137
    }
138
139
    /**
140
     * Get all the ids
141
     *
142
     * @return array
143
     */
144
    public function getIds()
145
    {
146
        return [
147
            'containerId' => $this->getContainerId(),
148
            'userInfoId' => $this->getUserInfoId(),
149
            'serverInfoId' => $this->getServerInfoId(),
150
            'breadcrumbsId' => $this->getBreadcrumbsId(),
151
            'mainActionsId' => $this->getMainActionsId(),
152
            'serverActionsId' => $this->getServerActionsId(),
153
            'dbListId' => $this->getDbListId(),
154
            'schemaListId' => $this->getSchemaListId(),
155
            'dbMenuId' => $this->getDbMenuId(),
156
            'dbActionsId' => $this->getDbActionsId(),
157
            'dbContentId' => $this->getDbContentId(),
158
        ];
159
    }
160
161
    /**
162
     * Get the path to the config file
163
     *
164
     * @return string
165
     */
166
    public static function getConfigFile()
167
    {
168
        return realpath(__DIR__ . '/../config/config.php');
169
    }
170
171
    /**
172
     * Get a given server options
173
     *
174
     * @param string $server    The server name in the configuration
175
     *
176
     * @return array
177
     */
178
    public function getServerOptions(string $server)
179
    {
180
        return $this->getConfig()->getOption("servers.$server", []);
181
    }
182
183
    /**
184
     * Get the driver of a given server
185
     *
186
     * @param string $server    The server name in the configuration
187
     *
188
     * @return string
189
     */
190
    public function getServerDriver(string $server)
191
    {
192
        return $this->getConfig()->getOption("servers.$server.driver", '');
193
    }
194
195
    /**
196
     * Get the default server to connect to
197
     *
198
     * @return string
199
     */
200
    private function getDefaultServer()
201
    {
202
        $servers = $this->getConfig()->getOption('servers', []);
203
        $default = $this->getConfig()->getOption('default', '');
204
        if(array_key_exists($default, $servers))
205
        {
206
            return $default;
207
        }
208
        // if(count($servers) > 0)
209
        // {
210
        //     return $servers[0];
211
        // }
212
        return '';
213
    }
214
215
    /**
216
     * Get the HTML tags to include CSS code and files into the page
217
     *
218
     * The code must be enclosed in the appropriate HTML tags.
219
     *
220
     * @return string
221
     */
222
    public function getCss()
223
    {
224
        return $this->view()->render('adminer::codes::css', $this->getIds()) .
225
            "\n" . $this->view()->render('adminer::views::styles', $this->getIds());
226
    }
227
228
    /**
229
     * Get the HTML tags to include javascript code and files into the page
230
     *
231
     * The code must be enclosed in the appropriate HTML tags.
232
     *
233
     * @return string
234
     */
235
    public function getJs()
236
    {
237
        return $this->view()->render('adminer::codes::js', $this->getIds());
238
    }
239
240
    /**
241
     * Get the javascript code to include into the page
242
     *
243
     * The code must NOT be enclosed in HTML tags.
244
     *
245
     * @return string
246
     */
247
    public function getScript()
248
    {
249
        return $this->view()->render('adminer::codes::script', $this->getIds());
250
    }
251
252
    /**
253
     * Get the javascript code to execute after page load
254
     *
255
     * @return string
256
     */
257
    public function getReadyScript()
258
    {
259
        if(!($server = $this->getDefaultServer()))
260
        {
261
            return '';
262
        }
263
        return jaxon()->request(Server::class)->connect($server);
264
    }
265
266
    /**
267
     * Get the HTML code of the package home page
268
     *
269
     * @return string
270
     */
271
    public function getHtml()
272
    {
273
        // Add an HTML container block for each server in the config file
274
        $servers = $this->getConfig()->getOption('servers', []);
275
        array_walk($servers, function(&$server) {
276
            $server = $server['name'];
277
        });
278
279
        $connect = jaxon()->request(Server::class)->connect(pm()->select('adminer-dbhost-select'));
280
281
        return $this->view()->render('adminer::views::home', $this->getIds())
282
            ->with('connect', $connect)
283
            ->with('servers', $servers)
284
            ->with('default', $this->getConfig()->getOption('default', ''));
285
    }
286
}
287