static function sendRequestToHotelProvider($hotelSearchParams) { Yii::import('site.frontend.models.*'); Yii::import('site.frontend.components.*'); $hotelClient = new HotelBookClient(); $startTime = microtime(true); $variants = $hotelClient->fullHotelSearch($hotelSearchParams); $endTime = microtime(true); $startTime = microtime(true); self::storeToCache($hotelSearchParams, $variants); $endTime = microtime(true); $startTime = microtime(true); Yii::app()->hotelsRating->injectRating($variants->hotels, $hotelSearchParams->city); $endTime = microtime(true); //} $results = array(); if ($variants->responseStatus == ResponseStatus::ERROR_CODE_NO_ERRORS) { $startTime = microtime(true); $stack = new HotelStack($variants); $results = $stack->groupBy('hotelId', 117501)->mergeStepV2()->groupBy('rubPrice')->getJsonObject(1); $stack->saveHotelDb(); $nStack = new HotelStack(); $nStack->_hotels = $stack->getHotels(1); $resultsHotels = $stack->getJsonObject(); foreach ($resultsHotels['hotels'] as $i => $info) { $hotelInfo = $hotelClient->hotelDetail($info['hotelId']); if ($hotelInfo) { $hotelsDetails[$info['hotelId'] . 'd'] = self::prepare($hotelInfo); } } /* $query = array(); foreach ($resultsHotels['hotels'] as $i => $info) { $query[$info['hotelId']] = $hotelClient->hotelDetail($info['hotelId'], true); } $hotelClient->processAsyncRequests(); $endTime = microtime(true); Header('ExecutionTimeSortAndDetails:' . ($endTime - $startTime)); $hotelsDetails = array(); foreach ($query as $hotelId => $responseId) { if (isset($hotelClient->requests[$responseId]['result'])) { $hotelsDetails[$hotelId . 'd'] = self::prepare($hotelClient->requests[$responseId]['result']); //Yii::app()->cache->set('HotelDetails-' . $hotelId, $hotelsDetails[$hotelId . 'd']); } }/**/ $results['hotelsDetails'] = $hotelsDetails; return $results; } elseif ($variants->responseStatus == ResponseStatus::ERROR_CODE_EMPTY) { $results['hotels'] = array(); return $results; } else { return false; } }
public function run() { Yii::import('site.common.modules.hotel.models.*'); $hotelForm = new HotelForm(); if (isset($_REQUEST['HotelForm'])) { $hotelForm->attributes = $_REQUEST['HotelForm']; $rooms = array(); if (isset($_REQUEST['HotelRoomForm'])) { foreach ($_REQUEST['HotelRoomForm'] as $i => $info) { $room = new HotelRoomForm(); $room->attributes = $info; if ($room->validate()) { $rooms[] = $room; } } } $hotelForm->rooms = $rooms; if ($hotelForm->validate()) { $hotelSearchParams = new HotelSearchParams(); $hotelSearchParams->checkIn = date('Y-m-d', strtotime($hotelForm->fromDate)); $hotelSearchParams->city = City::getCityByPk($hotelForm->cityId); $hotelSearchParams->duration = $hotelForm->duration; foreach ($hotelForm->rooms as $room) { if ($room->childCount == 1) { $hotelSearchParams->addRoom($room->adultCount, $room->cots, $room->childAge); } else { $hotelSearchParams->addRoom($room->adultCount, $room->cots, false); } } $HotelClient = new HotelBookClient(); $pCacheId = md5(serialize($hotelSearchParams)); Yii::app()->pCache->set('hotelSearchParams' . $pCacheId, $hotelSearchParams, appParams('hotel_search_cache_time')); $resultSearch = $HotelClient->fullHotelSearch($hotelSearchParams); Yii::app()->hotelsRating->injectRating($resultSearch->hotels, $hotelSearchParams->city); $cacheId = substr(md5(uniqid('', true)), 0, 10); Yii::app()->cache->set('hotelResult' . $cacheId, $resultSearch, appParams('hotel_search_cache_time')); Yii::app()->cache->set('hotelSearchParams' . $cacheId, $hotelSearchParams, appParams('hotel_search_cache_time')); Yii::app()->cache->set('hotelForm' . $cacheId, $hotelForm, appParams('hotel_search_cache_time')); if ($resultSearch['hotels']) { $hotelStack = new HotelStack($resultSearch); $results = $hotelStack->groupBy('hotelId')->groupBy('roomSizeId')->groupBy('rubPrice')->sortBy('rubPrice', 2)->getJsonObject(); echo json_encode(array('pCacheId' => $pCacheId, 'cacheId' => $cacheId, 'hotels' => $results)); } else { echo json_encode(array('cacheId' => $cacheId, 'hotels' => array())); } } else { //invalid form throw new CHttpException(500, CHtml::errorSummary($hotelForm)); } Yii::app()->end(); } else { throw new CHttpException(404); } }
/** * Do HotelSearch requests with all combinations of room types, * @param HotelSearchParams $hotelSearchParams * @return HotelSearchResponse */ public function fullHotelSearch(HotelSearchParams $hotelSearchParams) { $rooms = $hotelSearchParams->rooms; //Make combinations to combinations Array uasort($rooms, 'HotelBookClient::compareArrayAdultCount'); $combinations = array(); foreach ($rooms as $key => $room) { $rooms[$key]['sizeCount'] = count(self::$roomSizeRoomTypesMap[$room['adultCount']]); $rooms[$key]['sizeIndex'] = 0; } $allCombined = false; // Make ALL possible combinations while (!$allCombined) { $combination = array(); $allCombined = true; foreach ($rooms as $key => $room) { if ($room['sizeCount'] !== $room['sizeIndex'] + 1) { $allCombined = false; } $rooms[$key]['roomSizeId'] = self::$roomSizeRoomTypesMap[$room['adultCount']][$room['sizeIndex']]; $combination[] = array('roomSizeId' => $rooms[$key]['roomSizeId'], 'child' => $rooms[$key]['childCount'], 'cots' => $rooms[$key]['cots'], 'ChildAge' => $rooms[$key]['childAge']); } sort($combination); $combinations[] = $combination; if (!$allCombined) { //next possible state $overflow = false; $iterationComplete = false; foreach ($rooms as $key => $room) { if ($room['sizeCount'] == 1) { continue; } if ($iterationComplete) { if ($overflow) { if ($room['sizeCount'] == $room['sizeIndex'] + 1) { $rooms[$key]['sizeIndex'] = 0; } else { $rooms[$key]['sizeIndex']++; $overflow = false; break; } } else { break; } } else { if ($room['sizeCount'] == $room['sizeIndex'] + 1) { $rooms[$key]['sizeIndex'] = 0; $iterationComplete = true; $overflow = true; } else { $rooms[$key]['sizeIndex']++; break; } } } } } //delete same combinations sort($combinations); foreach ($combinations as $key => $combination) { if (!isset($prevComb)) { $prevComb = $combination; continue; } if ($prevComb == $combination) { unset($combinations[$key]); } $prevComb = $combination; } unset($prevComb); unset($combination); //add requests to queue self::$groupId = substr(md5(uniqid('', true)), 0, 10); $params = array('cityId' => $hotelSearchParams->city->hotelbookId, 'checkIn' => $hotelSearchParams->checkIn, 'duration' => $hotelSearchParams->duration); if (!$params['cityId']) { throw new CException('City ' . $hotelSearchParams->city->localEn . ' does not linked with hotelBookId. You cannot find any hotels there'); } foreach ($combinations as $key => $combination) { $params['rooms'] = array(); foreach ($combination as $i => $room) { if (!isset($prevInd)) { $prevInd = $i; $roomNumber = 1; continue; } if ($combination[$i] === $combination[$prevInd]) { $roomNumber++; continue; } else { $combination[$prevInd]['roomNumber'] = $roomNumber; $params['rooms'][] = $combination[$prevInd]; $prevInd = $i; $roomNumber = 1; } } $combination[$prevInd]['roomNumber'] = $roomNumber; $params['rooms'][] = $combination[$prevInd]; //print_r($params); unset($prevInd); $this->hotelSearch($params, true); } //run all requests $startTime = microtime(true); $this->processAsyncRequests(); $endTime = microtime(true); //Header('ExecutionTimeProcessAsync:'.($endTime - $startTime)); self::$groupId = null; $hotels = array(); $errorDescriptions = array(); foreach ($this->requests as $request) { //echo count($request['result']->hotels).'<br>'; //die(); if (isset($request['result'])) { if ($request['result']->hotels) { foreach ($request['result']->hotels as $hotel) { $key = $hotel->key; if (isset($hotels[$key])) { //echo '--duplicate'; //echo 'have:'; //VarDumper::dump($hotels[$key]); //echo 'new:'; //VarDumper::dump($hotel); //echo "key: $key"; } $hotels[$key] = $hotel; } } if ($request['result']->errorsDescriptions) { foreach ($request['result']->errorsDescriptions as $desc) { $errorDescriptions[] = $desc; } } } else { print_r($request); die; } } //die(); $response = new HotelSearchResponse(); if ($hotels) { $response->responseStatus = ResponseStatus::ERROR_CODE_NO_ERRORS; } else { if (!$errorDescriptions) { $response->responseStatus = ResponseStatus::ERROR_CODE_EMPTY; } else { $response->responseStatus = ResponseStatus::ERROR_CODE_EXTERNAL; $response->errorsDescriptions = $errorDescriptions; } } /* //print_r($combinations); print_r(count($hotels)); print_r($errorDescriptions);*/ if ($hotels) { //echo "pre results count:".count($hotels); if (count($hotelSearchParams->rooms) == 1) { // O_o foreach ($hotelSearchParams->rooms as $room) { break; } if ($room['adultCount'] == 2 && $room['childCount'] == 0 && $room['cots'] == 0) { $allHotelStack = new HotelStack(array('hotels' => $hotels)); //echo "post results count:".count($allHotelStack->_hotels); //die(); //VarDumper::dump($allHotelStack);die(); $allHotelStack->groupBy('categoryId')->groupBy('roomSizeId')->groupBy('roomTypeId')->groupBy('centerDistance')->groupBy('rubPrice'); //$allHotelStack->groupBy('categoryId')->groupBy('centerDistance')->groupBy('rubPrice'); //print_r($allHotelStack); //die(); //VarDumper::dump($allHotelStack);die(); //VarDumper::dump($hotelStack->hotelStacks); foreach ($allHotelStack->hotelStacks as $categoryId => $hotelStack) { //categoryId - star rating (we need 3..5 stars) if ($categoryId == Hotel::STARS_THREE || $categoryId == Hotel::STARS_FOUR || $categoryId == Hotel::STARS_FIVE) { //echo "category: $categoryId<br>"; //VarDumper::dump($hotelStack); die(); $haveStack = false; foreach ($hotelStack->hotelStacks as $i => $hotelStackSize) { //VarDumper::dump($i); //echo "roomSizeId: $i<br>"; //todo: move to room class if (!in_array($i, array(appParams('HotelBook.room.DBL'), appParams('HotelBook.room.TWIN')))) { $hotelStack->deleteStackWithIndex("{$i}"); //unset($hotelStack->hotelStacks[$i]); } else { //echo "in 2"; foreach ($hotelStack->hotelStacks[$i]->hotelStacks as $j => $hotelStackType) { //echo "roomTypeId: $j<br>"; if (!in_array($j, appParams('HotelBook.room.STD'))) { $hotelStack->deleteStackWithIndex("{$i},{$j}"); //unset($hotelStack->hotelStacks[$i]->hotelStacks[$j]); } else { //echo "in 3"; foreach ($hotelStack->hotelStacks[$i]->hotelStacks[$j]->hotelStacks as $k => $hotelStackDistance) { //echo "distance: $k"; if ($k > appParams('HotelBook.distanceFromCityCenter')) { //echo "out $k"; $hotelStack->deleteStackWithIndex("{$i},{$j},{$k}"); //unset($hotelStack->hotelStacks[$i]->hotelStacks[$j]->hotelStacks[$k]); } else { //echo "in 4"; $haveStack = true; } } } } } } if ($haveStack) { $jsonObject = $hotelStack->sortBy('rubPrice', 5)->getHotel()->getJsonObject(); $jsonObject['cityId'] = $hotelSearchParams->city->id; $jsonObject['dateFrom'] = $hotelSearchParams->checkIn; $from = DateTime::createFromFormat('Y-m-d', $hotelSearchParams->checkIn); $jsonObject['dateTo'] = $from->add(new DateInterval('P' . $hotelSearchParams->duration . 'D'))->format('Y-m-d'); Yii::log('TRYING TO SAVE HOTEL CACHE'); $hotelCache = new HotelCache(); $hotelCache->populateFromJsonObject($jsonObject); $hotelCache->save(); } else { //echo "havent"; //VarDumper::dump($hotelStack->getJsonObject(5)); } } } } } $response->hotels = $hotels; } return $response; }
public function actionInfo($cacheId, $hotelId) { Yii::import('site.common.modules.hotel.models.*'); Yii::trace('my great message here', 'hotelController'); $hotelSearchParams = Yii::app()->cache->get('hotelSearchParams' . $cacheId); $resultSearch = Yii::app()->cache->get('hotelResult' . $cacheId); if ($resultSearch) { $hotelStack = new HotelStack($resultSearch); $hotelStack->groupBy('hotelId')->groupBy('roomShowName')->groupBy('rubPrice')->sortBy('rubPrice', 2); $resultsRecommended = $hotelStack->hotelStacks[$hotelId]->getAsJson(); $HotelClient = new HotelBookClient(); $hotels = $HotelClient->hotelSearchFullDetails($hotelSearchParams, $hotelId); $hotelStackFull = new HotelStack(array('hotels' => $hotels)); $resultsAll = $hotelStackFull->getAsJson(); $hotelInfo = $HotelClient->hotelDetail($hotelId); $this->render('resultInfo', array('items' => $this->generateItems(), 'autosearch' => false, 'cityName' => $hotelSearchParams->city->localRu, 'hotelInfo' => $hotelInfo, 'resultsRecommended' => $resultsRecommended, 'resultsAll' => $resultsAll, 'cacheId' => $cacheId)); } else { $this->redirect('/booking/hotel/'); } }