Commit d97732a8 by Luis Perez

Terminado modulo ingredientes

parent 7106746f
...@@ -22,11 +22,9 @@ class Controller_Listacompra_Index extends \Controller_App{ ...@@ -22,11 +22,9 @@ class Controller_Listacompra_Index extends \Controller_App{
public function before() public function before()
{ {
//Se cargan funciones de javascript especificas de esta funcionalidad //Se cargan funciones de javascript especificas de esta funcionalidad
Casset::js('listacompra/index.js'); Casset::js('listacompra/index.js');
$recipes = Model_Recipe::get_recipes_current_week(); $recipes = Model_Recipe::get_recipes_current_week();
//PEDIR INGREDIENTES //PEDIR INGREDIENTES
...@@ -79,7 +77,7 @@ class Controller_Listacompra_Index extends \Controller_App{ ...@@ -79,7 +77,7 @@ class Controller_Listacompra_Index extends \Controller_App{
foreach ($recipes as $recipe) foreach ($recipes as $recipe)
{ {
Log::error("ENTRO EN RECETAS!!!"); Log::error("ENTRO EN RECETAS!!!");
foreach ($recipe->ingredients as $ingrediente) foreach ($recipe->all_ingredients as $ingrediente)
{ {
Log::error("INTENTO BORRAR INGREDIENTE!!! $ingrediente->id"); Log::error("INTENTO BORRAR INGREDIENTE!!! $ingrediente->id");
$ingrediente->delete(); $ingrediente->delete();
...@@ -87,6 +85,7 @@ class Controller_Listacompra_Index extends \Controller_App{ ...@@ -87,6 +85,7 @@ class Controller_Listacompra_Index extends \Controller_App{
} }
Session::set_flash('success', "Se han borrado los ingredientes correctamente"); Session::set_flash('success', "Se han borrado los ingredientes correctamente");
return Response::redirect_back(); return Response::redirect_back();
} }
...@@ -94,9 +93,96 @@ class Controller_Listacompra_Index extends \Controller_App{ ...@@ -94,9 +93,96 @@ class Controller_Listacompra_Index extends \Controller_App{
{ {
$ingredients = Input::post('ingredients_ids'); $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
...@@ -31,7 +31,11 @@ class Model_Ingredient extends Model{ ...@@ -31,7 +31,11 @@ class Model_Ingredient extends Model{
public static function get_ingredients_week($recipes_ids) 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();
} }
......
...@@ -21,7 +21,8 @@ class Model_Recipe extends Model{ ...@@ -21,7 +21,8 @@ class Model_Recipe extends Model{
protected static $_primary_key = array('id'); protected static $_primary_key = array('id');
protected static $_has_many = array( protected static $_has_many = array(
'ingredients' => array( //CARGA INGREDIENTES CON CONDICIONES
'ingredients' => array(
'key_from' => 'id', 'key_from' => 'id',
'key_to' => 'recipe_local_id', 'key_to' => 'recipe_local_id',
'model_to' => 'Model_Ingredient', 'model_to' => 'Model_Ingredient',
...@@ -32,6 +33,13 @@ class Model_Recipe extends Model{ ...@@ -32,6 +33,13 @@ class Model_Recipe extends Model{
'group_by' => 'ingredient_id' '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( protected static $_observers = array(
...@@ -49,6 +57,35 @@ class Model_Recipe extends Model{ ...@@ -49,6 +57,35 @@ class Model_Recipe extends Model{
return self::recipes_current_week()->get(); 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 * @return \Orm\Query
*/ */
...@@ -58,9 +95,17 @@ class Model_Recipe extends Model{ ...@@ -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')); 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() 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; return false;
} }
...@@ -69,12 +114,13 @@ class Model_Recipe extends Model{ ...@@ -69,12 +114,13 @@ class Model_Recipe extends Model{
{ {
\DB::start_transaction(); \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(); return array();
foreach ($recipe->ingredients as $ingredientRecipe) foreach ($api_recipe->ingredients as $ingredientRecipe)
{ {
$ingredient = Model_Ingredient::forge(); $ingredient = Model_Ingredient::forge();
...@@ -96,7 +142,7 @@ class Model_Recipe extends Model{ ...@@ -96,7 +142,7 @@ class Model_Recipe extends Model{
} }
catch (Exception $e) catch (Exception $e)
{ {
Log::error($e->getMessage()); Log::error($e->getMessage(). " Line: ".$e->getLine(). " File: ".$e->getFile());
\DB::rollback_transaction(); \DB::rollback_transaction();
return false; return false;
} }
...@@ -105,8 +151,6 @@ class Model_Recipe extends Model{ ...@@ -105,8 +151,6 @@ class Model_Recipe extends Model{
return $this->ingredients; return $this->ingredients;
} }
/** /**
......
...@@ -52,9 +52,12 @@ return array( ...@@ -52,9 +52,12 @@ return array(
// ARTICULO // ARTICULO
'articulo-detalle' => 'articulo/index', 'articulo-detalle' => 'articulo/index',
'lista-compra' => 'listacompra/index', 'lista-compra' => 'listacompra/index',
'lista-compra/delete_all' => 'listacompra/index/delete_all', 'lista-compra/delete_all' => 'listacompra/index/delete_all',
'lista-compra/delete' => 'listacompra/index/delete_ingredients', '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',
......
...@@ -30,9 +30,9 @@ ...@@ -30,9 +30,9 @@
{% if count(ingredients) == 0 %} {% if count(ingredients) == 0 %}
<h4>Sin Ingredientes</h4> <h4>Sin Ingredientes</h4>
{% endif %} {% endif %}
{% for i in ingredients %} {% 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> <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 %} {% endfor %}
</ul> </ul>
</form> </form>
</div> </div>
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
<!--botones--> <!--botones-->
<div class="row "> <div class="row ">
<div class="col-md-6"> <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="#modalDeleteAll" ><i class="fa fa-trash"></i> Todos</button>
</div> </div>
<div class="col-md-6"> <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> <button class="btn btn-custom btn-delete mt20" data-toggle="modal" data-target="#modalDelete"><i class="fa fa-trash"></i> Marcados</button>
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<input type="text" class="form-control" name="product" id="product" value=""/> <input type="text" class="form-control" name="product" id="product" value=""/>
<div class="mt20"> <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> </div>
</form> </form>
</div> </div>
......
<!-- Modal DELETE PRODUCTOS --> <!-- Modal DELETE PRODUCTOS -->
<div class="modal fade" id="modalDelete" tabindex="-1" role="dialog" aria-labelledby="modalDelete"> <div class="modal fade" id="modalDeleteAll" tabindex="-1" role="dialog" aria-labelledby="modalDelete">
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
<div class="modal-content"> <div class="modal-content">
......
...@@ -275,6 +275,7 @@ input:checked + .slider:before { ...@@ -275,6 +275,7 @@ input:checked + .slider:before {
.btn-trivial{background-color: #b3b3b3} .btn-trivial{background-color: #b3b3b3}
.btn-other{background-color: #000} .btn-other{background-color: #000}
.btn-lista-compra{background-color: #ff7f2a} .btn-lista-compra{background-color: #ff7f2a}
.btn-add-product{background-color: #ff7f2a}
.btn-delete{background-color: #c83737} .btn-delete{background-color: #c83737}
......
var ids =[]; var ids_delete = [];
var ids_custom_delete = [];
var ids_to_buy = [];
$(document).ready(function() { $(document).ready(function() {
$(':input:checked').each(function () { $(':input:checked').each(function () {
ids += $(this).attr('rel'); ids_delete += $(this).attr('rel');
}) });
$('.btn-lista-compra').on('click', function (e) { $('.btn-lista-compra').on('click', function (e) {
get_checkeds(); 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) { $('.btn-delete-checked').on('click', function (e) {
get_checkeds(); get_checkeds();
$.ajax({ $.ajax({
url: "listacompra/delete", url: "lista-compra/delete",
type: "POST", type: "POST",
method:"post", method:"post",
data: { data: {
id: $('#profile_id').val(), ingredients_ids: ids_delete
inputs: inputs },
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) { success: function (data) {
...@@ -39,7 +123,6 @@ $(document).ready(function() { ...@@ -39,7 +123,6 @@ $(document).ready(function() {
} }
}) })
}) })
}); });
...@@ -49,10 +132,28 @@ $(document).ready(function() { ...@@ -49,10 +132,28 @@ $(document).ready(function() {
function get_checkeds() function get_checkeds()
{ {
$(':input:checked').each(function () { $(':input:checked').each(function () {
let id_val = $(this).attr('rel'); 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
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
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 to comment