/**
  * Creates a new Booking model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Booking();
     $bookingField = new BookingFields();
     $data = Yii::$app->request->post();
     if ($model->load(Yii::$app->request->post())) {
         if ($model->save()) {
             if (isset($data['BookingFields']['fields'])) {
                 $bookingField->fields = Json::encode($data['BookingFields']['fields']);
                 $bookingField->booking_id = $model->id;
                 $bookingField->save();
             }
             return $this->redirect(['view', 'id' => $model->id]);
         }
         return $this->render('create', ['model' => $model]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Creates a new Tour model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($id)
 {
     $tour = $this->findTour($id);
     $booking = new Booking(['tour_id' => $tour->id]);
     if ($booking->load(Yii::$app->request->post())) {
         $fields = ArrayHelper::map($tour->customFields, 'id', 'name');
         $result = [];
         foreach ($_POST['CustomField'] as $key => $field_value) {
             if (!array_key_exists($key, $fields)) {
                 continue;
             }
             $result[] = ['field' => $fields[$key], 'value' => $field_value, 'key' => $key];
         }
         $booking->result = json_encode($result);
         if ($booking->save()) {
             \Yii::$app->getSession()->setFlash('success', 'Thanks for booking our tour. We will contact you soon.');
             return $this->redirect(['tour/view', 'id' => $booking->tour_id]);
         }
     }
     return $this->render('create', ['tour' => $tour, 'booking' => $booking]);
 }
Esempio n. 3
0
 public function actionBook($id)
 {
     $model = new Booking();
     $bookingModelAttributes = ['group_num', 'agency_id'];
     $customFieldsAttributes = array_map(function ($item) {
         return 'field' . $item['id'];
     }, $this->findModel($id)->getFields()->all());
     $dynamicModelAttributes = array_merge($bookingModelAttributes, $customFieldsAttributes);
     $dynamicModel = new DynamicModel($dynamicModelAttributes);
     $dynamicModel->addRule($dynamicModelAttributes, 'required');
     if ($dynamicModel->load(Yii::$app->request->post()) && $dynamicModel->validate()) {
         $attributes = $dynamicModel->getAttributes($bookingModelAttributes);
         $model->link('tour', $this->findModel($id));
         $model->agency_id = $attributes['agency_id'];
         $model->group_num = $attributes['group_num'];
         $model->time = time();
         if ($model->validate()) {
             $fields = [];
             $keys = ['booking_id', 'tour_id', 'field_id', 'value'];
             foreach ($dynamicModel->getAttributes($customFieldsAttributes) as $key => $field) {
                 if (in_array($key, $customFieldsAttributes)) {
                     $fields[] = [$model->id, $id, str_replace('field', '', $key), $field];
                 }
             }
             if (!empty($fields)) {
                 Yii::$app->db->createCommand()->batchInsert('booking_fields', $keys, $fields)->execute();
                 $model->save();
                 return $this->redirect('/booking');
             }
         } else {
             var_dump($model);
             $model->delete();
         }
     }
     return $this->render('book', ['model' => $dynamicModel, 'tourModel' => $this->findModel($id)]);
 }