105 lines
3.8 KiB
PHP
105 lines
3.8 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'includes/S3Service.php';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
// User details
|
|
$name = $_POST['name'];
|
|
$email = $_POST['email'];
|
|
$password = $_POST['password'];
|
|
|
|
// Restaurant details
|
|
$restaurant_name = $_POST['restaurant_name'];
|
|
$restaurant_address = $_POST['restaurant_address'];
|
|
$restaurant_phone = $_POST['restaurant_phone'];
|
|
$lat = $_POST['lat'];
|
|
$lng = $_POST['lng'];
|
|
$location_label = $_POST['location_label'];
|
|
$location_notes = $_POST['location_notes'];
|
|
|
|
if (empty($name) || empty($email) || empty($password) || empty($restaurant_name) || empty($restaurant_address) || empty($restaurant_phone)) {
|
|
die('Please fill all required fields.');
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
die('Invalid email format.');
|
|
}
|
|
|
|
if (!is_numeric($lat) || !is_numeric($lng)) {
|
|
die('Invalid location data.');
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Check if email already exists
|
|
$sql = "SELECT id FROM users WHERE email = ?";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
throw new Exception('Email already exists.');
|
|
}
|
|
|
|
// Create the user with 'restaurant' role
|
|
$password_hash = password_hash($password, PASSWORD_BCRYPT);
|
|
$sql = "INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'restaurant_owner')";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$name, $email, $password_hash]);
|
|
$user_id = $pdo->lastInsertId();
|
|
|
|
// Create the restaurant
|
|
$sql = "INSERT INTO restaurants (name, address, phone_number, user_id, lat, lng, location_label, location_notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$restaurant_name, $restaurant_address, $restaurant_phone, $user_id, $lat, $lng, $location_label, $location_notes]);
|
|
$restaurant_id = $pdo->lastInsertId();
|
|
|
|
// Handle file uploads
|
|
if (isset($_FILES['documents'])) {
|
|
foreach ($_FILES['documents']['tmp_name'] as $doc_id => $tmp_path) {
|
|
if (!empty($tmp_path) && is_uploaded_file($tmp_path)) {
|
|
$file_name = $_FILES['documents']['name'][$doc_id];
|
|
$file_error = $_FILES['documents']['error'][$doc_id];
|
|
|
|
if ($file_error !== UPLOAD_ERR_OK) {
|
|
throw new Exception("Failed to upload file: " . $file_name);
|
|
}
|
|
|
|
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
|
|
$key = "documents/restaurants/{$user_id}/{$doc_id}_" . time() . "." . $extension;
|
|
|
|
$s3_url = S3Service::uploadFile($tmp_path, $key);
|
|
|
|
if ($s3_url) {
|
|
$sql = "INSERT INTO user_documents (user_id, document_id, file_path) VALUES (?, ?, ?)";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$user_id, $doc_id, $s3_url]);
|
|
} else {
|
|
throw new Exception("Failed to upload document to S3.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
// Log the user in
|
|
$_SESSION['user_id'] = $user_id;
|
|
$_SESSION['user_name'] = $name;
|
|
$_SESSION['user_role'] = 'restaurant';
|
|
$_SESSION['restaurant_id'] = $restaurant_id;
|
|
|
|
// Redirect to the restaurant dashboard
|
|
header("Location: restaurant/index.php");
|
|
exit;
|
|
|
|
} catch (Exception $e) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
die("Error: " . $e->getMessage());
|
|
}
|
|
}
|
|
?>
|