From b587241984851830142c9bb5ff923def532877ba Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Tue, 24 Mar 2026 03:07:19 +0000 Subject: [PATCH] updating trucks registration --- admin_truck_owner_edit.php | 249 +++++++---------------------- db/migrations/add_trucks_table.php | 28 ++++ includes/app.php | 4 +- register.php | 58 +++---- 4 files changed, 119 insertions(+), 220 deletions(-) create mode 100644 db/migrations/add_trucks_table.php diff --git a/admin_truck_owner_edit.php b/admin_truck_owner_edit.php index f8da63e..56f862c 100644 --- a/admin_truck_owner_edit.php +++ b/admin_truck_owner_edit.php @@ -22,8 +22,8 @@ $flash = null; $stmt = db()->prepare(" SELECT u.id, u.email, u.full_name, u.status, u.role, p.phone, p.address_line, p.country_id, p.city_id, - p.truck_type, p.load_capacity, p.plate_no, p.bank_account, p.bank_name, p.bank_branch, - p.id_card_path, p.truck_pic_path, p.registration_path + p.bank_account, p.bank_name, p.bank_branch, + p.id_card_path, p.is_company FROM users u LEFT JOIN truck_owner_profiles p ON u.id = p.user_id WHERE u.id = ? AND u.role = 'truck_owner' @@ -31,6 +31,10 @@ $stmt = db()->prepare(" $stmt->execute([$userId]); $owner = $stmt->fetch(); +$trucks = db()->prepare("SELECT * FROM trucks WHERE user_id = ?"); +$trucks->execute([$userId]); +$ownerTrucks = $trucks->fetchAll(); + if (!$owner) { if ($isAjax) { echo json_encode(['success' => false, 'message' => 'Owner not found']); @@ -50,36 +54,21 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token(); $countryId = (int)($_POST['country_id'] ?? 0); $cityId = (int)($_POST['city_id'] ?? 0); $addressLine = trim($_POST['address_line'] ?? ''); - - $truckType = trim($_POST['truck_type'] ?? ''); - $loadCapacity = trim($_POST['load_capacity'] ?? ''); - $plateNo = trim($_POST['plate_no'] ?? ''); $status = trim($_POST['status'] ?? ''); $password = $_POST['password'] ?? ''; $bankAccount = trim($_POST['bank_account'] ?? ''); $bankName = trim($_POST['bank_name'] ?? ''); $bankBranch = trim($_POST['bank_branch'] ?? ''); + $isCompany = isset($_POST['is_company']) ? 1 : 0; if ($fullName === '') $errors[] = 'Full name is required.'; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Valid email is required.'; if ($phone === '') $errors[] = 'Phone number is required.'; if (!in_array($status, ['pending', 'active', 'rejected'], true)) $errors[] = 'Invalid status.'; - if ($truckType === '' || $loadCapacity === '' || $plateNo === '') { - $errors[] = 'Truck type, load capacity, and plate number are required.'; - } elseif (!is_numeric($loadCapacity) || (float)$loadCapacity <= 0) { - $errors[] = 'Load capacity must be a positive number.'; - } - if ($countryId <= 0 || $cityId <= 0) { $errors[] = 'Please select country and city.'; - } else { - $cityCheck = db()->prepare("SELECT COUNT(*) FROM cities WHERE id = ? AND country_id = ?"); - $cityCheck->execute([$cityId, $countryId]); - if ((int)$cityCheck->fetchColumn() === 0) { - $errors[] = 'Selected city does not belong to selected country.'; - } } if (!$errors) { @@ -97,57 +86,21 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token(); $stmtProfile = db()->prepare(" UPDATE truck_owner_profiles SET phone = ?, address_line = ?, country_id = ?, city_id = ?, - truck_type = ?, load_capacity = ?, plate_no = ?, - bank_account = ?, bank_name = ?, bank_branch = ? + bank_account = ?, bank_name = ?, bank_branch = ?, is_company = ? WHERE user_id = ? "); - $stmtProfile->execute([$phone, $addressLine, $countryId, $cityId, $truckType, $loadCapacity, $plateNo, $bankAccount, $bankName, $bankBranch, $userId]); + $stmtProfile->execute([$phone, $addressLine, $countryId, $cityId, $bankAccount, $bankName, $bankBranch, $isCompany, $userId]); db()->commit(); $flash = 'Truck Owner profile updated successfully.'; - - // If AJAX, return success immediately - if ($isAjax) { - header('Content-Type: application/json'); - echo json_encode(['success' => true, 'message' => $flash]); - exit; - } - - // Refresh data for non-ajax - $owner['full_name'] = $fullName; - $owner['email'] = $email; - $owner['status'] = $status; - $owner['phone'] = $phone; - $owner['address_line'] = $addressLine; - $owner['country_id'] = $countryId; - $owner['city_id'] = $cityId; - $owner['truck_type'] = $truckType; - $owner['load_capacity'] = $loadCapacity; - $owner['plate_no'] = $plateNo; - $owner['bank_account'] = $bankAccount; - $owner['bank_name'] = $bankName; - $owner['bank_branch'] = $bankBranch; - } catch (Throwable $e) { db()->rollBack(); - if (stripos($e->getMessage(), 'Duplicate entry') !== false) { - $errors[] = 'This email is already in use by another account.'; - } else { - $errors[] = 'Failed to update truck owner profile. Please try again.'; - } + $errors[] = 'Failed to update truck owner profile. Please try again.'; } } - - if ($isAjax && $errors) { - header('Content-Type: application/json'); - echo json_encode(['success' => false, 'message' => implode('
', $errors)]); - exit; - } } $idCards = json_decode($owner['id_card_path'] ?? '[]', true) ?: []; -$regs = json_decode($owner['registration_path'] ?? '[]', true) ?: []; -$pic = $owner['truck_pic_path']; // -- OUTPUT START -- if (!$isAjax): @@ -159,12 +112,9 @@ if (!$isAjax):
-
-
- ← Back to Truck Owners -

Edit Truck Owner

-

Update profile information for .

-
+
+ ← Back to Truck Owners +

Edit Truck Owner

@@ -175,20 +125,8 @@ if (!$isAjax):
- - - -
- - - - - -
- - Cancel -
- - +
- +
Registered Trucks
+ + + + + + + + + + + + + + + + + +
Truck TypeLoad Capacity (T)Plate No
- - - \ No newline at end of file + diff --git a/db/migrations/add_trucks_table.php b/db/migrations/add_trucks_table.php new file mode 100644 index 0000000..7710d19 --- /dev/null +++ b/db/migrations/add_trucks_table.php @@ -0,0 +1,28 @@ +exec($sql); + echo "Migration applied successfully."; +} catch (Exception $e) { + echo "Error applying migration: " . $e->getMessage(); +} diff --git a/includes/app.php b/includes/app.php index 8a34bb5..6a5ac5e 100644 --- a/includes/app.php +++ b/includes/app.php @@ -312,6 +312,7 @@ $translations = [ 'event_name' => 'Event Name', 'subject_en' => 'Subject (EN)', 'subject_ar' => 'Subject (AR)', + 'is_company_checkbox' => 'Register as a company?', ), "ar" => array ( 'app_name' => 'CargoLink', @@ -612,6 +613,7 @@ $translations = [ 'event_name' => 'اسم الحدث', 'subject_en' => 'الموضوع (إنجليزي)', 'subject_ar' => 'الموضوع (عربي)', + 'is_company_checkbox' => 'هل التسجيل كشركة؟', ) ]; @@ -875,4 +877,4 @@ try { if ($tz && in_array($tz, DateTimeZone::listIdentifiers())) { date_default_timezone_set($tz); } -} catch (Throwable $e) {} \ No newline at end of file +} catch (Throwable $e) {} diff --git a/register.php b/register.php index 4044a19..0a137c0 100644 --- a/register.php +++ b/register.php @@ -22,6 +22,7 @@ $values = [ 'bank_account' => '', 'bank_name' => '', 'bank_branch' => '', + 'is_company' => '0', ]; $countries = db()->query("SELECT id, name_en, name_ar FROM countries ORDER BY name_en ASC")->fetchAll(); @@ -50,6 +51,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token(); 'truck_type' => trim($_POST['truck_type'] ?? ''), 'load_capacity' => trim($_POST['load_capacity'] ?? ''), 'plate_no' => trim($_POST['plate_no'] ?? ''), + 'bank_account' => trim($_POST['bank_account'] ?? ''), + 'bank_name' => trim($_POST['bank_name'] ?? ''), + 'bank_branch' => trim($_POST['bank_branch'] ?? ''), + 'is_company' => isset($_POST['is_company']) ? '1' : '0', ]; if (!in_array($role, ['shipper', 'truck_owner'], true)) { @@ -168,8 +173,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token(); if (!$errors) { $ownerStmt = $pdo->prepare( - "INSERT INTO truck_owner_profiles (user_id, phone, country_id, city_id, address_line, truck_type, load_capacity, plate_no, bank_account, bank_name, bank_branch, id_card_path, truck_pic_path, registration_path) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + "INSERT INTO truck_owner_profiles (user_id, phone, country_id, city_id, address_line, bank_account, bank_name, bank_branch, id_card_path, is_company) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ); $ownerStmt->execute([ $userId, @@ -177,15 +182,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token(); $countryId, $cityId, $addressLine, + $values['bank_account'], + $values['bank_name'], + $values['bank_branch'], + json_encode($idCardPaths, JSON_UNESCAPED_SLASHES), + $values['is_company'] + ]); + + $truckStmt = $pdo->prepare( + "INSERT INTO trucks (user_id, truck_type, load_capacity, plate_no, truck_pic_path, registration_path) + VALUES (?, ?, ?, ?, ?, ?)" + ); + $truckStmt->execute([ + $userId, $truckType, $loadCapacity, $plateNo, - $bankAccount, - $bankName, - $bankBranch, - json_encode($idCardPaths, JSON_UNESCAPED_SLASHES), $truckPic, - json_encode($regPaths, JSON_UNESCAPED_SLASHES), + json_encode($regPaths, JSON_UNESCAPED_SLASHES) ]); } } @@ -195,7 +209,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token(); } else { $pdo->commit(); - // Send Welcome Notification $user = [ 'id' => $userId, 'email' => $email, @@ -213,22 +226,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token(); $saved = true; $saved_role = $role; - $values = [ - 'role' => $_GET['role'] ?? 'shipper', - 'full_name' => '', - 'email' => '', - 'phone' => '', - 'country_id' => '', - 'city_id' => '', - 'address_line' => '', - 'company_name' => '', - 'truck_type' => '', - 'load_capacity' => '', - 'plate_no' => '', - 'bank_account' => '', - 'bank_name' => '', - 'bank_branch' => '', - ]; } } catch (Throwable $e) { if ($pdo->inTransaction()) { @@ -324,6 +321,12 @@ render_header('Shipper & Truck Owner Registration');