Commit 47a967fb authored by Denis S. Valdenaire's avatar Denis S. Valdenaire

file upload broken

parent 91ee9214
......@@ -27,7 +27,9 @@ $data = new data();
header("Content-Type: application/json");
$contexts = array("members" => "Adhérents",
"games" => "Jeux",
"esar_categories" => "Catégories ESAR");
"esar_categories" => "Catégories ESAR",
"medias" => "Medias"
);
if(!array_key_exists("o", $_REQUEST) || !array_key_exists($_REQUEST["o"], $contexts)) {
// FIXME send correct HTTP code
exit();
......@@ -45,6 +47,10 @@ switch($_REQUEST["o"]) {
include("controllers/esar_categories.php");
break;
case "medias":
include("controllers/medias.php");
break;
default:
//header("Location: ./accueil/index.php");
?>
......
......@@ -33,7 +33,6 @@ class Game {
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
//echo "check for $var... ";
if(array_key_exists($var, $_REQUEST)) {
if($var == "date_achat") {
$_REQUEST["date_achat"] = date_format(date_create_from_format('d-m-Y', $_REQUEST["date_achat"]),'m/d/Y');
......@@ -56,6 +55,31 @@ class Game {
}
}
public function create() {
$fields_sql = "(";
$datas_sql = "(";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
if($var == "date_achat") {
$_REQUEST["date_achat"] = date_format(date_create_from_format('d-m-Y', $_REQUEST["date_achat"]),'m/d/Y');
}
$this->$var = $_REQUEST[$var];
if($var == "date_achat") {
$_REQUEST["date_achat"] = date_format(date_create_from_format('m/d/Y', $_REQUEST["date_achat"]),'Y-m-d');
}
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
// SQL INSERT jeu
$sql = " INSERT INTO jeu ".substr($fields_sql, 0, -1).")
VALUES ".substr($datas_sql, 0, -1).")";
return $this->id_jeu = $GLOBALS["data"]->insert($sql);
}
public function render_json() {
echo json_encode($this);
}
......
......@@ -19,6 +19,22 @@ class Media {
$GLOBALS["data"]->select($sql, $medias, "Media");
return sizeof($medias);
}
public function create() {
// SQL INSERT medias
$sql = "INSERT INTO medias (description, id_jeu)
VALUES ('$description', ".$_REQUEST["game_id"].")";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public function update($filename, $filetype) {
$this->file = $filename."-".$this->id.".".$filetype;
// SQL UPDATE medias
$sql = "UPDATE medias
SET file = '".$this->file."'
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
?>
......@@ -19,6 +19,16 @@ This file is part of phpLudoreve.
// controller
$render = "list";
switch($_REQUEST["a"]) {
case "new":
case "create":
$game = new Game(0);
if($_REQUEST["a"] == "create") {
$game->create();
}
$_REQUEST["i"] = $game->id_jeu;
$render = "games/edit";
break;
case "edit_medias":
case "update":
case "edit":
......
<?php
/*
This file is part of phpLudoreve.
phpLudoreve is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
phpLudoreve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with phpLudoreve. If not, see <http://www.gnu.org/licenses/>.
*/
// controller
$render = "list";
switch($_REQUEST["a"]) {
case "upload": // for API
$media = new Media();
try {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["media"]["name"]);
$uploadOk = 1;
$filetype = pathinfo($target_file,PATHINFO_EXTENSION);
$media->description = pathinfo($target_file,PATHINFO_FILENAME);
$filename = basename($_FILES["media"]["tmp_name"]);
// DEBUG
print_r($_FILES);
// create and get back the id
// the reason for this is that the file name, to be garanteed unique
// will include the id from the DB
$media->create();
$media->update($filename, $filetype);
// then mv the file in uploads/ dir
// DEBUG echo "move_uploaded_file(".$_FILES["media"]["tmp_name"].",".$target_dir.$filename.");";
move_uploaded_file($_FILES["media"]["tmp_name"], $target_dir.$media->file);
// echo back the json
// echo '{"media_id": "'.$media->id.'", "description": "'.$media->description.'", "file":""}';
echo json_encode($media);
exit(); // no further rendering needed
} catch(data_exception $e) {
$render = "views/data_exception";
}
break;
}
......@@ -149,13 +149,15 @@ $(document).ready(function () {
var members = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('nom'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: 'api.php?o=members&a=name_list'
prefetch: { url : 'api.php?o=members&a=name_list',
cache: false }
});
var games = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('nom'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: 'api.php?o=games&a=name_list'
prefetch: { url : 'api.php?o=games&a=name_list',
cache: false }
});
$('#search-all .typeahead').typeahead({
......
......@@ -17,34 +17,34 @@ This file is part of phpLudoreve.
*/
?>
<script>
/* FIXME view form validation later
function validate_and_submit ()
{
if(document.forms["saisie"].nom.value == 0)
{
function validate_and_submit () {
if(document.defaultform.nom.value == 0) {
alert ("Vous n'avez pas saisi le nom!");
return false;
}
document.forms["saisie"].submit();
document.defaultform.submit();
return true;
}
*/
</script>
<?php
// since we are in the edit form, we have an existing $game from the controller
?>
<div class="col-sm-12" align="center">
<h2><?=$game->nom?>
<small>
<?php if($game->id_pret) { ?>
(INDISPONIBLE) FIXME : lien vers pret en cours
<?php if($game->id_jeu != 0) { ?>
<h2><?=$game->nom?>
<small>
<?php if($game->id_pret) { ?>
(INDISPONIBLE) FIXME : lien vers pret en cours
<?php } else { ?>
(DISPONIBLE)
<?php } ?>
</small></h2>
<!-- FIXME : put this in a side bar helper <
a href="index.php?o=prets&id_jeu=<?=$game->id_pret?>">Historique des prêts</a>
-->
<?php } else { ?>
(DISPONIBLE)
<h2>Nouveau jeu</h2>
<?php } ?>
</small></h2>
<!-- FIXME : put this in a side bar helper <
a href="index.php?o=prets&id_jeu=<?=$game->id_pret?>">Historique des prêts</a>
-->
</div>
<div class="form-group">
......@@ -136,7 +136,8 @@ function validate_and_submit ()
$('#datetimepicker1').datetimepicker({
locale: 'fr',
format: 'DD-MM-YYYY',
defaultDate: new Date('<?=$game->date_achat?>')
defaultDate: new Date(<?=($game->date_achat != ""
? "'".$game->date_achat."'" : "")?>)
})
.on('changeDate', function(ev){
$('#date_achat') = ev.format();
......@@ -147,13 +148,73 @@ function validate_and_submit ()
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="media">Medias</label>
<div class="col-sm-10">
<?php if(sizeof($game->medias)) { while(list($key, $val) = each($game->medias)) { ?>
<?php } } else { ?>
<div class="col-sm-10" style="margin-top: 5px">
<?php if(sizeof($game->medias)) { ?>
<div id="media_list">
<?php
// DEBUG
echo "<pre>"; print_r($game->medias); echo "</pre>";
while(list($key, $val) = each($game->medias)) {
?><div id="media_<?=$val->id?>"><?=$val->description?>
<a href="javascript:delete_file(<?=$val->id?>)">Effacer</a>
<?php
if(preg_match("/.jpg$/", $val["file"])) { ?>
<img width="100" height="80" src="uploads/<?=$val["file"]?>">
<?php } ?></div><?php
}
?>
</div>
<?php } else { ?>
Aucun média associé.
<?php } ?>
<a href="index.php?o=games&a=edit_medias&i=<?=$game->id_jeu?>">Gérer les médias</a>
<?php if ($game->id_jeu != 0) { ?>
<!-- begin medias form -->
<input type="file" name="media" id="media"/>
<input type="button" value="Ajouter" id="add_media" />
<script>
$('#add_media').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: 'api.php?o=medias&a=upload&game_id=<?=$game->id_jeu?>', //Server script to process data
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
}
return myXhr;
},
//Ajax events
//beforeSend: beforeSendHandler,
success: completeHandler,
// FIXME error: errorHandler,
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
});
function progressHandlingFunction(e){
if(e.lengthComputable){
$('progress').attr({value:e.loaded,max:e.total});
}
}
function completeHandler (response) {
$('#media_list').html(
$('#media_list').html() + '<div id="media_' +
response.id + '">' + response.description +
'<a href="javascript:delete_file(' + response.id + ')">Effacer</a>' +
'FIXME' + response.file + '</div>'
);
alert(response);
}
</script>
<!-- end medias form -->
<?php } else { ?>
Il faut enregistrer le jeu avant d'ajouter des médias.
<?php } ?>
</div>
</div>
<div class="form-group">
......@@ -170,10 +231,13 @@ function validate_and_submit ()
</div>
<div class="form-group">
<div class="col-sm-2 col-sm-offset-5">
<input type="submit" class="btn btn-primary" value="Valider" onClick="set_value('a', 'update');">
<?php if ($game->id_jeu != 0) { ?>
<div class="col-sm-4 col-sm-offset-4">
<input type="submit" class="btn btn-primary" value="Enregistrer les changements" onClick="set_value('a', 'update');">
<input type="button" class="btn btn-danger" value="Supprimer" onClick="if(confirm('Really ?')) {set_value('a','delete'); defaultform.submit()}">
<?php } else { ?>
<div class="col-sm-2 col-sm-offset-6">
<input type="button" class="btn btn-primary" value="Créer" onClick="set_value('a', 'create');validate_and_submit()">
<?php } ?>
</div>
</div>
<h1>Liste des jeux</h1>
<div class="col-sm-8" align="center">
<h2>Liste des jeux</h2>
</div>
<div class="col-sm-4" align="center">
<span class="btn btn-success" onClick="set_value('a', 'new'); defaultform.submit()">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouveau jeu ...</span>
</span>
</div>
<div class="col-sm-12" align="center">
<table id="list_jeu">
<thead>
<tr>
......@@ -24,6 +33,7 @@ while(list($key, $val) = each($games)) { ?>
<?php } ?>
</tbody>
</table>
</div>
<script>
$(document).ready(function() {
$('#list_jeu').DataTable(
......
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