frontend/controllers/HotelBookingController.php
/**
     * Creates a new HotelBooking model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new HotelBooking model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new HotelBooking model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new HotelBooking();
        $freeModel = new HotelBooking();
        $locale = Yii::$app->language;
        $base_rooms = HotelRoom::find()->all();
        $base_menus = HotelMenu::find()->all();
        $rooms = [];
        $menus = [];
        foreach ($base_rooms as $room) {
            $rooms[] = [
                'room' => $room,
                'translations' => $room->getHotelRoomTranslations()->where(['locale' => $locale])->one()
            ];
        }
        foreach ($base_menus as $menu) {
            $menus[] = [
                'menu' => $menu,
                'translations' => $menu->getHotelMenuTranslations()->where(['locale' => $locale])->one()
            ];
        }
        $request = Yii::$app->request->post();
        if (isset($request['HotelBooking'])) {
            $book = $request['HotelBooking'];
            $start_date = strtotime($book['start_date']);
            $end_date = strtotime($book['end_date']);
            if ($start_date > $end_date) {
                return $this->render('create', [
                    'model' => $freeModel,
                    'rooms' => $rooms,
                    'menus' => $menus,
                    'success' => false
                ]);
            }
            $hotelBooked = HotelBooking::find()->where([
                'room_id' => $book['room_id']
            ])->andWhere(
                '(start_date <= :startDate and end_date >= :startDate) or 
                            (start_date <= :endDate and end_date >= :endDate) or 
                            (start_date >= :startDate and end_date <= :endDate)'
            )->addParams([
                    ':startDate' => $start_date,
                    ':endDate' => $end_date
                ])->one();
            if ($hotelBooked) {
                return $this->render('create', [
                    'model' => $freeModel,
                    'rooms' => $rooms,
                    'menus' => $menus,
                    'success' => false,
                    'booked_start_date' => date('F j Y', $hotelBooked->start_date),
                    'booked_end_date' => date('F j Y', $hotelBooked->end_date),
                ]);
            } else {
                if ($model->customSave($book)) {
                    return $this->render('create', [
                        'model' => $freeModel,
                        'rooms' => $rooms,
                        'menus' => $menus,
                        'success' => true
                    ]);
                } else {
                    return $this->render('create', [
                        'model' => $freeModel,
                        'rooms' => $rooms,
                        'menus' => $menus,
                        'success' => false
                    ]);
                }
            }
        }
        return $this->render('create', [
            'model' => $model,
            'rooms' => $rooms,
            'menus' => $menus
        ]);
    }
public function actionCreate ( ) { $ model = new HotelBooking ( ) ; $ freeModel = new HotelBooking ( ) ; $ locale = Yii :: $ app -> language ; $ base_rooms = HotelRoom :: find ( ) -> all ( ) ; $ base_menus = HotelMenu :: find ( ) -> all ( ) ; $ rooms = [ ] ; $ menus = [ ] ; foreach ( $ base_rooms as $ room ) { $ rooms [ ] = [ 'room' => $ room , 'translations' => $ room -> getHotelRoomTranslations ( ) -> where ( [ 'locale' => $ locale ] ) -> one ( ) ] ; } foreach ( $ base_menus as $ menu ) { $ menus [ ] = [ 'menu' => $ menu , 'translations' => $ menu -> getHotelMenuTranslations ( ) -> where ( [ 'locale' => $ locale ] ) -> one ( ) ] ; } $ request = Yii :: $ app -> request -> post ( ) ; if ( isset ( $ request [ 'HotelBooking' ] ) ) { $ book = $ request [ 'HotelBooking' ] ; $ start_date = strtotime ( $ book [ 'start_date' ] ) ; $ end_date = strtotime ( $ book [ 'end_date' ] ) ; if ( $ start_date > $ end_date ) { return $ this -> render ( 'create' , [ 'model' => $ freeModel , 'rooms' => $ rooms , 'menus' => $ menus , 'success' => false ] ) ; } $ hotelBooked = HotelBooking :: find ( ) -> where ( [ 'room_id' => $ book [ 'room_id' ] ] ) -> andWhere ( '(start_date <= :startDate and end_date >= :startDate) or 
                            (start_date <= :endDate and end_date >= :endDate) or 
                            (start_date >= :startDate and end_date <= :endDate)' ) -> addParams ( [ ':startDate' => $ start_date , ':endDate' => $ end_date ] ) -> one ( ) ; if ( $ hotelBooked ) { return $ this -> render ( 'create' , [ 'model' => $ freeModel , 'rooms' => $ rooms , 'menus' => $ menus , 'success' => false , 'booked_start_date' => date ( 'F j Y' , $ hotelBooked -> start_date ) , 'booked_end_date' => date ( 'F j Y' , $ hotelBooked -> end_date ) , ] ) ; } else { if ( $ model -> customSave ( $ book ) ) { return $ this -> render ( 'create' , [ 'model' => $ freeModel , 'rooms' => $ rooms , 'menus' => $ menus , 'success' => true ] ) ; } else { return $ this -> render ( 'create' , [ 'model' => $ freeModel , 'rooms' => $ rooms , 'menus' => $ menus , 'success' => false ] ) ; } } } return $ this -> render ( 'create' , [ 'model' => $ model , 'rooms' => $ rooms , 'menus' => $ menus ] ) ; }
Creates a new HotelBooking model.
Creates a new HotelBooking model .
//        var_dump($request);exit;