145 lines
5.0 KiB
PHP
145 lines
5.0 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Game_model extends CI_Model {
|
|
|
|
public function search_by_title($keyword) {
|
|
$this->db->select('*');
|
|
$this->db->from('game');
|
|
$this->db->like('name', $keyword);
|
|
$query = $this->db->get();
|
|
return $query->result_array();
|
|
}
|
|
|
|
public function get_by_category($category_id) {
|
|
$this->db->select('game.*');
|
|
$this->db->from('game');
|
|
$this->db->join('game_category', 'game_category.gameId = game.id');
|
|
$this->db->where('game_category.categoryId', $category_id);
|
|
$query = $this->db->get();
|
|
return $query->result_array();
|
|
}
|
|
|
|
public function get_by_genre($genre_id) {
|
|
$this->db->select('game.*');
|
|
$this->db->from('game');
|
|
$this->db->join('game_genre', 'game_genre.gameId = game.id');
|
|
$this->db->where('game_genre.genreId', $genre_id);
|
|
$query = $this->db->get();
|
|
return $query->result_array();
|
|
}
|
|
|
|
public function get_by_id($game_id) {
|
|
$this->db->select('game.*, developer.name as developer_name, poster.jpeg as poster_url');
|
|
$this->db->from('game');
|
|
$this->db->join('developer', 'developer.id = game.developerId', 'left');
|
|
$this->db->join('poster', 'poster.id = game.posterId', 'left');
|
|
$this->db->where('game.id', $game_id);
|
|
$query = $this->db->get();
|
|
return $query->row_array();
|
|
}
|
|
|
|
|
|
public function ajouter_jeu($titre, $annee, $description, $prix, $developer_name = null, $categories = [], $genres = [], $jpeg_data = null) {
|
|
// 1. Gérer le développeur
|
|
$dev_id = NULL;
|
|
if (!empty($developer_name)) {
|
|
$dev = $this->db->get_where('developer', array('name' => $developer_name))->row_array();
|
|
if ($dev) {
|
|
$dev_id = $dev['id']; // Il existe déjà
|
|
} else {
|
|
$this->db->insert('developer', array('name' => $developer_name));
|
|
$dev_id = $this->db->insert_id(); // On le crée
|
|
}
|
|
}
|
|
|
|
$poster_id = NULL;
|
|
if (!empty($jpeg_data)) {
|
|
$this->db->insert('poster', array('jpeg' => $jpeg_data));
|
|
$poster_id = $this->db->insert_id();
|
|
}
|
|
|
|
$donnees = array(
|
|
'name' => $titre,
|
|
'releaseYear' => $annee,
|
|
'shortDescription' => $description,
|
|
'price' => empty($prix) ? NULL : $prix,
|
|
'developerId' => $dev_id,
|
|
'posterId' => $poster_id
|
|
);
|
|
$this->db->insert('game', $donnees);
|
|
$game_id = $this->db->insert_id();
|
|
|
|
if (!empty($categories)) {
|
|
foreach ($categories as $cat_id) {
|
|
$this->db->insert('game_category', array('gameId' => $game_id, 'categoryId' => $cat_id));
|
|
}
|
|
}
|
|
|
|
if (!empty($genres)) {
|
|
foreach ($genres as $gen_id) {
|
|
$this->db->insert('game_genre', array('gameId' => $game_id, 'genreId' => $gen_id));
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
public function modifier_jeu($id, $titre, $annee, $description, $prix, $developer_name = null, $categories = [], $genres = []) {
|
|
$dev_id = NULL;
|
|
if (!empty($developer_name)) {
|
|
$dev = $this->db->get_where('developer', array('name' => $developer_name))->row_array();
|
|
if ($dev) {
|
|
$dev_id = $dev['id'];
|
|
} else {
|
|
$this->db->insert('developer', array('name' => $developer_name));
|
|
$dev_id = $this->db->insert_id();
|
|
}
|
|
}
|
|
|
|
$donnees = array(
|
|
'name' => $titre,
|
|
'releaseYear' => $annee,
|
|
'shortDescription' => $description,
|
|
'price' => empty($prix) ? NULL : $prix,
|
|
'developerId' => $dev_id
|
|
);
|
|
$this->db->where('id', $id);
|
|
$this->db->update('game', $donnees);
|
|
|
|
$this->db->delete('game_category', array('gameId' => $id));
|
|
if (!empty($categories)) {
|
|
foreach ($categories as $cat_id) {
|
|
$this->db->insert('game_category', array('gameId' => $id, 'categoryId' => $cat_id));
|
|
}
|
|
}
|
|
|
|
$this->db->delete('game_genre', array('gameId' => $id));
|
|
if (!empty($genres)) {
|
|
foreach ($genres as $gen_id) {
|
|
$this->db->insert('game_genre', array('gameId' => $id, 'genreId' => $gen_id));
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function supprimer_jeu($id) {
|
|
return $this->db->delete('game', array('id' => $id));
|
|
}
|
|
|
|
public function get_tous_les_jeux($critere_tri = 'id') {
|
|
$tris_autorises = array('id', 'name', 'releaseYear', 'price');
|
|
if (!in_array($critere_tri, $tris_autorises)) {
|
|
$critere_tri = 'id';
|
|
}
|
|
$this->db->order_by($critere_tri, 'ASC');
|
|
$query = $this->db->get('game');
|
|
return $query->result_array();
|
|
}
|
|
|
|
public function get_all_games() {
|
|
return $this->get_tous_les_jeux();
|
|
}
|
|
} |