Commit 99e2f4e7965e7792bdea15454039d0b9b243918a
1 parent
b6b59902
Category to purpose image
Showing
14 changed files
with
904 additions
and
100 deletions
Show diff stats
backend/controllers/CategoryToPurposeController.php
0 → 100755
1 | +<?php | |
2 | + | |
3 | + namespace backend\controllers; | |
4 | + | |
5 | + use common\modules\language\models\Language; | |
6 | + use common\modules\product\models\Category; | |
7 | + use common\modules\rubrication\models\TaxGroup; | |
8 | + use common\modules\rubrication\models\TaxOption; | |
9 | + use Yii; | |
10 | + use common\models\CategoryToPurpose; | |
11 | + use common\models\CategoryToPurposeSearch; | |
12 | + use yii\web\Controller; | |
13 | + use yii\web\NotFoundHttpException; | |
14 | + use yii\filters\VerbFilter; | |
15 | + | |
16 | + /** | |
17 | + * CategoryToPurposeController implements the CRUD actions for CategoryToPurpose model. | |
18 | + */ | |
19 | + class CategoryToPurposeController extends Controller | |
20 | + { | |
21 | + /** | |
22 | + * @inheritdoc | |
23 | + */ | |
24 | + public function behaviors() | |
25 | + { | |
26 | + return [ | |
27 | + 'verbs' => [ | |
28 | + 'class' => VerbFilter::className(), | |
29 | + 'actions' => [ | |
30 | + 'delete' => [ 'POST' ], | |
31 | + ], | |
32 | + ], | |
33 | + ]; | |
34 | + } | |
35 | + | |
36 | + /** | |
37 | + * Lists all CategoryToPurpose models. | |
38 | + * | |
39 | + * @return mixed | |
40 | + */ | |
41 | + public function actionIndex() | |
42 | + { | |
43 | + $searchModel = new CategoryToPurposeSearch(); | |
44 | + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); | |
45 | + | |
46 | + return $this->render( | |
47 | + 'index', | |
48 | + [ | |
49 | + 'searchModel' => $searchModel, | |
50 | + 'dataProvider' => $dataProvider, | |
51 | + ] | |
52 | + ); | |
53 | + } | |
54 | + | |
55 | + /** | |
56 | + * Displays a single CategoryToPurpose model. | |
57 | + * | |
58 | + * @param integer $category_id | |
59 | + * @param integer $purpose_id | |
60 | + * | |
61 | + * @return mixed | |
62 | + */ | |
63 | + public function actionView($category_id, $purpose_id) | |
64 | + { | |
65 | + return $this->render( | |
66 | + 'view', | |
67 | + [ | |
68 | + 'model' => $this->findModel($category_id, $purpose_id), | |
69 | + ] | |
70 | + ); | |
71 | + } | |
72 | + | |
73 | + /** | |
74 | + * Creates a new CategoryToPurpose model. | |
75 | + * If creation is successful, the browser will be redirected to the 'view' page. | |
76 | + * | |
77 | + * @return mixed | |
78 | + */ | |
79 | + public function actionCreate() | |
80 | + { | |
81 | + $model = new CategoryToPurpose(); | |
82 | + | |
83 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { | |
84 | + return $this->redirect( | |
85 | + [ | |
86 | + 'view', | |
87 | + 'category_id' => $model->category_id, | |
88 | + 'purpose_id' => $model->purpose_id, | |
89 | + ] | |
90 | + ); | |
91 | + } else { | |
92 | + | |
93 | + $language_id = Language::getDefaultLanguage()->language_id; | |
94 | + | |
95 | + $categories = Category::find() | |
96 | + ->innerJoinWith('lang') | |
97 | + ->select( | |
98 | + [ | |
99 | + 'category_lang.name', | |
100 | + 'category.category_id', | |
101 | + ] | |
102 | + ) | |
103 | + ->where([ 'category_lang.language_id' => $language_id ]) | |
104 | + ->indexBy('category_id') | |
105 | + ->column(); | |
106 | + $purposes = TaxOption::find() | |
107 | + ->innerJoinWith('taxGroup') | |
108 | + ->where( | |
109 | + [ | |
110 | + 'tax_group.tax_group_id' => 5, | |
111 | + 'level' => 0, | |
112 | + 'tax_option_lang.language_id' => $language_id, | |
113 | + ] | |
114 | + ) | |
115 | + ->joinWith('lang') | |
116 | + ->select( | |
117 | + [ | |
118 | + 'tax_option_lang.value', | |
119 | + 'tax_option.tax_option_id', | |
120 | + ] | |
121 | + ) | |
122 | + ->indexBy('tax_option_id') | |
123 | + ->column(); | |
124 | + | |
125 | + return $this->render( | |
126 | + 'create', | |
127 | + [ | |
128 | + 'model' => $model, | |
129 | + 'categories' => $categories, | |
130 | + 'purposes' => $purposes, | |
131 | + ] | |
132 | + ); | |
133 | + } | |
134 | + } | |
135 | + | |
136 | + /** | |
137 | + * Updates an existing CategoryToPurpose model. | |
138 | + * If update is successful, the browser will be redirected to the 'view' page. | |
139 | + * | |
140 | + * @param integer $category_id | |
141 | + * @param integer $purpose_id | |
142 | + * | |
143 | + * @return mixed | |
144 | + */ | |
145 | + public function actionUpdate($category_id, $purpose_id) | |
146 | + { | |
147 | + $model = $this->findModel($category_id, $purpose_id); | |
148 | + | |
149 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { | |
150 | + return $this->redirect( | |
151 | + [ | |
152 | + 'view', | |
153 | + 'category_id' => $model->category_id, | |
154 | + 'purpose_id' => $model->purpose_id, | |
155 | + ] | |
156 | + ); | |
157 | + } else { | |
158 | + | |
159 | + $language_id = Language::getDefaultLanguage()->language_id; | |
160 | + | |
161 | + $categories = Category::find() | |
162 | + ->innerJoinWith('lang') | |
163 | + ->select( | |
164 | + [ | |
165 | + 'category_lang.name', | |
166 | + 'category.category_id', | |
167 | + ] | |
168 | + ) | |
169 | + ->where([ 'category_lang.language_id' => $language_id ]) | |
170 | + ->indexBy('category_id') | |
171 | + ->column(); | |
172 | + $purposes = TaxOption::find() | |
173 | + ->innerJoinWith('taxGroup') | |
174 | + ->where( | |
175 | + [ | |
176 | + 'tax_group.tax_group_id' => 5, | |
177 | + 'level' => 0, | |
178 | + 'tax_option_lang.language_id' => $language_id, | |
179 | + ] | |
180 | + ) | |
181 | + ->joinWith('lang') | |
182 | + ->select( | |
183 | + [ | |
184 | + 'tax_option_lang.value', | |
185 | + 'tax_option.tax_option_id', | |
186 | + ] | |
187 | + ) | |
188 | + ->indexBy('tax_option_id') | |
189 | + ->column(); | |
190 | + | |
191 | + return $this->render( | |
192 | + 'update', | |
193 | + [ | |
194 | + 'model' => $model, | |
195 | + 'categories' => $categories, | |
196 | + 'purposes' => $purposes, | |
197 | + ] | |
198 | + ); | |
199 | + } | |
200 | + } | |
201 | + | |
202 | + /** | |
203 | + * Deletes an existing CategoryToPurpose model. | |
204 | + * If deletion is successful, the browser will be redirected to the 'index' page. | |
205 | + * | |
206 | + * @param integer $category_id | |
207 | + * @param integer $purpose_id | |
208 | + * | |
209 | + * @return mixed | |
210 | + */ | |
211 | + public function actionDelete($category_id, $purpose_id) | |
212 | + { | |
213 | + $this->findModel($category_id, $purpose_id) | |
214 | + ->delete(); | |
215 | + | |
216 | + return $this->redirect([ 'index' ]); | |
217 | + } | |
218 | + | |
219 | + /** | |
220 | + * Finds the CategoryToPurpose model based on its primary key value. | |
221 | + * If the model is not found, a 404 HTTP exception will be thrown. | |
222 | + * | |
223 | + * @param integer $category_id | |
224 | + * @param integer $purpose_id | |
225 | + * | |
226 | + * @return CategoryToPurpose the loaded model | |
227 | + * @throws NotFoundHttpException if the model cannot be found | |
228 | + */ | |
229 | + protected function findModel($category_id, $purpose_id) | |
230 | + { | |
231 | + if (( $model = CategoryToPurpose::findOne( | |
232 | + [ | |
233 | + 'category_id' => $category_id, | |
234 | + 'purpose_id' => $purpose_id, | |
235 | + ] | |
236 | + ) ) !== null | |
237 | + ) { | |
238 | + return $model; | |
239 | + } else { | |
240 | + throw new NotFoundHttpException('The requested page does not exist.'); | |
241 | + } | |
242 | + } | |
243 | + } | ... | ... |
1 | +<?php | |
2 | + | |
3 | + use yii\helpers\Html; | |
4 | + use yii\widgets\ActiveForm; | |
5 | + | |
6 | + /** | |
7 | + * @var yii\web\View $this | |
8 | + * @var common\models\CategoryToPurpose $model | |
9 | + * @var array $purposes | |
10 | + * @var array $categories | |
11 | + * @var ActiveForm $form | |
12 | + **/ | |
13 | +?> | |
14 | + | |
15 | +<div class="category-to-purpose-form"> | |
16 | + | |
17 | + <?php $form = ActiveForm::begin([ 'options' => [ 'enctype' => 'multipart/form-data' ] ]); ?> | |
18 | + | |
19 | + <?= $form->field($model, 'category_id') | |
20 | + ->dropDownList($categories) ?> | |
21 | + | |
22 | + <?= $form->field($model, 'purpose_id') | |
23 | + ->dropDownList($purposes) ?> | |
24 | + | |
25 | + <?= $form->field($model, 'image') | |
26 | + ->widget( | |
27 | + \kartik\file\FileInput::className(), | |
28 | + [ | |
29 | + 'language' => 'ru', | |
30 | + 'options' => [ | |
31 | + 'accept' => 'image/*', | |
32 | + 'multiple' => false, | |
33 | + ], | |
34 | + 'pluginOptions' => [ | |
35 | + 'allowedFileExtensions' => [ | |
36 | + 'jpg', | |
37 | + 'gif', | |
38 | + 'png', | |
39 | + ], | |
40 | + 'initialPreview' => !empty( $model->getImageUrl( | |
41 | + 0, | |
42 | + false | |
43 | + ) ) ? \common\components\artboximage\ArtboxImageHelper::getImage( | |
44 | + $model->imageUrl, | |
45 | + 'list' | |
46 | + ) : '', | |
47 | + 'overwriteInitial' => true, | |
48 | + 'showRemove' => false, | |
49 | + 'showUpload' => false, | |
50 | + 'previewFileType' => 'image', | |
51 | + ], | |
52 | + ] | |
53 | + ); ?> | |
54 | + | |
55 | + <div class="form-group"> | |
56 | + <?= Html::submitButton( | |
57 | + $model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), | |
58 | + [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ] | |
59 | + ) ?> | |
60 | + </div> | |
61 | + | |
62 | + <?php ActiveForm::end(); ?> | |
63 | + | |
64 | +</div> | ... | ... |
1 | +<?php | |
2 | + | |
3 | +use yii\helpers\Html; | |
4 | +use yii\widgets\ActiveForm; | |
5 | + | |
6 | +/* @var $this yii\web\View */ | |
7 | +/* @var $model common\models\CategoryToPurposeSearch */ | |
8 | +/* @var $form yii\widgets\ActiveForm */ | |
9 | +?> | |
10 | + | |
11 | +<div class="category-to-purpose-search"> | |
12 | + | |
13 | + <?php $form = ActiveForm::begin([ | |
14 | + 'action' => ['index'], | |
15 | + 'method' => 'get', | |
16 | + ]); ?> | |
17 | + | |
18 | + <?= $form->field($model, 'category_id') ?> | |
19 | + | |
20 | + <?= $form->field($model, 'purpose_id') ?> | |
21 | + | |
22 | + <?= $form->field($model, 'image') ?> | |
23 | + | |
24 | + <div class="form-group"> | |
25 | + <?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?> | |
26 | + <?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?> | |
27 | + </div> | |
28 | + | |
29 | + <?php ActiveForm::end(); ?> | |
30 | + | |
31 | +</div> | ... | ... |
1 | +<?php | |
2 | + | |
3 | + use yii\helpers\Html; | |
4 | + | |
5 | + /** | |
6 | + * @var yii\web\View $this | |
7 | + * @var common\models\CategoryToPurpose $model | |
8 | + * @var array $purposes | |
9 | + * @var array $categories | |
10 | + **/ | |
11 | + | |
12 | + $this->title = Yii::t('app', 'Create Category To Purpose'); | |
13 | + $this->params[ 'breadcrumbs' ][] = [ | |
14 | + 'label' => Yii::t('app', 'Category To Purposes'), | |
15 | + 'url' => [ 'index' ], | |
16 | + ]; | |
17 | + $this->params[ 'breadcrumbs' ][] = $this->title; | |
18 | +?> | |
19 | +<div class="category-to-purpose-create"> | |
20 | + | |
21 | + <h1><?= Html::encode($this->title) ?></h1> | |
22 | + | |
23 | + <?= $this->render( | |
24 | + '_form', | |
25 | + [ | |
26 | + 'model' => $model, | |
27 | + 'categories' => $categories, | |
28 | + 'purposes' => $purposes, | |
29 | + ] | |
30 | + ) ?> | |
31 | + | |
32 | +</div> | ... | ... |
1 | +<?php | |
2 | + | |
3 | + use common\models\CategoryToPurpose; | |
4 | + use yii\helpers\Html; | |
5 | +use yii\grid\GridView; | |
6 | + | |
7 | +/* @var $this yii\web\View */ | |
8 | +/* @var $searchModel common\models\CategoryToPurposeSearch */ | |
9 | +/* @var $dataProvider yii\data\ActiveDataProvider */ | |
10 | + | |
11 | +$this->title = Yii::t('app', 'Category To Purposes'); | |
12 | +$this->params['breadcrumbs'][] = $this->title; | |
13 | +?> | |
14 | +<div class="category-to-purpose-index"> | |
15 | + | |
16 | + <h1><?= Html::encode($this->title) ?></h1> | |
17 | + | |
18 | + <p> | |
19 | + <?= Html::a(Yii::t('app', 'Create Category To Purpose'), ['create'], ['class' => 'btn btn-success']) ?> | |
20 | + </p> | |
21 | + <?= GridView::widget([ | |
22 | + 'dataProvider' => $dataProvider, | |
23 | + 'filterModel' => $searchModel, | |
24 | + 'columns' => [ | |
25 | + ['class' => 'yii\grid\SerialColumn'], | |
26 | + | |
27 | + [ | |
28 | + 'attribute' => 'category_id', | |
29 | + 'value' => function($model) { | |
30 | + /** | |
31 | + * @var CategoryToPurpose $model | |
32 | + */ | |
33 | + return $model->category->lang->name; | |
34 | + } | |
35 | + ], | |
36 | + [ | |
37 | + 'attribute' => 'purpose_id', | |
38 | + 'value' => function($model) { | |
39 | + /** | |
40 | + * @var CategoryToPurpose $model | |
41 | + */ | |
42 | + return $model->purpose->lang->value; | |
43 | + } | |
44 | + ], | |
45 | + [ | |
46 | + 'attribute' => 'image', | |
47 | + 'value' => 'imageUrl', | |
48 | + 'format' => 'image', | |
49 | + ], | |
50 | + | |
51 | + ['class' => 'yii\grid\ActionColumn'], | |
52 | + ], | |
53 | + ]); ?> | |
54 | +</div> | ... | ... |
1 | +<?php | |
2 | + | |
3 | + use yii\helpers\Html; | |
4 | + | |
5 | + /** | |
6 | + * @var yii\web\View $this | |
7 | + * @var common\models\CategoryToPurpose $model | |
8 | + * @var array $purposes | |
9 | + * @var array $categories | |
10 | + **/ | |
11 | + | |
12 | + $this->title = 'Обновить изображение '.$model->category->lang->name.' '.$model->purpose->lang->value; | |
13 | + $this->params[ 'breadcrumbs' ][] = [ | |
14 | + 'label' => Yii::t('app', 'Category To Purposes'), | |
15 | + 'url' => [ 'index' ], | |
16 | + ]; | |
17 | + $this->params[ 'breadcrumbs' ][] = [ | |
18 | + 'label' => $model->category->lang->name.' '.$model->purpose->lang->value, | |
19 | + 'url' => [ | |
20 | + 'view', | |
21 | + 'category_id' => $model->category_id, | |
22 | + 'purpose_id' => $model->purpose_id, | |
23 | + ], | |
24 | + ]; | |
25 | + $this->params[ 'breadcrumbs' ][] = Yii::t('app', 'Update'); | |
26 | +?> | |
27 | +<div class="category-to-purpose-update"> | |
28 | + | |
29 | + <h1><?= Html::encode($this->title) ?></h1> | |
30 | + | |
31 | + <?= $this->render( | |
32 | + '_form', | |
33 | + [ | |
34 | + 'model' => $model, | |
35 | + 'categories' => $categories, | |
36 | + 'purposes' => $purposes, | |
37 | + ] | |
38 | + ) ?> | |
39 | + | |
40 | +</div> | ... | ... |
1 | +<?php | |
2 | + | |
3 | +use yii\helpers\Html; | |
4 | +use yii\widgets\DetailView; | |
5 | + | |
6 | +/* @var $this yii\web\View */ | |
7 | +/* @var $model common\models\CategoryToPurpose */ | |
8 | + | |
9 | +$this->title = $model->category->lang->name.' '.$model->purpose->lang->value; | |
10 | +$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Category To Purposes'), 'url' => ['index']]; | |
11 | +$this->params['breadcrumbs'][] = $this->title; | |
12 | +?> | |
13 | +<div class="category-to-purpose-view"> | |
14 | + | |
15 | + <h1><?= Html::encode($this->title) ?></h1> | |
16 | + | |
17 | + <p> | |
18 | + <?= Html::a(Yii::t('app', 'Update'), ['update', 'category_id' => $model->category_id, 'purpose_id' => $model->purpose_id], ['class' => 'btn btn-primary']) ?> | |
19 | + <?= Html::a(Yii::t('app', 'Delete'), ['delete', 'category_id' => $model->category_id, 'purpose_id' => $model->purpose_id], [ | |
20 | + 'class' => 'btn btn-danger', | |
21 | + 'data' => [ | |
22 | + 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), | |
23 | + 'method' => 'post', | |
24 | + ], | |
25 | + ]) ?> | |
26 | + </p> | |
27 | + | |
28 | + <?= DetailView::widget([ | |
29 | + 'model' => $model, | |
30 | + 'attributes' => [ | |
31 | + [ | |
32 | + 'attribute' => 'category_id', | |
33 | + 'value' => $model->category->lang->name, | |
34 | + ], | |
35 | + [ | |
36 | + 'attribute' => 'purpose_id', | |
37 | + 'value' => $model->purpose->lang->value, | |
38 | + ], | |
39 | + [ | |
40 | + 'attribute' => 'image', | |
41 | + 'value' => $model->imageUrl, | |
42 | + 'format' => 'image', | |
43 | + ], | |
44 | + ], | |
45 | + ]) ?> | |
46 | + | |
47 | +</div> | ... | ... |
backend/views/layouts/main-sidebar.php
... | ... | @@ -50,7 +50,7 @@ use yii\widgets\Menu; |
50 | 50 | 'label' => 'Категории', |
51 | 51 | 'url' => ['/category'], |
52 | 52 | 'options' => ['class'=>\Yii::$app->user->can('category') ? '' :'hide'], |
53 | - 'active' => preg_match('/^category.*$/', $this->context->id), | |
53 | + 'active' => preg_match('/^category[\/.*]?$/', $this->context->id), | |
54 | 54 | ], |
55 | 55 | [ |
56 | 56 | 'label' => 'Бренды', |
... | ... | @@ -87,6 +87,12 @@ use yii\widgets\Menu; |
87 | 87 | 'url' => Url::toRoute(['/rubrication/tax-group', 'level'=> '1']), |
88 | 88 | 'options' => ['class'=>\Yii::$app->user->can('rubrication') ? '' :'hide'], |
89 | 89 | 'active' => preg_match('/^tax-group.*$/', $this->context->id) && (\Yii::$app->request->getQueryParam('level') == 1), |
90 | + ], | |
91 | + [ | |
92 | + 'label' => 'Изображения категорий', | |
93 | + 'url' => Url::toRoute(['/category-to-purpose']), | |
94 | + 'options' => ['class'=>\Yii::$app->user->can('category-to-purpose') ? '' :'hide'], | |
95 | + 'active' => preg_match('/^category-to-purpose.*$/', $this->context->id), | |
90 | 96 | ] |
91 | 97 | ] |
92 | 98 | ], | ... | ... |
1 | +<?php | |
2 | + | |
3 | +namespace common\models; | |
4 | + | |
5 | +use common\behaviors\SaveImgBehavior; | |
6 | +use common\modules\product\models\Category; | |
7 | +use common\modules\rubrication\models\TaxOption; | |
8 | +use Yii; | |
9 | + | |
10 | +/** | |
11 | + * This is the model class for table "category_to_purpose". | |
12 | + * | |
13 | + * @property integer $category_id | |
14 | + * @property integer $purpose_id | |
15 | + * @property string $image | |
16 | + * | |
17 | + * @property Category $category | |
18 | + * @property TaxOption $purpose | |
19 | + */ | |
20 | +class CategoryToPurpose extends \yii\db\ActiveRecord | |
21 | +{ | |
22 | + /** | |
23 | + * @inheritdoc | |
24 | + */ | |
25 | + public static function tableName() | |
26 | + { | |
27 | + return 'category_to_purpose'; | |
28 | + } | |
29 | + | |
30 | + public static function primaryKey() | |
31 | + { | |
32 | + return [ | |
33 | + 'category_id', | |
34 | + 'purpose_id' | |
35 | + ]; | |
36 | + } | |
37 | + | |
38 | + public function behaviors() | |
39 | + { | |
40 | + return [ | |
41 | + [ | |
42 | + 'class' => SaveImgBehavior::className(), | |
43 | + 'fields' => [ | |
44 | + [ | |
45 | + 'name' => 'image', | |
46 | + 'directory' => 'category_purpose', | |
47 | + ], | |
48 | + ], | |
49 | + ], | |
50 | + ]; | |
51 | + } | |
52 | + | |
53 | + /** | |
54 | + * @inheritdoc | |
55 | + */ | |
56 | + public function rules() | |
57 | + { | |
58 | + return [ | |
59 | + [['category_id', 'purpose_id'], 'required'], | |
60 | + [['category_id', 'purpose_id'], 'integer'], | |
61 | + [['image'], 'string', 'max' => 255], | |
62 | + [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'category_id']], | |
63 | + [['purpose_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxOption::className(), 'targetAttribute' => ['purpose_id' => 'tax_option_id']], | |
64 | + ]; | |
65 | + } | |
66 | + | |
67 | + /** | |
68 | + * @inheritdoc | |
69 | + */ | |
70 | + public function attributeLabels() | |
71 | + { | |
72 | + return [ | |
73 | + 'category_id' => Yii::t('app', 'Category ID'), | |
74 | + 'purpose_id' => Yii::t('app', 'Purpose ID'), | |
75 | + 'image' => Yii::t('app', 'Image'), | |
76 | + ]; | |
77 | + } | |
78 | + | |
79 | + /** | |
80 | + * @return \yii\db\ActiveQuery | |
81 | + */ | |
82 | + public function getCategory() | |
83 | + { | |
84 | + return $this->hasOne(Category::className(), ['category_id' => 'category_id']); | |
85 | + } | |
86 | + | |
87 | + /** | |
88 | + * @return \yii\db\ActiveQuery | |
89 | + */ | |
90 | + public function getPurpose() | |
91 | + { | |
92 | + return $this->hasOne(TaxOption::className(), ['tax_option_id' => 'purpose_id']); | |
93 | + } | |
94 | +} | ... | ... |
1 | +<?php | |
2 | + | |
3 | + namespace common\models; | |
4 | + | |
5 | + use yii\base\Model; | |
6 | + use yii\data\ActiveDataProvider; | |
7 | + | |
8 | + /** | |
9 | + * CategoryToPurposeSearch represents the model behind the search form about `common\models\CategoryToPurpose`. | |
10 | + */ | |
11 | + class CategoryToPurposeSearch extends CategoryToPurpose | |
12 | + { | |
13 | + /** | |
14 | + * @inheritdoc | |
15 | + */ | |
16 | + public function rules() | |
17 | + { | |
18 | + return [ | |
19 | + [ | |
20 | + [ | |
21 | + 'category_id', | |
22 | + 'purpose_id', | |
23 | + ], | |
24 | + 'string', | |
25 | + ], | |
26 | + ]; | |
27 | + } | |
28 | + | |
29 | + /** | |
30 | + * @inheritdoc | |
31 | + */ | |
32 | + public function scenarios() | |
33 | + { | |
34 | + // bypass scenarios() implementation in the parent class | |
35 | + return Model::scenarios(); | |
36 | + } | |
37 | + | |
38 | + /** | |
39 | + * Creates data provider instance with search query applied | |
40 | + * | |
41 | + * @param array $params | |
42 | + * | |
43 | + * @return ActiveDataProvider | |
44 | + */ | |
45 | + public function search($params) | |
46 | + { | |
47 | + $query = CategoryToPurpose::find() | |
48 | + ->joinWith('category.lang') | |
49 | + ->joinWith('purpose.lang'); | |
50 | + | |
51 | + // add conditions that should always apply here | |
52 | + | |
53 | + $dataProvider = new ActiveDataProvider( | |
54 | + [ | |
55 | + 'query' => $query, | |
56 | + ] | |
57 | + ); | |
58 | + | |
59 | + $dataProvider->setSort( | |
60 | + [ | |
61 | + 'attributes' => [ | |
62 | + 'category_id', | |
63 | + 'purpose_id', | |
64 | + ], | |
65 | + ] | |
66 | + ); | |
67 | + | |
68 | + $this->load($params); | |
69 | + | |
70 | + if (!$this->validate()) { | |
71 | + // uncomment the following line if you do not want to return any records when validation fails | |
72 | + // $query->where('0=1'); | |
73 | + return $dataProvider; | |
74 | + } | |
75 | + | |
76 | + // grid filtering conditions | |
77 | + | |
78 | + $query->andFilterWhere( | |
79 | + [ | |
80 | + 'like', | |
81 | + 'category_lang.name', | |
82 | + $this->category_id, | |
83 | + ] | |
84 | + ) | |
85 | + ->andFilterWhere( | |
86 | + [ | |
87 | + 'like', | |
88 | + 'tax_option_lang.value', | |
89 | + $this->purpose_id, | |
90 | + ] | |
91 | + ); | |
92 | + | |
93 | + return $dataProvider; | |
94 | + } | |
95 | + } | ... | ... |
common/translation/ru/app.php
... | ... | @@ -290,4 +290,9 @@ |
290 | 290 | 'Value' => 'Значение', |
291 | 291 | 'Articles ID' => 'ID статей', |
292 | 292 | 'Article To Rating ID' => 'Article To Rating ID', |
293 | + | |
294 | + 'Category To Purposes' => 'Изображения категорий по назначениям', | |
295 | + 'Create Category To Purpose' => 'Добавить изображение для категории по назначению', | |
296 | + 'Category ID' => 'Категория', | |
297 | + 'Purpose ID' => 'Назначение', | |
293 | 298 | ]; |
294 | 299 | \ No newline at end of file | ... | ... |
console/migrations/m170303_145834_add_category_to_purpose_table.php
0 → 100644
1 | +<?php | |
2 | + | |
3 | +use yii\db\Migration; | |
4 | + | |
5 | +class m170303_145834_add_category_to_purpose_table extends Migration | |
6 | +{ | |
7 | + public function up() | |
8 | + { | |
9 | + $this->createTable('category_to_purpose', [ | |
10 | + 'category_id' => $this->integer()->notNull(), | |
11 | + 'purpose_id' => $this->integer()->notNull(), | |
12 | + 'image' => $this->string() | |
13 | + ]); | |
14 | + $this->addForeignKey('category_to_purpose_to_category', 'category_to_purpose', 'category_id', 'category', 'category_id', 'CASCADE', 'CASCADE'); | |
15 | + $this->addForeignKey('category_to_purpose_to_purpose', 'category_to_purpose', 'purpose_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE'); | |
16 | + } | |
17 | + | |
18 | + public function down() | |
19 | + { | |
20 | + $this->dropForeignKey('category_to_purpose_to_category', 'category_to_purpose'); | |
21 | + $this->dropForeignKey('category_to_purpose_to_purpose', 'category_to_purpose'); | |
22 | + $this->dropTable('category_to_purpose'); | |
23 | + } | |
24 | +} | ... | ... |
frontend/controllers/FilterController.php
... | ... | @@ -2,6 +2,7 @@ |
2 | 2 | |
3 | 3 | namespace frontend\controllers; |
4 | 4 | |
5 | + use common\models\CategoryToPurpose; | |
5 | 6 | use common\modules\product\models\Brand; |
6 | 7 | use common\modules\product\models\Category; |
7 | 8 | use common\modules\product\models\ProductSearch; |
... | ... | @@ -25,16 +26,36 @@ |
25 | 26 | ->joinWith('products.lang') |
26 | 27 | ->joinWith('products.categories.lang') |
27 | 28 | ->joinWith('products.options.lang') |
28 | - ->where([ | |
29 | - 'category.category_id' => $category->category_id, | |
30 | - 'tax_option.tax_option_id' => $purpose->tax_option_id, | |
31 | - ]) | |
29 | + ->where( | |
30 | + [ | |
31 | + 'category.category_id' => $category->category_id, | |
32 | + 'tax_option.tax_option_id' => $purpose->tax_option_id, | |
33 | + ] | |
34 | + ) | |
32 | 35 | ->all(); |
33 | - return $this->render('index', [ | |
34 | - 'category' => $category, | |
35 | - 'purpose' => $purpose, | |
36 | - 'brands' => $brands, | |
37 | - ]); | |
36 | + $image = CategoryToPurpose::find() | |
37 | + ->where( | |
38 | + [ | |
39 | + 'category_id' => $category_id, | |
40 | + 'purpose_id' => $purpose_id, | |
41 | + ] | |
42 | + ) | |
43 | + ->andWhere( | |
44 | + [ | |
45 | + 'not', | |
46 | + [ 'image' => null ], | |
47 | + ] | |
48 | + ) | |
49 | + ->one(); | |
50 | + return $this->render( | |
51 | + 'index', | |
52 | + [ | |
53 | + 'category' => $category, | |
54 | + 'purpose' => $purpose, | |
55 | + 'brands' => $brands, | |
56 | + 'image' => $image, | |
57 | + ] | |
58 | + ); | |
38 | 59 | } |
39 | 60 | |
40 | 61 | /** |
... | ... | @@ -48,46 +69,57 @@ |
48 | 69 | $brandsAll = $category->activeBrands; |
49 | 70 | $purposes = TaxOption::find() |
50 | 71 | ->joinWith('lang', true, 'INNER JOIN') |
51 | - ->joinWith([ | |
52 | - 'products' => function($query) use ($category) { | |
53 | - $query->joinWith([ | |
54 | - 'categories' => function($query) use ($category) { | |
55 | - $query->andWhere([ 'category.category_id' => $category->category_id ]); | |
56 | - }, | |
57 | - ]); | |
58 | - }, | |
59 | - ]) | |
72 | + ->joinWith( | |
73 | + [ | |
74 | + 'products' => function ($query) use ($category) { | |
75 | + $query->joinWith( | |
76 | + [ | |
77 | + 'categories' => function ($query) use ($category) { | |
78 | + $query->andWhere( | |
79 | + [ 'category.category_id' => $category->category_id ] | |
80 | + ); | |
81 | + }, | |
82 | + ] | |
83 | + ); | |
84 | + }, | |
85 | + ] | |
86 | + ) | |
60 | 87 | ->joinWith('products.categories.lang') |
61 | 88 | ->joinWith('products.lang') |
62 | 89 | ->joinWith('products.brand.lang') |
63 | 90 | ->joinWith('taxGroup') |
64 | - ->where([ | |
65 | - 'category.category_id' => $category->category_id, | |
66 | - 'tax_group.tax_group_id' => 5, | |
67 | - ]) | |
91 | + ->where( | |
92 | + [ | |
93 | + 'category.category_id' => $category->category_id, | |
94 | + 'tax_group.tax_group_id' => 5, | |
95 | + ] | |
96 | + ) | |
68 | 97 | ->all(); |
69 | 98 | $brands = []; |
70 | - foreach($purposes as $purpose) { | |
99 | + foreach ($purposes as $purpose) { | |
71 | 100 | /** |
72 | 101 | * @var TaxOption $purpose |
73 | 102 | */ |
74 | 103 | $brands[ $purpose->tax_option_id ] = []; |
75 | - foreach($purpose->products as $product) { | |
104 | + foreach ($purpose->products as $product) { | |
76 | 105 | /** |
77 | 106 | * @var Brand $brand |
78 | 107 | */ |
79 | 108 | $brand = $product->brand; |
80 | - if(!empty( $brand )) { | |
109 | + if (!empty( $brand )) { | |
81 | 110 | $brands[ $purpose->tax_option_id ][ $brand->brand_id ] = $brand; |
82 | 111 | } |
83 | 112 | } |
84 | 113 | } |
85 | - return $this->render('category', [ | |
86 | - 'category' => $category, | |
87 | - 'purposes' => $purposes, | |
88 | - 'brands' => $brands, | |
89 | - 'brandsAll' => $brandsAll, | |
90 | - ]); | |
114 | + return $this->render( | |
115 | + 'category', | |
116 | + [ | |
117 | + 'category' => $category, | |
118 | + 'purposes' => $purposes, | |
119 | + 'brands' => $brands, | |
120 | + 'brandsAll' => $brandsAll, | |
121 | + ] | |
122 | + ); | |
91 | 123 | } |
92 | 124 | |
93 | 125 | /** |
... | ... | @@ -101,36 +133,45 @@ |
101 | 133 | $categories = Category::find() |
102 | 134 | ->joinWith('products.lang') |
103 | 135 | ->joinWith('products.brand.lang') |
104 | - ->joinWith([ | |
105 | - 'products' => function($query) use ($purpose) { | |
106 | - $query->joinWith([ | |
107 | - 'options' => function($query) use ($purpose) { | |
108 | - $query->andWhere([ 'tax_option.tax_option_id' => $purpose->tax_option_id ]); | |
109 | - }, | |
110 | - ]); | |
111 | - }, | |
112 | - ]) | |
136 | + ->joinWith( | |
137 | + [ | |
138 | + 'products' => function ($query) use ($purpose) { | |
139 | + $query->joinWith( | |
140 | + [ | |
141 | + 'options' => function ($query) use ($purpose) { | |
142 | + $query->andWhere( | |
143 | + [ 'tax_option.tax_option_id' => $purpose->tax_option_id ] | |
144 | + ); | |
145 | + }, | |
146 | + ] | |
147 | + ); | |
148 | + }, | |
149 | + ] | |
150 | + ) | |
113 | 151 | ->joinWith('products.options.lang') |
114 | 152 | ->where([ 'tax_option.tax_option_id' => $purpose->tax_option_id ]) |
115 | 153 | ->all(); |
116 | 154 | $brands = []; |
117 | - foreach($categories as $category) { | |
155 | + foreach ($categories as $category) { | |
118 | 156 | $brands[ $category->category_id ] = []; |
119 | - foreach($category->products as $product) { | |
157 | + foreach ($category->products as $product) { | |
120 | 158 | /** |
121 | 159 | * @var Brand $brand |
122 | 160 | */ |
123 | 161 | $brand = $product->brand; |
124 | - if(!empty( $brand )) { | |
162 | + if (!empty( $brand )) { | |
125 | 163 | $brands[ $category->category_id ][ $brand->brand_id ] = $brand; |
126 | 164 | } |
127 | 165 | } |
128 | 166 | } |
129 | - return $this->render('purpose', [ | |
130 | - 'purpose' => $purpose, | |
131 | - 'categories' => $categories, | |
132 | - 'brands' => $brands, | |
133 | - ]); | |
167 | + return $this->render( | |
168 | + 'purpose', | |
169 | + [ | |
170 | + 'purpose' => $purpose, | |
171 | + 'categories' => $categories, | |
172 | + 'brands' => $brands, | |
173 | + ] | |
174 | + ); | |
134 | 175 | } |
135 | 176 | |
136 | 177 | public function actionBrand($category_id, $purpose_id, $brand_id) |
... | ... | @@ -149,18 +190,23 @@ |
149 | 190 | ->joinWith('brand.lang') |
150 | 191 | ->joinWith('options.lang') |
151 | 192 | ->joinWith('categories.lang') |
152 | - ->andWhere([ | |
153 | - 'category.category_id' => $category->category_id, | |
154 | - 'tax_option.tax_option_id' => $purpose->tax_option_id, | |
155 | - 'brand.brand_id' => $brand->brand_id, | |
156 | - ]); | |
157 | - return $this->render('brand', [ | |
158 | - 'category' => $category, | |
159 | - 'purpose' => $purpose, | |
160 | - 'brand' => $brand, | |
161 | - 'searchModel' => $searchModel, | |
162 | - 'dataProvider' => $dataProvider, | |
163 | - ]); | |
193 | + ->andWhere( | |
194 | + [ | |
195 | + 'category.category_id' => $category->category_id, | |
196 | + 'tax_option.tax_option_id' => $purpose->tax_option_id, | |
197 | + 'brand.brand_id' => $brand->brand_id, | |
198 | + ] | |
199 | + ); | |
200 | + return $this->render( | |
201 | + 'brand', | |
202 | + [ | |
203 | + 'category' => $category, | |
204 | + 'purpose' => $purpose, | |
205 | + 'brand' => $brand, | |
206 | + 'searchModel' => $searchModel, | |
207 | + 'dataProvider' => $dataProvider, | |
208 | + ] | |
209 | + ); | |
164 | 210 | } |
165 | 211 | |
166 | 212 | public function actionCategoryBrand($category_id, $brand_id) |
... | ... | @@ -177,26 +223,34 @@ |
177 | 223 | $query->with('variants.lang') |
178 | 224 | ->joinWith('brand.lang') |
179 | 225 | ->joinWith('categories.lang') |
180 | - ->andWhere([ | |
181 | - 'category.category_id' => $category->category_id, | |
182 | - 'brand.brand_id' => $brand->brand_id, | |
183 | - ]); | |
184 | - return $this->render('category-brand', [ | |
185 | - 'category' => $category, | |
186 | - 'brand' => $brand, | |
187 | - 'searchModel' => $searchModel, | |
188 | - 'dataProvider' => $dataProvider, | |
189 | - ]); | |
226 | + ->andWhere( | |
227 | + [ | |
228 | + 'category.category_id' => $category->category_id, | |
229 | + 'brand.brand_id' => $brand->brand_id, | |
230 | + ] | |
231 | + ); | |
232 | + return $this->render( | |
233 | + 'category-brand', | |
234 | + [ | |
235 | + 'category' => $category, | |
236 | + 'brand' => $brand, | |
237 | + 'searchModel' => $searchModel, | |
238 | + 'dataProvider' => $dataProvider, | |
239 | + ] | |
240 | + ); | |
190 | 241 | } |
191 | 242 | |
192 | 243 | public function actionCategoryBrands($category_id) |
193 | 244 | { |
194 | 245 | $category = $this->findCategory($category_id); |
195 | 246 | $brands = $category->activeBrands; |
196 | - return $this->render('category-brands', [ | |
197 | - 'category' => $category, | |
198 | - 'brands' => $brands, | |
199 | - ]); | |
247 | + return $this->render( | |
248 | + 'category-brands', | |
249 | + [ | |
250 | + 'category' => $category, | |
251 | + 'brands' => $brands, | |
252 | + ] | |
253 | + ); | |
200 | 254 | } |
201 | 255 | |
202 | 256 | /** |
... | ... | @@ -209,12 +263,14 @@ |
209 | 263 | { |
210 | 264 | $model = TaxOption::find() |
211 | 265 | ->joinWith('taxGroup') |
212 | - ->where([ | |
213 | - 'tax_option.tax_option_id' => $id, | |
214 | - 'tax_group.tax_group_id' => 5, | |
215 | - ]) | |
266 | + ->where( | |
267 | + [ | |
268 | + 'tax_option.tax_option_id' => $id, | |
269 | + 'tax_group.tax_group_id' => 5, | |
270 | + ] | |
271 | + ) | |
216 | 272 | ->one(); |
217 | - if(empty( $model )) { | |
273 | + if (empty( $model )) { | |
218 | 274 | throw new NotFoundHttpException(); |
219 | 275 | } |
220 | 276 | return $model; |
... | ... | @@ -229,11 +285,13 @@ |
229 | 285 | private function findCategory($id) |
230 | 286 | { |
231 | 287 | $model = Category::find() |
232 | - ->where([ | |
233 | - 'category.category_id' => $id, | |
234 | - ]) | |
288 | + ->where( | |
289 | + [ | |
290 | + 'category.category_id' => $id, | |
291 | + ] | |
292 | + ) | |
235 | 293 | ->one(); |
236 | - if(empty( $model )) { | |
294 | + if (empty( $model )) { | |
237 | 295 | throw new NotFoundHttpException(); |
238 | 296 | } |
239 | 297 | return $model; |
... | ... | @@ -251,11 +309,13 @@ |
251 | 309 | * @var Brand $model |
252 | 310 | */ |
253 | 311 | $model = Brand::find() |
254 | - ->where([ | |
255 | - 'brand.brand_id' => $id, | |
256 | - ]) | |
312 | + ->where( | |
313 | + [ | |
314 | + 'brand.brand_id' => $id, | |
315 | + ] | |
316 | + ) | |
257 | 317 | ->one(); |
258 | - if(empty( $model )) { | |
318 | + if (empty( $model )) { | |
259 | 319 | throw new NotFoundHttpException(); |
260 | 320 | } |
261 | 321 | return $model; | ... | ... |
frontend/views/filter/index.php
1 | 1 | <?php |
2 | 2 | /** |
3 | - * @var $this yii\web\View | |
4 | - * @var Category $category | |
5 | - * @var TaxOption $purpose | |
6 | - * @var Brand[] $brands | |
3 | + * @var $this yii\web\View | |
4 | + * @var Category $category | |
5 | + * @var TaxOption $purpose | |
6 | + * @var Brand[] $brands | |
7 | + * @var CategoryToPurpose $image | |
7 | 8 | */ |
8 | 9 | use common\components\artboximage\ArtboxImageHelper; |
10 | + use common\models\CategoryToPurpose; | |
9 | 11 | use common\modules\product\models\Brand; |
10 | 12 | use common\modules\product\models\Category; |
11 | 13 | use common\modules\rubrication\models\TaxOption; |
... | ... | @@ -35,7 +37,11 @@ |
35 | 37 | <div class="col-xs-12 col-sm-9 col-md-7 col-lg-7"> |
36 | 38 | <div class="style article-img"> |
37 | 39 | <?php |
38 | - echo ArtboxImageHelper::getImage($category->getImageUrl(), 'full_filter'); | |
40 | + if($image) { | |
41 | + echo ArtboxImageHelper::getImage($image->imageUrl, 'full_filter'); | |
42 | + } else { | |
43 | + echo ArtboxImageHelper::getImage($category->getImageUrl(), 'full_filter'); | |
44 | + } | |
39 | 45 | ?> |
40 | 46 | </div> |
41 | 47 | <div class="style content-txt"> |
... | ... | @@ -48,13 +54,16 @@ |
48 | 54 | <div class="col-xs-12 col-sm-3 col-md-3 col-lg-2"> |
49 | 55 | <div class="brand-link"> |
50 | 56 | <?php |
51 | - foreach($brands as $brand) { | |
52 | - echo Html::a(ArtboxImageHelper::getImage($brand->getImageUrl(), 'brand_image_filter'), [ | |
53 | - 'filter/brand', | |
54 | - 'category_id' => $category->category_id, | |
55 | - 'brand_id' => $brand->brand_id, | |
56 | - 'purpose_id' => $purpose->tax_option_id, | |
57 | - ]); | |
57 | + foreach ($brands as $brand) { | |
58 | + echo Html::a( | |
59 | + ArtboxImageHelper::getImage($brand->getImageUrl(), 'brand_image_filter'), | |
60 | + [ | |
61 | + 'filter/brand', | |
62 | + 'category_id' => $category->category_id, | |
63 | + 'brand_id' => $brand->brand_id, | |
64 | + 'purpose_id' => $purpose->tax_option_id, | |
65 | + ] | |
66 | + ); | |
58 | 67 | } |
59 | 68 | ?> |
60 | 69 | </div> | ... | ... |