[ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'actions' => ['logout', 'index','create','update','view','delete','download-img', 'download-gallery' ], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post'], ], ], ]; } /** * Lists all Schools models. * @return mixed */ public function actionIndex() { $searchModel = new SchoolsSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * Displays a single Schools model. * @param integer $id * @return mixed */ public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new Schools model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Schools(); $trainers = [new Trainers()]; $post_data = Yii::$app->request->post(); if(isset($post_data['Schools']['spots_id'])){ $post_data['Schools']['spots_id'] = implode(',',$post_data['Schools']['spots_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(); } } } $trainers = Yii::$app->request->post('Trainers'); if($trainers){ foreach($trainers as $hotel){ $trainers_model = new Trainers(); $trainers_model->photo = isset($hotel['photo']) ? $hotel['photo'] : ''; $trainers_model->name = isset($hotel['name']) ? $hotel['name'] : ''; $trainers_model->description = isset($hotel['description']) ? $hotel['description'] : ''; $trainers_model->school_id = $model->id; $trainers_model->save(); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'hotels' => $trainers, 'model' => $model, ]); } } /** * Updates an existing Schools 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->findTrainers($id); $post_data = Yii::$app->request->post(); if(isset($post_data['Schools']['spots_id'])){ $post_data['Schools']['spots_id'] = implode(',',$post_data['Schools']['spots_id'] ); } if ($model->load($post_data) && $model->save()) { $fields = Yii::$app->request->post('Fields'); Fields::deleteAll(['table_id'=>$id, 'table_name'=>'Schools']); 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 = 'Schools'; $field_model->table_id = $model->id; $field_model->field_type = $k; $field_model->save(); } } } $trainers = Yii::$app->request->post('Trainers'); Trainers::deleteAll(['school_id'=>$id]); if($trainers){ foreach($trainers as $hotel){ $trainers_model = new Trainers(); $trainers_model->photo = isset($hotel['photo']) ? $hotel['photo'] : ''; $trainers_model->name = isset($hotel['name']) ? $hotel['name'] : ''; $trainers_model->description = isset($hotel['description']) ? $hotel['description'] : ''; $trainers_model->school_id = $model->id; $trainers_model->save(); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'hotels' => $hotels, 'model' => $model, ]); } } /** * Deletes an existing Schools 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 Schools model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Schools the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Schools::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } public function actionDownloadImg() { $model = new Schools(); if ($model->load(Yii::$app->request->post())) { $model->file = UploadedFile::getInstance($model, 'file'); $md5_file = md5_file($model->file->tempName); $imgDir = Yii::getAlias('@storage/cars/'.$md5_file.'/'); $imageAlias = Yii::getAlias($imgDir.'160x146'.'.'.$model->file->extension); $imageLink = '/storage/cars/'.$md5_file.'/160x146'.'.'.$model->file->extension; if(!is_dir($imgDir)) { mkdir($imgDir, 0777, true); } $model->file->saveAs($imageAlias); Image::thumbnail($imageAlias, 160, 146, $mode = \Imagine\Image\ManipulatorInterface::THUMBNAIL_INSET) ->save(Yii::getAlias($imageAlias), ['quality' => 50]); return json_encode(['link'=>$imageLink]); } } public function actionDownloadGallery() { $model = new Schools(); 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 findTrainers($id) { if (($model = Trainers::find()->where(['school_id'=>$id])->all()) !== null) { return $model; } else { return [new Trainers()]; } } }