@@ -38,18 +38,22 @@ function execute() {
38
38
if (file_exists (PROJECT_PATH_SPECIFIC . "config.system.php " )) {
39
39
require_once PROJECT_PATH_SPECIFIC . "config.system.php " ;
40
40
$ this ->oModel ->set ('sqlite_file ' , PROJECT_SQLITE_FILE );
41
- $ this ->oModel ->set ('mysql ' , PROJECT_DB_MYSQL );
41
+ $ this ->oModel ->set ('backend ' , PROJECT_DB_BACKEND );
42
42
$ this ->oModel ->set ('mysql_host ' , PROJECT_DB_MYSQL_HOST );
43
43
$ this ->oModel ->set ('mysql_dbname ' , PROJECT_DB_MYSQL_DBNAME );
44
44
$ this ->oModel ->set ('mysql_username ' , PROJECT_DB_MYSQL_USERNAME );
45
45
$ this ->oModel ->set ('mysql_password ' , PROJECT_DB_MYSQL_PASSWORD );
46
+ $ this ->oModel ->set ('pgsql_host ' , PROJECT_DB_PGSQL_HOST );
47
+ $ this ->oModel ->set ('pgsql_dbname ' , PROJECT_DB_PGSQL_DBNAME );
48
+ $ this ->oModel ->set ('pgsql_username ' , PROJECT_DB_PGSQL_USERNAME );
49
+ $ this ->oModel ->set ('pgsql_password ' , PROJECT_DB_PGSQL_PASSWORD );
46
50
$ this ->oModel ->set ('encryption_key ' , BAIKAL_ENCRYPTION_KEY );
47
51
}
48
52
49
53
$ this ->oForm = $ this ->oModel ->formForThisModelInstance ([
50
54
"close " => false ,
51
- "hook.validation " => [$ this , "validateConnection " ],
52
- "hook.morphology " => [$ this , "hideMySQLFieldWhenNeeded " ],
55
+ "hook.validation " => [$ this , "validateSQLConnection " ],
56
+ "hook.morphology " => [$ this , "hideSQLFieldWhenNeeded " ],
53
57
]);
54
58
55
59
if ($ this ->oForm ->submitted ()) {
@@ -99,11 +103,11 @@ function render() {
99
103
return $ oView ->render ();
100
104
}
101
105
102
- function validateConnection ($ oForm , $ oMorpho ) {
106
+ function validateMySQLConnection ($ oForm , $ oMorpho ) {
103
107
if ($ oForm ->refreshed ()) {
104
108
return true ;
105
109
}
106
- $ bMySQLEnabled = $ oMorpho ->element ("mysql " )->value ();
110
+ $ bMySQLEnabled = $ oMorpho ->element ("backend " )->value () == ' mysql ' ;
107
111
108
112
if ($ bMySQLEnabled ) {
109
113
$ sHost = $ oMorpho ->element ("mysql_host " )->value ();
@@ -129,7 +133,7 @@ function validateConnection($oForm, $oMorpho) {
129
133
$ sMessage .= "<br /><p>Nothing has been saved. <strong>Please, add these tables to the database before pursuing Baïkal initialization.</strong></p> " ;
130
134
131
135
$ oForm ->declareError (
132
- $ oMorpho ->element ("mysql " ),
136
+ $ oMorpho ->element ("backend " ),
133
137
$ sMessage
134
138
);
135
139
} else {
@@ -142,7 +146,7 @@ function validateConnection($oForm, $oMorpho) {
142
146
143
147
return true ;
144
148
} catch (\Exception $ e ) {
145
- $ oForm ->declareError ($ oMorpho ->element ("mysql " ),
149
+ $ oForm ->declareError ($ oMorpho ->element ("backend " ),
146
150
"Baïkal was not able to establish a connexion to the MySQL database as configured.<br />MySQL says: " . $ e ->getMessage ());
147
151
$ oForm ->declareError ($ oMorpho ->element ("mysql_host " ));
148
152
$ oForm ->declareError ($ oMorpho ->element ("mysql_dbname " ));
@@ -211,10 +215,10 @@ function validateConnection($oForm, $oMorpho) {
211
215
212
216
function hideMySQLFieldWhenNeeded (\Formal \Form $ oForm , \Formal \Form \Morphology $ oMorpho ) {
213
217
if ($ oForm ->submitted ()) {
214
- $ bMySQL = (intval ( $ oForm ->postValue ("mysql " )) === 1 );
218
+ $ bMySQL = ($ oForm ->postValue ("backend " ) == ' mysql ' );
215
219
} else {
216
220
// oMorpho won't have the values from the model set on it yet
217
- $ bMySQL = $ this ->oModel ->get ("mysql " ) ;
221
+ $ bMySQL = $ this ->oModel ->get ("backend " ) == ' mysql ' ;
218
222
}
219
223
220
224
if ($ bMySQL === true ) {
@@ -226,4 +230,103 @@ function hideMySQLFieldWhenNeeded(\Formal\Form $oForm, \Formal\Form\Morphology $
226
230
$ oMorpho ->remove ("mysql_password " );
227
231
}
228
232
}
233
+
234
+ function validatePgSQLConnection ($ oForm , $ oMorpho ) {
235
+ $ bPgSqlEnabled = $ oMorpho ->element ("backend " )->value () == 'pgsql ' ;
236
+
237
+ if ($ bPgSqlEnabled ) {
238
+ $ sHost = $ oMorpho ->element ("pgsql_host " )->value ();
239
+ $ sDbname = $ oMorpho ->element ("pgsql_dbname " )->value ();
240
+ $ sUsername = $ oMorpho ->element ("pgsql_username " )->value ();
241
+ $ sPassword = $ oMorpho ->element ("pgsql_password " )->value ();
242
+
243
+ try {
244
+ $ oDb = new \Flake \Core \Database \Pgsql (
245
+ $ sHost ,
246
+ $ sDbname ,
247
+ $ sUsername ,
248
+ $ sPassword
249
+ );
250
+
251
+ if (($ aMissingTables = \Baikal \Core \Tools::isDBStructurallyComplete ($ oDb )) !== true ) {
252
+ # Checking if all tables are missing
253
+ $ aRequiredTables = \Baikal \Core \Tools::getRequiredTablesList ();
254
+ if (count ($ aRequiredTables ) !== count ($ aMissingTables )) {
255
+ $ sMessage = "<br /><p><strong>Database is not structurally complete.</strong></p> " ;
256
+ $ sMessage .= "<p>Missing tables are: <strong> " . implode ("</strong>, <strong> " , $ aMissingTables ) . "</strong></p> " ;
257
+ $ sMessage .= "<p>You will find the SQL definition of Baïkal tables in this file: <strong>Core/Resources/Db/PgSQL/db.sql</strong></p> " ;
258
+ $ sMessage .= "<br /><p>Nothing has been saved. <strong>Please, add these tables to the database before pursuing Baïkal initialization.</strong></p> " ;
259
+
260
+ $ oForm ->declareError (
261
+ $ oMorpho ->element ("backend " ),
262
+ $ sMessage
263
+ );
264
+ } else {
265
+ # All tables are missing
266
+ # We add these tables ourselves to the database, to initialize Baïkal
267
+ $ sSqlDefinition = file_get_contents (PROJECT_PATH_CORERESOURCES . "Db/PgSQL/db.sql " );
268
+ $ oDb ->getPDO ()->exec ($ sSqlDefinition );
269
+ }
270
+ }
271
+
272
+ return true ;
273
+ } catch (\Exception $ e ) {
274
+ $ oForm ->declareError (
275
+ $ oMorpho ->element ("backend " ),
276
+ "Baïkal was not able to establish a connexion to the PostgreSQL database as configured.<br />PostgreSQL says: " . $ e ->getMessage ()
277
+ );
278
+
279
+ $ oForm ->declareError (
280
+ $ oMorpho ->element ("pgsql_host " )
281
+ );
282
+
283
+ $ oForm ->declareError (
284
+ $ oMorpho ->element ("pgsql_dbname " )
285
+ );
286
+
287
+ $ oForm ->declareError (
288
+ $ oMorpho ->element ("pgsql_username " )
289
+ );
290
+
291
+ $ oForm ->declareError (
292
+ $ oMorpho ->element ("pgsql_password " )
293
+ );
294
+ }
295
+ }
296
+ }
297
+
298
+ public function validateSQLConnection ($ oForm , $ oMorpho ) {
299
+ if ($ oMorpho ->element ("backend " )->value () == 'mysql ' ) {
300
+ $ this ->validateMySQLConnection ($ oForm , $ oMorpho );
301
+ } elseif ($ oMorpho ->element ("backend " )->value () == 'pgsql ' ) {
302
+ $ this ->validatePgSQLConnection ($ oForm , $ oMorpho );
303
+ }
304
+ }
305
+
306
+ public function hideSqlFieldWhenNeeded (\Formal \Form $ oForm , \Formal \Form \Morphology $ oMorpho ) {
307
+ if ($ oMorpho ->element ("backend " )->value () == 'mysql ' ) {
308
+ $ this ->hideMySQLFieldWhenNeeded ($ oForm , $ oMorpho );
309
+ } elseif ($ oMorpho ->element ("backend " )->value () == 'pgsql ' ) {
310
+ $ this ->hidePgSQLFieldWhenNeeded ($ oForm , $ oMorpho );
311
+ }
312
+ }
313
+
314
+ public function hidePgSQLFieldWhenNeeded (\Formal \Form $ oForm , \Formal \Form \Morphology $ oMorpho ) {
315
+ if ($ oForm ->submitted ()) {
316
+ $ bPgSQL = ($ oForm ->postValue ("backend " )) == 'pgsql ' ;
317
+ } else {
318
+ // oMorpho won't have the values from the model set on it yet
319
+ $ bPgSQL = $ this ->oModel ->get ("backend " ) == 'pgsql ' ;
320
+ }
321
+
322
+ if ($ bPgSQL === true ) {
323
+ $ oMorpho ->remove ("sqlite_file " );
324
+ $ this ->hideMySQLFieldWhenNeeded ($ oForm , $ oMorpho );
325
+ } else {
326
+ $ oMorpho ->remove ("pgsql_host " );
327
+ $ oMorpho ->remove ("pgsql_dbname " );
328
+ $ oMorpho ->remove ("pgsql_username " );
329
+ $ oMorpho ->remove ("pgsql_password " );
330
+ }
331
+ }
229
332
}
0 commit comments