Commit b0eff0ad authored by thejoelinux's avatar thejoelinux

fix #10 fix #69 fix #76

parent 242571ee
ALTER TABLE lud_games ADD archived TINYINT NOT NULL DEFAULT 0 AFTER game_type;
ALTER TABLE `lud_loans` ADD INDEX(`game_id`);
ALTER TABLE `lud_loans` ADD INDEX(`member_id`);
ALTER TABLE `lud_loans` ADD FOREIGN KEY (`game_id`)
REFERENCES `lud_games`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE `lud_loans` ADD FOREIGN KEY (`member_id`)
REFERENCES `lud_members`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
......@@ -5,6 +5,7 @@ class Game extends Record {
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;
public $archived;
// array containing the medias associated with the game
public $medias;
......@@ -25,7 +26,7 @@ class Game extends Record {
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,
g.age_min, g.age_max, g.game_type,
g.age_min, g.age_max, g.game_type, g.archived,
l.id as loan_id, l.end_date AS loan_end_date,
r.id AS reservation_id, r.member_id AS reservation_member_id, r.reservation_date,
CONCAT(m.firstname, ' ', m.lastname) AS reservation_member_name
......@@ -73,7 +74,7 @@ class Game extends Record {
}
// SQL SELECT lud_games lud_esar_categories lud_loans
$sql = "SELECT g.id, g.name, g.age_min, g.age_max,
CONCAT (ec.label, ' - ', ec.name) AS label,
CONCAT (ec.label, ' - ', ec.name) AS label, g.archived,
l.id as loan_status
FROM ".Game::$table." g
LEFT OUTER JOIN ".Esar_Category::$table." ec ON g.esar_category_id = ec.id
......@@ -83,4 +84,15 @@ class Game extends Record {
$GLOBALS["data"]->select($sql, $games, "Game");
return sizeof($games);
}
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;
}
}
......@@ -101,6 +101,10 @@ class AppController {
header($_SERVER['SERVER_PROTOCOL'] . ' 409 Conflict', true, 409);
break;
case "422":
header($_SERVER['SERVER_PROTOCOL'] . ' 422 Unprocessable entity', true, 422);
break;
case "500":
default:
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
......@@ -273,7 +277,6 @@ class AppController {
$objects = array();
$classname = $this->model;
$classname::$method($objects);
error_log("in the method _list of app");
if($this->format == "json") {
// send dt_format=1 in URL to send format for datatables
if(array_key_exists("dt_format", $_REQUEST)) {
......
......@@ -94,10 +94,20 @@ class GamesController extends AppController {
return $render;
}
// API CALL
function _confirm_delete() {
// FIXME : call the model, delete depedants items
// and the game itself
return "games/list";
try {
$game = Game::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($game->id != 0) {
$game->delete();
echo json_encode($game);
} else {
AppController::http_error(404);
}
} catch(data_exception $e) {
AppController::http_error();
}
exit();
}
function _loans() {
......@@ -168,17 +178,29 @@ class GamesController extends AppController {
}
function _list() {
if($this->format == "json") {
parent::_list();
}
return "games/list";
}
// API CALL
function _toggle_archive() {
try {
Game::fetch_all($games);
if($this->format == "json") {
echo json_encode($games);
exit();
$classname = $this->model;
$object = $classname::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($object->loan_id != "") {
AppController::http_error(422);
} else {
if(!$object->toggle_archive($object->archived == 1 ? 0 : 1)) {
AppController::http_error(422);
}
echo json_encode($object);
}
$this->set("games", $games);
$render = "games/list";
} catch(data_exception $e) {
$render = "data_exception";
} catch (data_exception $e) {
AppController::http_error();
echo json_encode($e);
}
return $render;
exit();
}
}
......@@ -13,6 +13,7 @@
<link rel="stylesheet" href="/cdn/css/bootstrap/multiselect/bootstrap-multiselect.css">
<link rel="stylesheet" href="/cdn/css/bootstrap/slider/5.3.7/bootstrap-slider.min.css">
<link rel="stylesheet" href="/cdn/css/jquery/jqplot/jqplot.min.css">
<link rel="stylesheet" href="css/styles.css">
<script src="/cdn/js/jquery/2.1.4/jquery.min.js"></script>
<title>{% block title %}{% endblock %} - Ludothèque</title>
</head>
......
......@@ -227,8 +227,20 @@ $(document).ready(function() {
'Cette action n\'est possible que si le jeu n\'a été\n' +
'l\'objet d\'aucun emprunt.';
if(confirm(msg)) {
$('#a').val('confirm_delete');
document.defaultform.submit();
// send ajax call to delete
$.ajax({url: 'api.php?o=games&a=confirm_delete&i=' + $('#i').val(),
success: function(output) {
// display confirmation
alert('Le jeu a bien été supprimé');
// redirect to list
window.location.href='index.php?o=games&a=list';
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Erreur lors de la supression : ce jeu a probablement'
+ ' déjà été emprunté.');
return false;
}
});
}
});
$('#back_button').click(function(){
......
......@@ -3,70 +3,157 @@
{% block content %}
<div class="panel panel-default">
<div class="panel-heading">
<span style="font-size: 150%;" class="glyphicon glyphicon-knight"></span>
<span style="font-size: 150%; font-weight: bold">
Jeux
<span style="font-size: 150%;" class="glyphicon glyphicon-knight"></span>
<span style="font-size: 150%; font-weight: bold">
Jeux
</span>
<span class="btn btn-success btn-md" style="float: right" id="new_button">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvel jeu...</span>
</span>
</div>
<button type="button" class="btn btn-success btn-md" style="float: right"
id="new_button">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouveau jeu...</span>
</button>
</div>
<div class="panel-body">
<div class="col-sm-12" align="center">
<table id="list_games" style="display:none">
<thead>
<tr>
<th>Nom</th>
<th>Age min.</th>
<th>Age max.</th>
<th>ESAR</th>
<th>Etat</th>
</tr>
</thead>
<tbody>
{% for key, val in games %}
<tr>
<td>
<a href="index.php?o=games&a=edit&i={{ val.id }}">{{ val.name }}</a>
</td>
<td>{{ val.age_min }}</td>
<td>{{ val.age_max }}</td>
<td>
{{ val.label }}
</td>
<td>
{% if val.loan_status == '' %} Libre {% else %} Emprunté {% endif %}
</td>
</tr>
{% endfor %}
</tbody>
<table id="list_games">
<thead>
<tr>
<th>Nom</th>
<th>Age min.</th>
<th>Age max.</th>
<th>ESAR</th>
<th>Etat</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<!-- end of panel -->
</div>
</div>
<style>
.dataTables_length {
padding-top: 0.25em;
}
</style>
<script>
$(document).ready(function() {
var table = $('#list_games').DataTable({
"autoWidth": false,
"fnDrawCallback": function() {
$(this).show();
},
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
var row = oSettings.aoData[iDataIndex].nTr;
var show = $(".toggle_row_display").val();
var archived = oSettings.aoData[iDataIndex]._aData.archived;
var borrowed = oSettings.aoData[iDataIndex]._aData.loan_status;
if(show === "all") {
return (archived != 1);
} else if(show === "archived") {
return (archived == 1);
} else if(show === "borrowed") {
return (borrowed !== null);
} else {
return true;
}
}
);
var table = $('#list_games').DataTable({
"autoWidth": false,
"dom": '<"filter_comp">frtlp',
"ajax": "api.php?o=games&a=list&dt_format=1",
"language": {
"url": "/cdn/js/jquery/datatables/1.10.9/dataTables.french.json"
},
"columnDefs": [
{
"targets": [1,2],
className: "text-center"
},{
"targets": [1,2,3],
"defaultContent": ''
},{
"targets": 4,
"defaultContent": 'Disponible',
"render": function(data, type, row) {
if(row.loan_status !== null) {
return 'Emprunté';
}
}
},{
"targets": 5, // action
"render": function(data, type, row) {
var html = '<button type="button" class="btn btn-primary btn-xs edit_button" '
+ ' data-toggle="tooltip" title="Editer" '
+ ' data-id="' + row.id + '"><i class="glyphicon glyphicon-edit"></i></button> ';
if(row.loan_status === null) {
var color = 'success';
var icon = 'open';
var help = 'Désarchiver';
if(row.archived !== "1") {
color = 'danger';
icon = 'close';
help = 'Archiver le jeu';
}
return html + '<button data-toggle="tooltip" title="' + help + '" '
+ ' type="button" class="btn btn-' + color + ' btn-xs toggle_archive_button" '
+ ' data-id="' + row.id + '"><i class="glyphicon glyphicon-folder-' + icon + '"></i></button>';
} else {
return html;
}
}
}
],
"columns": [
{"data": "name"},
{"data": "age_min"},
{"data": "age_max"},
{"data": "label"},
{"data": "loan_status"},
{"data": null}
],
"fnDrawCallback": function() {
$(this).show();
$('.toggle_archive_button').on("click", function(e) {
var button = $(e.currentTarget);
$.ajax({url: 'api.php?o=games&a=toggle_archive&i=' + button.data('id'),
success: function(output) {
table.ajax.reload(null, false);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Erreur lors de l\'archivage');
return false;
}
});
});
$('.edit_button').on("click", function(e) {
var button = $(e.currentTarget);
window.location.href='index.php?o=games&a=edit&i=' + button.data('id');
});
if($("div.filter_comp").html() === "") {
$("div.filter_comp").html('<select class="toggle_row_display pull-left" style="margin-top: 5px">'
+ '<option value="all">Tous sauf archivés</option>'
+ '<option value="borrowed">Seulement les empruntés</option>'
+ '<option value="archived">Seulement les archivés</option></select>');
$(".toggle_row_display").on("change", function() {
table.draw();
});
}
}
});
$('#new_button').click(function(){
$('#a').val('new');
defaultform.submit();
window.location.href='index.php?o=games&a=new';
});
});
</script>
{% endblock %}
......@@ -14,6 +14,12 @@
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvelle adhésion...</span>
</button>
{% if member.account_id == 0 %}
<button type="button" class="btn btn-danger btn-md" style="float: right">
<i class="glyphicon glyphicon-plus"></i>
<span>Créer le compte !</span>
</button>
{% endif %}
</div>
<div class="panel-body">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment