Merge pull request 'BenBack' (#6) from BenBack into master
Reviewed-on: #6
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
return [
|
||||
'host' => 'localhost',
|
||||
'port' => 3306,
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'user' => 'benjamin',
|
||||
'pass' => '011235813',
|
||||
'name' => 'siterecette'
|
||||
];
|
||||
0
public/assets/css/style.css
Normal file → Executable file
0
public/assets/css/style.css
Normal file → Executable file
0
public/assets/images/Logo.jpg
Normal file → Executable file
0
public/assets/images/Logo.jpg
Normal file → Executable file
|
Before Width: | Height: | Size: 141 KiB After Width: | Height: | Size: 141 KiB |
110
public/assets/js/advanced-search.js
Executable file
110
public/assets/js/advanced-search.js
Executable file
@@ -0,0 +1,110 @@
|
||||
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="${recette.url}">
|
||||
<img class="recette-preview-image" src="${recette.photo}">
|
||||
<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 : ${recette.nb_ingredients}</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") || searchInput;
|
||||
|
||||
if( searchInput !== undefined && searchInput != "" ) {
|
||||
advancedSearch();
|
||||
}
|
||||
|
||||
SEARCHBAR.onsubmit = function( e ){
|
||||
e.preventDefault();
|
||||
searchInput = document.getElementById( 'searchFormField' ).value;
|
||||
advancedSearch();
|
||||
}
|
||||
|
||||
});
|
||||
0
public/assets/js/login.js
Normal file → Executable file
0
public/assets/js/login.js
Normal file → Executable file
0
public/index.php
Normal file → Executable file
0
public/index.php
Normal file → Executable file
0
public/uploads/.gitignore
vendored
Normal file
0
public/uploads/.gitignore
vendored
Normal file
@@ -21,6 +21,12 @@ class Ingredient extends Model {
|
||||
*/
|
||||
public string $nom_ingredient;
|
||||
|
||||
/**
|
||||
* L'URL vers l'image de l'ingrédient.
|
||||
* @var string
|
||||
*/
|
||||
public string $photo_ingredient;
|
||||
|
||||
/**
|
||||
* Retourne le numéro de l'ingrédient.
|
||||
* @return int
|
||||
|
||||
@@ -35,7 +35,7 @@ class IngredientRepository extends Repository implements LinkableInterface {
|
||||
return [
|
||||
'table' => 'Ingredient',
|
||||
'columns' => [
|
||||
'num_ingredient', 'nom_ingredient'
|
||||
'num_ingredient', 'nom_ingredient', 'photo_ingredient',
|
||||
],
|
||||
'link_recettes' => 'Listeingredient'
|
||||
];
|
||||
|
||||
70
src/Domain/Ingredients/IngredientsAPIController.php
Normal file
70
src/Domain/Ingredients/IngredientsAPIController.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Ingredients;
|
||||
|
||||
use App\Domain\Controller;
|
||||
use App\Helpers\UploadFiles;
|
||||
use App\Http\JSONResponse;
|
||||
use App\Http\Request;
|
||||
|
||||
class IngredientsAPIController extends Controller {
|
||||
|
||||
public static function defineRoutes(): array
|
||||
{
|
||||
return [
|
||||
self::Route( routeUrl: '/api/ingredients/create', routeName: 'api->ingredients->create', routeAction: 'create', routeMethods: ['POST'] ),
|
||||
self::Route( routeUrl: '/api/ingredients/edit', routeName: 'api->ingredients->edit', routeAction: 'edit', routeMethods: ['POST'] ),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
public function create(){
|
||||
|
||||
$name = Request::post( 'name' );
|
||||
$fileNameField = "image";
|
||||
if( !$name || $name == "" )
|
||||
JSONResponse::sendError( [ 'error' => 'Name not defined' ] );
|
||||
|
||||
$urlOrError = UploadFiles::uploadFile( $fileNameField );
|
||||
if( is_int( $urlOrError ) ){
|
||||
JSONResponse::sendError( [ 'error' => $urlOrError ] );
|
||||
}
|
||||
|
||||
$ingredient = new Ingredient();
|
||||
|
||||
$ingredient->num_ingredient = 0;
|
||||
$ingredient->nom_ingredient = $name;
|
||||
$ingredient->photo_ingredient = $urlOrError;
|
||||
|
||||
if( !new IngredientRepository()->add( $ingredient ) )
|
||||
JSONResponse::sendError( [ 'error' => 'An error occured while adding ingredient' ] );
|
||||
|
||||
JSONResponse::sendSuccess( [ 'image_url' => $urlOrError ] );
|
||||
|
||||
}
|
||||
|
||||
public function edit(){
|
||||
|
||||
$id = Request::post( 'id' ) ?? 0;
|
||||
$ingredient = new IngredientRepository()->getByID( $id );
|
||||
if( !$ingredient )
|
||||
JSONResponse::sendError( [ 'error' => 'Ingredient not found' ] );
|
||||
|
||||
$name = Request::post( 'name' );
|
||||
$fileNameField = "image";
|
||||
|
||||
if( $name ) {
|
||||
$ingredient->nom_ingredient = $name;
|
||||
}
|
||||
|
||||
$urlOrError = UploadFiles::uploadFile( $fileNameField );
|
||||
if( !is_int( $urlOrError ) ){
|
||||
$ingredient->photo_ingredient = $urlOrError;
|
||||
}
|
||||
|
||||
if( !new IngredientRepository()->update( $ingredient ) )
|
||||
JSONResponse::sendError( [ 'error' => 'An error occured while updating ingredient' ] );
|
||||
|
||||
JSONResponse::sendSuccess( [] );
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,13 @@ use App\Domain\Ingredients\Ingredient;
|
||||
use App\Domain\Ingredients\IngredientRepository;
|
||||
use App\Domain\Ingredients\UseIngredientsInterface;
|
||||
use App\Domain\Model;
|
||||
use App\Domain\Tags\Tag;
|
||||
use App\Helpers\Markdown;
|
||||
|
||||
/**
|
||||
* Classe qui va permettre de gérer tous les objets recette.
|
||||
*/
|
||||
#[\AllowDynamicProperties]
|
||||
class Recette extends Model {
|
||||
|
||||
public int $num_recette;
|
||||
@@ -43,6 +45,15 @@ class Recette extends Model {
|
||||
return new RecetteRepository()->getAllLinkedIngredients( $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère une liste de tous les tags liés à la recette.
|
||||
* @return Tag[]|null
|
||||
*/
|
||||
public function getAllLinkedTags(): ?array
|
||||
{
|
||||
return new RecetteRepository()->getAllLinkedTags( $this );
|
||||
}
|
||||
|
||||
public function getNumberOfIngredients(): int {
|
||||
|
||||
$response = $this->getAllLinkedIngredients();
|
||||
|
||||
@@ -90,13 +90,47 @@ class RecetteRepository extends Repository implements UseIngredientsInterface, U
|
||||
* @return Recette|null
|
||||
*/
|
||||
public function getBySlug( string $slug ): ?Recette {
|
||||
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE slug = {$slug}";
|
||||
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE slug = '{$slug}'";
|
||||
$results = $this->selectGetAll($sqlQuery);
|
||||
if( $results === null || count( $results ) > 1 )
|
||||
return null;
|
||||
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,12 @@
|
||||
namespace App\Domain\Recettes;
|
||||
|
||||
use App\Domain\Controller;
|
||||
use App\Domain\Ingredients\IngredientRepository;
|
||||
use App\Domain\Tags\TagRepository;
|
||||
use App\Helpers\UploadFiles;
|
||||
use App\Http\JSONResponse;
|
||||
use App\Http\Request;
|
||||
use App\Http\Router;
|
||||
|
||||
class RecettesAPIController extends Controller {
|
||||
|
||||
@@ -10,7 +16,279 @@ class RecettesAPIController extends Controller {
|
||||
{
|
||||
return [
|
||||
self::Route( routeUrl: '/api/recettes/list', routeName: 'api->recettes->list', routeAction: 'list', routeMethods: ['POST'] ),
|
||||
self::Route( routeUrl: '/api/recettes/create', routeName: 'api->recettes->create', routeAction: 'create', routeMethods: ['POST'] ),
|
||||
self::Route( routeUrl: '/api/recettes/edit', routeName: 'api->recettes->edit', routeAction: 'edit', routeMethods: ['POST'] ),
|
||||
self::Route( routeUrl: '/api/recettes/delete', routeName: 'api->recettes->delete', routeAction: 'delete', routeMethods: ['POST'] ),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
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){
|
||||
$r = $recetteRepo->getByID( $recette['num_recette'] );
|
||||
$r->url = Router::getRouteURL( 'recettes->show', $r->slug );
|
||||
$r->nb_ingredients = $r->getNumberOfIngredients();
|
||||
return $r;
|
||||
}, $resp ?? [] );
|
||||
JSONResponse::sendSuccess( [ 'data' => $resp ] );
|
||||
|
||||
}
|
||||
|
||||
public function create(){
|
||||
|
||||
// Récupération des champs.
|
||||
$name = Request::post( 'nom' ) ?? "";
|
||||
$temps = Request::post( 'temps' ) ?? "";
|
||||
$fileField = "image";
|
||||
|
||||
$tagsId = explode( ",", Request::post( 'tag' ) ) ?? [];
|
||||
$ingredientsId = explode( ",", Request::post( 'ingr' ) ) ?? [];
|
||||
|
||||
if( $tagsId == [ "" ] )
|
||||
$tagsId = [];
|
||||
if( $ingredientsId == [ "" ] )
|
||||
$ingredientsId = [];
|
||||
|
||||
$tagsId = array_map( 'intval', $tagsId );
|
||||
$ingredientsId = array_map( 'intval', $ingredientsId );
|
||||
|
||||
$description = Request::post( 'description' );
|
||||
|
||||
// Vérification.
|
||||
if( $name == "" || $temps == "" || $ingredientsId == [] || $description == "" )
|
||||
JSONResponse::sendError( [ 'error' => "One required fields is missing" ] );
|
||||
|
||||
// Upload & Vérification de l'image.
|
||||
$urlOrError = UploadFiles::uploadFile( $fileField );
|
||||
if( is_int( $urlOrError ) ){
|
||||
JSONResponse::sendError( [ 'error' => $urlOrError ] );
|
||||
}
|
||||
|
||||
// Vérification d'une entrée qui existerait déjà.
|
||||
$slug = strtolower( $name );
|
||||
$slug = str_replace( ' ', '-', $slug );
|
||||
|
||||
$recetteRepo = new RecetteRepository();
|
||||
|
||||
if( $recetteRepo->getBySlug( $slug ) ){
|
||||
JSONResponse::sendError( [ 'error' => "Recette already exists" ] );
|
||||
}
|
||||
|
||||
// Création de la recette.
|
||||
$recette = new Recette();
|
||||
|
||||
$recette->num_recette = 0;
|
||||
$recette->titre_recette = $name;
|
||||
$recette->slug = $slug;
|
||||
|
||||
$recette->temps_de_preparation = intval( $temps );
|
||||
$recette->photo = $urlOrError;
|
||||
$recette->description_recette = $description;
|
||||
$recette->publication_date = date("Y-m-d");
|
||||
|
||||
// Importation de la recette.
|
||||
if( !$recetteRepo->add( $recette ) ){
|
||||
JSONResponse::sendError( [ 'error' => "Error adding recette" ] );
|
||||
}
|
||||
|
||||
// Pour actualiser l'ID.
|
||||
$recette = $recetteRepo->getBySlug( $slug );
|
||||
|
||||
// Ajout du lien ingrédients recettes.
|
||||
foreach( $ingredientsId as $ingredientId ){
|
||||
$ingredient = new IngredientRepository()->getByID( $ingredientId );
|
||||
if( $ingredient == null )
|
||||
continue;
|
||||
$recetteRepo->addAnIngredient( $ingredient, $recette );
|
||||
}
|
||||
|
||||
// Ajout du lien Recettes tags.
|
||||
if( $tagsId != [] ){
|
||||
foreach( $tagsId as $tagId ){
|
||||
$tag = new TagRepository()->getByID( $tagId );
|
||||
if( $tag == null )
|
||||
continue;
|
||||
$recetteRepo->addATag( $tag, $recette );
|
||||
}
|
||||
}
|
||||
|
||||
JSONResponse::sendSuccess( [ 'recette' => $recette ] );
|
||||
}
|
||||
|
||||
public function edit(){
|
||||
|
||||
// Récupération des champs.
|
||||
$id = Request::post( 'id' );
|
||||
$name = Request::post( 'nom' ) ?? "";
|
||||
$temps = Request::post( 'temps' ) ?? "";
|
||||
$fileField = "image";
|
||||
|
||||
$tagsId = explode( ",", Request::post( 'tag' ) ) ?? [];
|
||||
$ingredientsId = explode( ",", Request::post( 'ingr' ) ) ?? [];
|
||||
|
||||
if( $tagsId == [ "" ] )
|
||||
$tagsId = [];
|
||||
if( $ingredientsId == [ "" ] )
|
||||
$ingredientsId = [];
|
||||
|
||||
$tagsId = array_map( 'intval', $tagsId );
|
||||
$ingredientsId = array_map( 'intval', $ingredientsId );
|
||||
|
||||
$description = Request::post( 'description' );
|
||||
|
||||
// Vérification.
|
||||
if( $id == "" || $name == "" || $temps == "" || $ingredientsId == [] || $description == "" )
|
||||
JSONResponse::sendError( [ 'error' => "One required fields is missing" ] );
|
||||
|
||||
$recetteRepo = new RecetteRepository();
|
||||
$recette = $recetteRepo->getByID( $id );
|
||||
if( !$recette ){
|
||||
JSONResponse::sendError( [ 'error' => "Recette not found" ] );
|
||||
}
|
||||
|
||||
// Upload & Vérification de l'image.
|
||||
$urlOrError = UploadFiles::uploadFile( $fileField );
|
||||
if( is_int( $urlOrError ) ){
|
||||
// Ingore image.
|
||||
} else {
|
||||
$recette->photo = $urlOrError;
|
||||
}
|
||||
|
||||
// Vérification d'une entrée qui existerait déjà.
|
||||
$slug = strtolower( $name );
|
||||
$slug = str_replace( ' ', '-', $slug );
|
||||
|
||||
$recette->titre_recette = $name;
|
||||
$recette->slug = $slug;
|
||||
|
||||
$recette->temps_de_preparation = intval( $temps );
|
||||
$recette->description_recette = $description;
|
||||
|
||||
// Importation de la recette.
|
||||
if( !$recetteRepo->update( $recette ) ){
|
||||
JSONResponse::sendError( [ 'error' => "Error updating recette" ] );
|
||||
}
|
||||
|
||||
// Actualisation des ingrédients.
|
||||
$ingrObjArray = $recette->getAllLinkedIngredients();
|
||||
if( !$ingrObjArray || $ingrObjArray === [] ){ // Si il n'y a pas déjà d'autres ingrédients, on ajoute tous ceux présents ici.
|
||||
|
||||
foreach( $ingredientsId as $ingredientId ){
|
||||
$ingredient = new IngredientRepository()->getByID( $ingredientId );
|
||||
if( $ingredient == null )
|
||||
continue;
|
||||
$recetteRepo->addAnIngredient( $ingredient, $recette );
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$ingrIntArray = array_map( function($ingr){
|
||||
return intval( $ingr->getID() );
|
||||
}, $ingrObjArray );
|
||||
|
||||
foreach( $ingrIntArray as $ingrId ){
|
||||
// Si un ingrédient n'est pas dans la nouvelle liste d'ingrédients, il faut retirer le lien.
|
||||
if( !in_array( $ingrId, $ingredientsId ) ){
|
||||
$ingredient = new IngredientRepository()->getByID( $ingrId );
|
||||
if( $ingredient == null )
|
||||
continue;
|
||||
$recetteRepo->removeAnIngredient( $ingredient, $recette );
|
||||
}
|
||||
}
|
||||
|
||||
foreach( $ingredientsId as $ingrId ){
|
||||
// Si un ingrédient n'est pas dans l'ancienne liste, il faut ajouter un lien.
|
||||
if( !in_array( $ingrId, $ingrIntArray ) ){
|
||||
$ingredient = new IngredientRepository()->getByID( $ingrId );
|
||||
if( $ingredient == null )
|
||||
continue;
|
||||
$recetteRepo->addAnIngredient( $ingredient, $recette );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Actualisation des tags.
|
||||
if( $tagsId != [] ) {
|
||||
$tagObjArray = $recette->getAllLinkedTags();
|
||||
if (!$tagObjArray || $tagObjArray === []) { // Si il n'y a pas déjà d'autres tags, on ajoute tous ceux présents ici.
|
||||
|
||||
foreach ($tagsId as $tagId) {
|
||||
$tag = new TagRepository()->getByID($tagId);
|
||||
if ($tag == null)
|
||||
continue;
|
||||
$recetteRepo->addATag($tag, $recette);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$tagIntArray = array_map(function ($ingr) {
|
||||
return intval( $ingr->getID() );
|
||||
}, $tagObjArray);
|
||||
|
||||
foreach ($tagIntArray as $tagId) {
|
||||
// Si un ingrédient n'est pas dans la nouvelle liste d'ingrédients, il faut retirer le lien.
|
||||
if (!in_array($tagId, $tagsId)) {
|
||||
$tag = new TagRepository()->getByID($tagId);
|
||||
if ($tag == null)
|
||||
continue;
|
||||
$recetteRepo->removeATag($tag, $recette);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($tagsId as $tagId) {
|
||||
// Si un ingrédient n'est pas dans l'ancienne liste, il faut ajouter un lien.
|
||||
if (!in_array($tagId, $tagIntArray)) {
|
||||
$tag = new TagRepository()->getByID($tagId);
|
||||
if ($tag == null)
|
||||
continue;
|
||||
$recetteRepo->addATag($tag, $recette);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
JSONResponse::sendSuccess( [ 'recette' => $recette ] );
|
||||
}
|
||||
|
||||
public function delete(){
|
||||
$id = Request::post( 'id' );
|
||||
$recette = new RecetteRepository()->getByID( $id );
|
||||
if( !$recette )
|
||||
JSONResponse::sendError( [ 'error' => 'Recette not found' ] );
|
||||
|
||||
$recetteRepo = new RecetteRepository();
|
||||
|
||||
// Retirer tous les liens Ingrédients/Tags.
|
||||
foreach( ( $recette->getAllLinkedIngredients() ?? [] ) as $ingr ){
|
||||
$recetteRepo->removeAnIngredient( $ingr, $recette );
|
||||
}
|
||||
foreach( ( $recette->getAllLinkedTags() ?? [] ) as $tag ){
|
||||
$recetteRepo->removeATag( $tag, $recette );
|
||||
}
|
||||
|
||||
if( !$recetteRepo->delete( $recette ) )
|
||||
JSONResponse::sendError( [ 'error' => 'An error occured while deleting recette' ] );
|
||||
|
||||
JSONResponse::sendSuccess();
|
||||
|
||||
}
|
||||
}
|
||||
72
src/Domain/Tags/TagsAPIController.php
Normal file
72
src/Domain/Tags/TagsAPIController.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Tags;
|
||||
|
||||
use App\Domain\Controller;
|
||||
use App\Helpers\UploadFiles;
|
||||
use App\Http\JSONResponse;
|
||||
use App\Http\Request;
|
||||
|
||||
class TagsAPIController extends Controller {
|
||||
|
||||
public static function defineRoutes(): array
|
||||
{
|
||||
return [
|
||||
self::Route( routeUrl: '/api/tags/create', routeName: 'api->tags->create', routeAction: 'create', routeMethods: ['POST'] ),
|
||||
self::Route( routeUrl: '/api/tags/edit', routeName: 'api->tags->edit', routeAction: 'edit', routeMethods: ['POST'] ),
|
||||
self::Route( routeUrl: '/api/tags/delete', routeName: 'api->tags->delete', routeAction: 'delete', routeMethods: ['POST'] ),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
public function create(){
|
||||
|
||||
$name = Request::post( 'name' );
|
||||
if( !$name || $name == "" )
|
||||
JSONResponse::sendError( [ 'error' => 'Name not defined' ] );
|
||||
|
||||
$tag = new Tag();
|
||||
$tag->num_tag = 0;
|
||||
$tag->nom_tag = $name;
|
||||
|
||||
if( !new TagRepository()->add( $tag ) )
|
||||
JSONResponse::sendError( [ 'error' => 'An error occured while adding tag' ] );
|
||||
|
||||
JSONResponse::sendSuccess();
|
||||
|
||||
}
|
||||
|
||||
public function edit(){
|
||||
|
||||
$id = Request::post( 'id' ) ?? 0;
|
||||
$tag = new TagRepository()->getByID( $id );
|
||||
if( !$tag )
|
||||
JSONResponse::sendError( [ 'error' => 'Tag not found' ] );
|
||||
|
||||
$name = Request::post( 'name' );
|
||||
if( !$name || $name == "" )
|
||||
JSONResponse::sendError( [ 'error' => 'Name not defined' ] );
|
||||
|
||||
$tag->nom_tag = $name;
|
||||
|
||||
if( !new TagRepository()->update( $tag ) )
|
||||
JSONResponse::sendError( [ 'error' => 'An error occured while updating tag' ] );
|
||||
|
||||
JSONResponse::sendSuccess();
|
||||
|
||||
}
|
||||
|
||||
public function delete(){
|
||||
|
||||
$id = Request::post( 'id' ) ?? 0;
|
||||
$tag = new TagRepository()->getByID( $id );
|
||||
if( !$tag )
|
||||
JSONResponse::sendError( [ 'error' => 'Tag not found' ] );
|
||||
|
||||
if( !new TagRepository()->delete( $tag ) )
|
||||
JSONResponse::sendError( [ 'error' => 'An error occured while deleting tag' ] );
|
||||
|
||||
JSONResponse::sendSuccess();
|
||||
|
||||
}
|
||||
}
|
||||
51
src/Helpers/UploadFiles.php
Normal file
51
src/Helpers/UploadFiles.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Http\Router;
|
||||
use App\Kernel;
|
||||
|
||||
class UploadFiles {
|
||||
|
||||
public static function uploadFolderPath(){
|
||||
return APP_ROOT . 'public/uploads/';
|
||||
}
|
||||
public static function uploadFolderUri(){
|
||||
return Kernel::$configs['general']['website_url'] . 'uploads/';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
*
|
||||
* @return string|int
|
||||
*
|
||||
* 1: Pas de fichier $fileArg
|
||||
* 2: Erreur dans le fichier
|
||||
* 3: Déjà existant.
|
||||
* 4: Erreur dans le déplacement.
|
||||
*/
|
||||
public static function uploadFile( string $fileArg ): string|int {
|
||||
|
||||
if( !isset( $_FILES[ $fileArg ] ) )
|
||||
return 1;
|
||||
|
||||
$file = $_FILES[ $fileArg ];
|
||||
if( $file['error'] != 0 )
|
||||
return 2;
|
||||
|
||||
$tempFileName = $file['tmp_name'];
|
||||
$fileName = $file['name'];
|
||||
|
||||
$full_name = self::uploadFolderPath() . $fileName;
|
||||
$full_uri = self::uploadFolderUri() . $fileName;
|
||||
|
||||
if( file_exists( $full_name ) )
|
||||
return 3;
|
||||
|
||||
if( !move_uploaded_file($tempFileName, $full_name) )
|
||||
return 4;
|
||||
|
||||
return $full_uri;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 ..." value="<?php echo \App\Http\Request::get( 's' ); ?>">
|
||||
</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