diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index 18affef..bf74208 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -61,8 +61,17 @@ */ public function actionView($id) { + $model = $this->findModel($id); + $categories = $model->getCategories() + ->with('lang') + ->all(); + $variants = $model->getVariants() + ->with('lang') + ->all(); return $this->render('view', [ - 'model' => $this->findModel($id), + 'model' => $this->findModel($id), + 'categories' => $categories, + 'variants' => $variants, ]); } @@ -257,7 +266,6 @@ 'method' => $model->type, 'target' => $target, ]); - // $model->$method(); } else { $model->errors[] = 'File can not be upload or other error'; } @@ -318,12 +326,6 @@ return $this->render('export', [ 'model' => $model, ]); - // $model = new Export(); - // if(( $file = $model->process(Yii::getAlias('@uploadDir')) )) { - // return Yii::$app->response->sendFile($file) - // ->send(); - // } - // throw new NotFoundHttpException('Error'); } /** @@ -337,7 +339,11 @@ */ protected function findModel($id) { - if(( $model = Product::findOne($id) ) !== NULL) { + if(( $model = Product::find() + ->where([ 'product_id' => $id ]) + ->with('lang') + ->one() ) !== NULL + ) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); diff --git a/common/modules/product/models/ProductSearch.php b/common/modules/product/models/ProductSearch.php index 6503697..57f8add 100755 --- a/common/modules/product/models/ProductSearch.php +++ b/common/modules/product/models/ProductSearch.php @@ -17,7 +17,9 @@ public $category_id; - public $variant_sku; + public $product_name; + + public $variant_count; public function behaviors() { @@ -35,7 +37,9 @@ { return [ [ - [ 'variant_sku' ], + [ + 'product_name', + ], 'safe', ], [ @@ -43,7 +47,6 @@ 'brand_id', 'product_id', 'category_id', - 'brand_id', ], 'integer', ], @@ -58,6 +61,18 @@ ]; } + public function attributeLabels() + { + $labels = parent::attributeLabels(); + $new_labels = [ + 'category_id' => 'Category ID', + 'brand_id' => 'Brand ID', + 'product_name' => 'Product name', + 'variant_count' => 'Variant count', + ]; + return array_merge($labels, $new_labels); + } + /** * @inheritdoc */ @@ -79,9 +94,14 @@ $query = Product::find(); + $query->select([ + 'product.*', + 'COUNT(product_variant.product_variant_id) as count', + ]); + $query->joinWith([ 'categories', - /*'variant'*/ + 'lang', ]) ->joinWith([ 'brand' => function($query) { @@ -90,9 +110,14 @@ */ $query->joinWith('lang'); }, - ]); + ]) + ->joinWith('variants'); - $query->groupBy([ 'product.product_id', 'brand_lang.name' ]); + $query->groupBy([ + 'product.product_id', + 'brand_lang.name', + 'product_lang.name', + ]); $dataProvider = new ActiveDataProvider([ 'query' => $query, @@ -104,11 +129,19 @@ $dataProvider->setSort([ 'attributes' => [ - 'brand_id' => [ + 'product_id', + 'product_name' => [ + 'asc' => [ 'product_lang.name' => SORT_ASC ], + 'desc' => [ 'product_lang.name' => SORT_DESC ], + ], + 'brand_id' => [ 'asc' => [ 'brand_lang.name' => SORT_ASC ], 'desc' => [ 'brand_lang.name' => SORT_DESC ], 'default' => SORT_DESC, - 'label' => 'Brand name', + ], + 'variant_count' => [ + 'asc' => [ 'count' => SORT_ASC ], + 'desc' => [ 'count' => SORT_DESC ], ], ], ]); @@ -133,9 +166,11 @@ 'product.product_id' => $this->product_id, 'product_category.category_id' => $this->category_id, ]); - - // $query->andFilterWhere(['ilike', 'brand.name', $this->brand_name]); - // $query->andFilterWhere(['ilike', 'product_variant.sku', $this->variant_sku]); + $query->andFilterWhere([ + 'like', + 'product_lang.name', + $this->product_name, + ]); return $dataProvider; } diff --git a/common/modules/product/views/manage/index.php b/common/modules/product/views/manage/index.php index 66ee363..938b841 100755 --- a/common/modules/product/views/manage/index.php +++ b/common/modules/product/views/manage/index.php @@ -3,16 +3,20 @@ use common\modules\product\models\Brand; use common\modules\product\models\Category; use common\modules\product\models\Product; + use common\modules\product\models\ProductSearch; + use yii\data\ActiveDataProvider; use yii\helpers\Html; use yii\grid\GridView; use kartik\select2\Select2; use common\components\artboxtree\ArtboxTreeHelper; use common\modules\product\helpers\ProductHelper; + use yii\web\View; - /* @var $this yii\web\View */ - /* @var $searchModel common\modules\product\models\ProductSearch */ - /* @var $dataProvider yii\data\ActiveDataProvider */ - + /** + * @var View $this + * @var ProductSearch $searchModel + * @var ActiveDataProvider $dataProvider + */ $this->title = Yii::t('product', 'Products'); $this->params[ 'breadcrumbs' ][] = $this->title; ?> @@ -29,13 +33,25 @@ 'columns' => [ 'product_id', [ - 'label' => Yii::t('product', 'Brand'), + 'attribute' => 'product_name', + 'value' => 'lang.name', + ], + [ + 'label' => Yii::t('product', 'Brand'), 'attribute' => 'brand_id', - 'value' => 'brand.lang.name', - 'filter' => Select2::widget([ + 'value' => 'brand.lang.name', + 'filter' => Select2::widget([ 'model' => $searchModel, 'attribute' => 'brand_id', - 'data' => Brand::find()->joinWith('lang')->select(['brand_lang.name', 'brand.brand_id'])->asArray()->indexBy('brand_id')->column(), + 'data' => Brand::find() + ->joinWith('lang') + ->select([ + 'brand_lang.name', + 'brand.brand_id', + ]) + ->asArray() + ->indexBy('brand_id') + ->column(), 'language' => 'ru', 'options' => [ 'placeholder' => Yii::t('product', 'Select brand'), @@ -54,7 +70,9 @@ * @var Product $model */ $categories = []; - foreach($model->getCategories()->with('lang')->all() as $category) { + foreach($model->getCategories() + ->with('lang') + ->all() as $category) { /** * @var Category $category */ @@ -76,18 +94,15 @@ ], ]), ], - // [ - // 'label' => Yii::t('product', 'SKU'), - // 'attribute' => 'variant_sku', - // 'value' => 'variant.sku', - // ], - // 'variant.price', - // 'variant.price_old', - // [ - // 'label' => Yii::t('product', 'Stock'), - // 'attribute' => 'variant_stock', - // 'value' => 'variant.stock_caption', - // ], + [ + 'attribute' => 'variant_count', + 'value' => function($model) { + /** + * @var Product $model + */ + return count($model->variants); + }, + ], [ 'class' => 'yii\grid\ActionColumn', 'template' => '{items} {view} |{is_top} {is_new} {akciya} | {update} {delete}', @@ -158,6 +173,8 @@ 'id' => $model->product_id, ]); break; + default: + return ''; } }, ], diff --git a/common/modules/product/views/manage/view.php b/common/modules/product/views/manage/view.php index 2b9c56b..6bc195e 100755 --- a/common/modules/product/views/manage/view.php +++ b/common/modules/product/views/manage/view.php @@ -1,37 +1,76 @@ title = $model->product_id; -$this->params['breadcrumbs'][] = ['label' => Yii::t('product', 'Products'), 'url' => ['index']]; -$this->params['breadcrumbs'][] = $this->title; + + use common\modules\product\models\Category; + use common\modules\product\models\Product; + use yii\helpers\ArrayHelper; + use yii\helpers\Html; + use yii\web\View; + use yii\widgets\DetailView; + + /** + * @var View $this + * @var Product $model + * @var Category[] $categories + */ + + $this->title = $model->lang->name; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => Yii::t('product', 'Products'), + 'url' => [ 'index' ], + ]; + $this->params[ 'breadcrumbs' ][] = $this->title; ?>
- = Html::a(Yii::t('product', 'Update'), ['update', 'id' => $model->product_id], ['class' => 'btn btn-primary']) ?> - = Html::a(Yii::t('product', 'Delete'), ['delete', 'id' => $model->product_id], [ + = Html::a(Yii::t('product', 'Update'), [ + 'update', + 'id' => $model->product_id, + ], [ 'class' => 'btn btn-primary' ]) ?> + = Html::a(Yii::t('product', 'Delete'), [ + 'delete', + 'id' => $model->product_id, + ], [ 'class' => 'btn btn-danger', - 'data' => [ + 'data' => [ 'confirm' => Yii::t('product', 'Are you sure you want to delete this item?'), - 'method' => 'post', + 'method' => 'post', ], ]) ?>
- + = DetailView::widget([ - 'model' => $model, + 'model' => $model, 'attributes' => [ 'product_id', -// 'brand.name', -// 'category.name', - 'image.imageUrl:image' + 'brand.lang.name', + [ + 'label' => \Yii::t('app', 'Categories'), + 'value' => implode('