Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
2018_web_app
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
inutralia
2018_web_app
Commits
d97732a8
Commit
d97732a8
authored
Dec 12, 2018
by
Luis Perez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Terminado modulo ingredientes
parent
7106746f
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
278 additions
and
34 deletions
+278
-34
index.php
fuel/app/classes/controller/listacompra/index.php
+92
-5
ingredient.php
fuel/app/classes/model/ingredient.php
+5
-1
recipe.php
fuel/app/classes/model/recipe.php
+52
-8
routes.php
fuel/app/config/routes.php
+6
-3
index.twig
fuel/app/views/listacompra/index.twig
+4
-4
modal_add_product.twig
fuel/app/views/listacompra/modal_add_product.twig
+1
-1
modal_delete_all.twig
fuel/app/views/listacompra/modal_delete_all.twig
+1
-1
custom.css
public/assets/css/custom.css
+1
-0
index.js
public/assets/js/listacompra/index.js
+113
-11
2018121117513_recipe_new_option.sql
sql/2018121117513_recipe_new_option.sql
+3
-0
No files found.
fuel/app/classes/controller/listacompra/index.php
View file @
d97732a8
...
...
@@ -22,11 +22,9 @@ class Controller_Listacompra_Index extends \Controller_App{
public
function
before
()
{
//Se cargan funciones de javascript especificas de esta funcionalidad
Casset
::
js
(
'listacompra/index.js'
);
$recipes
=
Model_Recipe
::
get_recipes_current_week
();
//PEDIR INGREDIENTES
...
...
@@ -79,7 +77,7 @@ class Controller_Listacompra_Index extends \Controller_App{
foreach
(
$recipes
as
$recipe
)
{
Log
::
error
(
"ENTRO EN RECETAS!!!"
);
foreach
(
$recipe
->
ingredients
as
$ingrediente
)
foreach
(
$recipe
->
all_
ingredients
as
$ingrediente
)
{
Log
::
error
(
"INTENTO BORRAR INGREDIENTE!!!
$ingrediente->id
"
);
$ingrediente
->
delete
();
...
...
@@ -87,6 +85,7 @@ class Controller_Listacompra_Index extends \Controller_App{
}
Session
::
set_flash
(
'success'
,
"Se han borrado los ingredientes correctamente"
);
return
Response
::
redirect_back
();
}
...
...
@@ -94,9 +93,96 @@ class Controller_Listacompra_Index extends \Controller_App{
{
$ingredients
=
Input
::
post
(
'ingredients_ids'
);
foreach
(
$ingredients
as
$ingredient
)
if
(
count
(
$ingredients
)
>
0
)
{
$ingredients
=
Model_Ingredient
::
query
()
->
where
(
'ingredient_id'
,
'IN'
,
$ingredients
)
->
get
();
if
(
!
$ingredients
)
{
return
;
}
foreach
(
$ingredients
as
$ingredient
)
{
$ingredient
->
delete
();
}
}
}
public
function
post_delete_ingredients_ids
()
{
$ingredients
=
Input
::
post
(
'ingredients_ids'
);
if
(
count
(
$ingredients
)
>
0
)
{
$ingredient
->
delete
();
$ingredients
=
Model_Ingredient
::
query
()
->
where
(
'id'
,
'IN'
,
$ingredients
)
->
get
();
if
(
!
$ingredients
)
{
return
;
}
foreach
(
$ingredients
as
$ingredient
)
{
$ingredient
->
delete
();
}
}
}
public
function
post_add_custom_ingredient
()
{
Log
::
error
(
"ENTOROROROR ADD"
);
$desc
=
Input
::
post
(
'ingredient'
);
Log
::
error
(
"DESC
$desc
"
);
try
{
$custom_recipe
=
Model_Recipe
::
get_custom_recipe
();
$ingredient
=
Model_Ingredient
::
query
()
->
where
(
'desc'
,
$desc
)
->
where
(
'recipe_local_id'
,
$custom_recipe
->
id
)
->
get_one
();
Log
::
error
(
"BUSCO EL INGREDIENTE!! "
.
((
$ingredient
)
?
$ingredient
->
id
:
""
));
if
(
!
$ingredient
)
{
$ingredient
=
Model_Ingredient
::
forge
();
$ingredient
->
desc
=
$desc
;
$ingredient
->
recipe_local_id
=
$custom_recipe
->
id
;
$ingredient
->
used
=
'S'
;
$ingredient
->
created_at
=
date
(
'Y-m-d H:i:s'
);
$ingredient
->
save
();
}
else
{
return
false
;
}
}
catch
(
Exception
$e
)
{
Log
::
error
(
"Error al intentar generar Ingrediente personalizado:"
.
$e
->
getMessage
()
.
" Linea: "
.
$e
->
getLine
()
.
" Fichero: "
.
$e
->
getFile
());
}
return
true
;
}
public
function
post_use_ingredient
()
{
$array
=
Input
::
post
(
'ids_used'
);
if
(
count
(
$array
)
>
0
)
{
Log
::
error
(
print_r
(
$array
,
true
));
$recipes
=
Model_Recipe
::
recipes_current_week
()
->
related
(
'ingredients'
)
->
where
(
'ingredients.ingredient_id'
,
'in'
,
$array
)
->
get
();
foreach
(
$recipes
as
$recipe
)
{
foreach
(
$recipe
->
ingredients
as
$ingredient
)
{
$ingredient
->
used
=
'S'
;
$ingredient
->
save
();
}
}
}
}
}
\ No newline at end of file
fuel/app/classes/model/ingredient.php
View file @
d97732a8
...
...
@@ -31,7 +31,11 @@ class Model_Ingredient extends Model{
public
static
function
get_ingredients_week
(
$recipes_ids
)
{
return
self
::
query
()
->
where
(
'recipe_local_id'
,
'in'
,
$recipes_ids
)
->
order_by
(
'used'
,
'DESC'
)
->
group_by
(
'ingredient_id'
)
->
get
();
//Mergeamos ingredientes de las recetas con los custom ingredients
return
self
::
query
()
->
where
(
'ingredient_id'
,
'!='
,
null
)
->
where
(
'recipe_local_id'
,
'in'
,
$recipes_ids
)
->
order_by
(
'used'
,
'DESC'
)
->
group_by
(
'ingredient_id'
)
->
get
()
+
self
::
query
()
->
where
(
'ingredient_id'
,
null
)
->
where
(
'recipe_local_id'
,
'in'
,
$recipes_ids
)
->
get
();
}
...
...
fuel/app/classes/model/recipe.php
View file @
d97732a8
...
...
@@ -21,7 +21,8 @@ class Model_Recipe extends Model{
protected
static
$_primary_key
=
array
(
'id'
);
protected
static
$_has_many
=
array
(
'ingredients'
=>
array
(
//CARGA INGREDIENTES CON CONDICIONES
'ingredients'
=>
array
(
'key_from'
=>
'id'
,
'key_to'
=>
'recipe_local_id'
,
'model_to'
=>
'Model_Ingredient'
,
...
...
@@ -32,6 +33,13 @@ class Model_Recipe extends Model{
'group_by'
=>
'ingredient_id'
)
),
//FUERZA LA CARGA DE TODOS LOS INGREDIENTES
'all_ingredients'
=>
array
(
'key_from'
=>
'id'
,
'key_to'
=>
'recipe_local_id'
,
'model_to'
=>
'Model_Ingredient'
,
'cascade_delete'
=>
false
),
);
protected
static
$_observers
=
array
(
...
...
@@ -49,6 +57,35 @@ class Model_Recipe extends Model{
return
self
::
recipes_current_week
()
->
get
();
}
/**
* @desc Obtiene recetas personalizadas. Esta se utiliza para los ingredientes Random
* @return Model_Recipe|Model|\Orm\Query
* @throws Exception
*/
public
static
function
get_custom_recipe
()
{
$recipes
=
self
::
recipes_current_week
();
$recipes
=
$recipes
->
where
(
'option'
,
'custom'
)
->
get_one
();
if
(
!
$recipes
)
{
$recipes
=
Model_Recipe
::
forge
();
$recipes
->
week_year
=
date
(
'W'
);
$recipes
->
year
=
date
(
'Y'
);
$recipes
->
user_id
=
\Fuel\Core\Session
::
get
(
'user_id'
);
$recipes
->
option
=
"custom"
;
$recipes
->
save
();
}
return
$recipes
;
}
/**
* @return \Orm\Query
*/
...
...
@@ -58,9 +95,17 @@ class Model_Recipe extends Model{
return
self
::
query
()
->
where
(
'week_year'
,
date
(
'W'
))
->
where
(
'year'
,
date
(
'Y'
))
->
where
(
'user_id'
,
\Fuel\Core\Session
::
get
(
'user_id'
));
}
/**
* @desc Genera los ingredientes de una receta consultándolos a la API de iNutralia, si no están ya generados.
* @return array|bool|mixed
*/
public
function
load_ingredients
()
{
if
(
$this
->
loaded
==
'S'
||
count
(
$this
->
ingredients
)
>
0
)
//Tres condiciones por las que salimos directamente.
//OPCIÓN 1: el producto ya se cargó anteriormente, por lo tanto no es necesario cargarlo de nuevo
//OPCIÓN 2: Si en algún momento ya se establecieron ingredientes, no se vuelven a cargar.
//OPCIÓN 3: Si recipe_id==null, se trata de una receta personalizada por lo tanto no existe en iNutralia, por lo tanto no hay que buscarla.
if
(
$this
->
loaded
==
'S'
||
count
(
$this
->
ingredients
)
>
0
||
$this
->
recipe_id
==
null
)
{
return
false
;
}
...
...
@@ -69,12 +114,13 @@ class Model_Recipe extends Model{
{
\DB
::
start_transaction
();
$recipe
=
$this
->
ws_recipe_get
(
$this
->
recipe_id
);
//Receta que se trae de la API de iNutralia
$api_recipe
=
$this
->
ws_recipe_get
(
$this
->
recipe_id
);
if
(
!
$
recipe
)
if
(
!
$
api_recipe
&&
isset
(
$api_recipe
->
ingredients
)
)
return
array
();
foreach
(
$recipe
->
ingredients
as
$ingredientRecipe
)
foreach
(
$
api_
recipe
->
ingredients
as
$ingredientRecipe
)
{
$ingredient
=
Model_Ingredient
::
forge
();
...
...
@@ -96,7 +142,7 @@ class Model_Recipe extends Model{
}
catch
(
Exception
$e
)
{
Log
::
error
(
$e
->
getMessage
());
Log
::
error
(
$e
->
getMessage
()
.
" Line: "
.
$e
->
getLine
()
.
" File: "
.
$e
->
getFile
()
);
\DB
::
rollback_transaction
();
return
false
;
}
...
...
@@ -105,8 +151,6 @@ class Model_Recipe extends Model{
return
$this
->
ingredients
;
}
/**
...
...
fuel/app/config/routes.php
View file @
d97732a8
...
...
@@ -52,9 +52,12 @@ return array(
// ARTICULO
'articulo-detalle'
=>
'articulo/index'
,
'lista-compra'
=>
'listacompra/index'
,
'lista-compra/delete_all'
=>
'listacompra/index/delete_all'
,
'lista-compra/delete'
=>
'listacompra/index/delete_ingredients'
,
'lista-compra'
=>
'listacompra/index'
,
'lista-compra/delete_all'
=>
'listacompra/index/delete_all'
,
'lista-compra/delete'
=>
'listacompra/index/delete_ingredients'
,
'lista-compra/deleteIds'
=>
'listacompra/index/delete_ingredients_ids'
,
'lista-compra/addcustomingredient'
=>
'listacompra/index/add_custom_ingredient'
,
'lista-compra/useingredient'
=>
'listacompra/index/use_ingredient'
,
...
...
fuel/app/views/listacompra/index.twig
View file @
d97732a8
...
...
@@ -30,9 +30,9 @@
{%
if
count
(
ingredients
)
==
0
%}
<h4>
Sin Ingredientes
</h4>
{%
endif
%}
{%
for
i
in
ingredients
%}
<li>
<input
id=
"check_
{{
i.id
}}
"
name=
"check_
{{
i.id
}}
"
type=
"checkbox"
rel=
"
{{
i.id
}}
"
/><label
for=
"check_
{{
i.id
}}
"
>
{{
i.desc
}}
</label></li>
{%
endfor
%}
{%
for
i
in
ingredients
%}
<li>
<input
id=
"check_
{{
i.id
}}
"
name=
"check_
{{
i.id
}}
"
type=
"checkbox"
used=
"
{{
i.used
}}
"
rel=
"
{{
i.ingredient_id
}}
"
{%
if
i.ingredient_id
==
null
%}
rel2=
"
{{
i.id
}}
"
{%
endif
%}
/><label
for=
"check_
{{
i.id
}}
"
>
{{
i.desc
}}
</label></li>
{%
endfor
%}
</ul>
</form>
</div>
...
...
@@ -43,7 +43,7 @@
<!--botones-->
<div
class=
"row "
>
<div
class=
"col-md-6"
>
<button
class=
"btn btn-custom btn-delete mt20"
data-toggle=
"modal"
data-target=
"#modalDelete"
><i
class=
"fa fa-trash"
></i>
Todos
</button>
<button
class=
"btn btn-custom btn-delete mt20"
data-toggle=
"modal"
data-target=
"#modalDelete
All
"
><i
class=
"fa fa-trash"
></i>
Todos
</button>
</div>
<div
class=
"col-md-6"
>
<button
class=
"btn btn-custom btn-delete mt20"
data-toggle=
"modal"
data-target=
"#modalDelete"
><i
class=
"fa fa-trash"
></i>
Marcados
</button>
...
...
fuel/app/views/listacompra/modal_add_product.twig
View file @
d97732a8
...
...
@@ -18,7 +18,7 @@
<input
type=
"text"
class=
"form-control"
name=
"product"
id=
"product"
value=
""
/>
<div
class=
"mt20"
>
<a
class=
"btn btn-custom btn-
lista-compra
"
><i
class=
"fa fa-plus"
></i>
AÑADIR
</a>
<a
class=
"btn btn-custom btn-
add-product
"
><i
class=
"fa fa-plus"
></i>
AÑADIR
</a>
</div>
</form>
</div>
...
...
fuel/app/views/listacompra/modal_delete_all.twig
View file @
d97732a8
<!-- Modal DELETE PRODUCTOS -->
<div
class=
"modal fade"
id=
"modalDelete"
tabindex=
"-1"
role=
"dialog"
aria-labelledby=
"modalDelete"
>
<div
class=
"modal fade"
id=
"modalDelete
All
"
tabindex=
"-1"
role=
"dialog"
aria-labelledby=
"modalDelete"
>
<div
class=
"modal-dialog"
role=
"document"
>
<div
class=
"modal-content"
>
...
...
public/assets/css/custom.css
View file @
d97732a8
...
...
@@ -275,6 +275,7 @@ input:checked + .slider:before {
.btn-trivial
{
background-color
:
#b3b3b3
}
.btn-other
{
background-color
:
#000
}
.btn-lista-compra
{
background-color
:
#ff7f2a
}
.btn-add-product
{
background-color
:
#ff7f2a
}
.btn-delete
{
background-color
:
#c83737
}
...
...
public/assets/js/listacompra/index.js
View file @
d97732a8
var
ids
=
[];
var
ids_delete
=
[];
var
ids_custom_delete
=
[];
var
ids_to_buy
=
[];
$
(
document
).
ready
(
function
()
{
$
(
':input:checked'
).
each
(
function
()
{
ids
+=
$
(
this
).
attr
(
'rel'
);
})
ids
_delete
+=
$
(
this
).
attr
(
'rel'
);
})
;
$
(
'.btn-lista-compra'
).
on
(
'click'
,
function
(
e
)
{
get_checkeds
();
$
.
ajax
({
url
:
"lista-compra/useingredient"
,
type
:
"POST"
,
method
:
"post"
,
data
:
{
ids_used
:
ids_to_buy
},
success
:
function
(
data
)
{
window
.
location
.
href
=
'http://www.delsuper.es/inutralia?ids='
+
ids
.
toString
()
})
if
(
data
!=
""
&&
data
!=
0
)
{
// Modal success
$
(
'#modalSuccess'
).
modal
(
'show'
);
}
else
{
// Modal error
}
setTimeout
(
function
(){
location
.
reload
();
},
2000
);
}
});
window
.
location
.
href
=
'http://www.delsuper.es/inutralia?ids='
+
ids_to_buy
.
toString
()
});
$
(
'.btn-delete-checked'
).
on
(
'click'
,
function
(
e
)
{
get_checkeds
();
$
.
ajax
({
url
:
"listacompra/delete"
,
url
:
"lista
-
compra/delete"
,
type
:
"POST"
,
method
:
"post"
,
data
:
{
id
:
$
(
'#profile_id'
).
val
(),
inputs
:
inputs
ingredients_ids
:
ids_delete
},
success
:
function
(
data
)
{
if
(
data
!=
""
&&
data
!=
0
)
{
// Modal success
$
(
'#modalSuccess'
).
modal
(
'show'
);
}
else
{
// Modal error
}
setTimeout
(
function
(){
location
.
reload
();
},
2000
);
}
});
$
.
ajax
({
url
:
"lista-compra/deleteIds"
,
type
:
"POST"
,
method
:
"post"
,
data
:
{
ingredients_ids
:
ids_custom_delete
},
success
:
function
(
data
)
{
if
(
data
!=
""
&&
data
!=
0
)
{
// Modal success
$
(
'#modalSuccess'
).
modal
(
'show'
);
}
else
{
// Modal error
}
setTimeout
(
function
(){
location
.
reload
();
},
2000
);
}
});
});
$
(
'.btn-add-product'
).
on
(
'click'
,
function
()
{
var
product
=
$
(
'#product'
).
val
();
$
.
ajax
({
url
:
"lista-compra/addcustomingredient"
,
type
:
"POST"
,
method
:
"post"
,
data
:
{
ingredient
:
product
},
success
:
function
(
data
)
{
...
...
@@ -39,7 +123,6 @@ $(document).ready(function() {
}
})
})
});
...
...
@@ -49,10 +132,28 @@ $(document).ready(function() {
function
get_checkeds
()
{
$
(
':input:checked'
).
each
(
function
()
{
let
id_val
=
$
(
this
).
attr
(
'rel'
);
if
(
jQuery
.
inArray
(
id_val
,
ids
)
<
0
)
if
(
id_val
==
""
||
id_val
==
null
)
{
ids
.
push
(
id_val
);
let
id_real_val
=
$
(
this
).
attr
(
'rel2'
);
if
(
jQuery
.
inArray
(
id_real_val
,
ids_custom_delete
)
<
0
)
{
ids_custom_delete
.
push
(
id_real_val
);
}
}
else
{
if
(
jQuery
.
inArray
(
id_val
,
ids_delete
)
<
0
)
{
ids_delete
.
push
(
id_val
);
}
if
(
jQuery
.
inArray
(
id_val
,
ids_to_buy
)
<
0
&&
$
(
this
).
attr
(
'used'
)
==
"N"
)
{
ids_to_buy
.
push
(
id_val
);
}
}
});
}
\ No newline at end of file
sql/2018121117513_recipe_new_option.sql
0 → 100644
View file @
d97732a8
ALTER
TABLE
`recipes`
MODIFY
COLUMN
`option`
enum
(
'lunchFirst'
,
'lunchSecond'
,
'dinnerFirst'
,
'dinnerSecond'
,
'custom'
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
AFTER
`year`
;
\ No newline at end of file
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