diff --git a/backend/components/base/BaseActiveRecord.php b/backend/components/base/BaseActiveRecord.php index 9d4c19d..c13d9c5 100755 --- a/backend/components/base/BaseActiveRecord.php +++ b/backend/components/base/BaseActiveRecord.php @@ -9,12 +9,16 @@ namespace backend\components\base; -use yii\base\ErrorException; +use common\components\exceptions\OrdinaryActiveRecordException; class BaseActiveRecord extends \yii\db\ActiveRecord { + /** + * @param int $row + * @throws OrdinaryActiveRecordException + * выбрасывает специальное исключения, наполняя сообщение с массива ошибок модели (после попытки валидации) + */ public function throwStringErrorException($row = 0){ - $errors_str = ''; if ($row != 0) { $errors_str = "Ошибка в строке {$row} "; @@ -22,7 +26,9 @@ class BaseActiveRecord extends \yii\db\ActiveRecord { foreach ($this->getErrors() as $error) { $errors_str .= implode( array_values($error) ); } - throw new ErrorException( $errors_str ); + $ex = new OrdinaryActiveRecordException( $errors_str ); + $ex->active_record_name = static::formName(); + throw $ex; } } \ No newline at end of file diff --git a/backend/controllers/BrandsController.php b/backend/controllers/BrandsController.php index 520403a..4af9da2 100755 --- a/backend/controllers/BrandsController.php +++ b/backend/controllers/BrandsController.php @@ -9,6 +9,7 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\filters\AccessControl; +use yii\web\UploadedFile; /** * BrandsController implements the CRUD actions for Brands model. @@ -82,14 +83,8 @@ class BrandsController extends Controller { $model = new Brands(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { - //return $this->redirect(['view', 'id' => $model->BRAND]); - return $this->redirect(['index']); - } else { - return $this->render('create', [ - 'model' => $model, - ]); - } + return $this->saveIMG( $model, 'create' ); + } /** @@ -102,14 +97,8 @@ class BrandsController extends Controller { $model = $this->findModel($id); - if ($model->load(Yii::$app->request->post()) && $model->save()) { - //return $this->redirect(['view', 'id' => $model->BRAND]); - return $this->redirect(['index']); - } else { - return $this->render('update', [ - 'model' => $model, - ]); - } + return $this->saveIMG( $model, 'update' ); + } /** @@ -140,4 +129,31 @@ class BrandsController extends Controller throw new NotFoundHttpException('The requested page does not exist.'); } } + + /** + * @param $model + * @return string|\yii\web\Response + * сохраняет картинку и валидирует модель + * выбрасывает исключение в случае ошибки + */ + protected function saveIMG( $model, $action ){ + if( $model->load(Yii::$app->request->post()) ) { + $model->file = UploadedFile::getInstance($model, 'file'); + $model->IMG = $model->file->name; + // первый проход - валидируем, сохраняем файл, + if ( $model->validate() ) { + // сохраним файл + $model->file->saveAs( Yii::getAlias('@storage') . '/files/brands/' . $model->IMG ); + if ( $model->save() ) { + return $this->redirect(['index']); + } + } + $model->throwStringErrorException(); + } else { + return $this->render($action, [ + 'model' => $model, + ]); + } + + } } diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index 083b6d4..242b64f 100755 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -3,6 +3,7 @@ namespace backend\controllers; use backend\models\Details; use common\components\exceptions\CrossParsingException; +use common\components\exceptions\OrdinaryActiveRecordException; use common\components\exceptions\PriceParsingException; use common\components\exceptions\RgParsingException; use common\components\ModelArrayValidator; @@ -76,6 +77,10 @@ class ParserController extends BaseController $action_name = ['results']; }elseif ( $exception instanceof RgParsingException ) { $action_name = ['rg-grup/results']; + }elseif ( $exception instanceof OrdinaryActiveRecordException ) { + // для обычной модели возвращаемся в index в качестве контроллера используем имя модели + $action_name = strtolower($exception->active_record_name); + $action_name = ["$action_name/index"]; } if ( $exception !== null ) { diff --git a/backend/views/brands/_form.php b/backend/views/brands/_form.php index c0449e3..3807320 100755 --- a/backend/views/brands/_form.php +++ b/backend/views/brands/_form.php @@ -17,14 +17,22 @@ use yii\widgets\ActiveForm; echo $form->field($model, 'BRAND')->textInput(['maxlength' => true])->label('Название') ?> - = $form->field($model, 'CONTENT')->textarea(['rows' => 6]) ?> + = $form->field($model, 'CONTENT')->widget(\mihaildev\ckeditor\CKEditor::className(),[ + 'editorOptions' => \mihaildev\elfinder\ElFinder::ckeditorOptions('elfinder',[ + 'preset' => 'full', //разработанны стандартные настройки basic, standard, full данную возможность не обязательно использовать + 'inline' => false, //по умолчанию false]), + // 'filebrowserUploadUrl'=>Yii::$app->getUrlManager()->createUrl('news/images-upload') + ] + ) + ]); ?> = $form->field($model, 'if_oem')->checkbox() ?> - = $form->field($model, 'IMG')->fileInput() ?> + = $form->field($model, 'file')->fileInput()->label(false)->hint('имя файла должно иметь только латинские символы и цифры',['tag'=>'span'])?>