diff --git a/controllers/BrandController.php b/controllers/BrandController.php index fefa714..f411692 100755 --- a/controllers/BrandController.php +++ b/controllers/BrandController.php @@ -2,9 +2,11 @@ namespace artweb\artbox\ecommerce\controllers; + use artweb\artbox\ecommerce\models\BrandSize; use Yii; use artweb\artbox\ecommerce\models\Brand; use artweb\artbox\ecommerce\models\BrandSearch; + use yii\data\ActiveDataProvider; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; @@ -172,11 +174,35 @@ return $this->redirect([ 'index' ]); } + public function actionSize($id) + { + $model = $this->findModel($id); + + $dataProvider = new ActiveDataProvider( + [ + 'query' => BrandSize::find() + ->where( + [ + 'brand_id' => $id, + ] + ), + ] + ); + + return $this->render( + 'size', + [ + 'model' => $model, + 'dataProvider' => $dataProvider, + ] + ); + } + public function actionDeleteImage($id) { $model = $this->findModel($id); - $model->image = null; - $model->updateAttributes(['image']); + $model->image = NULL; + $model->updateAttributes([ 'image' ]); return true; } @@ -194,7 +220,7 @@ if (( $model = Brand::find() ->with('lang') ->where([ 'id' => $id ]) - ->one() ) !== null + ->one() ) !== NULL ) { return $model; } else { diff --git a/controllers/BrandSizeController.php b/controllers/BrandSizeController.php index f4596f1..0e6edb4 100755 --- a/controllers/BrandSizeController.php +++ b/controllers/BrandSizeController.php @@ -1,124 +1,209 @@ [ - 'class' => VerbFilter::className(), - 'actions' => [ - 'delete' => ['POST'], + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => [ 'POST' ], + ], ], - ], - ]; - } - - /** - * Lists all BrandSize models. - * @return mixed - */ - public function actionIndex() - { - $searchModel = new BrandSizeSearch(); - $dataProvider = $searchModel->search(Yii::$app->request->queryParams); - - return $this->render('index', [ - 'searchModel' => $searchModel, - 'dataProvider' => $dataProvider, - ]); - } - - /** - * Displays a single BrandSize model. - * @param integer $id - * @return mixed - */ - public function actionView($id) - { - return $this->render('view', [ - 'model' => $this->findModel($id), - ]); - } - - /** - * Creates a new BrandSize model. - * If creation is successful, the browser will be redirected to the 'view' page. - * @return mixed - */ - public function actionCreate() - { - $model = new BrandSize(); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->id]); - } else { - return $this->render('create', [ - 'model' => $model, - ]); + ]; } - } - - /** - * Updates an existing BrandSize 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); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->id]); - } else { - return $this->render('update', [ - 'model' => $model, - ]); + + /** + * Lists all BrandSize models. + * + * @return mixed + */ + public function actionIndex() + { + $searchModel = new BrandSizeSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render( + 'index', + [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ] + ); } - } - - /** - * Deletes an existing BrandSize 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 BrandSize model based on its primary key value. - * If the model is not found, a 404 HTTP exception will be thrown. - * @param integer $id - * @return BrandSize the loaded model - * @throws NotFoundHttpException if the model cannot be found - */ - protected function findModel($id) - { - if (($model = BrandSize::findOne($id)) !== null) { - return $model; - } else { - throw new NotFoundHttpException('The requested page does not exist.'); + + /** + * Displays a single BrandSize model. + * + * @param integer $id + * + * @return mixed + */ + public function actionView($id) + { + return $this->render( + 'view', + [ + 'model' => $this->findModel($id), + ] + ); + } + + /** + * Creates a new BrandSize model. + * If creation is successful, the browser will be redirected to the 'view' page. + * + * @return mixed + */ + public function actionCreate() + { + $model = new BrandSize(); + + $categories = ArrayHelper::map( + Category::find() + ->with('lang') + ->asArray() + ->all(), + 'id', + 'lang.title' + ); + + $brands = ArrayHelper::map( + Brand::find() + ->with('lang') + ->asArray() + ->all(), + 'id', + 'lang.title' + ); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect( + [ + 'view', + 'id' => $model->id, + ] + ); + } else { + return $this->render( + 'create', + [ + 'model' => $model, + 'categories' => $categories, + 'brands' => $brands, + ] + ); + } + } + + /** + * Updates an existing BrandSize 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); + + $categories = ArrayHelper::map( + Category::find() + ->with('lang') + ->asArray() + ->all(), + 'id', + 'lang.title' + ); + + $brands = ArrayHelper::map( + Brand::find() + ->with('lang') + ->asArray() + ->all(), + 'id', + 'lang.title' + ); + + if (!empty(\Yii::$app->request->post('BrandSize')[ 'categories' ])) { + $model->unlinkAll('categories', true); + foreach (\Yii::$app->request->post('BrandSize')[ 'categories' ] as $item) { + if ($category = Category::findOne($item)) { + $model->link('categories', $category); + } + } + } + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect( + [ + 'view', + 'id' => $model->id, + ] + ); + } else { + return $this->render( + 'update', + [ + 'model' => $model, + 'categories' => $categories, + 'brands' => $brands, + ] + ); + } + } + + /** + * Deletes an existing BrandSize 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 BrandSize model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * + * @param integer $id + * + * @return BrandSize the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (( $model = BrandSize::findOne($id) ) !== NULL) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } } } -} diff --git a/models/BrandSize.php b/models/BrandSize.php index a87854a..d13a07e 100755 --- a/models/BrandSize.php +++ b/models/BrandSize.php @@ -59,8 +59,8 @@ class BrandSize extends \yii\db\ActiveRecord { return [ 'id' => Yii::t('app', 'ID'), - 'brand_id' => Yii::t('app', 'Brand ID'), - 'image' => Yii::t('app', 'Image'), + 'brand_id' => Yii::t('app', 'Брэнд'), + 'image' => Yii::t('app', 'Сетка'), ]; } diff --git a/views/brand-size/_form.php b/views/brand-size/_form.php index 9eb062c..dee388c 100755 --- a/views/brand-size/_form.php +++ b/views/brand-size/_form.php @@ -9,9 +9,11 @@ use artweb\artbox\components\artboximage\ArtboxImageHelper; /** - * @var View $this - * @var BrandSize $model + * @var View $this + * @var BrandSize $model * @var ActiveForm $form + * @var array $categories + * @var array $brands */ ?> @@ -20,7 +22,20 @@ = $form->field($model, 'brand_id') - ->textInput() ?> + ->widget( + Select2::className(), + [ + 'data' => $brands, + 'language' => 'ru', + 'options' => [ + 'placeholder' => Yii::t('product', 'Select categories'), + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ] + ) ?> = $form->field($model, 'image') ->widget( @@ -65,7 +80,7 @@ ->widget( Select2::className(), [ - 'data' => [], + 'data' => $categories, 'language' => 'ru', 'options' => [ 'placeholder' => Yii::t('product', 'Select categories'), @@ -75,7 +90,8 @@ 'allowClear' => true, ], ] - )->label(\Yii::t('app', 'Категории')) ?> + ) + ->label(\Yii::t('app', 'Категории')) ?>
- = Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> - = Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], [ - 'class' => 'btn btn-danger', - 'data' => [ - 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), - 'method' => 'post', +
+ = Html::a( + Yii::t('app', 'Update'), + [ + 'update', + 'id' => $model->id, + ], + [ 'class' => 'btn btn-primary' ] + ) ?> + = Html::a( + Yii::t('app', 'Delete'), + [ + 'delete', + 'id' => $model->id, + ], + [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), + 'method' => 'post', + ], + ] + ) ?> +
+ + = DetailView::widget( + [ + 'model' => $model, + 'attributes' => [ + 'id', + 'brand_id', + 'image', ], - ]) ?> - - - = DetailView::widget([ - 'model' => $model, - 'attributes' => [ - 'id', - 'brand_id', - 'image', - ], - ]) ?> + ] + ) ?>+ = Html::a(Yii::t('app', 'Create Brand Size'), [ 'create' ], [ 'class' => 'btn btn-success' ]) ?> +
+ = GridView::widget( + [ + 'dataProvider' => $dataProvider, + 'columns' => [ + [ 'class' => 'yii\grid\SerialColumn' ], + + 'id', + 'brand_id', + 'image', + + [ 'class' => 'yii\grid\ActionColumn' ], + ], + ] + ); ?> +