Skip to content
Snippets Groups Projects
Forked from IF3110-2023-01-18 / Tugas Besar 1
30 commits behind the upstream repository.
Event.php 8.30 KiB
<?php

require_once(__DIR__ . '/../../db/connect.php');
require_once(__DIR__ . '/../util.php');

class EventModel {

        public function createEvent($event_name, $stock, $event_price, $event_date, $event_location, $gambar, $vid) {
        global $db;
        $success = true;
    
        if ($gambar !== null && isset($gambar['name']) && isset($gambar['tmp_name'])) {
            $imageFilePath = '../assets/images/' . $gambar['name'];
    
            // Move the uploaded file to the desired directory
            if (move_uploaded_file($gambar['tmp_name'], $imageFilePath)) {
                echo "File uploaded successfully";
            } else {
                echo "Error moving file to destination";
                $imageFilePath = null; // Set to null if there's an error
            }
        } else {
            echo "No image uploaded";
            $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";
            } else {
                echo "Error moving file to destination";
                $vidFilePath = null; // Set to null if there's an error
            }
        } else {
            echo "No video uploaded";
            $vidFilePath = null; // Set to null if no file is uploaded
        }

        if ($imageFilePath === null || $vidFilePath === null) {
            $success = false;
            return $success;
        }
    
        $stmt = $db->prepare("INSERT INTO events (event_name, event_stock, event_price, event_date, event_location, gambar, vid) VALUES (?, ?, ?, ?, ?, ?, ?)");
    
        if ($stmt->execute([$event_name, $stock, $event_price, $event_date, $event_location, $imageFilePath, $vidFilePath])) {
            echo "Event created successfully";
            return $success;
        } else {
            echo "Error creating event: " . print_r($stmt->errorInfo(), true);
            $success = false;
            return $success;
        }
    }
    
    public function updateEvent($event_id, $event_name, $event_price, $event_date, $event_location, $gambar, $vid) {
        global $db;
        $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
    
        echo "Updating Event with ID: $event_id, Name: $event_name, Price: $event_price, Date: $event_date, Location: $event_location";
        
        if ($gambar !== null && isset($gambar['name']) && isset($gambar['tmp_name'])) {
            $imageFilePath = '../assets/images/' . $gambar['name'];
    
            // 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 {
            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
        }
        
        // chek wheter image or video is null or not
        if ($imageFilePath === null && $vidFilePath === null) {
            $sql = "UPDATE events SET event_name = '$event_name', event_price = '$event_price', event_date = '$event_date', event_location = '$event_location' WHERE event_id = $event_id";
        } elseif ($imageFilePath === null) {
            $sql = "UPDATE events SET event_name = '$event_name', event_price = '$event_price', event_date = '$event_date', event_location = '$event_location', vid = '$vidFilePath' WHERE event_id = $event_id";
        } elseif ($vidFilePath === null) {
            $sql = "UPDATE events SET event_name = '$event_name', event_price = '$event_price', event_date = '$event_date', event_location = '$event_location', gambar = '$imageFilePath' WHERE event_id = $event_id";
        } else {
            $sql = "UPDATE events SET event_name = '$event_name', event_price = '$event_price', event_date = '$event_date', event_location = '$event_location', gambar = '$imageFilePath', vid = '$vidFilePath' WHERE event_id = $event_id";
        }
    
        $rowCount = $db->exec($sql);
    
        if ($rowCount !== false) {
            echo "Event updated successfully. Rows affected: $rowCount";
        } else {
            echo "Error updating event: " . print_r($db->errorInfo(), true);
        }
        return "Event updated successfully";
    }
    
    
    
    public function deleteEvent($eventId) {
        global $db;
    
        try {
            // Delete associated pembelian records
            $stmtPembelian = $db->prepare("DELETE FROM pembelian WHERE ticket_id IN (SELECT ticket_id FROM tickets WHERE event_id = ?)");
            $stmtPembelian->execute([$eventId]);
    
            // Delete associated tickets
            $stmtTickets = $db->prepare("DELETE FROM tickets WHERE event_id = ?");
            $stmtTickets->execute([$eventId]);
    
            // Delete the event
            $stmtEvent = $db->prepare("DELETE FROM events WHERE event_id = ?");
            $stmtEvent->execute([$eventId]);
    
            echo "Event, associated tickets, and pembelian records deleted successfully<br>";
    
            return "Event, associated tickets, and pembelian records deleted successfully";
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage() . "<br>";
    
            return "Error: " . $e->getMessage();
        }
    }
    
    public function getAllEvents() {
        global $db;
        $stmt = $db->prepare("SELECT * FROM events");
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getEvent($eventId) {
        global $db;
        $stmt = $db->prepare("SELECT * FROM events WHERE event_id = ?");
        $stmt->execute([$eventId]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function getLastEventId() {
        global $db;
        $stmt = $db->prepare("SELECT event_id FROM events ORDER BY event_id DESC LIMIT 1");
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC)['event_id'];
    }

    public function searchEvents($searchQuery, $sortKey, $minStock) {
        global $db;

        try {
            $sql = "SELECT * FROM events WHERE 1"; // Start with a true condition

            // Add search condition 
            if (!empty($searchQuery)) {
                $sql .= " AND (LOWER(event_name) LIKE CONCAT('%', :query, '%') OR LOWER(event_location) LIKE CONCAT('%', :query, '%'))";
            }


            // Add filter condition 
            if ($minStock !== null) {
                $sql .= " AND event_stock >= :minStock";
            }

            // Add sort condition
            if ($sortKey === 'name') {
                $sql .= " ORDER BY event_name ASC";
            } elseif ($sortKey === 'location') {
                $sql .= " ORDER BY event_location ASC";
            }

            $stmt = $db->prepare($sql);

            // Bind search query parameter
            if (!empty($searchQuery)) {
                $lowerSearchQuery = strtolower("%" . $searchQuery . "%");
                $stmt->bindParam(':query', $lowerSearchQuery, PDO::PARAM_STR);
            }

            // Bind filter parameter 
            if ($minStock !== null) {
                $stmt->bindParam(':minStock', $minStock, PDO::PARAM_INT);
            }

            $stmt->execute();

            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            throw new Exception("Error searching events: " . $e->getMessage());
        }
    }

    


}
?>