/**
  * 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 load_dishes_view($dishes_name, $category)
 {
     $message = array('title', 'search_name', 'search', 'description', 'name', 'category', 'image', 'create_dish', 'edit', 'delete', 'are_you_sure', 'yes', 'cancel');
     $data = $this->common->set_language_and_data('dishes', $message);
     $this->load->library('pagination');
     $config['base_url'] = $dishes_name == NULL && $category == NULL ? base_url() . '/admin/dishes' : base_url() . '/admin/dishes/search';
     if ($dishes_name == NULL) {
         $config['total_rows'] = Dishes_model::get_num_of_dishes_by_category($category);
     } elseif ($dishes_name != NULL) {
         $config['total_rows'] = Dishes_model::get_num_of_dishes_by_category($category, $dishes_name);
     }
     $config['per_page'] = 10;
     $config['use_page_numbers'] = TRUE;
     $config['uri_segment'] = $dishes_name == NULL && $category == NULL ? 3 : 4;
     $config['num_links'] = 3;
     $config['full_tag_open'] = "<ul class='pagination'>";
     $config['full_tag_close'] = "</ul>";
     $config['first_link'] = false;
     $config['last_link'] = false;
     $config['num_tag_open'] = '<li>';
     $config['num_tag_close'] = '</li>';
     $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
     $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
     $config['next_tag_open'] = "<li>";
     $config['next_tagl_close'] = "</li>";
     $config['prev_tag_open'] = "<li>";
     $config['prev_tagl_close'] = "</li>";
     $config['first_tag_open'] = "<li>";
     $config['first_tagl_close'] = "</li>";
     $config['last_tag_open'] = "<li>";
     $config['last_tagl_close'] = "</li>";
     $this->pagination->initialize($config);
     $data['pagination'] = $this->pagination->create_links();
     $data['page'] = $dishes_name == NULL && $category == NULL ? $this->uri->segment(3) ? $this->uri->segment(3) : 0 : ($this->uri->segment(4) ? $this->uri->segment(4) : 0);
     $dishes = Dishes_model::get_all_dishes($config['per_page'], ($data['page'] == 0 ? $data['page'] : $data['page'] - 1) * $config['per_page'], $dishes_name, $category);
     $data['dishes'] = $dishes;
     $this->load->model('categories_model');
     $data['categories'] = Categories_model::get_all_categories();
     $this->common->load_view('admin/dishes/dishes', $data);
 }