Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Juan Montilla
/
TBW2223_equipo12
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bec8ead4
authored
May 05, 2023
by
Juan Montilla
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Formulario receta
parent
407f4a72
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
13 deletions
app/Config/Routes.php
app/Controllers/InsertRecipeController.php
app/Views/pages/insertRecipe.php
public/js/insert.js
app/Config/Routes.php
View file @
bec8ead4
...
...
@@ -33,14 +33,14 @@ use App\Controllers\User;
$routes
->
match
([
'get'
],
'/'
,
[
User
::
class
,
'login'
]);
$routes
->
match
([
'get'
,
'post'
],
'/login'
,
[
User
::
class
,
'login'
]);
$routes
->
match
([
'post'
],
'/insert_recipe'
,
[
InsertRecipeController
::
class
,
'insertRecipe'
]);
$routes
->
match
([
'get'
,
'post'
],
'/loginAjax'
,
[
User
::
class
,
'loginAjax'
]);
$routes
->
match
([
'get'
,
'post'
],
'/registerAjax'
,
[
User
::
class
,
'registerAjax'
]);
$routes
->
match
([
'post'
],
'/insert_recipe'
,
[
InsertRecipeController
::
class
,
'insert_recipe'
]);
$routes
->
match
([
'get'
],
'/home'
,
[
User
::
class
,
'user_ok'
]);
// Rutas para formulario de ingresar tareas
$routes
->
get
(
'/insert_recipe'
,
'InsertRecipeController::index'
);
$routes
->
match
([
'get'
,
'post'
],
'/search_ingredient'
,
'InsertRecipeController::search_ingredient'
);
$routes
->
post
(
'/insert_recipe'
,
'InsertRecipeController::insert_recipe'
);
$routes
->
get
(
'login'
,
'Pages::viewLogin'
);
...
...
app/Controllers/InsertRecipeController.php
View file @
bec8ead4
...
...
@@ -13,7 +13,6 @@ class InsertRecipeController extends Controller
.
view
(
'templates/footer'
);
}
public
function
search_ingredient
()
{
// Obtener la consulta de búsqueda desde el formulario
$query
=
$this
->
request
->
getVar
(
'query'
);
...
...
@@ -27,7 +26,33 @@ class InsertRecipeController extends Controller
// Devolver los ingredientes coincidentes en formato JSON
return
$this
->
response
->
setJSON
(
$ingredients
);
}
public
function
insert_recipe
()
{
// Cargar los modelos necesarios
$recipeModel
=
new
\App\Models\RecipesModel
();
$recipesIngredientModel
=
new
\App\Models\RecipesIngredientModel
();
// Obtener los datos del formulario
$recipeData
=
$this
->
request
->
getPost
();
$selectedIngredients
=
json_decode
(
$recipeData
[
'selected_ingredients'
],
true
);
// Eliminar el elemento 'selected_ingredients' de los datos de la receta
unset
(
$recipeData
[
'selected_ingredients'
]);
// Insertar la receta en la tabla 'recipes'
$recipeId
=
$recipeModel
->
insert
(
$recipeData
);
// Insertar los ingredientes seleccionados y sus cantidades en la tabla 'recipes_ingredient'
foreach
(
$selectedIngredients
as
$ingredient
)
{
$recipesIngredientModel
->
insert
([
'id_recipe'
=>
$recipeId
,
'id_ingredient'
=>
$ingredient
[
'id'
],
'amount'
=>
$ingredient
[
'amount'
]
]);
}
// Redireccionar a la página principal (o cualquier otra página que desees)
return
redirect
()
->
to
(
'/home'
);
}
}
app/Views/pages/insertRecipe.php
View file @
bec8ead4
...
...
@@ -83,18 +83,16 @@
<div
class=
"modal-content"
>
<div
class=
"modal-header"
>
<h5
class=
"modal-title"
id=
"quantityModalLabel"
>
Ingresa la cantidad
</h5>
<button
type=
"button"
class=
"close"
data-dismiss=
"modal"
aria-label=
"Close"
>
<span
aria-hidden=
"true"
>
×
</span>
</button>
</div>
<div
class=
"modal-body"
>
<div
class=
"form-group"
>
<label
for=
"ingredient_quantity"
>
Cantidad
</label>
<input
type=
"text"
class=
"form-control"
id=
"ingredient_quantity"
name=
"ingredient_quantity"
placeholder=
"Ej: 2 tazas"
>
<input
type=
"text"
class=
"form-control"
id=
"ingredient_quantity"
name=
"ingredient_quantity"
placeholder=
"Ej: 2 tazas
, 1/2 cucharada, 4 kg...
"
>
</div>
</div>
<div
class=
"modal-footer"
>
<button
type=
"button"
class=
"btn btn-secondary"
data-dismiss=
"modal"
>
Cancelar
</button>
<button
type=
"button"
class=
"btn btn-secondary"
id=
"cancel_quantity"
data-dismiss=
"modal"
>
Cancelar
</button>
<button
type=
"button"
class=
"btn btn-primary"
id=
"save_quantity"
>
Guardar
</button>
</div>
</div>
...
...
public/js/insert.js
View file @
bec8ead4
// Seleccionar elementos DOM
const
ingredientSearch
=
document
.
querySelector
(
'#ingredient_search'
);
const
selectedIngredients
=
document
.
querySelector
(
'#selected_ingredients'
);
const
cancelQuantityBtn
=
document
.
getElementById
(
"cancel_quantity"
);
cancelQuantityBtn
.
onclick
=
function
()
{
$
(
"#quantityModal"
).
modal
(
"hide"
);
};
const
recipeForm
=
document
.
querySelector
(
"form.my-form"
);
recipeForm
.
addEventListener
(
"submit"
,
function
(
event
)
{
const
selectedIngredients
=
Array
.
from
(
document
.
querySelectorAll
(
".selected-ingredient"
));
const
ingredientsData
=
selectedIngredients
.
map
((
ingredientElem
)
=>
{
return
{
id
:
ingredientElem
.
dataset
.
ingredientId
,
amount
:
ingredientElem
.
querySelector
(
".ingredient-amount"
).
textContent
,
};
});
const
hiddenInput
=
document
.
createElement
(
"input"
);
hiddenInput
.
type
=
"hidden"
;
hiddenInput
.
name
=
"selected_ingredients"
;
hiddenInput
.
value
=
JSON
.
stringify
(
ingredientsData
);
recipeForm
.
appendChild
(
hiddenInput
);
});
// Array para almacenar palabras clave seleccionadas
let
ingredients
=
[];
...
...
@@ -44,7 +69,7 @@ function searchIngredients(query) {
saveQuantityBtn
.
onclick
=
function
()
{
const
quantity
=
document
.
getElementById
(
"ingredient_quantity"
).
value
;
if
(
quantity
)
{
addIngredient
(
ingredient
.
name
,
ingredient
.
id
,
quantity
);
addIngredient
(
ingredient
.
name
,
ingredient
.
id
,
quantity
,
ingredient
.
icon
);
$
(
"#quantityModal"
).
modal
(
"hide"
);
document
.
getElementById
(
"ingredient_quantity"
).
value
=
""
;
}
...
...
@@ -59,16 +84,18 @@ function searchIngredients(query) {
// Función para agregar un ingrediente
function
addIngredient
(
ingredientName
,
ingredientId
)
{
function
addIngredient
(
ingredientName
,
ingredientId
,
quantity
,
icon
)
{
// Agregar el ingrediente al array y actualizar el campo de selección
if
(
ingredientName
&&
!
ingredients
.
find
(
ing
=>
ing
.
name
===
ingredientName
))
{
ingredients
.
push
({
name
:
ingredientName
,
id
:
ingredientId
});
ingredients
.
push
({
name
:
ingredientName
,
id
:
ingredientId
,
quantity
:
quantity
,
icon
:
icon
});
updateSelectedIngredients
();
ingredientSearch
.
value
=
''
;
}
}
// Función para eliminar una palabra clave
function
removeIngredient
(
ingredient
)
{
// Eliminar la palabra clave del array y actualizar el campo de selección
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment