Passed
Push — main ( a89c91...baad3c )
by Thierry
07:54
created

Package::getJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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