la recherche fonctionne :D
This commit is contained in:
부모
d720b185db
커밋
24da539d65
3개의 변경된 파일과 95개의 추가작업 그리고 57개의 파일을 삭제
42
api.php
42
api.php
|
@ -29,51 +29,59 @@
|
||||||
|
|
||||||
|
|
||||||
if($_SERVER['REQUEST_METHOD'] === 'GET'){
|
if($_SERVER['REQUEST_METHOD'] === 'GET'){
|
||||||
if(isset($_GET["auth"])){
|
// enlève les variables de requète
|
||||||
|
$endpoint = explode("?",array_pop($url_parts))[0];
|
||||||
|
|
||||||
|
switch($endpoint){
|
||||||
|
case 'auth':
|
||||||
try{
|
try{
|
||||||
$_SESSION["utilisateur_authentifie"] = true;
|
$_SESSION["utilisateur_authentifie"] = true;
|
||||||
session_regenerate_id(true);
|
session_regenerate_id(true);
|
||||||
$_SESSION["heure_debut"] = time();
|
$_SESSION["heure_debut"] = time();
|
||||||
echo json_encode(["status"=>"1","msg"=>"Authentification réussie."]);
|
echo(json_encode(["status"=>"1","msg"=>"Authentification réussie."]));
|
||||||
}catch(Exception $e){
|
}catch(Exception $e){
|
||||||
echo( json_encode(["status"=> "0","msg"=> $e->getMessage() ]) );
|
echo( json_encode(["status"=> "0","msg"=> $e->getMessage() ]) );
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
case 'unauth':
|
||||||
|
|
||||||
if(isset($_GET["unauth"])){
|
|
||||||
$_SESSION["utilisateur_authentifie"] = false;
|
$_SESSION["utilisateur_authentifie"] = false;
|
||||||
echo json_encode(["status"=>"1","msg"=>"Déconnection réussie."]);
|
echo json_encode(["status"=>"1","msg"=>"Déconnection réussie."]);
|
||||||
session_destroy();
|
session_destroy();
|
||||||
session_abort();
|
session_abort();
|
||||||
}
|
break;
|
||||||
|
|
||||||
if(isset($_GET["test_auth"])){
|
case 'test_auth':
|
||||||
if($_SESSION["utilisateur_authentifie"] == true){
|
if($_SESSION["utilisateur_authentifie"] == true){
|
||||||
echo(json_encode(["status"=> "1","msg"=> "Utilisateur bien authentifié."]));
|
echo(json_encode(["status"=> "1","msg"=> "Utilisateur bien authentifié."]));
|
||||||
}else{
|
}else{
|
||||||
echo(json_encode(["status"=> "4","msg"=> "Utilisateur non authentifié."]));
|
echo(json_encode(["status"=> "4","msg"=> "Utilisateur non authentifié."]));
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'rechercher':
|
||||||
if (isset($_GET["chercher"])) {
|
// Exemple URL: /api.php/chercher?req=math&duree=30&themes=algebre,geometrie
|
||||||
// Example URL: /api/chercher?rech=math&duree=30&tags=algebre,geometrie
|
|
||||||
|
|
||||||
$query = isset($_GET["req"]) ? $_GET["req"] : "";
|
$query = isset($_GET["req"]) ? $_GET["req"] : "";
|
||||||
$length = isset($_GET["duree"]) ? $_GET["duree"] : "";
|
$length = isset($_GET["duree"]) ? $_GET["duree"] : "";
|
||||||
$themes = isset($_GET["duree"]) ? explode(",", $_GET["themes"]) : [];
|
$themes = isset($_GET["themes"]) ? explode(",", $_GET["themes"]) : [];
|
||||||
|
//print_r($_GET);
|
||||||
try {
|
try {
|
||||||
$results = searchExercises($query, $length, $themes);
|
$results = RechercheExercices($query, $length, $themes);
|
||||||
echo json_encode(["status" => "1", "results" => $results]);
|
echo json_encode(["status" => "1", "resultats" => $results]);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
echo json_encode(["status" => "0", "msg" => $e->getMessage()]);
|
echo json_encode(["status" => "0", "msg" => $e->getMessage()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
echo(json_encode(['status'=> '2','msg'=> "Ce point d'arrivée n'existe pas dans l'api."]));
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
22
bdd.php
22
bdd.php
|
@ -69,7 +69,7 @@ function saveFilesFromPost($postData,$id_ensemble) {
|
||||||
|
|
||||||
|
|
||||||
// Check if the $_POST variable is set and contains files
|
// Check if the $_POST variable is set and contains files
|
||||||
echo(print_r($_FILES,true));
|
//echo(print_r($_FILES,true));
|
||||||
|
|
||||||
if (isset($_FILES) && is_array($_FILES)) {
|
if (isset($_FILES) && is_array($_FILES)) {
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ function saveFilesFromPost($postData,$id_ensemble) {
|
||||||
|
|
||||||
// pour tester, pas implémenté les commentaires globaux ni les themes
|
// pour tester, pas implémenté les commentaires globaux ni les themes
|
||||||
$sql="INSERT INTO documents (titre,type,upload_path,commentaire_auteur,ensemble_id) VALUES(?,?,?,?,?)";
|
$sql="INSERT INTO documents (titre,type,upload_path,commentaire_auteur,ensemble_id) VALUES(?,?,?,?,?)";
|
||||||
$conn->execute_query($sql,array($safe_titre,$safe_type,$filePath,$postData['commentaire_doc_'.$i],$id_ensemble));
|
$conn->execute_query($sql,array($safe_titre,$safe_type,"archives/"+$uniqueFileName,$postData['commentaire_doc_'.$i],$id_ensemble));
|
||||||
}catch(Exception $e){
|
}catch(Exception $e){
|
||||||
echo(json_encode(['status'=> '0','msg'=>$e->getMessage()]));
|
echo(json_encode(['status'=> '0','msg'=>$e->getMessage()]));
|
||||||
//exit;
|
//exit;
|
||||||
|
@ -200,24 +200,21 @@ function saveFilesFromPost($postData,$id_ensemble) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function searchExercises($query, $length, $tags)
|
function RechercheExercices($query, $length, $tags)
|
||||||
{
|
{
|
||||||
$conn = new mysqli($GLOBALS["servername"], $GLOBALS["username"], $GLOBALS["password"], $GLOBALS["dbname"]);
|
global $conn;
|
||||||
|
|
||||||
if ($conn->connect_error) {
|
|
||||||
throw new Exception("Connection failed: " . $conn->connect_error);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build the SQL query based on the search parameters
|
// Build the SQL query based on the search parameters
|
||||||
$sql = "SELECT * FROM exercices";
|
$sql = "SELECT * FROM documents";
|
||||||
|
|
||||||
if (!empty($query) || !empty($length) || !empty($tags)) {
|
if (!empty($query) || !empty($length) || !empty($tags)) {
|
||||||
$sql .= " WHERE";
|
$sql .= " WHERE ";
|
||||||
}
|
}
|
||||||
|
|
||||||
$conditions = [];
|
$conditions = [];
|
||||||
|
|
||||||
if (!empty($query)) {
|
if (!empty($query)) {
|
||||||
|
$query = htmlspecialchars($query);
|
||||||
$conditions[] = "titre LIKE '%$query%'";
|
$conditions[] = "titre LIKE '%$query%'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,14 +224,15 @@ function searchExercises($query, $length, $tags)
|
||||||
|
|
||||||
if (!empty($tags)) {
|
if (!empty($tags)) {
|
||||||
$tagConditions = array_map(function ($tag) {
|
$tagConditions = array_map(function ($tag) {
|
||||||
return "EXISTS (SELECT 1 FROM exercices_themes et, themes t WHERE et.exercice_id = e.id AND et.theme_id = t.id AND t.name = '$tag')";
|
$tag = htmlspecialchars($tag);
|
||||||
|
return "EXISTS (SELECT * FROM exercices_themes AS et INNER JOIN themes AS t ON et.exercice_id = t.id WHERE et.theme_id = t.id AND t.name = '$tag')";
|
||||||
}, $tags);
|
}, $tags);
|
||||||
|
|
||||||
$conditions[] = implode(" AND ", $tagConditions);
|
$conditions[] = implode(" AND ", $tagConditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql .= implode(" AND ", $conditions);
|
$sql .= implode(" AND ", $conditions);
|
||||||
|
//echo $sql;
|
||||||
// Execute the query
|
// Execute the query
|
||||||
$result = $conn->query($sql);
|
$result = $conn->query($sql);
|
||||||
|
|
||||||
|
|
38
index.php
38
index.php
|
@ -17,17 +17,23 @@
|
||||||
<div id="user_status">
|
<div id="user_status">
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<form>
|
||||||
|
<input type="text" id="recherche_input" placeholder="Rechercher une fiche, annale ...">
|
||||||
|
<input type="text" id="themes_input" placeholder="themes séparés par une virgule">
|
||||||
|
<input type="number" id="duree_input" placeholder="durée en minutes">
|
||||||
|
</form>
|
||||||
</body>
|
</body>
|
||||||
<script>
|
<script>
|
||||||
async function test_auth(){
|
async function test_auth(){
|
||||||
resp = await fetch("/annales/api.php?test_auth");
|
resp = await fetch("/annales/api.php/test_auth");
|
||||||
data = await resp.json();
|
data = await resp.json();
|
||||||
document.getElementById("user_status").innerText = data["msg"];
|
document.getElementById("user_status").innerText = data["msg"];
|
||||||
}
|
}
|
||||||
|
|
||||||
// fonction de test, innutile en prod
|
// fonction de test, innutile en prod
|
||||||
async function authenticate_user(){
|
async function authenticate_user(){
|
||||||
resp = await fetch("/annales/api.php?auth");
|
resp = await fetch("/annales/api.php/auth");
|
||||||
data = await resp.json();
|
data = await resp.json();
|
||||||
if(data.status == 1){
|
if(data.status == 1){
|
||||||
document.getElementById("user_status").innerText = data["msg"];
|
document.getElementById("user_status").innerText = data["msg"];
|
||||||
|
@ -36,14 +42,40 @@
|
||||||
|
|
||||||
|
|
||||||
async function unauthenticate_user(){
|
async function unauthenticate_user(){
|
||||||
resp = await fetch("/annales/api.php?unauth");
|
resp = await fetch("/annales/api.php/unauth");
|
||||||
data = await resp.json();
|
data = await resp.json();
|
||||||
if(data.status == 1){
|
if(data.status == 1){
|
||||||
document.getElementById("user_status").innerText = data["msg"];
|
document.getElementById("user_status").innerText = data["msg"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async function rechercher(){
|
||||||
|
var req = document.getElementById("recherche_input").value;
|
||||||
|
|
||||||
|
|
||||||
|
resp = await fetch("/annales/api.php/rechercher?req="+req);
|
||||||
|
data = await resp.json();
|
||||||
|
if(data.status == 1){
|
||||||
|
data.resultats.forEach(doc => {
|
||||||
|
const img = document.createElement("img");
|
||||||
|
img.src = doc.upload_path;
|
||||||
|
document.body.appendChild(img);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
test_auth();
|
test_auth();
|
||||||
|
document.getElementById("recherche_input").onkeydown =function(event) {
|
||||||
|
if (event.key === "Enter"){
|
||||||
|
rechercher();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</html>
|
</html>
|
불러오는 중…
Reference in a new issue