Exemplo n.º 1
0
 public function getSearchResults($start, $limit, $ordering, $direction, $ignorePagination = false, $jsonResult = false)
 {
     $this->currentOrdering = $ordering;
     $this->currentDirection = $direction;
     $params = $this->getState('params');
     $searchid = $params['searchid'];
     $newsearch = $params['newsearch'];
     $pricerange = $params['pricerange'];
     $merchantResults = $params['merchantResults'];
     $condominiumsResults = $params['condominiumsResults'];
     $sessionkey = 'search.' . $searchid . '.results';
     $session = JFactory::getSession();
     $results = null;
     if ($newsearch == "0") {
         $cachedresults = $session->get($sessionkey);
         //$_SESSION[$sessionkey];
         try {
             if (isset($cachedresults) && !empty($cachedresults)) {
                 $results = (array) json_decode(gzuncompress(base64_decode($cachedresults)));
             }
             //			echo 'sessionkey: ',  $sessionkey, "<br />";
             //$results = $cachedresults;
         } catch (Exception $e) {
             //			echo 'Exception: ',   $e->getMessage(), "<br />";
             //echo 'Caught exception: ',  $e->getMessage(), "\n";
         }
     } else {
         BFCHelper::setFilterSearchParamsSession(null);
     }
     if ($results == null) {
         //			echo 'No result: <br />';
         $options = array('path' => $this->urlSearchAllCalculate, 'data' => array('$format' => 'json', 'topRresult' => 0, 'lite' => 1));
         $this->applyDefaultFilter($options);
         $url = $this->helper->getQuery($options);
         $results = null;
         $r = $this->helper->executeQuery($url);
         if (isset($r)) {
             $res = json_decode($r);
             //				$results = $res->d->results ?: $res->d;
             if (!empty($res->d->results)) {
                 $results = $res->d->results;
             } elseif (!empty($res->d)) {
                 $results = $res->d;
             }
             try {
                 if (!empty($results)) {
                     //						shuffle($results);
                     $resultsCat = array();
                     $resultsBook = array();
                     $resultsCat = array_filter($results, function ($result) {
                         return $result->IsCatalog;
                     });
                     if (!empty($resultsCat)) {
                         shuffle($resultsCat);
                         $resultsBook = array_filter($results, function ($result) {
                             return !$result->IsCatalog;
                         });
                         if (!empty($resultsBook)) {
                             shuffle($resultsBook);
                             $results = array_merge($resultsBook, $resultsCat);
                         } else {
                             $results = $resultsCat;
                         }
                     } else {
                         shuffle($results);
                     }
                 }
             } catch (Exception $e) {
                 //echo 'Caught exception: ',  $e->getMessage(), "\n";
             }
         }
         // purge last searchid. clears unusable session data
         //			$lastsearchid = $session->get('search.last', '', 'com_bookingforconnector');
         //
         //			if ($lastsearchid != '') {
         //				$this->purgeSessionValues($session, "booking");
         //				$session->set('search.last','', 'com_bookingforconnector');
         //				// purge static searchresult
         //				//BFCHelper::setSearchResult($lastsearchid, null);
         //			}
         // saves parameters into session
         BFCHelper::setSearchParamsSession($params);
         //			if(!empty($results)){
         //				if($pricerange !=='0' && strpos($pricerange,'|') !== false ){ // se ho un valore per pricerange diverso da 0 allora splitto per ;
         //					$priceranges = explode("|", $pricerange);
         //					$pricemin = $priceranges[0];
         //					$pricemax = $priceranges[1];
         //
         //
         //					// price min filtering
         //					if ($pricemin > 0) {
         //						$results = array_filter($results, function($result) use ($pricemin) {
         //							return $result->Price >= $pricemin;
         //						});
         //					}
         //					// price max filtering
         //					if ($pricemax > 0) {
         //						$results = array_filter($results, function($result) use ($pricemax) {
         //							return $result->Price <= $pricemax;
         //						});
         //					}
         //				}
         //			}
         $onlystay = $params['onlystay'];
         if (!empty($results) && $onlystay == '1') {
             $results = array_filter($results, function ($result) {
                 //					return $result->Price >0 ;
                 return $result->IsCatalog || !$result->IsCatalog && $result->Price > 0;
             });
         }
         try {
             // save current search in session to disable all further calculations upon reordering and filtering
             $compr = base64_encode(gzcompress(json_encode($results), true));
         } catch (Exception $e) {
             echo 'Caught exception: ', $e->getMessage(), "\n";
         }
         $session->set($sessionkey, $compr);
         //ciclo per filtrare i possibili filtri
         $filtersenabled = array();
         $filtersenabled['count'] = 0;
         if (!empty($results)) {
             $filtersenabled['count'] = count($results);
             //per condominiumsResults non \E8 necessario filtrare i condomini per caratteristiche, si basano sulle risorse
             if ($merchantResults) {
                 //					$tmpstars = array_unique(array_map(function ($i) { return $i->MrcRating; }, $results));
                 //					$filtersenabled['stars'] = implode(',',$tmpstars);
                 $filtersenabled['stars'] = array_count_values(array_filter(array_map(function ($i) {
                     return $i->MrcRating;
                 }, $results)));
                 //					$tmplocationzones = array_unique(array_map(function ($i) { return $i->MrcZoneId; }, $results));
                 //					$filtersenabled['locationzones'] = implode(',',$tmplocationzones);
                 $filtersenabled['locationzones'] = array_count_values(array_map(function ($i) {
                     return $i->MrcZoneId;
                 }, $results));
             } else {
                 //					$tmpstars = array_unique(array_map(function ($i) { return $i->ResRating; }, $results));
                 //					$filtersenabled['stars'] = implode(',',$tmpstars);
                 $filtersenabled['stars'] = array_count_values(array_filter(array_map(function ($i) {
                     return $i->ResRating;
                 }, $results)));
                 //					$tmplocationzones = array_unique(array_map(function ($i) { return $i->ResZoneId; }, $results));
                 //					$filtersenabled['locationzones'] = implode(',',$tmplocationzones);
                 $filtersenabled['locationzones'] = array_count_values(array_map(function ($i) {
                     return $i->ResZoneId;
                 }, $results));
                 $tmpRooms = array_count_values(array_map(function ($i) {
                     return $i->Rooms;
                 }, $results));
                 //					asort($tmpRooms);
                 ksort($tmpRooms);
                 //					$filtersenabled['rooms'] = implode(',',$tmpRooms);
                 $filtersenabled['rooms'] = $tmpRooms;
             }
             //				$tmpRateplanName = array_unique(array_map(function ($i) { return $i->RateplanName; }, $results));
             //				$filtersenabled['rateplanname'] = implode(',',$tmpRateplanName);
             $filtersenabled['rateplanname'] = array_count_values(array_filter(array_map(function ($i) {
                 return $i->RateplanName;
             }, $results)));
             //				$tmpmastertypologies = array_unique(array_map(function ($i) { return $i->MasterTypologyId; }, $results));
             //				$filtersenabled['mastertypologies'] = implode(',',$tmpmastertypologies);
             $filtersenabled['mastertypologies'] = array_count_values(array_map(function ($i) {
                 return $i->MasterTypologyId;
             }, $results));
             // elenco merchantGroup presenti nella ricerca
             //				$tmpmerchantgroups = array_unique(explode(",",array_reduce($results,
             //						function($returnedList, $item){
             //							$val =  preg_replace('/\s+/', '', $item->MrcTagsIdList);
             //							if (!empty($val)) {
             //								$returnedList .= "," .$val;
             //							}
             //							return $returnedList;
             //						}
             //						)));
             //				foreach( $tmpmerchantgroups as $key => $value ) {
             //					if( empty( $tmpmerchantgroups[ $key ] ) )
             //						unset( $tmpmerchantgroups[ $key ] );
             //				}
             //				$filtersenabled['merchantgroups'] = implode(',',$tmpmerchantgroups);
             $tmpmerchantgroups = array_count_values(explode(",", array_reduce($results, function ($returnedList, $item) {
                 $val = preg_replace('/\\s+/', '', $item->MrcTagsIdList);
                 if (!empty($val)) {
                     $returnedList .= "," . $val;
                 }
                 $resval = preg_replace('/\\s+/', '', $item->TagsIdList);
                 if (!empty($resval)) {
                     $returnedList .= "," . $resval;
                 }
                 return $returnedList;
             })));
             //				foreach( $tmpmerchantgroups as $key => $value ) {
             //					if( empty( $tmpmerchantgroups[ $key ] ) )
             //						unset( $tmpmerchantgroups[ $key ] );
             //				}
             $filtersenabled['merchantgroups'] = $tmpmerchantgroups;
             // elenco Servizi presenti nella ricerca
             //				$tmpservices = array_unique(explode(",",array_reduce($results,
             //						function($returnedList, $item){
             //							$val =  preg_replace('/\s+/', '', $item->MrcServiceIdList);
             //							if (!empty($val)) {
             //								$returnedList .= "," .$val;
             //							}
             //							$val =  preg_replace('/\s+/', '', $item->ResServiceIdList);
             //							if (!empty($val)) {
             //								$returnedList .= "," .$val;
             //							}
             //
             //							return $returnedList;
             //						}
             //				)));
             //				foreach( $tmpservices as $key => $value ) {
             //					if( empty( $tmpservices[ $key ] ) )
             //						unset( $tmpservices[ $key ] );
             //				}
             //				$filtersenabled['services'] = implode(',',$tmpservices);
             $tmpservices = array_count_values(explode(",", array_reduce($results, function ($returnedList, $item) {
                 $val = preg_replace('/\\s+/', '', $item->ResServiceIdList);
                 if (!empty($val)) {
                     $returnedList .= "," . $val;
                 }
                 return $returnedList;
             })));
             //				foreach( $tmpservices as $key => $value ) {
             //					if( empty( $tmpservices[ $key ] ) )
             //						unset( $tmpservices[ $key ] );
             //				}
             $filtersenabled['services'] = $tmpservices;
             $tmpservicesmerchants = array_count_values(explode(",", array_reduce($results, function ($returnedList, $item) {
                 $val = preg_replace('/\\s+/', '', $item->MrcServiceIdList);
                 if (!empty($val)) {
                     $returnedList .= "," . $val;
                 }
                 return $returnedList;
             })));
             $filtersenabled['servicesmerchants'] = $tmpservicesmerchants;
             // elenco BookingType presenti nella ricerca
             //				bookableonly
             //				$tmpbookingtype = array_unique(array_map(function ($i) { return $i->BookingType; }, $results));
             //				$tmpbookingtype = array_unique(array_map(function ($i) { return $i->IsBookable; }, $results));
             //				foreach( $tmpbookingtype as $key => $value ) {
             //					if( empty( $tmpbookingtype[ $key ] ) )
             //						unset( $tmpbookingtype[ $key ] );
             //				}
             //				$filtersenabled['bookingtypes'] = implode(',',$tmpbookingtype);
             $filtersenabled['bookingtypes'] = array_count_values(array_filter(array_map(function ($i) {
                 return intval($i->IsBookable);
             }, $results)));
             //				$tmpoffers = array_unique(array_map(function ($i) { return $i->TotalPrice>$i->Price; }, $results));
             //				$tmpoffers = array_unique(array_map(function ($i) { return !empty($i->DiscountId); }, $results));
             //				$tmpoffers = array_unique(array_map(function ($i) { return $i->IsOffer; }, $results));
             //				foreach( $tmpoffers as $key => $value ) {
             //					if( empty( $tmpoffers[ $key ] ) )
             //						unset( $tmpoffers[ $key ] );
             //				}
             //				$filtersenabled['offers'] = implode(',',$tmpoffers);
             $tmpoffers = array_count_values(array_filter(array_map(function ($i) {
                 return intval($i->IsOffer && $i->TotalPrice > $i->Price);
             }, $results)));
             $filtersenabled['offers'] = $tmpoffers;
             $prices = array_map(function ($i) {
                 return $i->Price;
             }, array_filter($results, function ($rs) {
                 return !$rs->IsCatalog;
             }));
             //				$prices = array_map(function ($i) { return $i->Price; }, $results) ;
             if (!empty($prices)) {
                 $filtersenabled['pricemin'] = round(min($prices) - 1, 0, PHP_ROUND_HALF_DOWN);
                 $filtersenabled['pricemax'] = round(max($prices) + 1, 0, PHP_ROUND_HALF_UP);
             }
         }
         BFCHelper::setEnabledFilterSearchParamsSession($filtersenabled);
     }
     $results = $this->filterResults($results);
     // ordering is taking place here only for simple results, merchants are ordered by the grouping function
     if (!empty($results)) {
         if (isset($ordering) && !$merchantResults) {
             $catValues = array();
             foreach ($results as $key => $row) {
                 $catValues[$key] = $row->IsCatalog;
             }
             switch (strtolower($ordering)) {
                 case 'stay':
                     $pricesValues = array();
                     foreach ($results as $key => $row) {
                         $pricesValues[$key] = $row->TotalPrice;
                     }
                     array_multisort($catValues, SORT_ASC, $pricesValues, $direction == 'desc' ? SORT_DESC : SORT_ASC, $results);
                     //					usort($results, function($a,$b) use ( $ordering, $direction) {
                     //						return BFCHelper::orderBy($a, $b, 'TotalPrice', $direction);
                     //					});
                     break;
                 case 'rooms':
                     $RoomsValues = array();
                     foreach ($results as $key => $row) {
                         $RoomsValues[$key] = $row->Rooms;
                     }
                     array_multisort($catValues, SORT_ASC, $RoomsValues, $direction == 'desc' ? SORT_DESC : SORT_ASC, $results);
                     //					usort($results, function($a,$b) use ( $ordering, $direction) {
                     //						return BFCHelper::orderBy($a, $b, 'Rooms', $direction);
                     //					});
                     break;
                 case 'offer':
                     $discountValues = array();
                     foreach ($results as $key => $row) {
                         $discountValues[$key] = $row->PercentVariation;
                     }
                     array_multisort($catValues, SORT_ASC, $discountValues, $direction == 'desc' ? SORT_DESC : SORT_ASC, $results);
                     //					usort($results, function($a,$b) use ( $ordering, $direction) {
                     ////						return BFCHelper::orderBySingleDiscount($a, $b, $direction);
                     //						return BFCHelper::orderBy($a, $b, 'PercentVariation', $direction);
                     //					});
                     break;
                 default:
                     $randomRes = array();
                     foreach ($results as $key => $row) {
                         $randomRes[$key] = $key;
                     }
                     array_multisort($catValues, SORT_ASC, $randomRes, SORT_ASC, $results);
                     break;
             }
             //			} else {
             //			usort($results, function($a,$b) use ( $ordering, $direction) {
             //				return $a->IsCatalog - $b->IsCatalog;
             //			});
         }
     }
     if ($condominiumsResults && !empty($results)) {
         // grouping and ordering
         $results = $this->groupResultsByCondominium($results);
     }
     if ($merchantResults && !empty($results)) {
         // grouping and ordering
         $results = $this->groupResultsByMerchant($results, $ordering, $direction);
     }
     $this->count = count($results);
     if (!$ignorePagination && isset($start) && (isset($limit) && $limit > 0) && !empty($results)) {
         $results = array_slice($results, $start, $limit);
         $params = $this->getState('params');
         $checkin = $params['checkin'];
         $duration = $params['duration'];
         $persons = $params['paxes'];
         $paxages = $params['paxages'];
     }
     if ($jsonResult && !empty($results)) {
         $arr = array();
         foreach ($results as $result) {
             $val = new StdClass();
             if ($merchantResults) {
                 $val->MerchantId = $result->MerchantId;
                 $val->XGooglePos = $result->XGooglePos;
                 $val->YGooglePos = $result->YGooglePos;
             } elseif ($condominiumsResults) {
                 $val->Resource = new StdClass();
                 $val->Resource->ResourceId = $result->CondominiumId;
                 $val->Resource->XGooglePos = $result->XGooglePos;
                 $val->Resource->YGooglePos = $result->YGooglePos;
             } else {
                 $val->Resource = new StdClass();
                 $val->Resource->ResourceId = $result->ResourceId;
                 $val->Resource->XGooglePos = $result->ResLat;
                 $val->Resource->YGooglePos = $result->ResLng;
             }
             $arr[] = $val;
         }
         return json_encode($arr);
     }
     return $results;
     //return $jsonResult ? json_encode($results) : $results;
 }