public function actionCreate($slug = null) { $model = new File(); if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } else { if ($fileInstanse = UploadedFile::getInstance($model, 'file')) { $model->file = $fileInstanse; if ($model->validate(['file'])) { $model->file = Upload::file($fileInstanse, 'files', false); $model->size = $fileInstanse->size; if ($model->save()) { $this->flash('success', Yii::t('easyii/file', 'File created')); return $this->redirect(['/admin/' . $this->module->id]); } else { $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors())); } } else { $this->flash('error', Yii::t('easyii/file', 'File error. {0}', $model->formatErrors())); } } else { $this->flash('error', Yii::t('yii', '{attribute} cannot be blank.', ['attribute' => $model->getAttributeLabel('file')])); } return $this->refresh(); } } else { if ($slug) { $model->slug = $slug; } return $this->render('create', ['model' => $model]); } }
public function actionCreate($slug = null) { $model = new File(); $model->loadTranslations_custom(); if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } else { /*if(($fileInstanse = UploadedFile::getInstance($model, 'file'))) { $model->file = $fileInstanse; if($model->validate(['file'])){ $model->file = Upload::file($fileInstanse, 'files', false); $model->size = $fileInstanse->size; if($model->save()){ $this->flash('success', Yii::t('easyii/file', 'File created')); return $this->redirect(['/admin/'.$this->module->id]); } else{ $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors())); } } else { $this->flash('error', Yii::t('easyii/file', 'File error. {0}', $model->formatErrors())); } } else { $this->flash('error', Yii::t('yii', '{attribute} cannot be blank.', ['attribute' => $model->getAttributeLabel('file')])); }*/ //return $this->refresh(); foreach (array_keys(Yii::$app->params['mlConfig']['languages']) as $lang) { $file_lang = $lang != Yii::$app->params['mlConfig']['default_language'] ? 'file_' . $lang : 'file'; if ($fileInstanse = UploadedFile::getInstance($model, $file_lang)) { $model->{$file_lang} = $fileInstanse; if ($model->validate([$file_lang])) { $model->{$file_lang} = Upload::file($fileInstanse, 'files', false); } else { $this->flash('error', Yii::t('easyii/file', 'File error. {0}', $model->formatErrors())); } } } if ($model->save()) { $this->flash('success', Yii::t('easyii/cooperation', 'Cooperation created')); return $this->redirect(['/admin/' . $this->module->id]); } else { $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors())); return $this->refresh(); } } } else { if ($slug) { $model->slug = $slug; } return $this->render('create', ['model' => $model]); } }