Skip to content
Snippets Groups Projects
Forked from IF3110-2023-01-18 / Tugas Besar 1
15 commits ahead of the upstream repository.
PembelianController.php 2.53 KiB
<?php
// app/controllers/pembelianController.php

ob_start();
// require_once 'Pembelian.php';
require_once(__DIR__ . '/../Models/Pembelian.php');

class PembelianController {
    private $pembelianModel;
    
    
    public function __construct() {
        $this->pembelianModel = new PembelianModel();
    }

    public function createPembelian($ticketId, $userId, $createdTime) {
        $pembelianModel = new PembelianModel();
        return $this->pembelianModel->createPembelian($ticketId, $userId, $createdTime);
    }

    public function getPembelian($pembelianId) {
        return $this->pembelianModel->getPembelian($pembelianId);
    }

    public function updatePembelian($pembelianId, $ticketId, $userId, $createdTime) {
        return $this->pembelianModel->updatePembelian($pembelianId, $ticketId, $userId, $createdTime);
    }

    public function deletePembelian($pembelianId) {
        return $this->pembelianModel->deletePembelian($pembelianId);
    }

    public function getAllPembelian(){
        return $this->pembelianModel->getAllPembelian();
    }

    public function getAllPembelianWithDetails(){
        return $this->pembelianModel->getAllPembelianWithDetails();
    }

    public function getPembelianByUserId($userId) {
        return $this->pembelianModel->getPembelianByUserId($userId);
    }

    public function paginateHistory($pembelian=[],$page=1,$pagesize=10) {
        $totalPembelian = count($pembelian);
        $maxpage = ceil($totalPembelian / $pagesize);
        if ($page > $maxpage) {
            $page = $maxpage;
        }
        $offset = $pagesize * ($page - 1);
        $selectedPembelian = array_slice($pembelian, $offset, $pagesize);
    
        return [
            'pembelian' => $selectedPembelian,
            'total' => $totalPembelian,
            'page' => $page,
            'maxpage' => $maxpage,
        ];
    }

    public function purchaseTicket($ticketId,$userId) {
        $createdTime = date("Y-m-d H:i:s");
        $this->createPembelian($ticketId,$userId,$createdTime);
    }

    public function handleRequest() {
        if (isset($_POST['purchaseAction'])) {
            if ($_POST['purchaseAction'] === 'purchaseTicket') {
                unset($_POST['purchaseAction']);
                $ticketId = $_POST['ticketId'];
                $userId = $_POST['userId'];
                $this->purchaseTicket($ticketId,$userId);
                header("Location: /app/Views/history/history.php");
                ob_end_flush();
            } else {
                // others
            }
        }
    }
    
}
?>