9da056b480
- Implemented roadmap.php for displaying and managing roadmap items with filters for quarter and status. - Created schema.sql to define the database structure for users, invites, daily plans, tasks, brainstorm sessions, brainstorm cards, roadmap items, and integrations. - Developed users.php for admin panel to manage team members and invitations, including functionality to create and revoke invites.
69 lines
2.4 KiB
PHP
69 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* MariaDB PDO Databaseverbinding & Automatische Migratie
|
|
*/
|
|
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function get_db_connection(): PDO {
|
|
static $pdo = null;
|
|
|
|
if ($pdo === null) {
|
|
$dsn = sprintf(
|
|
'mysql:host=%s;port=%s;dbname=%s;charset=%s',
|
|
DB_HOST,
|
|
DB_PORT,
|
|
DB_NAME,
|
|
DB_CHARSET
|
|
);
|
|
|
|
$options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
];
|
|
|
|
try {
|
|
$pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
|
|
// Zorg ervoor dat tabellen bestaan
|
|
ensure_tables_exist($pdo);
|
|
} catch (PDOException $e) {
|
|
// Als de database zelf nog niet bestaat, probeer deze aan te maken indien gebruiker rechten heeft
|
|
if ($e->getCode() == 1049) {
|
|
try {
|
|
$tempDsn = sprintf('mysql:host=%s;port=%s;charset=%s', DB_HOST, DB_PORT, DB_CHARSET);
|
|
$tempPdo = new PDO($tempDsn, DB_USER, DB_PASS, $options);
|
|
$tempPdo->exec(sprintf('CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci', DB_NAME));
|
|
$pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
|
|
ensure_tables_exist($pdo);
|
|
return $pdo;
|
|
} catch (PDOException $ex) {
|
|
die("Database verbindingsfout: Kan database '" . htmlspecialchars(DB_NAME) . "' niet aanmaken of vinden. Maak deze handmatig aan in MariaDB.");
|
|
}
|
|
}
|
|
die("MariaDB Verbindingsfout: " . htmlspecialchars($e->getMessage()) . "<br><small>Controleer uw instellingen in config/config.php of .env</small>");
|
|
}
|
|
}
|
|
|
|
return $pdo;
|
|
}
|
|
|
|
/**
|
|
* Zorgt ervoor dat alle tabellen uit schema.sql aanwezig zijn in MariaDB
|
|
*/
|
|
function ensure_tables_exist(PDO $pdo): void {
|
|
try {
|
|
$result = $pdo->query("SHOW TABLES LIKE 'users'");
|
|
if ($result->rowCount() === 0) {
|
|
$schemaFile = __DIR__ . '/../schema.sql';
|
|
if (file_exists($schemaFile)) {
|
|
$sql = file_get_contents($schemaFile);
|
|
$pdo->exec($sql);
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
// Tabellen bestaan waarschijnlijk al of er is een permissiebeperking
|
|
}
|
|
}
|
|
|