39038-vm/register.php
2026-03-07 13:28:03 +00:00

201 lines
8.3 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php';
$errors = [];
$saved = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$role = $_POST['role'] ?? 'shipper';
$email = trim($_POST['email'] ?? '');
$passwordRaw = (string)($_POST['password'] ?? '');
if (!in_array($role, ['shipper', 'truck_owner'], true)) {
$errors[] = 'Invalid role selected.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Please provide a valid email address.';
}
if (strlen($passwordRaw) < 6) {
$errors[] = 'Password must be at least 6 characters.';
}
if (!$errors) {
$password = password_hash($passwordRaw, PASSWORD_DEFAULT);
$stmt = db()->prepare("INSERT INTO users (email, password, role) VALUES (?, ?, ?)");
$stmt->execute([$email, $password, $role]);
$userId = (int)db()->lastInsertId();
if ($role === 'truck_owner') {
$truckType = trim($_POST['truck_type'] ?? '');
$loadCapacity = trim($_POST['load_capacity'] ?? '');
$plateNo = trim($_POST['plate_no'] ?? '');
if ($truckType === '' || $loadCapacity === '' || $plateNo === '') {
$errors[] = 'Please complete truck details.';
} elseif (!is_numeric($loadCapacity)) {
$errors[] = 'Load capacity must be numeric.';
}
if (!$errors) {
$uploadDir = __DIR__ . '/uploads/profiles/' . $userId . '/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0775, true);
}
$allowed = ['image/jpeg' => 'jpg', 'image/png' => 'png', 'image/webp' => 'webp'];
$saveImage = static function (string $tmpName, string $prefix) use ($uploadDir, $allowed): ?string {
$mime = mime_content_type($tmpName) ?: '';
if (!isset($allowed[$mime])) {
return null;
}
$filename = uniqid($prefix, true) . '.' . $allowed[$mime];
$target = $uploadDir . $filename;
if (!move_uploaded_file($tmpName, $target)) {
return null;
}
return 'uploads/profiles/' . basename($uploadDir) . '/' . $filename;
};
$idCardPaths = [];
foreach (array_slice($_FILES['id_card']['tmp_name'] ?? [], 0, 2) as $tmp) {
if (!is_uploaded_file($tmp)) {
continue;
}
$path = $saveImage($tmp, 'id_');
if ($path) {
$idCardPaths[] = $path;
}
}
$regPaths = [];
foreach (array_slice($_FILES['registration']['tmp_name'] ?? [], 0, 2) as $tmp) {
if (!is_uploaded_file($tmp)) {
continue;
}
$path = $saveImage($tmp, 'reg_');
if ($path) {
$regPaths[] = $path;
}
}
$truckPic = null;
$truckTmp = $_FILES['truck_picture']['tmp_name'] ?? '';
if (is_uploaded_file($truckTmp)) {
$truckPic = $saveImage($truckTmp, 'truck_');
}
if (count($idCardPaths) < 2 || count($regPaths) < 2 || !$truckPic) {
$errors[] = 'Please upload all required truck-owner images (ID front/back, registration front/back, truck photo).';
} else {
$profileStmt = db()->prepare(
"INSERT INTO truck_owner_profiles (user_id, truck_type, load_capacity, plate_no, id_card_path, truck_pic_path, registration_path)
VALUES (?, ?, ?, ?, ?, ?, ?)"
);
$profileStmt->execute([
$userId,
$truckType,
$loadCapacity,
$plateNo,
json_encode($idCardPaths, JSON_UNESCAPED_SLASHES),
$truckPic,
json_encode($regPaths, JSON_UNESCAPED_SLASHES),
]);
}
}
}
if (!$errors) {
$saved = true;
}
}
}
render_header('Register Account');
?>
<div class="page-intro">
<h1 class="section-title mb-1">Create account</h1>
<p class="muted mb-0">Register as a shipper or truck owner using a clean onboarding form.</p>
</div>
<div class="panel p-4">
<?php if ($saved): ?>
<div class="alert alert-success">Registration completed successfully.</div>
<?php endif; ?>
<?php if ($errors): ?>
<div class="alert alert-warning"><?= e(implode(' ', $errors)) ?></div>
<?php endif; ?>
<form method="post" enctype="multipart/form-data" id="regForm" novalidate>
<div class="row g-3">
<div class="col-md-4">
<label class="form-label" for="role">Role</label>
<select name="role" id="role" class="form-select" onchange="toggleFields()" required>
<option value="shipper">Shipper</option>
<option value="truck_owner">Truck Owner</option>
</select>
</div>
<div class="col-md-4">
<label class="form-label" for="email">Email</label>
<input type="email" name="email" id="email" class="form-control" required>
</div>
<div class="col-md-4">
<label class="form-label" for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" minlength="6" required>
</div>
</div>
<div id="truckFields" class="mt-4" style="display:none;">
<h2 class="h5 mb-3">Truck owner details</h2>
<div class="row g-3">
<div class="col-md-4">
<label class="form-label" for="truck_type">Truck type</label>
<input type="text" name="truck_type" id="truck_type" class="form-control">
</div>
<div class="col-md-4">
<label class="form-label" for="load_capacity">Load capacity (tons)</label>
<input type="number" name="load_capacity" id="load_capacity" class="form-control" step="0.01" min="0.1">
</div>
<div class="col-md-4">
<label class="form-label" for="plate_no">Plate number</label>
<input type="text" name="plate_no" id="plate_no" class="form-control">
</div>
<div class="col-md-4">
<label class="form-label" for="id_card">ID card (front & back)</label>
<input type="file" name="id_card[]" id="id_card" class="form-control" accept="image/png,image/jpeg,image/webp" multiple>
</div>
<div class="col-md-4">
<label class="form-label" for="truck_picture">Clear truck photo</label>
<input type="file" name="truck_picture" id="truck_picture" class="form-control" accept="image/png,image/jpeg,image/webp">
</div>
<div class="col-md-4">
<label class="form-label" for="registration">Truck registration (front & back)</label>
<input type="file" name="registration[]" id="registration" class="form-control" accept="image/png,image/jpeg,image/webp" multiple>
</div>
</div>
</div>
<div class="mt-4 d-flex gap-2">
<button type="submit" class="btn btn-primary">Create account</button>
<a class="btn btn-outline-dark" href="<?= e(url_with_lang('admin_dashboard.php')) ?>">Back to admin</a>
</div>
</form>
</div>
<script>
function toggleFields() {
const role = document.getElementById('role').value;
const truckFields = document.getElementById('truckFields');
const isOwner = role === 'truck_owner';
truckFields.style.display = isOwner ? 'block' : 'none';
truckFields.querySelectorAll('input').forEach((input) => {
input.required = isOwner;
});
}
toggleFields();
</script>
<?php render_footer(); ?>