Commit a5baaf48 by Juan Montilla

Fotos en base de datos y subida de recetas

parent 68b0e6fe
...@@ -38,9 +38,10 @@ $routes->match(['get', 'post'], '/registerAjax', [User::class, 'registerAjax']); ...@@ -38,9 +38,10 @@ $routes->match(['get', 'post'], '/registerAjax', [User::class, 'registerAjax']);
$routes->match(['get'], '/home', [User::class, 'user_ok']); $routes->match(['get'], '/home', [User::class, 'user_ok']);
$routes->get('recipes/image/(:num)', 'RecipesController::getImage/$1'); // Ruta para obtener una imagen de una receta dado un id
$routes->get('recipe/image/(:num)', 'RecipesController::show_image/$1');
// Rutas para formulario de ingresar tareas // Rutas para formulario de ingresar recetas
$routes->get('/insert_recipe', 'InsertRecipeController::index'); $routes->get('/insert_recipe', 'InsertRecipeController::index');
$routes->match(['get', 'post'], '/search_ingredient', 'InsertRecipeController::search_ingredient'); $routes->match(['get', 'post'], '/search_ingredient', 'InsertRecipeController::search_ingredient');
$routes->post('/insert_recipe', 'InsertRecipeController::insert_recipe'); $routes->post('/insert_recipe', 'InsertRecipeController::insert_recipe');
......
...@@ -40,6 +40,13 @@ class InsertRecipeController extends Controller ...@@ -40,6 +40,13 @@ class InsertRecipeController extends Controller
// Eliminar el elemento 'selected_ingredients' de los datos de la receta // Eliminar el elemento 'selected_ingredients' de los datos de la receta
unset($recipeData['selected_ingredients']); unset($recipeData['selected_ingredients']);
// Manejar el archivo de imagen
$photo = $this->request->getFile('photo');
if ($photo->isValid() && !$photo->hasMoved()) {
$photoBlob = file_get_contents($photo->getRealPath());
$recipeData['photo'] = $photoBlob;
}
// Insertar la receta en la tabla 'recipes' // Insertar la receta en la tabla 'recipes'
$recipeId = $recipeModel->insert($recipeData); $recipeId = $recipeModel->insert($recipeData);
...@@ -55,4 +62,5 @@ class InsertRecipeController extends Controller ...@@ -55,4 +62,5 @@ class InsertRecipeController extends Controller
// Redireccionar a la página principal (o cualquier otra página que desees) // Redireccionar a la página principal (o cualquier otra página que desees)
return redirect()->to('/home'); return redirect()->to('/home');
} }
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Controllers; namespace App\Controllers;
use App\Models\RecipesModel; use App\Models\RecipesModel;
use App\Models\RecipesIngredientModel;
use CodeIgniter\Controller; use CodeIgniter\Controller;
class RecipesController extends Controller class RecipesController extends Controller
...@@ -23,10 +24,26 @@ class RecipesController extends Controller ...@@ -23,10 +24,26 @@ class RecipesController extends Controller
'ingredients' => $ingredients, 'ingredients' => $ingredients,
]; ];
return view('templates/header',$data) return view('templates/header', $data)
.view('pages/recipe_view', $data) . view('pages/recipe_view', $data)
. view('templates/footer'); . view('templates/footer');
} }
public function show_image($id)
} {
$recipesModel = new \App\Models\RecipesModel();
$recipe = $recipesModel->find($id);
if ($recipe) {
$photo = $recipe->photo;
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($photo);
$this->response->setHeader('Content-Type', $mimeType);
$this->response->setBody($photo);
$this->response->send();
}
}
}
\ No newline at end of file
...@@ -187,8 +187,7 @@ ...@@ -187,8 +187,7 @@
<div class="card info-card sales-card"> <div class="card info-card sales-card">
<div class="row"> <div class="row">
<div class="col-md-3 imagen-container"> <div class="col-md-3 imagen-container">
<img src="imagenes/platos/<?php echo $row->photo; ?>" alt="" <img src="<?php echo base_url('recipe/image/' . $row->id); ?>" alt="" class="img-fluid rounded-start">
class="img-fluid rounded-start">
</div> </div>
<div class="col-md-9"> <div class="col-md-9">
...@@ -226,11 +225,6 @@ ...@@ -226,11 +225,6 @@
</a> </a>
</div> </div>
<!-- Fin de la tarjeta de la receta --> <!-- Fin de la tarjeta de la receta -->
<?php <?php
} }
} }
......
...@@ -76,16 +76,16 @@ function getYoutubeVideoId($url) { ...@@ -76,16 +76,16 @@ function getYoutubeVideoId($url) {
} }
</style> </style>
<main id="main" class="mainview"> <main id="mainview" class="mainview">
<section class="section dashboard"> <section class="section dashboard">
<div class="container"> <div class="container">
<div class="recipe-header"> <div class="recipe-header">
<h1>Receta:</h1>
<h1><?php echo $recipe->name; ?></h1> <h1><?php echo $recipe->name; ?></h1>
<img src="../imagenes/platos/<?php echo $recipe->photo; ?>" alt="<?php echo $recipe->name; ?>" /> <img src="<?php echo base_url('recipe/image/' . $recipe->id); ?>" alt="<?php echo $recipe->name; ?>" />
</div> </div>
<p><?php echo $recipe->description; ?></p> <p><?php echo $recipe->description; ?></p>
......
<h1>User list</h1> <h1>Admin Page</h1>
<h2>Lista de usuarios</h2>
<!-- LISTA DE USUARIOS -->
<?php <?php
if (sizeof($users) > 0) { if (sizeof($users) > 0) {
foreach ($users as $row) { foreach ($users as $row) {
...@@ -9,4 +11,75 @@ if (sizeof($users) > 0) { ...@@ -9,4 +11,75 @@ if (sizeof($users) > 0) {
} }
} else { } else {
echo "No user"; echo "No user";
} }
\ No newline at end of file ?>
<h2> Recetas </h2>
<!-- LISTA DE RECETAS -->
<?php
$recipesModel = new \App\Models\RecipesModel();
$recipes = $recipesModel->findAll();
?>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Temporada</th>
<th>Origen</th>
<th>Foto</th>
<th>Vegano</th>
<th>Descripción</th>
<th>Instrucciones</th>
<th>Enlace</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<?php foreach ($recipes as $row): ?>
<tr>
<td>
<?= $row->id; ?>
</td>
<td>
<?= $row->name; ?>
</td>
<td>
<?= $row->season; ?>
</td>
<td>
<?= $row->origin; ?>
</td>
<td>
<img src="<?= base_url('recipe/image/' . $row->id); ?>" alt="" class="img-thumbnail"
style="width: 100px;">
</td>
<td>
<?= $row->is_vegan ? 'Sí' : 'No'; ?>
</td>
<td>
<?= mb_strimwidth($row->description, 0, 10, "..."); ?>
</td>
<td>
<?= mb_strimwidth($row->instructions, 0, 10, "..."); ?>
</td>
<td>
<?= $row->link; ?>
</td>
<td>
<button class="btn btn-danger" onclick="deleteRecipe(<?= $row->id; ?>)">Borrar</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script>
function borrarReceta(recipeId) {
//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
}
</script>
\ No newline at end of file
...@@ -109,50 +109,38 @@ function removeIngredient(ingredient) { ...@@ -109,50 +109,38 @@ function removeIngredient(ingredient) {
function updateSelectedIngredients() { function updateSelectedIngredients() {
// Limpiar el campo de selección // Limpiar el campo de selección
selectedIngredients.innerHTML = ''; selectedIngredients.innerHTML = '';
// Agregar cada ingrediente seleccionado al campo de selección // Agregar cada ingrediente seleccionado al campo de selección
ingredients.forEach(function (ingredient) { ingredients.forEach(function (ingredient) {
const ingredientElement = document.createElement('div'); const ingredientElement = document.createElement('div');
ingredientElement.classList.add('selected-ingredient'); ingredientElement.classList.add('selected-ingredient');
ingredientElement.setAttribute('data-id', ingredient.id); ingredientElement.setAttribute('data-ingredient-id', ingredient.id);
// Crear el elemento de imagen para el icono del ingrediente // Crear el elemento de imagen para el icono del ingrediente
const iconElement = document.createElement('img'); const iconElement = document.createElement('img');
iconElement.classList.add('ingredient-icon'); iconElement.classList.add('ingredient-icon');
iconElement.src = '../imagenes/ingredientes/' + ingredient.icon; iconElement.src = '../imagenes/ingredientes/' + ingredient.icon;
ingredientElement.appendChild(iconElement); ingredientElement.appendChild(iconElement);
const ingredientNameAndQuantity = document.createTextNode( // Agregar el nombre del ingrediente y la cantidad
`${ingredient.name} (${ingredient.quantity})` const ingredientNameAndQuantity = document.createElement('span');
); ingredientNameAndQuantity.textContent = `${ingredient.name} (${ingredient.quantity})`;
ingredientElement.appendChild(ingredientNameAndQuantity); ingredientNameAndQuantity.classList.add('ingredient-amount');
ingredientElement.appendChild(ingredientNameAndQuantity);
const removeBtn = document.createElement('button');
removeBtn.classList.add('btn', 'btn-danger', 'btn-sm'); // Agregar el botón para eliminar el ingrediente
removeBtn.textContent = 'x'; const removeBtn = document.createElement('button');
removeBtn.addEventListener('click', function () { removeBtn.classList.add('btn', 'btn-danger', 'btn-sm');
removeIngredient(ingredient); removeBtn.textContent = 'x';
}); removeBtn.addEventListener('click', function () {
removeIngredient(ingredient);
ingredientElement.appendChild(removeBtn); });
selectedIngredients.appendChild(ingredientElement);
ingredientElement.appendChild(removeBtn);
// Crear un campo oculto para almacenar el ID y la cantidad del ingrediente seleccionado selectedIngredients.appendChild(ingredientElement);
const hiddenInputId = document.createElement('input');
hiddenInputId.setAttribute('type', 'hidden');
hiddenInputId.setAttribute('name', 'selected_ingredient_ids[]');
hiddenInputId.setAttribute('value', ingredient.id);
ingredientElement.appendChild(hiddenInputId);
const hiddenInputQuantity = document.createElement('input');
hiddenInputQuantity.setAttribute('type', 'hidden');
hiddenInputQuantity.setAttribute('name', 'selected_ingredient_quantities[]');
hiddenInputQuantity.setAttribute('value', ingredient.quantity);
ingredientElement.appendChild(hiddenInputQuantity);
selectedIngredients.appendChild(ingredientElement);
}); });
} }
......
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 sign in to comment