BenBack #6
@@ -3,7 +3,7 @@
|
||||
return [
|
||||
'host' => 'localhost',
|
||||
'port' => 3306,
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'user' => 'benjamin',
|
||||
'pass' => '011235813',
|
||||
'name' => 'siterecette'
|
||||
];
|
||||
111
public/assets/js/advanced-search.js
Normal file
111
public/assets/js/advanced-search.js
Normal file
@@ -0,0 +1,111 @@
|
||||
document.addEventListener('DOMContentLoaded', function(){
|
||||
|
||||
let searchInput = "";
|
||||
let tagsId = [];
|
||||
let ingredientsId = [];
|
||||
|
||||
function buildCards( data ){
|
||||
|
||||
if( Array.isArray( data ) ){
|
||||
data = Object.assign({}, data );
|
||||
}
|
||||
|
||||
document.getElementById( "recetteList" ).innerHTML = '';
|
||||
for( const [key, recette] of Object.entries( data ) ) {
|
||||
let HTML_CONTENT = `<a class="recette-icone" href="<?php V::routeUrl( 'recettes->show', $recette->slug ); ?>">
|
||||
<img class="recette-preview-image" src="random-recette.jpg">
|
||||
<div class="recette-icone-content">
|
||||
<h3>${recette.titre_recette}</h3>
|
||||
<ul>
|
||||
<li>Temps de préparation : ${recette.temps_de_preparation}</li>
|
||||
<li>Nombre d'ingrédients : data.nb_ing</li>
|
||||
</ul>
|
||||
</div>
|
||||
</a>`
|
||||
document.getElementById("recetteList").innerHTML += HTML_CONTENT;
|
||||
}
|
||||
}
|
||||
|
||||
function advancedSearch(){
|
||||
|
||||
let form = new FormData();
|
||||
form.append( "title", searchInput );
|
||||
form.append( "tagsId", tagsId );
|
||||
form.append( "ingredientsId", ingredientsId );
|
||||
|
||||
const XML = new XMLHttpRequest();
|
||||
XML.open( "POST", '/api/recettes/list' );
|
||||
XML.onreadystatechange = function ( e ) {
|
||||
if( XML.status === 200 && XML.readyState === XMLHttpRequest.DONE ) {
|
||||
try {
|
||||
console.log( XML.response );
|
||||
let response = JSON.parse(XML.response);
|
||||
console.log( "Reponse parsé" );
|
||||
buildCards( response.data );
|
||||
} catch( e ) {
|
||||
console.log( "Ce n'est pas un fichier JSON" );
|
||||
}
|
||||
}
|
||||
}
|
||||
XML.send( form );
|
||||
}
|
||||
|
||||
const INGREDIENTSLIST = document.getElementById( 'ingredientsList' );
|
||||
for( let item of INGREDIENTSLIST.getElementsByTagName( 'li' ) ){
|
||||
item.addEventListener( 'click', function(){
|
||||
|
||||
let ingredientId = Number( item.getAttribute( 'data-ingredient-id' ) );
|
||||
let index = ingredientsId.indexOf( ingredientId );
|
||||
|
||||
if( index > -1 ) {
|
||||
ingredientsId.splice( index, 1 );
|
||||
item.classList.add( 'tag-unselected' );
|
||||
item.classList.remove( 'tag-selected' );
|
||||
} else {
|
||||
ingredientsId.push( ingredientId );
|
||||
item.classList.add( 'tag-selected' );
|
||||
item.classList.remove( 'tag-unselected' );
|
||||
}
|
||||
|
||||
advancedSearch();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
const TAGSLIST = document.getElementById( 'tagsList' );
|
||||
for( let item of TAGSLIST.getElementsByTagName( 'li' ) ){
|
||||
item.addEventListener( 'click', function(){
|
||||
|
||||
let tagId = Number( item.getAttribute( 'data-tag-id' ) );
|
||||
let index = tagsId.indexOf( tagId );
|
||||
|
||||
if( index > -1 ) {
|
||||
tagsId.splice( index, 1 );
|
||||
item.classList.add( 'tag-unselected' );
|
||||
item.classList.remove( 'tag-selected' );
|
||||
} else {
|
||||
tagsId.push( tagId );
|
||||
item.classList.add( 'tag-selected' );
|
||||
item.classList.remove( 'tag-unselected' );
|
||||
}
|
||||
|
||||
advancedSearch();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
const SEARCHBAR = document.getElementById( 'searchForm' );
|
||||
searchInput = new URLSearchParams(document.location.search).get("s");
|
||||
console.log( searchInput );
|
||||
|
||||
if( searchInput !== "" ) {
|
||||
advancedSearch();
|
||||
}
|
||||
|
||||
SEARCHBAR.onsubmit = function( e ){
|
||||
e.preventDefault();
|
||||
searchInput = document.getElementById( 'searchFormField' ).value;
|
||||
advancedSearch();
|
||||
}
|
||||
|
||||
});
|
||||
@@ -97,6 +97,40 @@ class RecetteRepository extends Repository implements UseIngredientsInterface, U
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
public function advancedRecetteSearch( string $title = "", array $tagsId= [], array $ingredientsId= [] ): ?array {
|
||||
|
||||
$tableLinkIngredients = IngredientRepository::getStructure()['link_recettes'];
|
||||
$tableLinkTags = TagRepository::getStructure()['link_recettes'];
|
||||
|
||||
$sqlQuery = "SELECT ({$this->tableName}.num_recette) FROM {$this->tableName} LEFT JOIN {$tableLinkIngredients} ON {$this->tableName}.num_recette = {$tableLinkIngredients}.num_recette LEFT JOIN {$tableLinkTags} ON {$this->tableName}.num_recette = {$tableLinkTags}.num_recette";
|
||||
|
||||
if( $title != "" || $tagsId !== [] || $ingredientsId !== [] ) {
|
||||
$sqlQuery .= " WHERE ";
|
||||
if ($title != "")
|
||||
$sqlQuery .= " titre_recette LIKE '%{$title}%' AND";
|
||||
if ($tagsId !== [])
|
||||
$sqlQuery .= " num_tag IN (" . implode(",", $tagsId) . ") AND";
|
||||
if ($ingredientsId !== [])
|
||||
$sqlQuery .= " num_ingredient IN (" . implode(",", $ingredientsId) . ") AND";
|
||||
$sqlQuery = substr($sqlQuery, 0, -3);
|
||||
}
|
||||
$sqlQuery .= ";";
|
||||
|
||||
$results = $this->selectGetAll($sqlQuery, true);
|
||||
if( $results === null )
|
||||
return null;
|
||||
|
||||
$alreadyGettedId = [];
|
||||
for( $i = 0; $i < count( $results ); $i++ ){
|
||||
if( in_array( $results[$i]['num_recette'], $alreadyGettedId ) )
|
||||
unset( $results[$i] );
|
||||
else
|
||||
$alreadyGettedId[] = $results[$i]['num_recette'];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function add( Model $recette ): bool {
|
||||
return $this->addEntity( $recette );
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Domain\Recettes;
|
||||
|
||||
use App\Domain\Controller;
|
||||
use App\Http\JSONResponse;
|
||||
use App\Http\Request;
|
||||
|
||||
class RecettesAPIController extends Controller {
|
||||
|
||||
@@ -13,4 +15,29 @@ class RecettesAPIController extends Controller {
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
public function list(){
|
||||
|
||||
$title = Request::post( 'title' ) ?? "";
|
||||
$tagsId = explode( ",", Request::post( 'tagsId' ) ) ?? [];
|
||||
$ingredientsId = explode( ",", Request::post( 'ingredientsId' ) ) ?? [];
|
||||
|
||||
if( $tagsId == [ "" ] )
|
||||
$tagsId = [];
|
||||
if( $ingredientsId == [ "" ] )
|
||||
$ingredientsId = [];
|
||||
|
||||
$tagsId = array_map( 'intval', $tagsId );
|
||||
$ingredientsId = array_map( 'intval', $ingredientsId );
|
||||
|
||||
|
||||
$recetteRepo = new RecetteRepository();
|
||||
$resp = $recetteRepo->advancedRecetteSearch( $title, $tagsId, $ingredientsId );
|
||||
|
||||
$resp = array_map( function($recette) use ($recetteRepo){
|
||||
return $recetteRepo->getByID( $recette['num_recette'] );
|
||||
}, $resp ?? [] );
|
||||
JSONResponse::sendSuccess( [ 'data' => $resp ] );
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,5 @@
|
||||
new \App\Domain\Ingredients\IngredientRepository()->add($ing);
|
||||
*/
|
||||
|
||||
$recette = new \App\Domain\Recettes\RecetteRepository()->getByID( 2 );
|
||||
$ingredient = new \App\Domain\Ingredients\IngredientRepository()->getByID( 2 );
|
||||
// var_dump( $recette );
|
||||
// var_dump( $ingredient );
|
||||
|
||||
// new \App\Domain\Recettes\RecetteRepository()->removeAnIngredient( $ingredient, $recette );
|
||||
var_dump( $recette->getAllLinkedIngredients() );
|
||||
var_dump( new \App\Domain\Recettes\RecetteRepository()->advancedRecetteSearch( "", [ 4 ], [] ) );
|
||||
?>
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
<li><a id="login" class= "nav-element" href="<?php V::routeUrl( 'login'); ?>">Login</a></li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
<input type="text" class="search-form search-form-recette" placeholder="Rechercher une recette ...">
|
||||
<form id="searchForm" method="get" action="<?php V::routeUrl( 'recettes->index'); ?>">
|
||||
<input name="s" id="searchFormField" type="text" class="search-form search-form-recette" placeholder="Rechercher une recette ...">
|
||||
</form>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?php use App\Infrastructure\View as V; ?>
|
||||
<?php use App\Infrastructure\View as V; use App\Http\Router;?>
|
||||
|
||||
<div class="sidebar">
|
||||
<div class="tag-cont">
|
||||
@@ -11,9 +11,9 @@
|
||||
<input type="text" class="search-form search-form-tag" name="search-tag" placeholder="Rechercher..." >
|
||||
</form>
|
||||
<div class="tag-unselected-div">
|
||||
<ul>
|
||||
<ul id="tagsList">
|
||||
<?php foreach( V::arg( 'tagsList') as $tag ): ?>
|
||||
<li class="tag tag-unselected" onclick="test()"><?php echo $tag->nom_tag; ?></li>
|
||||
<li class="tag tag-unselected" data-tag-id="<?php echo $tag->num_tag;?>"><?php echo $tag->nom_tag; ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
@@ -28,12 +28,13 @@
|
||||
<form class="sidebar-search" action="none">
|
||||
<input type="text" class="search-form search-form-tag" name="search-ingr" placeholder="Rechercher..." >
|
||||
</form>
|
||||
<div class="tag-unselected-div">
|
||||
<ul>
|
||||
<div class="ing-unselected-div">
|
||||
<ul id="ingredientsList">
|
||||
<?php foreach( V::arg( 'ingredientsList') as $tag ): ?>
|
||||
<li class="tag tag-unselected" onclick="test()"><?php echo $tag->nom_ingredient; ?></li>
|
||||
<li class="tag tag-unselected" data-ingredient-id="<?php echo $tag->num_ingredient;?>"><?php echo $tag->nom_ingredient; ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="<?php echo Router::getAssetURL( 'js/advanced-search.js' ); ?>" defer></script>
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
<?php V::partial( 'tag-sidebar' ); ?>
|
||||
<div class="content">
|
||||
<div class="recettes">
|
||||
<div id="recetteList" class="recettes">
|
||||
<?php if( V::arg( 'recettesList' ) != null ) foreach( V::arg( 'recettesList' ) as $recette ): ?>
|
||||
<a class="recette-icone" href="<?php V::routeUrl( 'recettes->show', $recette->slug ); ?>">
|
||||
<img class="recette-preview-image" src="random-recette.jpg">
|
||||
|
||||
Reference in New Issue
Block a user