[ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], ], ], ]; } /** * Lists all Spots models. * @return mixed */ public function actionIndex() { $searchModel = new SpotsSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * Displays a single Spots model. * @param integer $id * @return mixed */ public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new Spots model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Spots(); $hotels = [new Hotel()]; $post_data = Yii::$app->request->post(); if(isset($post_data['Spots']['schools_id'])){ $post_data['Spots']['schools_id'] = implode(',',$post_data['Spots']['schools_id'] ); } if ($model->load($post_data) && $model->save()) { $fields = Yii::$app->request->post('Fields'); if($fields){ foreach($fields as $k => $field){ foreach($field as $row){ $field_model = new Fields(); $field_model->field_name = isset($row['description']) ? $row['description'] : ''; $field_model->value = isset($row['value']) ? $row['value'] : ''; $field_model->table_name = 'Spots'; $field_model->table_id = $model->id; $field_model->field_type = $k; $field_model->save(); } } } $hotels = Yii::$app->request->post('Hotel'); if($hotels){ foreach($hotels as $hotel){ $hotel_model = new Hotel(); $hotel_model->photo = isset($hotel['photo']) ? $hotel['photo'] : ''; $hotel_model->name = isset($hotel['name']) ? $hotel['name'] : ''; $hotel_model->address = isset($hotel['address']) ? $hotel['address'] : ''; $hotel_model->price = isset($hotel['price']) ? $hotel['price'] : ''; $hotel_model->distance = isset($hotel['distance']) ? $hotel['distance'] : ''; $hotel_model->rating = isset($hotel['rating']) ? $hotel['rating'] : ''; $hotel_model->spot_id = $model->id; $hotel_model->save(); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'hotels' => $hotels, 'model' => $model, ]); } } /** * Updates an existing Spots model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $hotels = $this->findHotels($id); $post_data = Yii::$app->request->post(); if(isset($post_data['Spots']['schools_id'])){ $post_data['Spots']['schools_id'] = implode(',',$post_data['Spots']['schools_id'] ); } if ($model->load($post_data) && $model->save()) { $fields = Yii::$app->request->post('Fields'); Fields::deleteAll(['table_id'=>$id, 'table_name'=>'Spots']); if($fields){ foreach($fields as $k => $field){ foreach($field as $row){ $field_model = new Fields(); $field_model->field_name = isset($row['description']) ? $row['description'] : ''; $field_model->value = isset($row['value']) ? $row['value'] : ''; $field_model->table_name = 'Spots'; $field_model->table_id = $model->id; $field_model->field_type = $k; $field_model->save(); } } } $hotels = Yii::$app->request->post('Hotel'); Hotel::deleteAll(['spot_id'=>$id]); if($hotels){ foreach($hotels as $hotel){ $hotel_model = new Hotel(); $hotel_model->photo = isset($hotel['photo']) ? $hotel['photo'] : ''; $hotel_model->name = isset($hotel['name']) ? $hotel['name'] : ''; $hotel_model->address = isset($hotel['address']) ? $hotel['address'] : ''; $hotel_model->price = isset($hotel['price']) ? $hotel['price'] : ''; $hotel_model->distance = isset($hotel['distance']) ? $hotel['distance'] : ''; $hotel_model->rating = isset($hotel['rating']) ? $hotel['rating'] : ''; $hotel_model->spot_id =$model->id; $hotel_model->save(); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'hotels' => $hotels, 'model' => $model, ]); } } /** * Deletes an existing Spots model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $this->findModel($id)->delete(); return $this->redirect(['index']); } /** * Finds the Spots model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Spots the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Spots::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } public function actionDownloadGallery() { $model = new Spots(); if ($model->load(Yii::$app->request->post())) { $model->file_three = UploadedFile::getInstance($model, 'file_three'); $md5_file = md5_file($model->file_three->tempName); $imgDir = Yii::getAlias('@storage/images/'.$md5_file.'/'); $imageAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file_three->extension); $imageAlias_224X189 = Yii::getAlias($imgDir.'224X189'.'.'.$model->file_three->extension); $imageAlias_44X44 = Yii::getAlias($imgDir.'44X44'.'.'.$model->file_three->extension); $imageLink = '/storage/images/'.$md5_file.'/224X189'.'.'.$model->file_three->extension; if(!is_dir($imgDir)) { mkdir($imgDir, 0777, true); } $model->file_three->saveAs($imageAlias); Image::thumbnail($imageAlias, 224, 189 ) ->save(Yii::getAlias($imageAlias_224X189), ['quality' => 50]); Image::thumbnail($imageAlias, 44, 44 ) ->save(Yii::getAlias($imageAlias_44X44), ['quality' => 50]); $view = $this->renderPartial('@app/views/goods/_gallery_item', [ 'item' => ['image'=>$imageLink], ]); return json_encode(['link'=>$imageLink, 'view' =>$view]); } } protected function findHotels($id) { if (($model = Hotel::find()->where(['spot_id'=>$id])->all()) !== null) { return $model; } else { return [new Hotel()]; } } }