SAE_2.02/CodeIgniter-3.1.13/application/models/Model_music.php

133 lines
4.5 KiB
PHP
Raw Normal View History

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2024-05-19 15:27:13 +02:00
class Model_music extends CI_Model {
public function __construct(){
$this->load->database();
}
2024-05-19 15:27:13 +02:00
2024-05-22 20:59:59 +02:00
public function getAlbums($limit, $offset, $order_by = 'year', $genre_id = null, $artist_id = null){
$sql = "SELECT album.id, album.name, album.year, artist.name as artistName, genre.name as genreName, cover.jpeg
FROM album
JOIN artist ON album.artistid = artist.id
JOIN genre ON album.genreid = genre.id
JOIN cover ON album.coverid = cover.id";
if ($genre_id) {
$sql .= " WHERE genre.id = ?";
$params[] = $genre_id;
}
if ($artist_id) {
if ($genre_id) {
$sql .= " AND artist.id = ?";
} else {
$sql .= " WHERE artist.id = ?";
}
$params[] = $artist_id;
}
$sql .= " ORDER BY album." . $order_by . " LIMIT ? OFFSET ?";
$params[] = $limit;
$params[] = $offset;
$query = $this->db->query($sql, $params);
return $query->result();
}
2024-05-19 15:27:13 +02:00
2024-05-22 20:59:59 +02:00
public function get_total_albums($genre_id = null, $artist_id = null){
$sql = "SELECT COUNT(*) as total_albums FROM album
JOIN genre ON album.genreid = genre.id
JOIN artist ON album.artistid = artist.id";
$params = array(); // Initialiser le tableau de paramètres
if ($genre_id) {
$sql .= " WHERE genre.id = ?";
$params[] = $genre_id;
}
if ($artist_id) {
if ($genre_id) {
$sql .= " AND artist.id = ?";
} else {
$sql .= " WHERE artist.id = ?";
}
$params[] = $artist_id;
}
$query = $this->db->query($sql, $params);
2024-05-19 15:27:13 +02:00
$result = $query->row();
return $result->total_albums;
}
2024-05-22 20:59:59 +02:00
// Méthodes pour obtenir les genres et les artistes pour les filtres
public function getGenres(){
$query = $this->db->query("SELECT id, name FROM genre ORDER BY name");
return $query->result();
}
public function getArtists(){
$query = $this->db->query("SELECT id, name FROM artist ORDER BY name");
return $query->result();
}
public function get_album_by_id($id){
$query = $this->db->query(
"SELECT album.id, album.name, album.year, artist.name as artistName, genre.name as genreName, cover.jpeg
FROM album
JOIN artist ON album.artistid = artist.id
JOIN genre ON album.genreid = genre.id
JOIN cover ON album.coverid = cover.id
WHERE album.id = ?", array($id)
);
$album = $query->row();
2024-05-22 20:59:59 +02:00
if ($album) {
$query = $this->db->query(
"SELECT track.id, track.diskNumber, track.number, track.duration, song.name as songName
FROM track
JOIN song ON track.songid = song.id
WHERE track.albumid = ?
ORDER BY track.diskNumber, track.number", array($id)
);
$album->tracks = $query->result();
}
2024-05-22 20:59:59 +02:00
return $album;
}
2024-05-22 20:59:59 +02:00
2024-05-20 21:59:37 +02:00
public function getMusiques($limit, $offset) {
$query = $this->db->query(
2024-05-20 22:42:38 +02:00
"SELECT song.id, song.name, artist.id as artist_id, artist.name as artistName, album.name as album_name, track.albumid as album_id, cover.jpeg as cover
2024-05-20 21:59:37 +02:00
FROM song
JOIN track ON song.id = track.songid
JOIN album ON track.albumid = album.id
JOIN artist ON album.artistid = artist.id
JOIN cover ON album.coverid = cover.id
LIMIT $limit OFFSET $offset"
);
return $query->result();
}
2024-05-22 20:59:59 +02:00
2024-05-20 22:42:38 +02:00
2024-05-20 21:59:37 +02:00
public function get_total_musiques(){
$query = $this->db->query("SELECT COUNT(*) as total_musiques FROM song");
$result = $query->row();
return $result->total_musiques;
}
2024-05-22 20:59:59 +02:00
2024-05-20 22:42:38 +02:00
public function getAlbumsByArtiste($artiste_id){
$query = $this->db->query("
SELECT album.id, album.name, album.year, artist.name as artistName, genre.name as genreName, cover.jpeg
FROM album
JOIN artist ON album.artistid = artist.id
JOIN genre ON album.genreid = genre.id
JOIN cover ON album.coverid = cover.id
WHERE artist.id = ?
", array($artiste_id));
return $query->result();
}
2024-05-19 15:27:13 +02:00
}
?>