controller.php 2.59 KB
Newer Older
Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
1 2 3
<?php
require ('view.php');

Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
4 5
class historyController {
    public static function showHistoryController() {
6 7 8 9 10 11 12 13 14 15 16 17
        if (isLogin()) {
            $user_token = getTokenLogin();
            $user = getUserInfo(getUserIDbyToken($user_token));
            $params = [
                'username' => $user['username'],
                'dataHistoryItem' => self::selectBookOrderByUserId($user['ID'])
            ];
            historyView::viewHistory($params);
        } else {
            $login = '../login';
            header('Location: '.$login);
        }
Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
18
    }
19 20 21 22
    public static function selectBookOrderByUserId (string $userId) {
        $conn = connect_to_mysql();
        $arrResult = array();
        if ($conn != null) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
//            $sql_query = "SELECT * FROM book_order JOIN books ON book_id=books.ID where user_Id=$userId ORDER BY tanggal DESC";
            $sql_query = "
                        SELECT 
                            *,
                            (CASE
                                WHEN EXISTS (SELECT * FROM review where Nomor_Order=order_id) THEN TRUE
                                ELSE FALSE
                            END) as reviewed
                        FROM 
                            book_order 
                        JOIN 
                            books
                        ON 
                            book_id=books.ID 
                        where user_Id=$userId ORDER BY tanggal DESC, Nomor_Order DESC
            ";
39 40 41
            $result = $conn->query($sql_query);
            if ($result != NULL){
                while ($row = $result->fetch_assoc()) {
Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
42
                    $arrResult[] = self::orderItemsToViewHistoryParams($row);
43 44 45 46 47 48 49 50 51 52
                }
            }
            $conn->close();
            return $arrResult;
        } else {
            return null;
        }
    }

    private static function orderItemsToViewHistoryParams(array $orderItem){
53
        $defaultImageUrl = '../images/books_picture/default.jpg';
Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
54 55 56 57 58 59
        if (fileProcessing::isExistBookImage($orderItem['book_id'])) {
            $imgSource = '../'.fileProcessing::getImageBookPathFromRoot($orderItem['book_id']);
        } else {
            $imgSource = $defaultImageUrl;
        }

60
        $result = [
Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
61 62 63 64 65 66
            'itemName' => $orderItem['Judul'],
            'numberOfItem' => $orderItem['jumlah'],
            'statusReviewed' => $orderItem['has_reviewed'],
            'orderNumber' => $orderItem['Nomor_Order'],
            'date' => dateDataBaseToIndonesiaDate($orderItem['tanggal']),
            'imgSource' => $imgSource,
67 68
            'reviewUrl' => 'review'
        ];
Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
69
        return $result;
70
    }
Wildan Dicky Alnatara's avatar
Wildan Dicky Alnatara committed
71
}