224 lines
7.8 KiB
PHP
224 lines
7.8 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/mysqli-class.php';
|
|
|
|
function db(): MysqliDb
|
|
{
|
|
static $db = null;
|
|
if ($db instanceof MysqliDb) {
|
|
return $db;
|
|
}
|
|
|
|
$user = 'virtualuser';
|
|
$pass = 'ltnwGXZqvQJqXjPPQgmO';
|
|
$dbName = 'shabby_road';
|
|
$host = 'virtualengage.c1qdknkurtfn.ap-south-1.rds.amazonaws.com';
|
|
$port = 3306;
|
|
|
|
$attempts = [
|
|
['socket' => null, 'host' => $host, 'port' => $port],
|
|
];
|
|
|
|
$lastError = '';
|
|
foreach ($attempts as $attempt) {
|
|
try {
|
|
$db = new MysqliDb([
|
|
'host' => $attempt['host'],
|
|
'username' => $user,
|
|
'password' => $pass,
|
|
'db' => $dbName,
|
|
'port' => $attempt['port'],
|
|
'socket' => $attempt['socket'],
|
|
'charset' => 'utf8mb4',
|
|
]);
|
|
bootstrap_schema($db);
|
|
return $db;
|
|
} catch (Exception $e) {
|
|
$lastError = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
throw new Exception('Database connection failed: ' . $lastError);
|
|
}
|
|
|
|
function bootstrap_schema(MysqliDb $db): void
|
|
{
|
|
$db->rawQuery("CREATE TABLE IF NOT EXISTS admins (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
email VARCHAR(255) NOT NULL UNIQUE,
|
|
password_hash VARCHAR(255) NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
$resourceTable = ensure_resources_table($db);
|
|
|
|
$db->rawQuery("CREATE TABLE IF NOT EXISTS sessions (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
title VARCHAR(255) NOT NULL,
|
|
description TEXT NOT NULL,
|
|
tag VARCHAR(50) DEFAULT NULL,
|
|
cta_label VARCHAR(100) DEFAULT 'Register',
|
|
start_date DATE DEFAULT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
$db->rawQuery("CREATE TABLE IF NOT EXISTS contacts (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL,
|
|
email VARCHAR(255) NOT NULL,
|
|
organization VARCHAR(255) DEFAULT NULL,
|
|
message TEXT NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
$db->rawQuery("CREATE TABLE IF NOT EXISTS newsletter_subscriptions (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
first_name VARCHAR(255) DEFAULT NULL,
|
|
email VARCHAR(255) NOT NULL UNIQUE,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
$db->rawQuery("CREATE TABLE IF NOT EXISTS tutorials (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
title VARCHAR(255) NOT NULL,
|
|
description TEXT NOT NULL,
|
|
status VARCHAR(50) NOT NULL DEFAULT 'active',
|
|
file_path VARCHAR(255) DEFAULT NULL,
|
|
video_url VARCHAR(255) DEFAULT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
seed_default_admin($db);
|
|
seed_sample_resources($db, $resourceTable);
|
|
seed_sample_sessions($db);
|
|
seed_sample_tutorials($db);
|
|
}
|
|
|
|
function seed_default_admin(MysqliDb $db): void
|
|
{
|
|
$row = $db->getOne("SELECT 1 FROM admins WHERE email = ?", ['ravi@stratawiz.com']);
|
|
if ($row) {
|
|
return;
|
|
}
|
|
$hash = password_hash('qwerty', PASSWORD_DEFAULT);
|
|
$db->insert('admins', ['email' => 'ravi@stratawiz.com', 'password_hash' => $hash]);
|
|
}
|
|
|
|
function seed_sample_resources(MysqliDb $db, string $table): void
|
|
{
|
|
$count = (int) $db->getValue("SELECT COUNT(*) as c FROM `$table`");
|
|
if ($count > 0) {
|
|
return;
|
|
}
|
|
|
|
$samples = [
|
|
['title' => 'Understanding Lung Function in Adult Populations', 'summary' => 'A concise explainer on spirometry metrics and how to interpret lung function assessments in community settings.', 'label' => 'Publication'],
|
|
['title' => 'Lung Function in India: Overview', 'summary' => 'Key findings from multi-city research that maps prevalence of respiratory conditions across demographic groups.', 'label' => 'Research Brief'],
|
|
['title' => 'Childhood Lung Function in India', 'summary' => 'Practical guidance for pediatric screening teams and frontline clinicians working with young patients.', 'label' => 'Toolkit'],
|
|
['title' => 'Understanding Lung Function in Rural Populations', 'summary' => 'Field notes and methodologies for conducting door-to-door lung health studies with limited resources.', 'label' => 'Field Guide'],
|
|
];
|
|
|
|
foreach ($samples as $sample) {
|
|
$db->insert($table, $sample);
|
|
}
|
|
}
|
|
|
|
function seed_sample_sessions(MysqliDb $db): void
|
|
{
|
|
$count = (int) $db->getValue("SELECT COUNT(*) as c FROM sessions");
|
|
if ($count > 0) {
|
|
return;
|
|
}
|
|
|
|
$samples = [
|
|
['title' => 'Nutrition guidance for advanced lung conditions', 'description' => 'Upcoming webinar on dietary support for patients with complex respiratory conditions.', 'tag' => 'New'],
|
|
['title' => 'Workshop: Field data quality for respiratory studies', 'description' => 'Hands-on clinic for research leads to strengthen field protocols and audits.', 'tag' => 'Update'],
|
|
['title' => 'Training cohort launch: Spirometry essentials', 'description' => 'New batch begins soon with live mentoring and case reviews.', 'tag' => 'Upcoming'],
|
|
];
|
|
|
|
foreach ($samples as $sample) {
|
|
$db->insert('sessions', $sample);
|
|
}
|
|
}
|
|
|
|
function seed_sample_tutorials(MysqliDb $db): void
|
|
{
|
|
$count = (int) $db->getValue("SELECT COUNT(*) as c FROM tutorials");
|
|
if ($count > 0) {
|
|
return;
|
|
}
|
|
|
|
$samples = [
|
|
['title' => 'Spirometry Basics', 'description' => 'Step-by-step guide for accurate spirometry setup and testing.', 'status' => 'active'],
|
|
['title' => 'Inhaler Technique', 'description' => 'Visual walkthrough on patient education for inhaler use.', 'status' => 'active'],
|
|
];
|
|
|
|
foreach ($samples as $sample) {
|
|
$db->insert('tutorials', $sample);
|
|
}
|
|
}
|
|
|
|
function ensure_resources_table(MysqliDb $db): string
|
|
{
|
|
$candidates = ['resources_table', 'resources'];
|
|
|
|
// Prefer any existing table that already has rows.
|
|
foreach ($candidates as $name) {
|
|
$exists = $db->getValue("SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?", [$name]);
|
|
if ($exists) {
|
|
$count = (int) $db->getValue("SELECT COUNT(*) FROM `$name`");
|
|
if ($count > 0) {
|
|
add_resource_columns_if_missing($db, $name);
|
|
return $name;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fallback to first existing, even if empty.
|
|
foreach ($candidates as $name) {
|
|
$exists = $db->getValue("SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?", [$name]);
|
|
if ($exists) {
|
|
add_resource_columns_if_missing($db, $name);
|
|
return $name;
|
|
}
|
|
}
|
|
|
|
$name = 'resources';
|
|
$db->rawQuery("CREATE TABLE IF NOT EXISTS `$name` (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
title VARCHAR(255) NOT NULL,
|
|
summary TEXT NOT NULL,
|
|
label VARCHAR(100) NOT NULL,
|
|
file_path VARCHAR(255) DEFAULT NULL,
|
|
video_url VARCHAR(255) DEFAULT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
add_resource_columns_if_missing($db, $name);
|
|
return $name;
|
|
}
|
|
|
|
function add_resource_columns_if_missing(MysqliDb $db, string $table): void
|
|
{
|
|
$columns = [
|
|
'file_path' => 'ALTER TABLE `%s` ADD COLUMN file_path VARCHAR(255) DEFAULT NULL',
|
|
'video_url' => 'ALTER TABLE `%s` ADD COLUMN video_url VARCHAR(255) DEFAULT NULL',
|
|
];
|
|
|
|
foreach ($columns as $column => $alter) {
|
|
$exists = $db->getValue("SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?", [$table, $column]);
|
|
if (!$exists) {
|
|
$db->rawQuery(sprintf($alter, $table));
|
|
}
|
|
}
|
|
}
|
|
|
|
function resource_table_name(MysqliDb $db): string
|
|
{
|
|
static $resolved;
|
|
if ($resolved) {
|
|
return $resolved;
|
|
}
|
|
$resolved = ensure_resources_table($db);
|
|
return $resolved;
|
|
}
|