bon ça marche pas mais c'est le début

This commit is contained in:
thaaoblues 2023-10-22 20:01:47 +02:00
parent 394055b19b
commit 8158f4f3fa
3 changed files with 51 additions and 19 deletions

11
api.php
View file

@ -12,7 +12,6 @@
*/ */
include("session_verif.php"); include("session_verif.php");
include("test_creds.php");
include("bdd.php"); include("bdd.php");
@ -62,9 +61,15 @@
if($_SERVER['REQUEST_METHOD'] === 'POST'){ if($_SERVER['REQUEST_METHOD'] === 'POST'){
switch(array_shift($url_parts)){
switch(array_pop($url_parts)){
case "aj_doc": case "aj_doc":
ajouter_doc($_POST); try{
ajouter_doc($_POST);
}catch(Exception $e){
echo( json_encode(["status"=> "0","msg"=> $e->getMessage() ]) );
}
break; break;
default: default:
echo(json_encode(["status"=> "2","msg"=> "Opération inconnue."])); echo(json_encode(["status"=> "2","msg"=> "Opération inconnue."]));

44
bdd.php
View file

@ -1,26 +1,33 @@
<?php <?php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
include("annales/test_creds.php");
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "archivinsa";
function ajouter_doc($request){ function ajouter_doc($request){
saveFilesFromPost($request);
if (isset($request['files']) && is_array($request['files'])) { $conn = new mysqli($GLOBALS["servername"], $GLOBALS["username"], $GLOBALS["password"], $GLOBALS["dbname"]);
foreach ($request['files'] as $file) {
$sql="INSERT INTO "; // Check connection
} if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO ensemble (commentaire_auteur) VALUES(\"\")";
try{
$conn->execute_query($sql,array("",));
saveFilesFromPost($request,mysqli_insert_id($conn),$conn);
}catch(Exception $e){
echo(json_encode(["status"=>"0","msg"=>$e]));
} }
} }
function saveFilesFromPost($postData) { function saveFilesFromPost($postData,$id_ensemble,$conn) {
// Check if the $_POST variable is set and contains files // Check if the $_POST variable is set and contains files
if (isset($postData['files']) && is_array($postData['files'])) { if (isset($postData['files']) && is_array($postData['files'])) {
// Directory to save the files // Directory to save the files
@ -47,6 +54,15 @@ function saveFilesFromPost($postData) {
} else { } else {
echo(json_encode(["status"=>"0","msg"=>"Error saving file '$uniqueFileName'"])); echo(json_encode(["status"=>"0","msg"=>"Error saving file '$uniqueFileName'"]));
} }
//update the database
$safe_titre = htmlspecialchars($postData['titre']);
$safe_type = htmlspecialchars($postData['type']);
// pour tester, pas implémenté les commentaires globaux ni les themes
$sql="INSERT INTO documents (titre,type,upload_path,commentaire_auteur,ensemble_id) VALUES(?,?,?,?,?)";
$conn->execute_query($sql, array("titre"=> $safe_titre,"type"=>$safe_type,"upload_path"=> $uploadDir,"commentaire_auteur"=>"","ensemble_id"=>$id_ensemble));
} }
} else { } else {
echo(json_encode(["status"=>"2","msg"=>"No files in the POST data."])); echo(json_encode(["status"=>"2","msg"=>"No files in the POST data."]));

View file

@ -14,6 +14,14 @@
<!-- Button to open the camera --> <!-- Button to open the camera -->
<button onclick="openCamera()">Open Camera</button> <button onclick="openCamera()">Open Camera</button>
<input type="text" placeholder="titre" id="titre"></input>
<select id="select_type">
<option value="annale">annale</option>
<option value="fiche_revision">fiche_revision</option>
</select>
<script> <script>
function uploadFiles() { function uploadFiles() {
const fileInput = document.getElementById('fileInput'); const fileInput = document.getElementById('fileInput');
@ -21,13 +29,16 @@ function uploadFiles() {
// Create FormData object to append files // Create FormData object to append files
const formData = new FormData(); const formData = new FormData();
formData.append("type",document.getElementById("select_type").getAttribute("value"));
formData.append("titre",document.getElementById("titre").getAttribute("value"));
// Append each selected file to the FormData // Append each selected file to the FormData
for (const file of fileInput.files) { for (const file of fileInput.files) {
formData.append('files[]', file); formData.append('files[]', file);
} }
// Make a POST request using Fetch API // Make a POST request using Fetch API
fetch('annales/api.php/aj_doc.php', { fetch('api.php/aj_doc', {
method: 'POST', method: 'POST',
body: formData body: formData
}) })
@ -68,7 +79,7 @@ function openCamera() {
mediaStream.getTracks().forEach(track => track.stop()); mediaStream.getTracks().forEach(track => track.stop());
// Make a POST request to upload the image // Make a POST request to upload the image
fetch('annales/api.php/aj_doc', { fetch('api.php/aj_doc', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',