game.php 3.52 KB
Newer Older
thejoelinux's avatar
thejoelinux committed
1 2
<?php

3
class Game extends Record {
4 5 6 7
    public $id;
    public $name, $reference, $maker, $category, $esar_category_id;
    public $comments, $maker_info, $content_inventory, $aquisition_date, $price;
    public $players_min, $players_max, $age_min, $age_max, $game_type;
thejoelinux's avatar
thejoelinux committed
8
    public $archived;
thejoelinux's avatar
thejoelinux committed
9

Denis Valdenaire's avatar
Denis Valdenaire committed
10 11
    // array containing the medias associated with the game
    public $medias;
12 13
    // loan history
    public $loans;
Denis Valdenaire's avatar
Denis Valdenaire committed
14

thejoelinux's avatar
thejoelinux committed
15
    public static $table = "lud_games";
16

17 18 19 20 21
    public function __construct($id = 0) {
        if (!$this->id) {
            $this->id = $id;
        }
    }
Denis Valdenaire's avatar
Denis Valdenaire committed
22

23
    public static function fetch($id) {
thejoelinux's avatar
thejoelinux committed
24
        // SQL SELECT lud_games lud_loans lud_reservations lud_members
25 26 27 28
        $sql = "SELECT g.id, g.name, g.reference, g.maker, g.category, g.esar_category_id,
            g.comments, g.maker_info, g.content_inventory, 
                DATE_FORMAT(g.aquisition_date, '%m/%d/%Y') as aquisition_date,
            g.price, g.players_min, g.players_max,
thejoelinux's avatar
thejoelinux committed
29
            g.age_min, g.age_max, g.game_type, g.archived,
30
            l.id as loan_id, l.end_date AS loan_end_date,
thejoelinux's avatar
thejoelinux committed
31
            r.id AS reservation_id, r.member_id AS reservation_member_id, r.reservation_date,
32
                CONCAT(m.firstname, ' ', m.lastname) AS reservation_member_name
thejoelinux's avatar
thejoelinux committed
33 34 35 36
            FROM ".Game::$table." g
                LEFT OUTER JOIN ".Loan::$table." l ON (g.id = l.game_id AND l.is_back = 0)
                LEFT OUTER JOIN ".Reservation::$table." r ON (g.id = r.game_id)
                LEFT OUTER JOIN ".Member::$table." m ON (r.member_id = m.id)
37
            WHERE g.id = ".$id;
38
        $GLOBALS["data"]->find($sql, $game, "Game");
39 40 41 42 43
        return $game;
    }

    public function render_json() {
        echo json_encode($this);
Denis Valdenaire's avatar
Denis Valdenaire committed
44 45
    }

46
    // probably not used - done in javascript on the edit view for the game
Denis Valdenaire's avatar
Denis Valdenaire committed
47 48
    public function fetch_medias() {
        $this->medias = array();
49
        Media::fetch_all($this->medias, $this->id);
Denis Valdenaire's avatar
Denis Valdenaire committed
50 51 52
        return sizeof($this->medias);
    }

53 54 55 56 57 58
    public function fetch_loans() {
        $this->loans = array();
        Loan::fetch_loans($this->loans, $this->id);
        return sizeof($this->loans);
    }

59 60 61 62 63 64
    public function create_reservation() {
        $reservation = new Reservation();
        $reservation->create();
        return $reservation->id;
    }

65 66 67 68
    public function delete_reservation() {
        Reservation::delete_game_reservation($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
    }

69
    public static function fetch_all(&$games) {
Denis Valdenaire's avatar
Denis Valdenaire committed
70
        $games = array();
71 72 73 74
        $where_clause = "";
        if(array_key_exists("filter", $_REQUEST) && $_REQUEST["filter"] == "available") {
            $where_clause = "WHERE l.id IS NULL";
        }
thejoelinux's avatar
thejoelinux committed
75
        // SQL SELECT lud_games lud_esar_categories lud_loans
76
        $sql = "SELECT g.id, g.name, g.age_min, g.age_max,
thejoelinux's avatar
thejoelinux committed
77
            CONCAT (ec.label, ' - ', ec.name) AS label, g.archived,
thejoelinux's avatar
thejoelinux committed
78
            l.id as loan_status
thejoelinux's avatar
thejoelinux committed
79 80 81
            FROM ".Game::$table." g
                LEFT OUTER JOIN ".Esar_Category::$table." ec ON g.esar_category_id = ec.id
                LEFT OUTER JOIN ".Loan::$table." l ON (g.id = l.game_id AND l.is_back = 0)
82
            $where_clause   
thejoelinux's avatar
thejoelinux committed
83
            ORDER BY g.name"; 
Denis Valdenaire's avatar
Denis Valdenaire committed
84 85 86
        $GLOBALS["data"]->select($sql, $games, "Game");
        return sizeof($games);
    }
thejoelinux's avatar
thejoelinux committed
87 88 89 90 91 92 93 94 95 96 97

    public function toggle_archive($val) {
        // SQL UPDATE lud_games
        $sql = " UPDATE ".Game::$table." SET archived = $val
            WHERE id = ".$this->id;
        if($GLOBALS["data"]->update($sql)) {
            $this->archived = $val;
            return true;
        }
        return false;
    }
thejoelinux's avatar
thejoelinux committed
98
}