98 lines
3.9 KiB
PHP
98 lines
3.9 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'includes/S3Service.php';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$full_name = $_POST['full_name'];
|
|
$email = $_POST['email'];
|
|
$password = $_POST['password'];
|
|
$phone_number = $_POST['phone_number'];
|
|
$vehicle_details = $_POST['vehicle_details'];
|
|
|
|
if (empty($full_name) || empty($email) || empty($password) || empty($phone_number) || empty($vehicle_details)) {
|
|
die('Please fill all required fields.');
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
die('Invalid email format.');
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Check if user 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.');
|
|
}
|
|
|
|
// Insert into users table
|
|
$password_hash = password_hash($password, PASSWORD_BCRYPT);
|
|
$sql = "INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'driver')";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$full_name, $email, $password_hash]);
|
|
$user_id = $pdo->lastInsertId();
|
|
|
|
// Insert into drivers table
|
|
$sql = "INSERT INTO drivers (user_id, full_name, phone_number, vehicle_details, approval_status) VALUES (?, ?, ?, ?, 'pending')";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$user_id, $full_name, $phone_number, $vehicle_details]);
|
|
|
|
// 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/drivers/{$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.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Send email notification to admins
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
$stmt_emails = $pdo->prepare("SELECT email FROM email_recipients WHERE form_type = ?");
|
|
$stmt_emails->execute(['driver_signup']);
|
|
$recipients = $stmt_emails->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
if (!empty($recipients)) {
|
|
$subject = 'New Driver Signup: ' . $full_name;
|
|
$html_content = "<p>A new driver has signed up and is awaiting approval.</p><p><strong>Name:</strong> {$full_name}</p><p><strong>Email:</strong> {$email}</p><p>Please visit the admin panel to review and approve the application.</p>";
|
|
$text_content = "A new driver has signed up and is awaiting approval.\nName: {$full_name}\nEmail: {$email}\nPlease visit the admin panel to review and approve the application.";
|
|
MailService::sendMail($recipients, $subject, $html_content, $text_content);
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
header("Location: driver_pending_approval.php");
|
|
exit;
|
|
|
|
} catch (Exception $e) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
die("Error: " . $e->getMessage());
|
|
}
|
|
}
|
|
?>
|