/**
  * Get all dishes
  * url: http://localhost/dishes
  * Method: GET
  * @return      json
  */
 function dishes_get()
 {
     $this->authenticate();
     $messages_lang = $this->common->set_language_for_server_api('dishes_api', array('get_dishes_success', 'get_dishes_failure'));
     $result = Dishes_model::get_all_dishes();
     $response = array();
     if (!is_null($result)) {
         $dishes = array();
         foreach ($result as $temp) {
             $dish = array();
             $dish['id'] = (int) $temp->id;
             $dish['name'] = $temp->name;
             $dish['description'] = $temp->description;
             $dish['category'] = $temp->category;
             $dish['image'] = $temp->image;
             array_push($dishes, $dish);
         }
         $response['status'] = $messages_lang['success'];
         $response['message'] = $messages_lang['get_dishes_success'];
         $response['data'] = $dishes;
     } else {
         $response['status'] = $messages_lang['failure'];
         $response['message'] = $messages_lang['get_dishes_failure'];
     }
     $this->response($response, 200);
 }
 /**
  * Get all favorite dishes with votes in a week
  *
  * @param       int  $user_id
  * @return      array
  */
 function get_all_favorite_dishes_with_votes_number_of_user($user_id)
 {
     $dishes = array();
     $first_day_of_week = $this->find_first_date_of_week();
     $voted_dish_ids_in_week_arr = array();
     $voted_dish_ids_in_week = $this->get_votes_of_user_in_week($user_id, $first_day_of_week)->votes;
     if ($voted_dish_ids_in_week != NULL) {
         $voted_dish_ids_in_week_arr = substr_count($voted_dish_ids_in_week, ';') > 0 ? explode(";", $voted_dish_ids_in_week) : array($voted_dish_ids_in_week);
         $this->load->model('dishes_model');
         $result = Dishes_model::get_all_dishes();
         foreach ($result as $dish) {
             // Count dish_id have in voted dish ids in week or not
             $num_votes = in_array($dish->id, $voted_dish_ids_in_week_arr) ? 1 : 0;
             $dish->num_votes = $num_votes;
             // Push object dish in array $vote
             array_push($dishes, $dish);
         }
         usort($dishes, array($this, "compared_by_num_votes"));
     }
     return $dishes;
 }
 public function edit_dish($dish_id)
 {
     $image_data = $this->session->userdata('upload');
     $name = $this->input->post('name');
     $description = $this->input->post('description');
     $category = $this->input->post('category');
     $dish = array('name' => $name, 'description' => $description, 'category' => $category, 'image' => base_url(LINK_TO_IMAGE_OF_DISHES . $image_data['file_name']), 'image_file_name' => $image_data['file_name']);
     $result = Dishes_model::update_dish($dish_id, $dish);
     return $result;
 }
 public function dishes($category_id)
 {
     $this->common->authenticate();
     $this->load->model('dishes_model');
     echo json_encode(Dishes_model::get_dishes_by_category($category_id));
 }
 /**
  * Delete category
  *
  * @param       int  $category_id
  * @return      bool
  */
 function delete_category($category_id)
 {
     $this->db->trans_begin();
     $data = array('id' => $category_id);
     $this->db->delete('categories', $data);
     $query = $this->db->get_where('dishes', array('category_id' => $category_id));
     $have_dish_belong_to_this_category = $query->num_rows();
     if ($have_dish_belong_to_this_category > 0) {
         $dishes_id_belong_to_category = array();
         // Get all dishes id belongs to category
         foreach ($query->result() as $dish) {
             $dishes_id_belong_to_category[] = $dish->id;
         }
         // Delete list of dishes belongs to category
         $this->load->model('dishes_model');
         if (Dishes_model::delete_dishes($dishes_id_belong_to_category)) {
             $this->load->model('menus_model');
             // Delete list of dishes in menus have this category
             Menus_model::delete_dishes_in_menu_by_field('dish_id', $dishes_id_belong_to_category);
         }
     }
     if ($this->db->trans_status() === FALSE) {
         $this->db->trans_rollback();
         return FALSE;
     } else {
         $this->db->cache_delete('admin', 'dishes');
         $this->db->trans_commit();
         return TRUE;
     }
 }