Commit ba33361a authored by Denis S. Valdenaire's avatar Denis S. Valdenaire

debut des reservations

parent 261de3b4
<?php
//include("classes/data_exception.php");
//include("classes/rset.php");
class data {
......
<?php
class Reservation extends Record {
public $id, $game_id, $member_id;
public $created_at, $updated_at;
public $member_name;
public $table = "reservations";
public function __construct($id = 0)
{
if (!$this->id) {
$this->id = $id;
}
}
public static function fetch_all(&$reservations, $member_id, $limit = 0) {
$reservations = array();
// SQL SELECT reservations games members
$sql = " SELECT r.id, r.created_at, r.updated_at,
g.id AS game_id, g.name as game_name,
CONCAT(m.firstname, ' ', m.lastname) AS member_name, m.id AS member_id
FROM reservations r, games g, members m
WHERE
g.id = r.game_id
".($member_id == 0 ? "" : "AND l.member_id = ".$member_id)."
AND l.member_id = m.id
ORDER BY r.created_at ASC
".($limit != 0 ? "LIMIT 0,$limit" : "");
$GLOBALS["data"]->select($sql, $reservations, "Reservation", true);
return sizeof($reservations);
}
public static function fetch($id) {
// SQL SELECT reservations
$sql = " SELECT r.id, r.created_at, r.updated_at, r.member_id,
g.name as game_name
FROM reservations r, games g
WHERE r.id = $id
AND g.id = r.game_id";
$GLOBALS["data"]->select($sql, $reservation, "Reservation");
return $reservation;
}
public static function delete($id) {
// SQL SELECT reservations
$sql = " SELECT id
FROM reservations
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE reservations
$sql = " DELETE FROM reservations
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
}
?>
......@@ -25,6 +25,14 @@ class AppController {
$this->context[$var] = $val;
}
public function set_message($msg, $level = "info") {
if(!is_array($GLOBALS["ihm_message"])) {
$GLOBALS["ihm_message"] = array();
}
return array_push($GLOBALS["ihm_message"], array("level" => $level,
"msg" => $msg));
}
function render($view) {
try {
$template = $this->twig->loadTemplate($view.".html");
......@@ -123,7 +131,17 @@ class AppController {
$classname = $this->model;
$classname::fetch_all($objects);
if($format == "json") {
echo json_encode($objects);
// send dt_format=1 in URL to send format for datatables
if(array_key_exists("dt_format", $_REQUEST)) {
echo "{
\"sEcho\": 1,
\"iTotalRecords\": ".sizeof($objects).",
\"iTotalDisplayRecords\": ".sizeof($objects).",
\"data\":";
echo (sizeof($objects) ? json_encode($objects) : "[]")."}";
} else {
echo json_encode($objects);
}
exit(); // no further rendering needed
}
$this->set("objects", $objects);
......
<?php
class ReservationsController extends AppController {
public $model = "Reservation";
function ReservationsController() {
$this->AppController();
$function_name = "_".$_REQUEST["a"];
// to the view
$this->render($this->$function_name());
}
function _new() {
$classname = $this->model;
$object = new $classname(0);
$object->member_id = $_REQUEST["i"];
$this->set("object", $object);
$_REQUEST["i"] = 0;
return $_REQUEST["o"]."/edit";
}
function _switch_state() { // API CALL
try {
$reservation = Reservation::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($reservation->id != 0) {
$reservation->change_state($GLOBALS["data"]->db_escape_string($_REQUEST["state"]));
echo json_encode($reservation);
exit();
} else {
return "unprocessable";
}
} catch(data_exception $e) {
return "data_exception";
}
}
// FIXME : calling edit gets to the list - there is not point in editing
// such a simple model, you only want to delete it.
// so deletion could be done from the list, be it the list for a single user
// or the whole list.
// also, events that deletes calls the model
function _list() {
$format = (preg_match("/api.php/", $_SERVER["REQUEST_URI"])) ? "json" : "html";
try {
$classname = $this->model;
$classname::fetch_all($objects);
if($format == "json") {
echo json_encode($objects);
exit(); // no further rendering needed
}
$this->set("objects", $objects);
return $_REQUEST["o"]."/list";
} catch(data_exception $e) {
if($format == "json") {
header($_SERVER['SERVER_PROTOCOL'] . ' Internal Server Error', true, 500);
exit(); // no further rendering needed
}
return "data_exception";
}
}
}
......@@ -90,7 +90,7 @@ if(!array_key_exists("user_id", $_SESSION)) {
<li><a href="index.php?o=members">Adhérents</a></li>
<?php } ?>
<?php if($logged_user->has_role("admin")) { ?>
<li><a href="index.php?o=users&a=list">Comptes</a></li>
<li><a href="index.php?o=users&a=list">Utilisateurs</a></li>
<?php } ?>
<?php if(sizeof($menu_entries)) { ?>
<li class="dropdown">
......
CREATE TABLE `reservations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`game_id` int(11) DEFAULT NULL,
`member_id` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO schema_migrations VALUES ('201512180630');
......@@ -7,6 +7,10 @@
<small>
{% if game.loan_id %}
INDISPONIBLE : <a href="index.php?o=loans&a=edit&i={{ game.loan_id }}">jusqu'au {{ game.loan_end_date }}</a>
<span class="btn btn-success btn-md" style="float: right" id="reservation_button">
<i class="glyphicon glyphicon-plus"></i>
<span>Réserver...</span>
</span>
{% else %}
DISPONIBLE
{% endif %}
......@@ -201,4 +205,9 @@ $('#back_button').click(function(){
// and if yes, ask for confirmation from the user.
window.location.href='index.php?o=games&a=list';
});
$('#reservation_button').click(function(){
$('#o').val('reservations');
$('#a').val('new');
document.defaultform.submit();
});
</script>
......@@ -5,11 +5,11 @@
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>
<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>
</div>
<div class="panel-body">
......
<div class="panel panel-default">
<div class="panel-heading">
<h4><!-- class="panel-title" -->
<span class="glyphicon glyphicon-th-list" style="margin-right: 10px" ></span>
{% if object.id %} {{ object.game_name }} :: Emprunt du {{ object.start_date }} {% else %} Nouvelle réservation {% endif %}
</h4>
</div>
<div class="panel-body">
<div class="form-group">
{% if object.id %}
<div class="control-label col-sm-6">
Créé le : {{ object.created_at }} / Mis-à-jour le {{ object.updated_at }}
</div>
<!-- FIXME : this field may be a little bit useless when creating a new loan -->
<label class="control-label col-sm-2" for="is_back">Rendu ?</label>
<div class="col-sm-4">
<input type="checkbox" id="is_back" name="is_back" class="form-control is_back_cbx"
data-switch-with-ajax {{ object.is_back ? "checked" }}/>
</div>
{% else %}
<label class="control-label col-sm-2" for="game_id">Jeu</label>
<div class="col-sm-4">
<input type="hidden" name="game_id" id="game_id" value="">
<div id="search-games-for-loans" >
<input class="typeahead form-control" type="text" placeholder="Jeu...">
</div>
</div>
<div class="control-label col-sm-6">
Si la date de retour est vide, elle sera positionnée à 21 jours plus tard.
</div>
{% endif %}
</div>
<input type="hidden" name="member_id" id="member_id" value="{{ object.member_id }}">
<div class="form-group">
<div class="col-sm-12" align="center">
<input type="button" class="btn btn-primary" id="back_button" value="&lt;&lt; Retour à la liste">
{% if object.id %}
<input type="button" class="btn btn-success" id="save_button" value="Enregistrer les changements">
<input type="button" class="btn btn-danger" id="delete_button" value="Supprimer">
{% else %}
<input type="button" class="btn btn-success" id="save_button" value="Créer">
{% endif %}
</div>
</div>
<!-- end of panel -->
</div>
</div>
<script>
// buttons events
$(document).ready(function () {
$('#save_button').click(function(){
if(document.defaultform.start_date.value == 0) {
alert ("Vous n'avez pas saisi de date de début !");
return false;
}
// go to the members controllers, as this action should display
// the list of the loans for the current user
$('#o').val('members');
if($('#i').val() == 0) {
$('#a').val('create_loan');
} else {
$('#a').val('update_loan');
}
document.defaultform.submit();
return true;
});
$('#delete_button').click(function(){
var msg = 'Voulez-vous réellement supprimer un emprunt ?\n' +
'Cette action n\'est possible qu\'en cas d\'erreur de saisie.\n';
if(confirm(msg)) {
$('#a').val('delete_loan');
document.defaultform.submit();
}
});
$('#back_button').click(function(){
// TODO this function should verify that the object has not been modified
// and if yes, ask for confirmation from the user.
window.location.href='index.php?o=members&a=loans&i={{ object.member_id }}';
});
$('.is_back_cbx').bootstrapSwitch({
onText: "Oui",
offText: "Non",
}).on('switchChange.bootstrapSwitch', function(event, state) {
$.ajax({
url: 'api.php?o=loans&a=switch_state&i=' + {{ object.id ?: '' }}
+ "&state=" + (state ? 1 : 0), // post on the API
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
success: function(){
if(state) {
alert('Ce jeu est noté comme restitué aujourd\'hui.');
} else {
alert('Ce jeu est noté comme non restitué aujourd\'hui.');
}
},
error: function(){
alert('Une erreur a eu lieu lors de la restitution de ce jeu.');
},
cache: false,
contentType: false,
processData: false
});
});
});
</script>
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