Esempio n. 1
0
 public function lists($travel_agent = null, $tujuan = null, $keberangkatan = null, $budget = null)
 {
     // ------------------------------------------------------------------------
     // REDIRECT IF REQUEST URL
     // ------------------------------------------------------------------------
     $filters = Input::only('travel_agent', 'tujuan', 'keberangkatan_sejak', 'keberangkatan_hingga', 'budget');
     if (!empty(array_filter($filters))) {
         $filters = array_filter($filters);
         // Travel Agent
         if (!$filters['travel_agent']) {
             $filters['travel_agent'] = 'semua-travel-agent';
         }
         // Tujuan
         if (!$filters['tujuan']) {
             $filters['tujuan'] = 'semua-tujuan';
         }
         // Keberangkatan
         if ($filters['keberangkatan_sejak'] && $filters['keberangkatan_hingga']) {
             try {
                 $tmp1 = \Carbon\Carbon::createFromFormat('d-m-Y', $filters['keberangkatan_sejak']);
                 $tmp2 = \Carbon\Carbon::createFromFormat('d-m-Y', $filters['keberangkatan_hingga']);
             } catch (Exception $e) {
                 return App::abort(404);
             }
             if ($tmp1->lt($tmp2)) {
                 $keberangkatan_str = $tmp1->format('Ymd') . '-' . $tmp2->format('Ymd');
             } else {
                 $keberangkatan_str = $tmp2->format('Ymd') . '-' . $tmp1->format('Ymd');
             }
         } else {
             $keberangkatan_str = "semua-keberangkatan";
         }
         // Tujuan
         if (!$filters['budget']) {
             $filters['budget'] = 'semua-budget';
         }
         return redirect()->to(route('web.tour', ['travel_agent' => $filters['travel_agent'], 'tujuan' => $filters['tujuan'], 'keberangkatan' => $keberangkatan_str, 'budget' => $filters['budget']]) . (Input::has('place') ? '?place=' . Input::get('place') : ''));
     } else {
         $filters['travel_agent'] = $travel_agent;
         $filters['tujuan'] = $tujuan;
         $filters['keberangkatan'] = $keberangkatan;
         $filters['budget'] = $budget;
         $filters['place'] = Input::get('place');
         // TRAVEL AGENT
         if (str_is(strtolower($filters['travel_agent']), 'semua-travel-agent')) {
             unset($filters['travel_agent']);
         } else {
             if ($filters['travel_agent']) {
                 $filters['travel_agent'] = TravelAgent::SlugIs($filters['travel_agent'])->first();
                 if (!$filters['travel_agent']) {
                     return App::abort(404);
                 }
             }
             $this->layout->basic->filters['travel_agent'] = $filters['travel_agent'];
         }
         // TUJUAN
         if (str_is(strtolower($filters['tujuan']), 'semua-tujuan')) {
             unset($filters['tujuan']);
         } else {
             $tujuan_tree = Destination::findPath(str_replace(',', Destination::getDelimiter(), $filters['tujuan'] . '*'))->get();
             if (!$tujuan_tree) {
                 return App::abort(404);
             }
             // get tujuan object
             foreach ($tujuan_tree as $x) {
                 if (str_is($filters['tujuan'], $x->path_slug)) {
                     $filters['tujuan'] = $x;
                     break;
                 }
             }
             $this->layout->basic->filters['tujuan'] = $filters['tujuan'];
         }
         // KEBERANGKATAN
         if (str_is(strtolower($filters['keberangkatan']), 'semua-keberangkatan')) {
             unset($filters['keberangkatan']);
             $filters['keberangkatan']['from'] = \Carbon\Carbon::now();
             $filters['keberangkatan']['to'] = \Carbon\Carbon::now()->addYear(1);
         } elseif ($filters['keberangkatan']) {
             list($keberangkatan_from, $keberangkatan_to) = explode('-', $filters['keberangkatan']);
             try {
                 $keberangkatan_from = \Carbon\Carbon::createFromFormat('Ymd', $keberangkatan_from);
                 $keberangkatan_to = \Carbon\Carbon::createFromFormat('Ymd', $keberangkatan_to);
                 if ($keberangkatan_from->gt($keberangkatan_to)) {
                     $tmp = $keberangkatan_from;
                     $keberangkatan_from = $keberangkatan_to;
                     $keberangkatan_to = $tmp;
                 }
             } catch (Exception $e) {
                 App::abort(404);
             }
             unset($filters['keberangkatan']);
             $filters['keberangkatan']['from'] = $keberangkatan_from;
             $filters['keberangkatan']['to'] = $keberangkatan_to;
             $this->layout->basic->filters['keberangkatan'] = $filters['keberangkatan'];
         } else {
             $filters['keberangkatan']['from'] = \Carbon\Carbon::now();
             $filters['keberangkatan']['to'] = \Carbon\Carbon::now()->addYear(1);
         }
         // BUDGET
         if (str_is(strtolower($filters['budget']), 'semua-budget')) {
             unset($filters['budget']);
         } else {
             list($budget_min, $budget_max) = explode('-', $filters['budget']);
             if ($budget_min * 1 < 0) {
                 return App::abort(404);
             }
             if (!is_null($budget_max) && ($budget_max <= 0 || $budget_max <= $budget_min)) {
                 return App::abort(404);
             }
             unset($filters['budget']);
             $filters['budget'] = ['min' => $budget_min, 'max' => $budget_max];
             $this->layout->basic->filters['budget'] = $filters['budget'];
         }
         // PLACE
         if ($filters['place']) {
             $filters['place'] = \App\Place::slugIs($filters['place'])->first();
             if (!$filters['place']) {
                 return App::abort(404);
             }
         }
     }
     // ------------------------------------------------------------------------
     // QUERY
     // ------------------------------------------------------------------------
     $max_data = 50;
     $results = Cache::remember('tour_schedules_' . serialize($filters), 30, function () use($filters, $tujuan_tree, $max_data) {
         $results = $this->dispatch(new FindPublishedTourSchedules($filters['keberangkatan']['from'], $filters['keberangkatan']['to'], $tujuan_tree ? $tujuan_tree->lists('id') : null, $filters['budget']['min'] * 1, $filters['budget']['max'] ? $filters['budget']['max'] * 1 : 99999999999.0, $filters['travel_agent']->id, $filters['place']->slug, 0, $max_data, true));
         $results['data']->load('tour', 'tour.travel_agent', 'tour.travel_agent.active_packages', 'tour.places', 'tour.destinations', 'tour.travel_agent.images', 'tour.options');
         return $results;
     });
     $tour_schedules_count = $results['count'];
     $tour_schedules = $results['data'];
     // SORT BY TRAVEL AGENT PRIORITY
     // $tour_schedules = $tour_schedules->sortBy(function($data, $key){
     // 	return str_pad($data->tour->travel_agent->active_packages[0]->priority ? $data->tour->travel_agent->active_packages[0]->priority * -1 : 0, 2, 0, STR_PAD_LEFT) . $data->departure;
     // });
     // ------------------------------------------------------------------------
     // PREPARE FILTERS FOR RESULTS
     // ------------------------------------------------------------------------
     $filter_schedules = [];
     // durations
     foreach ($tour_schedules as $schedule) {
         $filter_schedules['durations'][$schedule->tour->duration_day * 1] = $schedule->tour->duration_day . 'D/' . $schedule->tour->duration_night . 'N';
         $filter_schedules['travel_agents'][$schedule->tour->travel_agent->id] = $schedule->tour->travel_agent->name;
         if (!$filter_schedules['price']['min'] || $filter_schedules['price']['min'] > $schedule->discounted_price) {
             $filter_schedules['price']['min'] = $schedule->discounted_price;
         }
         if ($filter_schedules['price']['max'] < $schedule->discounted_price) {
             $filter_schedules['price']['max'] = $schedule->discounted_price;
         }
     }
     ksort($filter_schedules['durations']);
     asort($filter_schedules['travel_agents']);
     // ------------------------------------------------------------------------------------------------------------
     // SHOW DISPLAY
     // ------------------------------------------------------------------------------------------------------------
     $this->layout->page = view($this->page_base_dir . 'tours');
     $this->layout->page->travel_agent = $travel_agent;
     $this->layout->page->tujuan_tree = $tujuan_tree;
     $this->layout->page->max_data = $max_data;
     $this->layout->page->filters = $filters;
     $this->layout->page->filter_schedules = $filter_schedules;
     $this->layout->page->option_list = $this->option_list;
     $this->layout->page->tour_schedules = $tour_schedules;
     $this->layout->page->tour_schedules_count = $tour_schedules_count;
     $this->layout->page->other_tours = $other_tours;
     // search tour
     $this->layout->page->all_travel_agents = $this->all_travel_agents;
     $this->layout->page->all_destinations = $this->all_destinations;
     $this->layout->page->departure_list = $this->departure_list;
     $this->layout->page->budget_list = $this->budget_list;
     $this->layout->page->tour_shortcut = $this->tour_shortcut;
     //
     $this->layout->title = "Paket Tour " . ($filters['tujuan'] ? 'ke ' . $filters['tujuan']->name : '') . ($filters['travel_agent'] ? ' oleh ' . $filters['travel_agent']->name : '') . ($filters['keberangkatan']['from'] && $filters['keberangkatan']['to'] ? ' keberangkatan ' . $filters['keberangkatan']['from']->format('d-m-Y') . ' s/d ' . $filters['keberangkatan']['to']->format('d-m-Y') : '') . ($filters['budget']['min'] ? ' harga ' . $filters['budget']['min'] . '-' . $filters['budget']['max'] : '') . ' - Capcus.id';
     $this->layout->og['title'] = $this->layout->title;
     $this->layout->og['type'] = 'website';
     $this->layout->og['image'] = $tour_schedules->count() ? $tour_schedules->first()->tour->places->first()->images->where('name', 'LargeImage')->first()->path : '';
     $this->layout->og['image:type'] = 'jpg';
     $this->layout->og['image:width'] = 800;
     $this->layout->og['image:height'] = 600;
     return $this->layout;
 }