1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin; |
4
|
|
|
|
5
|
|
|
use Jaxon\Plugin\AbstractPackage; |
6
|
|
|
use Lagdo\DbAdmin\Ajax\App\Admin; |
7
|
|
|
|
8
|
|
|
use function realpath; |
9
|
|
|
use function Jaxon\cl; |
10
|
|
|
use function Jaxon\rq; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Jaxon DbAdmin package |
14
|
|
|
*/ |
15
|
|
|
class DbAdminPackage extends AbstractPackage |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Get the path to the config file |
19
|
|
|
* |
20
|
|
|
* @return string|array |
21
|
|
|
*/ |
22
|
|
|
public static function config(): string |
23
|
|
|
{ |
24
|
|
|
return realpath(__DIR__ . '/../config/dbadmin.php'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the database servers |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function getServers(): array |
33
|
|
|
{ |
34
|
|
|
return $this->getOption('servers', []); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the name of a given server |
39
|
|
|
* |
40
|
|
|
* @param string $server The server name in the configuration |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getServerName(string $server): string |
45
|
|
|
{ |
46
|
|
|
return $this->getOption("servers.$server.name", ''); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get a given server options |
51
|
|
|
* |
52
|
|
|
* @param string $server The server name in the configuration |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getServerOptions(string $server): array |
57
|
|
|
{ |
58
|
|
|
return $this->getOption("servers.$server", []); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the driver of a given server |
63
|
|
|
* |
64
|
|
|
* @param string $server The server name in the configuration |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getServerDriver(string $server): string |
69
|
|
|
{ |
70
|
|
|
return $this->getOption("servers.$server.driver", ''); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if the user has access to a server |
75
|
|
|
* |
76
|
|
|
* @param string $server The database server |
77
|
|
|
* |
78
|
|
|
* return bool |
79
|
|
|
*/ |
80
|
|
|
public function getServerAccess(string $server): bool |
81
|
|
|
{ |
82
|
|
|
// Check in server options |
83
|
|
|
$serverAccess = $this->getOption("servers.$server.access.server", null); |
84
|
|
|
if($serverAccess === true || $serverAccess === false) |
85
|
|
|
{ |
86
|
|
|
return $serverAccess; |
87
|
|
|
} |
88
|
|
|
// Check in global options |
89
|
|
|
return $this->getOption('access.server', true) === true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the HTML tags to include CSS code and files into the page |
94
|
|
|
* |
95
|
|
|
* The code must be enclosed in the appropriate HTML tags. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getCss(): string |
100
|
|
|
{ |
101
|
|
|
return $this->view()->render('dbadmin::codes::css.html') . "\n<style>\n" . |
102
|
|
|
$this->view()->render('dbadmin::codes::styles.css') . "\n</style>\n"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the HTML tags to include javascript code and files into the page |
107
|
|
|
* |
108
|
|
|
* The code must be enclosed in the appropriate HTML tags. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getJs(): string |
113
|
|
|
{ |
114
|
|
|
return $this->view()->render('dbadmin::codes::js.html'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the javascript code to include into the page |
119
|
|
|
* |
120
|
|
|
* The code must NOT be enclosed in HTML tags. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getScript(): string |
125
|
|
|
{ |
126
|
|
|
return $this->view()->render('dbadmin::codes::script.js'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the javascript code to include into the page |
131
|
|
|
* |
132
|
|
|
* The code must NOT be enclosed in HTML tags. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getReadyScript(): string |
137
|
|
|
{ |
138
|
|
|
$defaultServer = $this->getOption('default'); |
139
|
|
|
return !$defaultServer || |
140
|
|
|
!$this->getOption("servers.$defaultServer") ? '' : |
141
|
|
|
rq(Admin::class)->server($defaultServer); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the HTML code of the package home page |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getHtml(): string |
150
|
|
|
{ |
151
|
|
|
return cl(Admin::class)->html(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|