Advertisement
Guest User

Untitled

a guest
Feb 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 22.29 KB | None | 0 0
  1. public function list(Request $request)
  2.     {
  3.         DB::enableQueryLog();
  4.  
  5.  
  6.         // Amenity Data
  7.         $amenity_data = Amenity::where('status', 1)->get()->toArray();
  8.  
  9.         // Get Area Data
  10.         $property_area_data = Property::select('area')->where('status', 1)->distinct()->whereNotNull('area')->get()->toArray();
  11.  
  12.         // Get Apartment Data
  13.         $property_apartment_data = array('Studio' => 'Studio', '1' => '1', '2' => '2', '3' => '3', '4' => '4');
  14.  
  15.         // Get Sqft Size
  16.         $sqft_min_size = Property::select('sqft')->where('status', 1)->min('sqft');
  17.         $sqft_max_size = Property::select('sqft')->where('status', 1)->max('sqft');
  18.  
  19.         // Get Price
  20.         $min_price = Property::select('monthly_price')->where('status', 1)->min('monthly_price');
  21.         $max_price = Property::select('monthly_price')->where('status', 1)->max('monthly_price');
  22.  
  23.         // Get Currency
  24.         $currency = Setting::where('key', 'currency')->first()->value;
  25.  
  26.         // Get Search Property Data
  27.         $property_data = Property::query()->with('propertyImages', 'PropertyAmenity.AmenityDetail')->where('status', 1);
  28.  
  29.         //->select(['*', DB::raw( '(3959 * acos( cos( radians(".$lat1.") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(".$lon1.") ) + sin( radians(".$lat1.") ) * sin( radians( latitude ) ) ) ) AS distance')])
  30.         //->havingRaw('distance < ?',[2.5]);
  31.  
  32.  
  33.         if (($request->input('call') == 'ajax' || $request->input('call') == "searchclick") && $request->input('lat') != "" && $request->input('lng') != "") {
  34.  
  35.  
  36.             //ajax call
  37.             $lat1 = $request->input('lat');
  38.             $lon1 = $request->input('lng');
  39.             $dist = $request->input('radius') != "" ? $request->input('radius') : 3;
  40.  
  41.             $select = DB::select("SELECT id, ( 3959 * acos( cos( radians(" . $lat1 . ") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(" . $lon1 . ") ) + sin( radians(" . $lat1 . ") ) * sin( radians( latitude ) ) ) ) AS distance FROM properties HAVING distance < " . $dist);
  42.  
  43.             $nearr = array();
  44.  
  45.             foreach ($select as $key => $value) {
  46.                 array_push($nearr, $value->id);
  47.             }
  48.             $property_data->whereIN('id', $nearr);
  49.  
  50.  
  51.             //$property_data->whereIN('id',array($select))
  52.         }
  53.  
  54.         $selected_area = "";
  55.         $selected_apartments = "";
  56.         $selected_guest = "";
  57.         $selected_size = "";
  58.         $selected_price = "";
  59.  
  60.         // if ($request->get('area') != '') {
  61.         //      $selected_area = $request->get('area');
  62.         //      $property_data->where('area',$request->get('area'));
  63.         // }
  64.  
  65.         if ($request->get('apartments') != '') {
  66.             $selected_apartments = $request->get('apartments');
  67.             $property_data->where('bedrooms', $request->get('apartments'));
  68.         }
  69.  
  70.         if ($request->get('guest') != '') {
  71.             $selected_guest = $request->get('guest');
  72.             $property_data->where('no_of_guest', $selected_guest);
  73.         }
  74.  
  75.         if ($request->get('size') != '') {
  76.             $selected_size = $request->get('size');
  77.             $property_data->whereBetween('sqft', array(explode(',', $request->get('size'))));
  78.         }
  79.  
  80.         if ($request->get('price') != '') {
  81.             $selected_price = $request->get('price');
  82.             $property_data->whereBetween('monthly_price', array(explode(',', $selected_price)));
  83.         }
  84.  
  85.         //area
  86.         if ($request->input('call') == "homebox" && $request->input('call') != 'ajax') // && $request->input('call_page')!=1)
  87.         {
  88.             //country
  89.             $address_component = $request->input('address_component');
  90.             if ($address_component['country'] != '' && $address_component['country'] != null) {
  91.                 $property_data->where('country', 'like', '%' . $address_component['country'] . '%');
  92.             }
  93.  
  94.  
  95.             $property_data->where(function ($property_data) use ($request) {
  96.                 foreach ($request->input('address_component') as $key => $value) {
  97.                     if ($value != '' && $value != null) {
  98.                         switch ($key) {
  99.                             /*case 'country':
  100.                                     # code...Where('name', 'like', '%' . Input::get('name') . '%')
  101.                                     $property_data->where('country',  'like', '%' . $value . '%');
  102.                             break;*/
  103.                             case 'state':
  104.                                 $property_data->Orwhere('state', 'like', '%' . $value . '%');
  105.                                 break;
  106.                             case 'postal_code':
  107.                                 $property_data->Orwhere('zipcode', 'like', '%' . $value . '%');
  108.                                 break;
  109.                             case 'administrative_area_level_1':
  110.                                 $property_data->Orwhere('city', 'like', '%' . $value . '%');
  111.                                 break;
  112.                             case 'locality':
  113.                                 $property_data->Orwhere('city', 'like', '%' . $value . '%');
  114.                                 break;
  115.                             case 'street_number':
  116.                                 # code...
  117.                                break;
  118.                             case 'route':
  119.                                 # code...
  120.                                break;
  121.                         }
  122.                     }
  123.                 }
  124.                 if ($request->input('area') != '') {
  125.                     $area_search = $request->input('area');
  126.                     $area_search = str_replace(',', '', $area_search);
  127.                     $area_search = str_replace(' ', ',', $area_search);
  128.                     $arr_area_search = explode(',', $area_search);
  129.                     $arr_area_search = array_filter($arr_area_search);
  130.                     $order_string = array();
  131.                     foreach ($arr_area_search as $key => $value) {
  132.                         $property_data->Orwhere('area', 'like', '%' . $value . '%');
  133.                         $property_data->Orwhere('country', 'like', '%' . $value . '%');
  134.                         $property_data->Orwhere('state', 'like', '%' . $value . '%');
  135.                         $order_string[] = "'" . $value . "'";
  136.                     }
  137.                     //$property_data->orderByRaw("FIELD(area, 'Owner', 'Admin', 'Member') ASC");
  138.                     $property_data->orderByRaw("FIELD(area, " . implode(',', $order_string) . ") ASC");
  139.  
  140.                 }
  141.             });
  142.  
  143.         }
  144.  
  145.  
  146.         //code added by Hirin
  147.         $checkpass = false;
  148.         if ($request->get('aminities') != '') {
  149.             $data_for_input_of_aminities = $request->get('aminities');
  150.             $selected_aminities = explode(",", $data_for_input_of_aminities);
  151.             //echo "<pre/>";print_r($selected_aminities);exit;
  152.             /*// Code added by Dwarkesh
  153.             $x = PropertyAmenity::select('property_id');
  154.             foreach ($selected_aminities as $key => $selected_aminity) {
  155.                     $x->where('amenity_id', $selected_aminity);
  156.             }
  157.  
  158.             $x = $x->pluck('property_id')->toArray();*/
  159.             $x = PropertyAmenity::select('property_id')->whereIn('amenity_id', $selected_aminities)->pluck('property_id')->toArray();
  160.  
  161.             $checkpass = true;
  162.  
  163. //$property_data->WhereIn('id',$x);
  164.  
  165. // $property_data->whereHas('PropertyAmenity', function ($query)  use($request){
  166. //      $selected_aminities = explode(",",$request->get('aminities'));
  167. //                          $query->whereIn('amenity_id', $selected_aminities);
  168. //                      });
  169.         } else {
  170.             $data_for_input_of_aminities = '';
  171.             $x = array();
  172.         }
  173.  
  174.         if ($request->get('date-range') != '') {
  175.             if ($request->get('date-range') != 'Check-in Check-out') {//DB::enableQueryLog();
  176.                 $checkpass = true;
  177.                 $date_range = explode(" to ", $request->get('date-range'));
  178.  
  179.                 /*CODE RT STARTS*/
  180.  
  181.                 $query_detail = PropertyAvailability::Distinct('property_id');
  182.  
  183.                 $query_detail->Where(function ($query) use ($date_range) {
  184.                     $query->where('start_date', '<=', $date_range[0]);
  185.                     $query->where('end_date', '>=', $date_range[1]);
  186.                     //$query->orWhereBetween('start_date', array($date_range[0], $date_range[1]));
  187.                     //$query->orWhereBetween('end_date', array($date_range[0], $date_range[1]));
  188.                 });
  189.                 $query_detail->where('status', 1);
  190.                 $available_property = $query_detail->pluck('property_id')->toArray();
  191.  
  192.  
  193.                 $date = \Carbon\Carbon::parse($date_range[1]);
  194.                 $now = \Carbon\Carbon::parse($date_range[0]);
  195.  
  196.                 $diff = $date->diffInMonths($now);
  197.                 $diff_inweek = $date->diffInWeeks($now);
  198.                 $diff_indays = $date->diffInDays($now);
  199.  
  200.                 foreach ($available_property as $ap) {
  201.  
  202.  
  203.                     $property_check_data = Property::select('properties.inquiry_less_then_one_month', 'properties.inquiry_one_to_three_month', 'properties.inquiry_three_to_six_month', 'properties.inquiry_six_to_twelve_month', 'properties.inquiry_more_then_twelve_month', 'properties.min_stay', 'properties.max_stay')->find($ap);
  204.                     if (count($property_check_data)) {
  205.                         if ($diff < 1) {
  206.                             $week_count = $property_check_data->inquiry_less_then_one_month;
  207.                         } elseif ($diff <= 3) {
  208.                             $week_count = $property_check_data->inquiry_one_to_three_month;
  209.                         } elseif ($diff <= 6) {
  210.                             $week_count = $property_check_data->inquiry_three_to_six_month;
  211.                         } elseif ($diff <= 12) {
  212.                             $week_count = $property_check_data->inquiry_six_to_twelve_month;
  213.                         } elseif ($diff > 12) {
  214.                             $week_count = $property_check_data->inquiry_more_then_twelve_month;
  215.                         }
  216.  
  217.                         if ($property_check_data->max_stay == 'No maximum stay') {
  218.                             $max_stay = $diff_indays;
  219.                         } else {
  220.                             $max_stay = ($property_check_data->max_stay * 30);
  221.                         }
  222.  
  223.                         $pavcheck = PropertyAvailability::where('property_availability.property_id', $ap)->where('property_availability.status', 0)->select('property_availability.end_date')->where('property_availability.end_date', '<=', $date)->orderby('property_availability.end_date', 'desc')->first();
  224.  
  225.  
  226.                         if (count($pavcheck)) {
  227.  
  228.                             $check_enddate = \Carbon\Carbon::parse($pavcheck->end_date);
  229.                             $check_startdate = $now;
  230.  
  231.                             $diff_weeks = $check_enddate->diffInWeeks($check_startdate);
  232.  
  233.  
  234.                             if ($diff_weeks >= $week_count || $diff_inweek < $property_check_data->min_stay || $diff_indays > $max_stay) {
  235.                                 if (($key = array_search($ap, $available_property)) !== false) {
  236.                                     unset($available_property[$key]);
  237.                                 }
  238.                             }
  239.                         } else {
  240.                             //echo $diff_inweek .'--'. $property_check_data->min_stay .'--'. $diff_indays .'--'. $max_stay;
  241.                             if ($diff_inweek < $property_check_data->min_stay || $diff_indays > $max_stay) {
  242.                                 if (($key = array_search($ap, $available_property)) !== false) {
  243.                                     unset($available_property[$key]);
  244.                                 }
  245.                             }
  246.  
  247.                         }
  248.                     }
  249.                 }
  250.  
  251.  
  252.             } else {
  253.                 $available_property = array();
  254.                 $date_range = array();
  255.             }
  256.         } else {
  257.             $available_property = array();
  258.             $date_range = array();
  259.         }
  260.         if ($checkpass == true) {
  261.             //$y = array_merge($x,$available_property);
  262.             if (count($x) > 0 && count($available_property) > 0) {
  263.                 $y = array_intersect($x, $available_property);
  264.             } else {
  265.                 $y = array_merge($x, $available_property);
  266.             }
  267.  
  268.             $property_data->WhereIn('id', $y);
  269.  
  270.         }
  271.  
  272.  
  273.         if (isset($_GET['area'])) {
  274.             $urla[] = "area=" . $_GET['area'];
  275.         }
  276.  
  277.         if (isset($_GET['apartments']))
  278.             $urla[] = "apartments=" . $_GET['apartments'];
  279.  
  280.         if (isset($_GET['price']))
  281.             $urla[] = "price=" . $_GET['price'];
  282.  
  283.         if (isset($_GET['date-range']))
  284.             $urla[] = "date-range=" . $_GET['date-range'];
  285.  
  286.         if (isset($_GET['aminities']))
  287.             $urla[] = "aminities=" . $_GET['aminities'];
  288.  
  289.         if (isset($_GET['guest']))
  290.             $urla[] = "guest=" . $_GET['guest'];
  291.  
  292.  
  293.         if (isset($_GET['address_component'])) {
  294.             $address_component = $_GET['address_component'];
  295.  
  296.             $urla[] = http_build_query(array('address_component' => $address_component), 'flags_');
  297.  
  298.         } else {
  299.             $address_component['country'] = '';
  300.             $address_component['state'] = '';
  301.             $address_component['postal_code'] = '';
  302.             $address_component['city'] = '';
  303.             $address_component['locality'] = '';
  304.             $address_component['administrative_area_level_1'] = '';
  305.             $address_component['street_number'] = '';
  306.             $address_component['route'] = '';
  307.         }
  308.  
  309.         $address_component['country'] = (isset($address_component['country']) ? $address_component['country'] : '');
  310.         $address_component['state'] = (isset($address_component['state']) ? $address_component['state'] : '');
  311.         $address_component['postal_code'] = (isset($address_component['postal_code']) ? $address_component['postal_code'] : '');
  312.         $address_component['city'] = (isset($address_component['city']) ? $address_component['city'] : '');
  313.         $address_component['locality'] = (isset($address_component['locality']) ? $address_component['locality'] : '');
  314.         $address_component['administrative_area_level_1'] = (isset($address_component['administrative_area_level_1']) ? $address_component['administrative_area_level_1'] : '');
  315.         $address_component['street_number'] = (isset($address_component['street_number']) ? $address_component['street_number'] : '');
  316.         $address_component['route'] = (isset($address_component['route']) ? $address_component['route'] : '');
  317.  
  318.         /*$address_component['state']='';
  319.         $address_component['postal_code']='';
  320.         $address_component['city']='';
  321.         $address_component['locality']='';
  322.         $address_component['administrative_area_level_1']='';
  323.         $address_component['street_number']='';
  324.         $address_component['route']='';*/
  325.         //dd($address_component);
  326.         if (!isset($urla)) {
  327.             $urla = "";
  328.         }
  329.  
  330.         $url = "";
  331.         if (count($urla) > 0 && !empty($urla)) {
  332.             $url = implode('&', $urla);
  333.         }
  334.  
  335.  
  336.         $searchResultAll = $property_data->get();
  337.         $searchResultObj = $property_data->paginate(12);
  338.  
  339.  
  340.         $searchResultArr = $searchResultObj->toArray();
  341.         //dd($searchResultArr);
  342.         $searchFormResult = $searchResultArr['data'];
  343.         if (count($searchResultAll)) {
  344.             $properties = array();
  345.             $bookmarks = array();
  346.             $mainlat = "";
  347.             $mainlong = "";
  348.  
  349.             //print_r($user->id);
  350.             //$user = \Auth::user();
  351.             //die();
  352.             foreach ($searchResultAll->toArray() as $key) {
  353.                 $image = isset($key['property_images']['0']['path']) ? $key['property_images']['0']['path'] : "";
  354.                 $properties[] = array($key['en_name'], $key['latitude'], $key['longitude'], $key['area'], $key['id'], $image, $key['bedrooms'], $key['bathrooms'], $key['sqft'], number_format($key['monthly_price']));
  355.  
  356.                 //           if(!empty($user))
  357.                 // $bookmark = Bookmark::where('user_id', $user->id)->where('property_id', $key['id'])->get()->first()   ;
  358.             }
  359.             $properties = json_encode($properties);
  360.         } else {
  361.             $properties = array();
  362.             $properties = json_encode($properties);
  363.         }
  364.  
  365.         if ($request->input('call') == 'ajax' && $request->input('call_page') != 1) {
  366.             /*$total_search = $searchResultArr['last_page'];
  367.             $paginate_html = '<ul class="pager">';
  368.             $current_url = $request->fullUrl();
  369.             $current_url = str_replace('ajax', '', $current_url);
  370.             for($i=1;$i<=$total_search;$i++)
  371.             {
  372.                     $paginate_html .= '<li';
  373.                     if($i==$request->page)
  374.                     {
  375.                             $paginate_html .= ' class="active my-active" ';
  376.                             $paginate_html .='><span>'.$i.'</span></li>';
  377.                     }
  378.                     else
  379.                     {
  380.                             $paginate_html .='><a href="'.$current_url.'&page='.$i.'"><span>'.$i.'</span></a></li>';
  381.                     }
  382.             }
  383.             $paginate_html .='</ul>';*/
  384.             $request->request->add(['call_page' => '1']);
  385.  
  386.             $paginate_html = $searchResultObj->appends(request()->input())->links('pagination')->toHtml();
  387.             $request->request->add(['call_page' => '0']);
  388.         }
  389.  
  390.         //$properties = array();
  391.         $mainlat = "";
  392.         $mainlong = "";
  393.  
  394.  
  395.         if (count($searchFormResult)) {
  396.             $mainlat = $searchFormResult[0]['latitude'];
  397.             $mainlong = $searchFormResult[0]['longitude'];
  398.         }
  399.  
  400.  
  401.         // foreach($searchFormResult as $key => $skey)
  402.         //       {
  403.         //            if ($skey['monthly_price']) {
  404.         //                    $searchFormResult[$key]['monthly_price'] = number_format($skey['monthly_price']);
  405.         //            }
  406.  
  407.         //      $image        = isset($skey['property_images']['0']['path']) ? $skey['property_images']['0']['path'] : "";
  408.         //      //$properties[] = array($key['en_name'], $key['latitude'], $key['longitude'], $key['area'] ,$key['id'], $image, $key['bedrooms'], $key['bathrooms'], $key['sqft'], number_format($key['monthly_price']));
  409.         //      $mainlat      = $skey['latitude'];
  410.         //      $mainlong     = $skey['longitude'];
  411.         //       }
  412.  
  413.         if (empty($mainlat)) {
  414.             $mainlat = "0.000000";
  415.         }
  416.  
  417.         if (empty($mainlong)) {
  418.             $mainlong = "0.000000";
  419.         }
  420.         //$properties = json_encode($properties);
  421.  
  422.         if (!isset($_COOKIE['modern_tokyo_recent_search'])) {
  423.             setcookie('modern_tokyo_recent_search', $url, time() + 60 * 60 * 24 * 365, '/');
  424.         } else {
  425.             unset($_COOKIE['modern_tokyo_recent_search']);
  426.             setcookie('modern_tokyo_recent_search', '', time() - 60 * 60 * 24 * 365, '/');
  427.             setcookie('modern_tokyo_recent_search', $url, time() + 60 * 60 * 24 * 365, '/');
  428.         }
  429.  
  430.  
  431.         if ($request->input('call') == 'ajax' && $request->input('call_page') != 1) {
  432.             $bookmark_property_ids = array();
  433.  
  434.             if (!empty(\Auth::user())) {
  435.                 $bookmark_property_ids = Bookmark::where('user_id', \Auth::user()->id)->pluck('property_id')->toArray();
  436.                 //print_r($bookmark_property_ids);
  437.                 // die();
  438.                 //inside login
  439.             } else {
  440.                 if (isset($_COOKIE['modern_tokyo_bookmark'])) {
  441.                     $bookmark_property_ids = explode('-', $_COOKIE['modern_tokyo_bookmark']);
  442.                     $bookmark_property_ids = array_unique($bookmark_property_ids);
  443.                 }
  444.             }
  445.  
  446.  
  447.             $finaldata['url'] = $url;
  448.             $finaldata['date_range'] = $date_range;
  449.             $finaldata['data_for_input_of_aminities'] = $data_for_input_of_aminities;
  450.             $finaldata['amenity_data'] = $amenity_data;
  451.             $finaldata['property_area_data'] = $property_area_data;
  452.             $finaldata['sqft_min_size'] = $sqft_min_size;
  453.             $finaldata['sqft_max_size'] = $sqft_max_size;
  454.             $finaldata['min_price'] = $min_price;
  455.             $finaldata['max_price'] = $max_price;
  456.             $finaldata['currency'] = $currency;
  457.             $finaldata['searchFormResult'] = $searchFormResult;
  458.             $finaldata['selected_area'] = $selected_area;
  459.             $finaldata['selected_apartments'] = $selected_apartments;
  460.             $finaldata['selected_guest'] = $selected_guest;
  461.             $finaldata['selected_size'] = $selected_size;
  462.             $finaldata['selected_price'] = $selected_price;
  463.             $finaldata['property_apartment_data'] = $property_apartment_data;
  464.             $finaldata['mainlat'] = $mainlat;
  465.             $finaldata['mainlong'] = $mainlong;
  466.             $finaldata['properties'] = $properties;
  467.             $finaldata['searchResultObj'] = $searchResultObj;
  468.             $finaldata['paginate_html'] = $paginate_html;
  469.             $finaldata['bookmarkdata'] = $bookmark_property_ids;
  470.  
  471.  
  472.             return json_encode($finaldata);
  473.  
  474.  
  475.         }
  476.  
  477.         // print_r($address_component);
  478.         // die();
  479.  
  480.  
  481.         //Search::create(['data'=>json_encode(compact('url','date_range','data_for_input_of_aminities','amenity_data','property_area_data', 'sqft_min_size', 'sqft_max_size', 'min_price', 'max_price', 'currency', 'searchFormResult', 'selected_area', 'selected_apartments', 'selected_guest', 'selected_size', 'selected_price', 'property_apartment_data', 'mainlat', 'mainlong', 'properties'))]);
  482.  
  483.         return view('front.list', compact('url', 'address_component', 'date_range', 'data_for_input_of_aminities', 'amenity_data', 'property_area_data', 'sqft_min_size', 'sqft_max_size', 'min_price', 'max_price', 'currency', 'searchFormResult', 'selected_area', 'selected_apartments', 'selected_guest', 'selected_size', 'selected_price', 'property_apartment_data', 'mainlat', 'mainlong', 'properties', 'searchResultObj', 'bookmarkdata'));
  484.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement