Ejemplo n.º 1
0
 public function newRecipe()
 {
     $return = array();
     $return['success'] = 'fail';
     $return['errors'] = array();
     if (!isset($this->params['title']) || strlen($this->params['title']) < 10) {
         array_push($return['errors'], Lang::get('at-least-10-characters-title'));
     }
     if (!isset($this->params['question-body']) || strlen($this->params['question-body']) < 30) {
         array_push($return['errors'], Lang::get('at-least-30-characters-body'));
     }
     if (!isset($this->params['quantity']) || sizeof($this->params['quantity']) < 3) {
         array_push($return['errors'], Lang::get('at-least-3-ingredients'));
     }
     if (sizeof($return['errors']) > 0) {
         return $this->return_json($return);
     }
     // if everything passes:
     $user_id = Session::get('id');
     $thumbnail = '';
     if (isset($_FILES)) {
         $file = $_FILES['file'];
         $path = UPLOAD_PATH . DS . $user_id . DS;
         $u = new Framework\Upload\Upload();
         $thumbnail = $u->single($file, $path);
         $thumbnail = '/app/files/uploads/' . $user_id . '/' . $thumbnail;
     }
     $recipe = new MfrUserRecipes();
     $recipe->user_id = Session::get('id');
     $recipe->date_created = time();
     $recipe->last_updated = time();
     $recipe->name = $this->params['title'];
     $recipe->instructions = $this->params['question-body'];
     $recipe->lang = 'pt';
     $recipe->thumbnail = $thumbnail;
     $recipe_id = $recipe->saveGetId();
     foreach ($this->params['ingredient'] as $key => $value) {
         $ingredient = MfrAlimentos::where('name_pt', trim($value))->first();
         if (isset($ingredient->id) && $ingredient->id > 0) {
             $addIngredient = new MfrUserRecipesAlimentos();
             $addIngredient->recipe_id = $recipe_id;
             $addIngredient->alimento_id = $ingredient->id;
             $addIngredient->date_created = time();
             $addIngredient->last_updated = time();
             $addIngredient->save();
         }
     }
     $return['success'] = 'ok';
     return $this->return_json($return);
 }