public function search_products()
 {
     $store_id = Request::segment(2);
     $this->store = Store::find($store_id);
     if (!$this->store) {
         if (Request::format() == 'html') {
             // Not Found Page
             die;
         } else {
             $response_array = array('success' => 'false', 'error_code' => '404', 'error' => 'Store not Found');
             $response_code = 200;
             $response = Response::json($response_array, $response_code);
             return $response;
         }
     }
     if (Input::has('per_page')) {
         $per_page = Input::get('per_page');
     } else {
         $per_page = 20;
     }
     if (Input::has('page')) {
         $page = Input::get('page');
     } else {
         $page = 0;
     }
     $shelf_data['products'] = array();
     $query = Product::where('store_id', $store_id);
     if (Input::has('q')) {
         $q = Input::get('q');
         $query = $query->where('products.name', 'LIKE', '%' . $q . '%');
     }
     $brands = $query->leftJoin('brands', 'products.brand_id', '=', 'brands.id')->select(DB::raw('distinct brands.*'))->get();
     $query = Product::where('store_id', $store_id);
     if (Input::has('q')) {
         $q = Input::get('q');
         $query = $query->where('products.name', 'LIKE', '%' . $q . '%');
     }
     $options = $query->leftJoin('product_tag', 'products.id', '=', 'product_tag.product_id')->Join('options', 'product_tag.option_id', '=', 'options.id')->select(DB::raw('distinct options.*'))->get();
     $query = Product::where('store_id', $store_id);
     $base_search_url = web_url() . "/store/" . $store_id . "/search?q=" . Input::get('q');
     if (Input::has('q')) {
         $q = Input::get('q');
         $query = $query->where('products.name', 'LIKE', '%' . $q . '%');
     }
     $base_search_url_options = $base_search_url;
     if (Input::has('options')) {
         $option_ids = Input::get('options');
         $query = $query->leftJoin('product_tag', 'products.id', '=', 'product_tag.product_id')->whereIn('product_tag.option_id', Input::get('options'));
         foreach (Input::get('options') as $option) {
             $base_search_url_options .= "&options[]=" . $option;
         }
     } else {
         $option_ids = array();
     }
     $base_search_url_brand = $base_search_url;
     if (Input::has('brand')) {
         $brand_id = Input::get('brand');
         $query = $query->where('brand_id', Input::get('brand'));
         $base_search_url_brand .= "&brand=" . Input::get('brand');
     } else {
         $brand_id = 0;
     }
     /*
     		if(Input::has('sort_by') && Input::get('sort_by') == 'price'){
     			$query = $query->orderBy('price');
     		} else {
     			$query = $query->orderBy('total_sale','desc');
     		} */
     $query = $query->orderBy('shelf_id');
     $products = $query->select('products.*')->distinct()->get();
     $shelves = array();
     $shelf_id = 0;
     foreach ($products as $product) {
         if ($product->shelf_id != $shelf_id) {
             $shelf = Shelf::find($product->shelf_id);
             $shelf_id = $product->shelf_id;
             $shelves[$shelf_id] = $shelf->toArray();
             $shelves[$shelf_id]['products'] = array();
         }
         $shelf_product = $product->toArray();
         array_push($shelves[$shelf_id]['products'], $shelf_product);
     }
     $response_array['shelves'] = $shelves;
     $response_array['brands'] = $brands->toArray();
     $response_array['options'] = $options->toArray();
     if (Request::format() == 'html' && $page == 0) {
         Session::put('store_id', $store_id);
         return View::make('search')->with('data', $response_array)->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores', $this->stores)->with('q', Input::get('q'))->with('base_search_url_brand', $base_search_url_brand)->with('base_search_url_options', $base_search_url_options)->with('option_ids', $option_ids)->with('brand_id', $brand_id);
     } else {
         $response_array['success'] = 'true';
         $response_code = 200;
         $response = Response::json($response_array, $response_code);
         return $response;
     }
 }
 public function save_shelf()
 {
     $id = Input::get('id');
     $store_id = Input::get('store_id');
     $department_id = Input::get('department_id');
     $shelf = Shelf::find($id);
     if (!$shelf) {
         $shelf = new Shelf();
     }
     $shelf->name = Input::get('name');
     $shelf->store_id = $store_id;
     $shelf->department_id = $department_id;
     $shelf->save();
     $message = "Successfully updated the shelf";
     $type = "success";
     return Redirect::to('/admin/store/' . $store_id . '/department/' . $department_id . '/shelves')->with('type', $type)->with('message', $message);
 }