From 7c872188a257b3047c177c7eb81078dbf0018547 Mon Sep 17 00:00:00 2001 From: RiFav <13521075@std.stei.itb.ac.id> Date: Wed, 4 Oct 2023 21:49:41 +0700 Subject: [PATCH] fix: update event on edit (temp) --- app/Models/Event.php | 54 ++++++++++++++++++++++++++++++++++---------- home.php | 46 ++++++++++++++++++++++--------------- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/app/Models/Event.php b/app/Models/Event.php index 81711d8..cebd4d8 100644 --- a/app/Models/Event.php +++ b/app/Models/Event.php @@ -9,23 +9,21 @@ class EventModel { echo "createEvent method called<br>"; global $db; - // Check if a photo is uploaded if ($gambar !== null && isset($gambar['name']) && isset($gambar['tmp_name'])) { - $photoFilePath = 'assets/photos/' . $gambar['name']; + $imageFilePath = 'assets/images/' . $gambar['name']; // Move the uploaded file to the desired directory - if (move_uploaded_file($gambar['tmp_name'], $photoFilePath)) { + if (move_uploaded_file($gambar['tmp_name'], $imageFilePath)) { echo "File uploaded successfully<br>"; } else { echo "Error moving file to destination<br>"; - $photoFilePath = null; // Set to null if there's an error + $imageFilePath = null; // Set to null if there's an error } } else { - echo "No photo uploaded<br>"; - $photoFilePath = null; // Set to null if no file is uploaded + echo "No image uploaded<br>"; + $imageFilePath = null; // Set to null if no file is uploaded } - // Check if a video is uploaded if ($vid !== null && isset($vid['name']) && isset($vid['tmp_name'])) { $vidFilePath = 'assets/videos/' . $vid['name']; @@ -43,7 +41,7 @@ class EventModel { $stmt = $db->prepare("INSERT INTO events (event_name, event_stock, event_date, event_details, gambar, event_location, vid) VALUES (?, ?, ?, ?, ?, ?, ?)"); - if ($stmt->execute([$event_name, $event_stock, $event_date, $details, $photoFilePath, $event_location, $vidFilePath])) { + if ($stmt->execute([$event_name, $event_stock, $event_date, $details, $imageFilePath, $event_location, $vidFilePath])) { echo "Event created successfully"; } else { echo "Error creating event: " . print_r($stmt->errorInfo(), true); @@ -55,13 +53,45 @@ class EventModel { $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1); echo "Updating Event with ID: $event_id, Stock: $event_stock, Event_Date: $event_date, Details: $details"; + + if ($gambar !== null && isset($gambar['name']) && isset($gambar['tmp_name'])) { + $imageFilePath = 'assets/images/' . $gambar['name']; - // Check whether a new image is provided - if ($gambar !== null) { - $sql = "UPDATE events SET event_name = '$event_name', event_stock = '$event_stock', event_date = '$event_date', event_details = '$details', gambar = '$gambar', event_location = '$event_location', vid = '$vid' WHERE event_id = $event_id"; + // Move the uploaded file to the desired directory + if (move_uploaded_file($gambar['tmp_name'], $imageFilePath)) { + echo "File uploaded successfully<br>"; + } else { + echo "Error moving file to destination<br>"; + $imageFilePath = null; // Set to null if there's an error + } } else { - $sql = "UPDATE events SET event_name = '$event_name', event_stock = '$event_stock', event_date = '$event_date', event_details = '$details', event_location = '$event_location', vid = '$vid' WHERE event_id = $event_id"; + echo "No image uploaded<br>"; + $imageFilePath = null; // Set to null if no file is uploaded } + + if ($vid !== null && isset($vid['name']) && isset($vid['tmp_name'])) { + $vidFilePath = 'assets/videos/' . $vid['name']; + + // Move the uploaded file to the desired directory + if (move_uploaded_file($vid['tmp_name'], $vidFilePath)) { + echo "File uploaded successfully<br>"; + } else { + echo "Error moving file to destination<br>"; + $vidFilePath = null; // Set to null if there's an error + } + } else { + echo "No video uploaded<br>"; + $vidFilePath = null; // Set to null if no file is uploaded + } + + // Check whether a new image is provided + // if ($gambar !== null) { + // $sql = "UPDATE events SET event_name = '$event_name', event_stock = '$event_stock', event_date = '$event_date', event_details = '$details', gambar = '$gambar', event_location = '$event_location', vid = '$vid' WHERE event_id = $event_id"; + // } else { + // $sql = "UPDATE events SET event_name = '$event_name', event_stock = '$event_stock', event_date = '$event_date', event_details = '$details', event_location = '$event_location', vid = '$vid' WHERE event_id = $event_id"; + // } + + $sql = "UPDATE events SET event_name = '$event_name', event_stock = '$event_stock', event_date = '$event_date', event_details = '$details', gambar = '$imageFilePath', event_location = '$event_location', vid = '$vidFilePath' WHERE event_id = $event_id"; $rowCount = $db->exec($sql); diff --git a/home.php b/home.php index 2b0c309..ba33bbc 100644 --- a/home.php +++ b/home.php @@ -27,12 +27,12 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $eventLocation = $_POST['event_location']; // $ = $_POST['vid']; - $photoFilePath = isset($_FILES['gambar']) && $_FILES['gambar']['error'] === 0 ? $_FILES['gambar'] : null; // Check if 'gambar' is set in the files array + $imageFilePath = isset($_FILES['gambar']) && $_FILES['gambar']['error'] === 0 ? $_FILES['gambar'] : null; // Check if 'gambar' is set in the files array $vidFilePath = isset($_FILES['vid']) && $_FILES['vid']['error'] === 0 ? $_FILES['vid'] : null; // Check if 'vid' is set in the files array // Check if the values are not empty if (!empty($eventName) && !empty($eventStock) && !empty($eventDate) && !empty($eventDetails) - && !empty($eventLocation) && $photoFilePath !== null && $vidFilePath !== null) { + && !empty($eventLocation) && $imageFilePath !== null && $vidFilePath !== null) { var_dump($_FILES); // Output for debugging purposes // // Check for file upload @@ -46,30 +46,36 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { // move_uploaded_file($uploadedFile['tmp_name'], $uploadedFilePath); // } - $eventController->createEvent($eventName, $eventStock, $eventCreatedTime, $eventDetails, $uploadedFilePath, $eventLocation, $vid); + $eventController->createEvent($eventName, $eventStock, $eventDate, $eventDetails, $imageFilePath, $eventLocation, $vidFilePath); } else { echo "Please fill in all the fields for creating an event."; } } elseif ($_POST['action'] === 'updateEvent') { $eventIdUpdate = $_POST['eventIdUpdate']; + $eventName = $_POST['event_name']; $eventStock = $_POST['eventStock']; $eventDate = $_POST['event_date']; $eventDetails = $_POST['eventDetails']; + $eventLocation = $_POST['event_location']; + + $imageFilePath = isset($_FILES['gambar']) && $_FILES['gambar']['error'] === 0 ? $_FILES['gambar'] : null; // Check if 'gambar' is set in the files array + $vidFilePath = isset($_FILES['vid']) && $_FILES['vid']['error'] === 0 ? $_FILES['vid'] : null; // Check if 'vid' is set in the files array - var_dump($_FILES); + var_dump($_FILES); // Output for debugging purposes // Check if the values are not empty - if (!empty($eventIdUpdate) && !empty($eventStock) && !empty($eventCreatedTime) && !empty($eventDetails)) { - $uploadedFilePath = isset($_FILES['gambar']) ? $_FILES['gambar'] : null; // Check if 'gambar' is set in the files array + if (!empty($eventIdUpdate) && !empty($eventName) && !empty($eventStock) && !empty($eventDate) + && !empty($eventDetails) && !empty($eventLocation) && $imageFilePath !== null && $vidFilePath !== null) { + // $uploadedFilePath = isset($_FILES['gambar']) ? $_FILES['gambar'] : null; // Check if 'gambar' is set in the files array - $uploadedFile = isset($_FILES['gambar']) ? $_FILES['gambar'] : null; - // Check for file upload - if ($uploadedFilePath !== null && $uploadedFilePath['error'] === 0) { - // Handle file upload - $uploadedFilePath = 'assets/' . $uploadedFile['name']; - move_uploaded_file($uploadedFile['tmp_name'], $uploadedFilePath); - } - - $eventController->updateEvent($eventIdUpdate, $eventStock, $eventCreatedTime, $eventDetails, $uploadedFilePath); + // $uploadedFile = isset($_FILES['gambar']) ? $_FILES['gambar'] : null; + // // Check for file upload + // if ($uploadedFilePath !== null && $uploadedFilePath['error'] === 0) { + // // Handle file upload + // $uploadedFilePath = 'assets/' . $uploadedFile['name']; + // move_uploaded_file($uploadedFile['tmp_name'], $uploadedFilePath); + // } + + $eventController->updateEvent($eventIdUpdate, $eventName, $eventStock, $eventDate, $eventDetails, $imageFilePath, $eventLocation, $vidFilePath); } else { echo "Please fill in all the fields for updating an event."; } @@ -199,7 +205,11 @@ $users = $userController->getAllUsers(); </div> <input type="text" name="event_location" placeholder="Event Location"> - <input type="text" name="vid" placeholder="Video URL"> + <!-- <input type="text" name="vid" placeholder="Video URL"> --> + <div id="drop-area-update"> + <p>Drag and drop an video file here or click to select one.</p> + <input type="file" id="file-input-update" name="vid" accept="video/*"> + </div> <button type="submit" name="action" value="updateEvent">Update</button> </form> @@ -258,9 +268,9 @@ $users = $userController->getAllUsers(); </div> - <div class="footer"> + <!-- <div class="footer"> © <?php echo date("Y"); ?> Ticket Ku. All rights reserved. - </div> + </div> --> <script defer> function toggleMenu() { -- GitLab