Modifications des tris dans musique

This commit is contained in:
stiti 2024-05-30 14:38:38 +02:00
parent 6f6d5ecacb
commit a00bb1852e
5 changed files with 75 additions and 41 deletions

View File

@ -12,34 +12,37 @@ class Musiques extends CI_Controller {
}
public function index($page = 1){
// Nombre de musiques par page
$this->load->model('Model_music');
$this->load->library('pagination');
$this->load->helper('url');
$this->load->helper('html');
$limit = 30;
// Calcul de l'offset pour la pagination
$offset = ($page - 1) * $limit;
// Récupérer la valeur de tri depuis la requête GET
$sort = $this->input->get('sort');
$genre_id = $this->input->get('genre_id');
$artist_id = $this->input->get('artist_id');
// Récupérer les musiques triées
$musiques = $this->Model_music->getMusiques($limit, $offset, $sort);
// Récupérer le nombre total de musiques pour la pagination
$musiques = $this->Model_music->getMusiques($limit, $offset, $sort, 'ASC', $genre_id, $artist_id);
$total_musiques = $this->Model_music->get_total_musiques();
// Calculer le nombre total de pages
$total_pages = ceil($total_musiques / $limit);
// Définition de la variable $current_page
$current_page = $page;
// Données à passer à la vue
$current_page = $page;
$genres = $this->Model_music->getGenres();
$artists = $this->Model_music->getArtists();
$data['musiques'] = $musiques;
$data['total_pages'] = $total_pages;
$data['current_page'] = $current_page;
$data['sort'] = $sort; // Passer la valeur de tri à la vue
$data['sort'] = $sort;
$data['genres'] = $genres;
$data['artists'] = $artists;
$data['genre_id'] = $genre_id;
$data['artist_id'] = $artist_id;
$data['title'] = "Musiques - Onzeur";
$data['css'] = "assets/css/musiques_list";
// Charger la vue
$this->load->view('layout/header_dark', $data);
$this->load->view('musiques_list', $data);
$this->load->view('layout/footer_dark');

View File

@ -83,8 +83,6 @@ class Playlists extends CI_Controller {
}
}
public function update($playlist_id) {
// Vérifier si l'utilisateur est connecté
$this->verify_playlist_ownership($playlist_id);
@ -113,7 +111,6 @@ class Playlists extends CI_Controller {
}
}
public function add_song($playlist_id) {
$this->verify_playlist_ownership($playlist_id);
@ -143,7 +140,6 @@ class Playlists extends CI_Controller {
}
}
public function delete($playlist_id) {
$this->verify_playlist_ownership($playlist_id);

View File

@ -101,7 +101,8 @@ class Model_music extends CI_Model {
return $album;
}
public function getMusiques($limit, $offset, $order_by = 'title', $order_direction = 'ASC') {
public function getMusiques($limit, $offset, $order_by = 'name', $order_direction = 'ASC', $genre_id = null, $artist_id = null) {
// Préparer la colonne de tri en fonction du paramètre $order_by
$order_column = '';
switch ($order_by) {
case 'artist':
@ -110,28 +111,47 @@ class Model_music extends CI_Model {
case 'album':
$order_column = 'album.name';
break;
case 'title':
$order_column = 'song.name';
case 'year':
$order_column = 'album.year';
break;
case 'name':
default:
$order_column = 'song.name';
break;
}
$query = $this->db->query(
"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
// Préparer la clause WHERE pour le genre et l'artiste, si nécessaire
$where_clause = '';
$params = array();
if ($genre_id) {
$where_clause .= " WHERE genre.id = ?";
$params[] = $genre_id;
}
if ($artist_id) {
$where_clause .= ($where_clause == '') ? ' WHERE' : ' AND';
$where_clause .= " artist.id = ?";
$params[] = $artist_id;
}
// Exécuter la requête avec la clause WHERE et l'ordre de tri spécifié
$query = $this->db->query("
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
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
JOIN genre ON album.genreid = genre.id
$where_clause
ORDER BY $order_column $order_direction
LIMIT $limit OFFSET $offset"
);
LIMIT $limit OFFSET $offset
", $params);
return $query->result();
}
public function get_all_songs() {
return $this->db->get('song')->result();
}

View File

@ -2,17 +2,33 @@
<div class="filters">
<form method="GET" action="<?php echo base_url('index.php/musiques/index'); ?>">
<label for="genre">Genre:</label>
<select name="genre_id" id="genre">
<option value="">Tous les genres</option>
<?php foreach($genres as $genre): ?>
<option value="<?php echo $genre->id; ?>" <?php echo ($genre->id == $genre_id) ? 'selected' : ''; ?>><?php echo $genre->name; ?></option>
<?php endforeach; ?>
</select>
<label for="artist">Artiste:</label>
<select name="artist_id" id="artist">
<option value="">Tous les artistes</option>
<?php foreach($artists as $artist): ?>
<option value="<?php echo $artist->id; ?>" <?php echo ($artist->id == $artist_id) ? 'selected' : ''; ?>><?php echo $artist->name; ?></option>
<?php endforeach; ?>
</select>
<label for="sort">Trier par:</label>
<select name="sort" id="sort">
<option value="name" <?php echo (isset($sort) && $sort == 'name') ? 'selected' : ''; ?>>Titre</option>
<option value="artist" <?php echo (isset($sort) && $sort == 'artist') ? 'selected' : ''; ?>>Artiste</option>
<option value="album" <?php echo (isset($sort) && $sort == 'album') ? 'selected' : ''; ?>>Album</option>
<option value="name" <?php echo ($sort == 'name') ? 'selected' : ''; ?>>Nom</option>
<option value="year" <?php echo ($sort == 'year') ? 'selected' : ''; ?>>Année</option>
</select>
<button type="submit">Filtrer</button>
</form>
</div>
<section class="list">
<?php foreach($musiques as $musique): ?>
<div>

View File

@ -53,7 +53,6 @@
</div>
<?php endif; ?>
<?php if (!empty($artistes)): ?>
<div class="section">
<h3 class="section-title">Artistes</h3>