Пример #1
0
 function index(Request $request)
 {
     $env = env('ADS_ENGINE');
     if (str_is('on', $env)) {
         $sort = 'ads.total_balance';
     } else {
         $sort = 'published_at';
     }
     /////////////////
     // Load Filter //
     /////////////////
     $filters = $request->only('id', 'name', 'slug', 'scheduled_since', 'scheduled_until', 'price_min', 'price_max', 'travel_agent_id', 'tags_destination', 'tags_poi', 'tags_interest', 'type', 'duration_day_min', 'duration_day_max', 'tour_package', 'promo', 'upcoming', 'mode', 'skip', 'take', 'with_count', 'with_travel_agent', 'with_related_tours', 'ask_for_recommended', 'with_unpublished');
     ///////////
     // Query //
     ///////////
     if (!$filters['take']) {
         $filters['take'] = 50;
     } else {
         $filters['take'] = min($filters['take'] * 1, 50);
     }
     $filters['skip'] = $filters['skip'] * 1;
     if ($filters['id']) {
         $data = new Collection();
         $dt = Model::with('travel_agent')->find($filters['id']);
         if ($filters['with_related_tours'] && $dt) {
             $dt->related_tours = Model::getRelatedTour($dt, 0, 12);
         }
         $data->push($dt);
     } else {
         /////////////////
         // MAKE QUERY  //
         /////////////////
         $q = Model::name($filters['name'])->slug($filters['slug']);
         if ($filters['with_unpublished']) {
             $q = $q;
         } else {
             $q = $q->published();
         }
         //////////////////////////////////////////////////
         // FILTER BY SCHEDULE (INC BUDGET) 				//
         //////////////////////////////////////////////////
         if ($filters['scheduled_since']) {
             try {
                 $filters['scheduled_since'] = \Carbon\Carbon::parse($filters['scheduled_since']);
             } catch (\Exception $e) {
                 return response()->json(JSend::fail(['schedule' => ['Invalid scheduled_since parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
             }
         }
         if ($filters['scheduled_until']) {
             try {
                 $filters['scheduled_until'] = \Carbon\Carbon::parse($filters['scheduled_until']);
             } catch (\Exception $e) {
                 return response()->json(JSend::fail(['scheduled_until' => ['Invalid scheduled_until parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
             }
         }
         if ($filters['scheduled_since'] && $filters['scheduled_until']) {
             $q = $q->TourScheduleBetween($filters['scheduled_since'], $filters['scheduled_until'], head(explode('-', $filters['budget'])), last(explode('-', $filters['budget'])), $type);
         }
         ///////////////////
         // FILTER BUDGET //
         ///////////////////
         if ($filters['price_min'] && $filters['price_max']) {
             $q = $q->priceBetween($filters['price_min'], $filters['price_max']);
         }
         /////////////////////////////
         // FILTER BY TRAVEL AGENT  //
         /////////////////////////////
         if ($filters['travel_agent_id']) {
             $q = $q->withTravelAgentId($filters['travel_agent_id']);
         }
         ///////////////////
         // FILTER BY TAG //
         ///////////////////
         if ($filters['tags_destination'] || $filters['tags_poi'] || $filters['tags_interest']) {
             $q = $q->TagDestination($filters['tags_destination']);
             $q = $q->TagPoi($filters['tags_poi']);
             $q = $q->TagInterest($filters['tags_interest']);
         }
         ////////////////////
         // FILTER BY TYPE //
         ////////////////////
         if ($filters['type']) {
             $q = $q->type($filters['type']);
         }
         ////////////////////////
         // FILTER BY DURATION //
         ////////////////////////
         if ($filters['duration_day_min'] && $filters['duration_day_max']) {
             $q = $q->durationDayBetween($filters['duration_day_min'], $filters['duration_day_max']);
         }
         ////////////////////////////
         // FILTER BY TOUR PACKAGE //
         ////////////////////////////
         if ($filters['tour_package']) {
             $q = $q->tourPackage($filters['tour_package']);
         }
         ////////////////////////////
         // FILTER BY PROMO 		  //
         ////////////////////////////
         if ($filters['promo']) {
             $q = $q->inPromo($filters['promo']);
         }
         ////////////////////////////
         // FILTER BY UPCOMING 	 //
         ////////////////////////////
         if ($filters['upcoming']) {
             $q = $q->upcomingtour();
         }
         ////////////////////
         // DO COUNT QUERY //
         ////////////////////
         if ($filters['with_count']) {
             $count = $q->count();
         }
         //////////
         // TAKE //
         //////////
         $filters['take'] = min(max(1, $filters['take']), 50);
         //////////
         // MODE //
         //////////
         switch (strtolower($filters['mode'])) {
             case 'latest':
                 $q = $q->orderBy($sort, 'desc');
                 break;
             case 'price-asc':
                 $q = $q->orderby('cron_cheapest_price', 'asc');
                 break;
             case 'price-desc':
                 $q = $q->orderby('cron_cheapest_price', 'desc');
                 break;
             case 'duration-asc':
                 $q = $q->orderBy('duration.day', 'asc');
                 break;
             case 'duration-desc':
                 $q = $q->orderBy('duration.day', 'desc');
                 break;
             case 'latest-created':
                 $q = $q->latest('created_at');
                 break;
         }
         ///////////////////////
         // WITH TRAVEL AGENT //
         ///////////////////////
         if ($filters['with_travel_agent']) {
             $q = $q->with('travel_agent');
         }
         //////////////
         // DO QUERY //
         //////////////
         $data = $q->skip($filters['skip'])->take($filters['take'])->get();
         ////////////////////////
         // WITH RELATED TOURS //
         ////////////////////////
         if ($filters['with_related_tours']) {
             foreach ($data as $k => $v) {
                 $data[$k]->related_tours = Model::getRelatedTour($v, 0, 12);
             }
         }
     }
     /////////////////
     // Recommended //
     /////////////////
     if ($count < 1 && $filters['ask_for_recommended']) {
         $recommended = [];
         $total_recommended = 4;
         $i = 0;
         $ids = [];
         foreach ($filters as $key => $value) {
             $q = Model::whereNotIn('_id', $ids)->published();
             //////////////////////////////////////////////////
             // FILTER BY SCHEDULE (INC BUDGET) 				//
             //////////////////////////////////////////////////
             if ($key == 'scheduled_since') {
                 try {
                     $value = \Carbon\Carbon::parse($value);
                     $q = $q->TourScheduleBetween($value, \Carbon\Carbon::now());
                 } catch (\Exception $e) {
                     return response()->json(JSend::fail(['schedule' => ['Invalid scheduled_since parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
                 }
             }
             if ($key == 'scheduled_until') {
                 try {
                     $value = \Carbon\Carbon::parse($value);
                     $q = $q->TourScheduleBetween(\Carbon\Carbon::now(), $value);
                 } catch (\Exception $e) {
                     return response()->json(JSend::fail(['scheduled_until' => ['Invalid scheduled_until parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
                 }
             }
             ///////////////////
             // FILTER BUDGET //
             ///////////////////
             if ($key == 'price_min') {
                 $q = $q->priceBetween(0, $value);
             }
             if ($key == 'price_max') {
                 $q = $q->priceBetween(0, $value);
             }
             /////////////////////////////
             // FILTER BY TRAVEL AGENT  //
             /////////////////////////////
             if ($key == 'travel_agent_id') {
                 $q = $q->withTravelAgentId($value);
             }
             ///////////////////
             // FILTER BY TAG //
             ///////////////////
             if ($key == 'tags_destination') {
                 $q = $q->TagDestination($value);
             }
             if ($key == 'tags_poi') {
                 $q = $q->TagPoi($value);
             }
             if ($key == 'tags_interest') {
                 $q = $q->TagInterest($value);
             }
             ////////////////////
             // FILTER BY TYPE //
             ////////////////////
             if ($key == 'type') {
                 $q = $q->type($value);
             }
             ////////////////////////
             // FILTER BY DURATION //
             ////////////////////////
             if ($key == 'duration_day_min') {
                 $q = $q->durationDayBetween(0, $value);
             }
             if ($key == 'duration_day_max') {
                 $q = $q->durationDayBetween(0, $value);
             }
             ////////////////////////////
             // FILTER BY TOUR PACKAGE //
             ////////////////////////////
             if ($key == 'tour_package') {
                 $q = $q->tourPackage($value);
             }
             ////////////////////////////
             // FILTER BY PROMO 		  //
             ////////////////////////////
             if ($key == 'promo') {
                 $q = $q->inPromo($value);
             }
             $i = $i + 1;
             if ($i < count($filters) - 1 && $total_recommended > 0) {
                 $data = $q->orderby($sort)->take(1)->with('travel_agent')->get();
                 if (count($data) > 0) {
                     $total_recommended = $total_recommended - 1;
                     $recommended = array_merge($recommended, $data->toArray());
                     $ids[] = $data[0]['_id'];
                 }
             } elseif ($total_recommended > 0) {
                 $data = $q->orderby($sort)->take($total_recommended)->with('travel_agent')->get();
                 if (count($data) == $total_recommended) {
                     $total_recommended = 0;
                     $recommended = array_merge($recommended, $data->toArray());
                     foreach ($data as $key2 => $value2) {
                         $ids[] = $value2['_id'];
                     }
                 } elseif (count($data) > 0) {
                     $total_recommended = $total_recommended - count($data);
                     $recommended = array_merge($recommended, $data->toArray());
                     foreach ($data as $key2 => $value2) {
                         $ids[] = $value2['_id'];
                     }
                 }
             }
         }
         if ($total_recommended > 0) {
             $q = Model::whereNotIn('_id', $ids)->published();
             $data = $q->orderby($sort)->take($total_recommended)->with('travel_agent')->get();
             $recommended = array_merge($recommended, $data->toArray());
             foreach ($data as $key2 => $value2) {
                 $ids[] = $value2['_id'];
             }
         }
         return response()->json(JSend::success(['count' => $count, 'data' => $recommended])->asArray())->setCallback($this->request->input('callback'));
     }
     //////////////
     // Response //
     //////////////
     return response()->json(JSend::success(['count' => $count, 'data' => $data->toArray()])->asArray())->setCallback($this->request->input('callback'));
 }