Example #1
0
 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);
     }
 }
Example #2
0
 public function actionIndex($isTab = false)
 {
     Yii::import('site.common.modules.hotel.models.*');
     $hotelForm = new HotelForm();
     if (isset($_POST['ajax']) && $_POST['ajax'] === 'hotel-form') {
         echo CActiveForm::validate($hotelForm);
         Yii::app()->end();
     }
     if (isset($_POST['HotelForm'])) {
         $hotelForm->attributes = $_POST['HotelForm'];
         $rooms = array();
         if (isset($_POST['HotelRoomForm'])) {
             foreach ($_POST['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();
             $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'));
             Yii::app()->user->setState('hotel.cacheId', $cacheId);
             //die();
             $this->redirect('/booking/hotel/result/cacheId/' . $cacheId);
         } else {
             $this->render('index', array('items' => $this->generateItems(), 'hotelForm' => $hotelForm, 'autosearch' => false, 'cityName' => '', 'duration' => 1));
         }
     } else {
         if ($isTab) {
             $this->renderPartial('index', array('items' => $this->generateItems(), 'hotelForm' => $hotelForm, 'autosearch' => false, 'cityName' => '', 'duration' => 1));
         } else {
             $this->render('index', array('items' => $this->generateItems(), 'hotelForm' => $hotelForm, 'autosearch' => false, 'cityName' => '', 'duration' => 1));
         }
     }
 }
Example #3
0
 /**
  * @param string city
  * @param string checkIn d.m.Y date
  * @param int duration # of nights inside hotel
  * @param array $rooms
  *  [Х][adt] - amount of adults inside room,
  *  [Х][chd] - amount of childs inside room,
  *  [Х][chdAge] - age of child inside room,
  *  [Х][cots] - cots inside room (0 or 1),
  */
 public function actionDefault($city, $checkIn, $duration, array $rooms, $format = 'json')
 {
     $hotelSearchParams = new HotelSearchParams();
     $hotelSearchParams->checkIn = date('Y-m-d', strtotime($checkIn));
     $possibleCities = CityManager::getCitiesWithHotels($city);
     if (!empty($possibleCities)) {
         $hotelSearchParams->city = City::model()->findByPk($possibleCities[0]['id']);
     } else {
         $city = CityManager::getCities($city);
         if (!empty($city)) {
             $hotelSearchParams->city = City::model()->findByPk($city[0]['id']);
         }
     }
     $hotelSearchParams->duration = $duration;
     foreach ($rooms as $i => $room) {
         if ($room['chd'] == 1) {
             $hotelSearchParams->addRoom($room['adt'], $room['cots'], $room['chdAge']);
         } elseif ($room['chd'] == 0) {
             $hotelSearchParams->addRoom($room['adt'], $room['cots'], false);
         } else {
             $this->sendError(200, 'Only 0 or 1 child at one hotel room accepted');
             Yii::app()->end();
         }
     }
     if (empty($possibleCities)) {
         $this->results = array('hotels' => array(), 'hotelsDetails' => array());
     } else {
         $this->results = HotelManager::sendRequestToHotelProvider($hotelSearchParams);
         if (!$this->results) {
             $this->results = array();
         }
     }
     $cacheId = md5(serialize($hotelSearchParams));
     $this->results['cacheId'] = $cacheId;
     $this->results['searchParams'] = $hotelSearchParams->getJsonObject();
     if ($format == 'json') {
         $this->sendJson($this->results);
     } elseif ($format == 'xml') {
         $this->sendXml($this->results, 'hotelSearchResults');
     } else {
         $this->sendError(400, 'Incorrect response format');
         Yii::app()->end();
     }
 }
 private function coverageForCity($city)
 {
     $hotelSearchParams = new HotelSearchParams();
     $hotelSearchParams->checkIn = date('Y-m-d', strtotime('10.08.2012'));
     $hotelSearchParams->city = $city;
     $hotelSearchParams->duration = 1;
     $hotelSearchParams->addRoom(1, 0, false);
     $HotelClient = new HotelBookClient();
     $resultSearch = $HotelClient->fullHotelSearch($hotelSearchParams);
     Yii::app()->hotelsRating->injectRating($resultSearch->hotels, $hotelSearchParams->city);
     if (!@count($resultSearch['hotels'])) {
         return 'No results';
     }
     $total = 0;
     $rated = 0;
     foreach ($resultSearch['hotels'] as $hotel) {
         $total++;
         if ($hotel->rating && $hotel->rating !== '-') {
             $rated++;
         }
     }
     return "{$rated}/{$total}" . '=' . intval($rated / $total * 100) . '%';
 }
Example #5
0
 public function fillFromSearchParams(HotelSearchParams $searchParams)
 {
     $this->searchParams = $searchParams;
     $this->city = $searchParams->city;
     $this->checkIn = $searchParams->checkIn;
     $this->checkOut = $searchParams->getCheckout();
     $this->rooms = array();
     foreach ($searchParams->rooms as $room) {
         $newRoom = array('adt' => $room['adultCount'], 'chd' => $room['childCount'], 'chdAge' => $room['childAge'], 'cots' => $room['cots']);
         $this->rooms[] = $newRoom;
     }
 }