38474-vm/setup.php
2026-02-18 08:32:50 +00:00

107 lines
4.2 KiB
PHP

<?php
// setup.php - Enterprise Setup Script
require_once __DIR__ . '/db/config.php';
echo "<h1>AFG CARS Enterprise Setup</h1>";
try {
global $pdo; // Assumes db/config.php creates $pdo
echo "<h3>1. Initializing Database Schema...</h3>";
// Read SQL file
$sql_file = __DIR__ . '/db/database.sql';
if (!file_exists($sql_file)) {
throw new Exception("Database SQL file not found at: $sql_file");
}
$sql_content = file_get_contents($sql_file);
// Split into individual queries (basic splitting by semicolon)
// Note: This is a simple splitter and might break on complex stored procedures, but sufficient for this schema.
$queries = explode(';', $sql_content);
foreach ($queries as $query) {
$query = trim($query);
if (!empty($query)) {
$pdo->exec($query);
}
}
echo "<p style='color:green'>Schema imported successfully.</p>";
echo "<h3>2. Seeding Data...</h3>";
// Seed Users (Admin & Customer)
// Check if admin exists to avoid duplicates if re-run
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email = ?");
$stmt->execute(['admin@afgcars.com']);
if ($stmt->fetchColumn() == 0) {
$password = password_hash('admin123', PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, ?)");
$stmt->execute(['Administrator', 'admin@afgcars.com', $password, 'admin']);
echo "<p>Admin user created (admin@afgcars.com / admin123)</p>";
}
$stmt->execute(['John Doe', 'user@example.com', password_hash('user123', PASSWORD_DEFAULT), 'user']);
echo "<p>Demo user created (user@example.com / user123)</p>";
// Seed Branches
// Tables are fresh from schema import, so no need to truncate
$branches = [
['Kabul Main', 'Kabul', 'Shar-e-Naw, Kabul', '+93 700 111 222'],
['Herat Branch', 'Herat', 'Main Road, Herat', '+93 700 333 444'],
['Mazar Center', 'Mazar-i-Sharif', 'Balkh Street, Mazar', '+93 700 555 666'],
['Kandahar Hub', 'Kandahar', 'Airport Road, Kandahar', '+93 700 777 888']
];
$stmt = $pdo->prepare("INSERT INTO branches (name, city, address, phone) VALUES (?, ?, ?, ?)");
foreach ($branches as $branch) {
$stmt->execute($branch);
}
echo "<p>Branches seeded.</p>";
// Seed Cars
// $pdo->exec("SET FOREIGN_KEY_CHECKS=0");
// $pdo->exec("TRUNCATE TABLE cars");
// $pdo->exec("SET FOREIGN_KEY_CHECKS=1");
$brands = ['Toyota', 'Lexus', 'Mercedes-Benz', 'BMW', 'Audi', 'Land Rover', 'Porsche', 'Tesla'];
$models = [
'Toyota' => ['Camry', 'Land Cruiser', 'Corolla', 'RAV4'],
'Lexus' => ['LX 600', 'RX 350', 'ES 350'],
'Mercedes-Benz' => ['S-Class', 'G-Wagon', 'E-Class'],
'BMW' => ['X7', 'X5', '7 Series'],
'Audi' => ['Q8', 'A8', 'RS7'],
'Land Rover' => ['Defender', 'Range Rover'],
'Porsche' => ['911 Carrera', 'Cayenne'],
'Tesla' => ['Model S', 'Model X']
];
$stmt = $pdo->prepare("INSERT INTO cars (brand, model, year, price, mileage, fuel_type, transmission, description, image_path, branch_id, is_featured, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for ($i = 1; $i <= 20; $i++) {
$brand = $brands[array_rand($brands)];
$model = $models[$brand][array_rand($models[$brand])];
$year = rand(2020, 2024);
$price = rand(25000, 150000);
$mileage = rand(0, 50000);
$fuel = rand(0, 1) ? 'Petrol' : 'Hybrid';
$desc = "Premium condition $brand $model. Full options, well maintained.";
$image = "assets/images/cars/car{$i}.jpg";
$branch_id = rand(1, 4);
$is_featured = ($i <= 6) ? 1 : 0; // First 6 are featured
$stmt->execute([
$brand, $model, $year, $price, $mileage, $fuel, 'Automatic',
$desc, $image, $branch_id, $is_featured, 'available'
]);
}
echo "<p>20 Demo cars seeded.</p>";
echo "<h3>Setup Complete!</h3>";
echo "<p><a href='index.php'>Go to Homepage</a></p>";
} catch (Exception $e) {
die("<h3 style='color:red'>Setup Failed: " . $e->getMessage() . "</h3>");
}