first commit
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Create System Tables
|
||||
* - Loads DB settings from ../xml/settings.xml
|
||||
* - Scans current dir for table definition files (*.php), excludes bootstrap scripts
|
||||
* - Drops related view + table, then runs $SQL from each included file
|
||||
* - Seeds data for certain tables (popovers, users)
|
||||
*
|
||||
* NOTE: Your table definition files should set: $SQL = "CREATE TABLE ...";
|
||||
*/
|
||||
define('INITIALIZATION_FILE_NAME', __DIR__ . '/../../xml/settings.xml');
|
||||
|
||||
$logs = [];
|
||||
|
||||
function logLine(array &$logs, string $msg): void {
|
||||
$logs[] = $msg;
|
||||
}
|
||||
|
||||
function runSQL(PDO $db, ?string $sql, string $context, array &$logs): void {
|
||||
if ($sql === null || trim($sql) === '') {
|
||||
logLine($logs, "⚠️ Skipping empty SQL for <strong>{$context}</strong>.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$db->exec($sql);
|
||||
} catch (PDOException $e) {
|
||||
logLine($logs, "❌ Error running <strong>{$context}</strong>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
||||
renderPage($logs, false);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function renderPage(array $logs, bool $ok): void {
|
||||
$status = $ok ? "✅ System Tables Created." : "❌ Script stopped due to an error.";
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Create System Tables</title>
|
||||
<style>
|
||||
body {
|
||||
background-color:#333;
|
||||
color:#fff;
|
||||
font-family:sans-serif;
|
||||
padding:1em;
|
||||
}
|
||||
.log {
|
||||
margin-bottom: .5em;
|
||||
}
|
||||
.done {
|
||||
margin-top: 1em;
|
||||
font-weight: bold;
|
||||
}
|
||||
code {
|
||||
background: rgba(255,255,255,.08);
|
||||
padding: .15em .35em;
|
||||
border-radius: 6px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<?php foreach ($logs as $line): ?>
|
||||
<div class="log"><?= $line ?></div>
|
||||
<?php endforeach; ?>
|
||||
<div class="done"><?= $status ?></div>
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
}
|
||||
|
||||
// --------------------
|
||||
// Load App Settings
|
||||
// --------------------
|
||||
if (!file_exists(INITIALIZATION_FILE_NAME)) {
|
||||
logLine($logs, "❌ Initialization file not found: <code>" . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . "</code>");
|
||||
renderPage($logs, false);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$applicationSettings = @simplexml_load_file(INITIALIZATION_FILE_NAME);
|
||||
if (!$applicationSettings) {
|
||||
logLine($logs, "❌ Unable to load Application Settings from <code>" . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . "</code>.");
|
||||
renderPage($logs, false);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// --------------------
|
||||
// Database Connection
|
||||
// --------------------
|
||||
$host = (string) ($applicationSettings->DatabaseServer ?? '');
|
||||
$database = (string) ($applicationSettings->DatabaseName ?? '');
|
||||
$user = (string) ($applicationSettings->DatabaseUser ?? '');
|
||||
$pass = (string) ($applicationSettings->DatabasePassword ?? '');
|
||||
|
||||
if ($host === '' || $database === '' || $user === '') {
|
||||
logLine($logs, "❌ Missing DB settings in settings.xml (DatabaseServer/DatabaseName/DatabaseUser).");
|
||||
renderPage($logs, false);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
$dsn = "mysql:host={$host};dbname={$database};charset=utf8mb4";
|
||||
$db = new PDO($dsn, $user, $pass, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
|
||||
$db->exec("SET FOREIGN_KEY_CHECKS = 0");
|
||||
} catch (Throwable $e) {
|
||||
logLine($logs, "❌ Unable to connect to the database.<br/>Error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
||||
renderPage($logs, false);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// --------------------
|
||||
// Find Table Files
|
||||
// --------------------
|
||||
$exclude = [
|
||||
'.', '..',
|
||||
'_createSystemTable.php',
|
||||
'_createTable.php',
|
||||
];
|
||||
|
||||
$dirFiles = scandir(__DIR__) ?: [];
|
||||
$tableFiles = array_values(array_filter($dirFiles, function ($f) use ($exclude) {
|
||||
if (in_array($f, $exclude, true))
|
||||
return false;
|
||||
if (pathinfo($f, PATHINFO_EXTENSION) !== 'php')
|
||||
return false;
|
||||
return is_file(__DIR__ . '/' . $f);
|
||||
}));
|
||||
|
||||
if (!$tableFiles) {
|
||||
logLine($logs, "⚠️ No table definition files found in <code>" . htmlspecialchars(__DIR__, ENT_QUOTES) . "</code>.");
|
||||
renderPage($logs, true);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// --------------------
|
||||
// Process Each File
|
||||
// --------------------
|
||||
foreach ($tableFiles as $tableFile) {
|
||||
|
||||
$tableName = pathinfo($tableFile, PATHINFO_FILENAME);
|
||||
logLine($logs, "Processing <strong>" . htmlspecialchars($tableName, ENT_QUOTES) . "</strong>...");
|
||||
|
||||
// Drop view + table
|
||||
try {
|
||||
$db->exec("DROP VIEW IF EXISTS `view_{$tableName}`");
|
||||
} catch (PDOException $e) {
|
||||
logLine($logs, "⚠️ Error dropping view <code>view_{$tableName}</code>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
||||
}
|
||||
|
||||
try {
|
||||
$db->exec("DROP TABLE IF EXISTS `{$tableName}`");
|
||||
} catch (PDOException $e) {
|
||||
logLine($logs, "⚠️ Error dropping table <code>{$tableName}</code>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
||||
}
|
||||
|
||||
// Include file (expects it to set $SQL)
|
||||
$SQL = null;
|
||||
$fullPath = __DIR__ . '/' . $tableFile;
|
||||
|
||||
try {
|
||||
$result = include $fullPath;
|
||||
if ($result === false) {
|
||||
logLine($logs, "❌ Failed to include <code>" . htmlspecialchars($tableFile, ENT_QUOTES) . "</code>. Skipping.");
|
||||
continue;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
logLine($logs, "❌ Exception including <code>" . htmlspecialchars($tableFile, ENT_QUOTES) . "</code>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
||||
renderPage($logs, false);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
runSQL($db, $SQL, $tableName, $logs);
|
||||
|
||||
// --------------------
|
||||
// Seeds / Extras
|
||||
// --------------------
|
||||
if ($tableFile === 'popovers.php') {
|
||||
|
||||
$sqlQueries = [
|
||||
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
||||
VALUES ('find_user', 'Find User', 'Find User...', '<p>Find by Name, Email, or Role</p>', CURDATE(), 'Init Script')",
|
||||
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
||||
VALUES ('find_report', 'Find Report', 'Find Report...', '<p>Find by Name or Description</p>', CURDATE(), 'Init Script')",
|
||||
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
||||
VALUES ('select_report', 'Select Report', 'Find Report...', '<p>Find by Name, Email, or Role</p>', CURDATE(), 'Init Script')",
|
||||
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
||||
VALUES ('find_popover', 'Find Popover', 'Find Popover...', '<p>Find by Name, Title or Placeholder</p>', CURDATE(), 'Init Script')",
|
||||
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
||||
VALUES ('find_journal', 'Find Journal', 'Find Journal...', '<p>Find by Name, Email, or Role</p>', CURDATE(), 'Init Script')",
|
||||
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
||||
VALUES ('find_status', 'Find Status', 'Find Status...', '<p>Find by Name</p>', CURDATE(), 'Init Script')",
|
||||
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
||||
VALUES ('find_dropdowntype', 'Find Dropdown Types', 'Find Dropdown Types...', '<p>Search for dropdown types</p>', CURDATE(), 'Init Script')",
|
||||
];
|
||||
|
||||
foreach ($sqlQueries as $sql) {
|
||||
runSQL($db, $sql, "popovers data", $logs);
|
||||
}
|
||||
}
|
||||
|
||||
if ($tableFile === 'users.php') {
|
||||
|
||||
$adminPasswordHash = password_hash("349Jamot@", PASSWORD_DEFAULT);
|
||||
|
||||
$sql = "INSERT INTO users (user_id, user_name, user_email, user_password, user_role, user_change_password, user_active, user_creator, user_created)
|
||||
VALUES ('jric11', 'James Richie', 'james.richie@onesourceits.com', " . $db->quote($adminPasswordHash) . ", 'Administrator', 0, 1, 'Init Script', CURDATE())";
|
||||
|
||||
runSQL($db, $sql, "initial admin user", $logs);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$db->exec("SET FOREIGN_KEY_CHECKS = 1");
|
||||
} catch (PDOException $e) {
|
||||
logLine($logs, "⚠️ Could not re-enable FOREIGN_KEY_CHECKS: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
||||
}
|
||||
|
||||
renderPage($logs, true);
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table dropdowns
|
||||
|
||||
(
|
||||
|
||||
dropdown_serial serial,
|
||||
dropdown_dropdowntype bigint unsigned default 0,
|
||||
dropdown_value varchar(100) default null,
|
||||
dropdown_default boolean default false,
|
||||
dropdown_active boolean default false,
|
||||
|
||||
dropdown_creator varchar(100) default null,
|
||||
dropdown_created datetime default null,
|
||||
dropdown_changer varchar(100) default null,
|
||||
dropdown_changed datetime default null,
|
||||
|
||||
primary key (dropdown_serial),
|
||||
unique key (dropdown_dropdowntype, dropdown_value),
|
||||
|
||||
foreign key (dropdown_dropdowntype)
|
||||
references dropdowntypes(dropdowntype_serial)
|
||||
on update cascade
|
||||
on delete restrict
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table dropdowntypes
|
||||
|
||||
(
|
||||
|
||||
dropdowntype_serial serial,
|
||||
dropdowntype_name varchar(100) default null,
|
||||
dropdowntype_title varchar(100) default null,
|
||||
dropdowntype_table varchar(100) default null,
|
||||
dropdowntype_column varchar(100) default null,
|
||||
|
||||
dropdowntype_creator varchar(100) default null,
|
||||
dropdowntype_created datetime default null,
|
||||
dropdowntype_changer varchar(100) default null,
|
||||
dropdowntype_changed datetime default null,
|
||||
|
||||
primary key (dropdowntype_serial),
|
||||
unique key (dropdowntype_name)
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table journal
|
||||
|
||||
(
|
||||
|
||||
journal_serial serial,
|
||||
journal_timestamp timestamp default current_timestamp,
|
||||
journal_origin varchar(100) default null,
|
||||
journal_ip varchar(128) default null,
|
||||
journal_entry varchar(1000) default null,
|
||||
|
||||
primary key (journal_serial),
|
||||
index (journal_timestamp)
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table notes
|
||||
|
||||
(
|
||||
|
||||
note_serial serial,
|
||||
note_type varchar(20) default null,
|
||||
note_reference bigint unsigned default 0,
|
||||
note_text text,
|
||||
note_timestamp timestamp default current_timestamp on update current_timestamp,
|
||||
note_origin varchar(100) default null,
|
||||
|
||||
primary key (note_serial),
|
||||
index (note_type),
|
||||
index (note_reference),
|
||||
index (note_timestamp)
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
?>
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table popovers
|
||||
|
||||
(
|
||||
|
||||
popover_serial serial,
|
||||
popover_name varchar(100) default null,
|
||||
popover_title varchar(100) default null,
|
||||
popover_placeholder varchar(100) default null,
|
||||
popover_text varchar(1000) default null,
|
||||
popover_creator varchar(100) default null,
|
||||
popover_created datetime default null,
|
||||
popover_changer varchar(100) default null,
|
||||
popover_changed datetime default null,
|
||||
|
||||
primary key (popover_serial),
|
||||
unique key (popover_name)
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table reports
|
||||
|
||||
(
|
||||
|
||||
report_serial serial,
|
||||
report_class varchar(100) default null,
|
||||
report_name varchar(100) default null,
|
||||
report_description varchar(100) default null,
|
||||
report_type varchar(10) default null,
|
||||
report_target varchar(50) default null,
|
||||
report_creator varchar(100) default null,
|
||||
report_created datetime default null,
|
||||
report_changer varchar(100) default null,
|
||||
report_changed datetime default null,
|
||||
|
||||
primary key (report_serial)
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table users
|
||||
|
||||
(
|
||||
|
||||
user_serial serial,
|
||||
user_id varchar(100) default null,
|
||||
user_name varchar(128) default null,
|
||||
user_email varchar(100) default null,
|
||||
user_client_name varchar(128) default null,
|
||||
user_password varchar(255) not null,
|
||||
user_role varchar(255) not null,
|
||||
user_change_password boolean default false,
|
||||
user_active boolean default true,
|
||||
user_login datetime default null,
|
||||
user_creator varchar(100) default null,
|
||||
user_created datetime default null,
|
||||
user_changer varchar(100) default null,
|
||||
user_changed datetime default null,
|
||||
|
||||
primary key (user_serial),
|
||||
unique key (user_name),
|
||||
index (user_name)
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
?>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
$SQL = "create table zipcodes
|
||||
|
||||
(
|
||||
|
||||
zipcode_serial serial,
|
||||
zipcode_code bigint not null default 0,
|
||||
zipcode_city varchar(50) not null default '',
|
||||
zipcode_state varchar(50) not null default '',
|
||||
zipcode_county varchar(50) not null default '',
|
||||
zipcode_country varchar(50) not null default '',
|
||||
|
||||
primary key (zipcode_serial),
|
||||
unique key (zipcode_code)
|
||||
|
||||
)
|
||||
|
||||
default character set = utf8";
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user