Commit 1f1f49552705969d385981a1a5ec0e1490f49fd1

Authored by Yarik
1 parent b789cb1a

Synonym

backend/views/category/_form.php
... ... @@ -20,8 +20,10 @@ use kartik\select2\Select2;
20 20 ]); ?>
21 21  
22 22 <?= $form->field($model, 'name')->textInput() ?>
23   -
  23 +
24 24 <?= $form->field($model, 'alias')->textInput(['maxlength' => true]) ?>
  25 +
  26 + <?= $form->field($model, 'synonym')->textInput(['maxlength' => true]) ?>
25 27  
26 28 <?= $form->field($model, 'parent_id')->dropDownList($categories, [
27 29 'prompt' => Yii::t('rubrication', 'Root category'),
... ...
common/modules/product/models/Category.php
... ... @@ -29,6 +29,7 @@ use yii\db\Query;
29 29 * @property integer $product_unit_id
30 30 * @property string $alias
31 31 * @property boolean $populary
  32 + * @property string $synonym
32 33 *
33 34 * @property CategoryName $categoryName
34 35 * @property Product[] $products
... ... @@ -89,7 +90,7 @@ class Category extends \yii\db\ActiveRecord
89 90 [['name'], 'string'],
90 91 [['parent_id', 'depth', 'category_name_id', 'product_unit_id'], 'integer'],
91 92 [['path', 'meta_desc', 'h1', 'seo_text'], 'string'],
92   - [['meta_title', 'image'], 'string', 'max' => 255],
  93 + [['meta_title', 'image', 'synonym'], 'string', 'max' => 255],
93 94 [['meta_robots'], 'string', 'max' => 50],
94 95 [['alias', 'name'], 'string', 'max' => 250],
95 96 [['populary'], 'boolean'],
... ... @@ -122,6 +123,7 @@ class Category extends \yii\db\ActiveRecord
122 123 'populary' => Yii::t('product', 'Populary'),
123 124 'name' => Yii::t('product', 'Name'),
124 125 'remote_id' => Yii::t('product', 'Remote ID'),
  126 + 'synonym' => Yii::t('product', 'Синоним'),
125 127 ];
126 128 }
127 129  
... ...
common/modules/product/models/Product.php
1 1 <?php
2   -
3   -namespace common\modules\product\models;
4   -
5   -use common\behaviors\Slug;
6   -use common\models\Event;
7   -use common\models\ProductToRating;
8   -use common\models\Share;
9   -use common\modules\comment\models\CommentModel;
10   -use common\modules\product\behaviors\FilterBehavior;
11   -use common\modules\rubrication\models\TaxGroup;
12   -use common\modules\rubrication\models\TaxOption;
13   -use Yii;
14   -use common\modules\relation\relationBehavior;
15   -use yii\db\ActiveQuery;
16   -use yii\db\ActiveRecord;
17   -use yii\helpers\ArrayHelper;
18   -
19   -/**
20   - * This is the model class for table "{{%product}}".
21   - *
22   - * @property string $name
23   - * @property integer $brand_id
24   - * @property integer $product_id
25   - * @property Category $category
26   - * @property array $categories
27   - * @property array of ProductVariant $variants
28   - * @property ProductVariant $variant
29   - * @property ProductImage $image
30   - * @property array $images
31   - * @property boolean $is_top
32   - * @property boolean $is_new
33   - * @property boolean $akciya
34   - * @property ProductToRating $averageRating
35   - * @property array $properties
36   - * @property ProductVariant $enabledVariant
37   - * @property array $enabledVariants
38   - */
39   -class Product extends \yii\db\ActiveRecord
40   -{
41   - /** @var array $_variants */
42   - public $_variants = [];
43   -
44   - /** @var array $_images */
45   - public $imagesUpload = [];
46   -
47   - /**
48   - * @inheritdoc
49   - */
50   - public function behaviors()
51   - {
52   - return [
53   - [
54   - 'class' => relationBehavior::className(),
55   - 'relations' => [
56   - 'product_categories' => 'entity1', // Product category
57   - 'product_option' => 'entity1' // Product category
58   - ]
59   - ],
60   - [
61   - 'class' =>FilterBehavior::className(),
62   - ],
63   - [
64   - 'class' => Slug::className(),
65   - 'in_attribute' => 'name',
66   - 'out_attribute' => 'alias',
67   - 'translit' => true
68   - ]
69   - ];
70   - }
71   -
72   - /**
73   - * @inheritdoc
74   - */
75   - public static function tableName()
76   - {
77   - return '{{%product}}';
78   - }
79   -
80   - /**
81   - * @inheritdoc
82   - */
83   - public function rules()
84   - {
85   - return [
86   -// [['categories'], 'required'],
87   - [['brand_id'], 'integer'],
88   - [['name'], 'string', 'max' => 150],
89   - [['alias'], 'string', 'max' => 250],
90   - [['categories', 'variants', 'options', 'imagesUpload'], 'safe'],
91   -// [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif', 'maxFiles' => 50],
92   - [['description', 'video'], 'safe'],
93   - [['is_top', 'is_new', 'akciya'], 'boolean'],
94   -// [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
95   - ];
96   - }
97   -
98   - /**
99   - * @inheritdoc
100   - */
101   - public function attributeLabels()
102   - {
103   - return [
104   - 'product_id' => Yii::t('product', 'ID'),
105   - 'name' => Yii::t('product', 'Name'),
106   - 'brand_id' => Yii::t('product', 'Brand'),
107   - 'categories' => Yii::t('product', 'Categories'), // relation behavior field
108   - 'category' => Yii::t('product', 'Category'), // relation behavior field
109   - 'image' => Yii::t('product', 'Image'),
110   - 'images' => Yii::t('product', 'Images'),
111   - 'description' => Yii::t('product', 'Description'),
112   - 'video' => Yii::t('product', 'Video embeded'),
113   - 'variants' => Yii::t('product', 'Variants'),
114   - 'is_top' => Yii::t('product', 'Is top'),
115   - 'is_new' => Yii::t('product', 'Is new'),
116   - 'akciya' => Yii::t('product', 'Is promo'),
117   - ];
118   - }
119   -
120   -
121   - public function withEventBanner(){
122   -
123   - }
124   -
125   -
126   -
127   - public function getEvents(){
128   - return $this->hasMany(Event::className(), ['event_id' => 'event_id'])->viaTable('events_to_products', ['product_id' => 'product_id']);
129   - }
130   -
131   - public function getUrl() {
132   - return '/product/'. $this->alias;
133   - }
134   -
135   - /**
136   - * @return \yii\db\ActiveQuery
137   - */
138   - public function getBrand()
139   - {
140   - return $this->hasOne(Brand::className(), ['brand_id' => 'brand_id']);
141   - }
142   -
143   - /**
144   - * @return \yii\db\ActiveQuery
145   - */
146   - public function getImage()
147   - {
148   - return $this->hasOne(ProductImage::className(), ['product_id' => 'product_id']);
149   - }
150   -
151   - /**
152   - * fetch stored image url
153   - * @return string
154   - */
155   - public function getImageUrl()
156   - {
157   - $image = empty($this->variant) ? null : $this->variant->image;
158   - return !empty($image) ? $image->imageUrl : '/images/no_photo.png';
159   - }
160   -
161   - /**
162   - * @return \yii\db\ActiveQuery
163   - */
164   - public function getImages()
165   - {
166   - return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id'])->where(['product_variant_id' => null]);
167   - }
168   -
169   - /**
170   - * @return \yii\db\ActiveQuery
171   - */
172   - public function getVariant()
173   - {
174   - return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']);
175   - }
176   -
177   - /**
178   - * @return \yii\db\ActiveQuery
179   - */
180   - public function getEnabledVariant()
181   - {
182   - return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]);
183   - }
184   -
185   - public function getVariantPrice() {
186   - return $this->variant->price;
187   - }
188   -
189   - public function getEnabledVariantPrice() {
190   - return $this->enabledVariants[0]->price;
191   - }
192   -
  2 +
  3 + namespace common\modules\product\models;
  4 +
  5 + use common\behaviors\Slug;
  6 + use common\models\Event;
  7 + use common\models\ProductToRating;
  8 + use common\models\Share;
  9 + use common\modules\comment\models\CommentModel;
  10 + use common\modules\product\behaviors\FilterBehavior;
  11 + use common\modules\rubrication\models\TaxGroup;
  12 + use common\modules\rubrication\models\TaxOption;
  13 + use Yii;
  14 + use common\modules\relation\relationBehavior;
  15 + use yii\db\ActiveQuery;
  16 + use yii\db\ActiveRecord;
  17 + use yii\helpers\ArrayHelper;
  18 +
193 19 /**
194   - * @return \yii\db\ActiveQuery
  20 + * This is the model class for table "{{%product}}".
  21 + *
  22 + * @property string $name
  23 + * @property integer $brand_id
  24 + * @property integer $product_id
  25 + * @property Category $category
  26 + * @property array $categories
  27 + * @property array of ProductVariant $variants
  28 + * @property ProductVariant $variant
  29 + * @property ProductImage $image
  30 + * @property array $images
  31 + * @property boolean $is_top
  32 + * @property boolean $is_new
  33 + * @property boolean $akciya
  34 + * @property string $fullname
  35 + * @property ProductToRating $averageRating
  36 + * @property array $properties
  37 + * @property ProductVariant $enabledVariant
  38 + * @property array $enabledVariants
195 39 */
196   - public function getVariants()
197   - {
198   - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']);
199   - }
200   -
201   - public function getEnabledVariants()
  40 + class Product extends \yii\db\ActiveRecord
202 41 {
203   - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0])->joinWith('image');
204   - }
205   -
206   - /*
207   - * Get variants grouped by type
208   - */
209   - public function getEnabledVariantsGrouped()
210   - {
211   - $variants = [];
212   - foreach ($this->enabledVariants as $variant) {
213   - $variants[$variant->product_variant_type_id ? $variant->product_variant_type_id : 1][] = $variant;
  42 + /** @var array $_variants */
  43 + public $_variants = [];
  44 +
  45 + /** @var array $_images */
  46 + public $imagesUpload = [];
  47 +
  48 + /**
  49 + * @inheritdoc
  50 + */
  51 + public function behaviors()
  52 + {
  53 + return [
  54 + [
  55 + 'class' => relationBehavior::className(),
  56 + 'relations' => [
  57 + 'product_categories' => 'entity1',
  58 + // Product category
  59 + 'product_option' => 'entity1'
  60 + // Product category
  61 + ],
  62 + ],
  63 + [
  64 + 'class' => FilterBehavior::className(),
  65 + ],
  66 + [
  67 + 'class' => Slug::className(),
  68 + 'in_attribute' => 'name',
  69 + 'out_attribute' => 'alias',
  70 + 'translit' => true,
  71 + ],
  72 + ];
214 73 }
215   - if (empty($variants)) {
216   - return [];
  74 +
  75 + /**
  76 + * @inheritdoc
  77 + */
  78 + public static function tableName()
  79 + {
  80 + return '{{%product}}';
217 81 }
218   - $ids = array_keys($variants);
219   - $variants_type = [];
220   - foreach(ProductVariantType::find()->select(['product_variant_type_id', 'name2'])->where(['product_variant_type_id' => $ids])->all() as $variant_type) {
221   - $variant_type->_variants = $variants[$variant_type->product_variant_type_id];
222   - $variants_type[] = $variant_type;
  82 +
  83 + /**
  84 + * @inheritdoc
  85 + */
  86 + public function rules()
  87 + {
  88 + return [
  89 + // [['categories'], 'required'],
  90 + [
  91 + [ 'brand_id' ],
  92 + 'integer',
  93 + ],
  94 + [
  95 + [ 'name' ],
  96 + 'string',
  97 + 'max' => 150,
  98 + ],
  99 + [
  100 + [ 'alias' ],
  101 + 'string',
  102 + 'max' => 250,
  103 + ],
  104 + [
  105 + [
  106 + 'categories',
  107 + 'variants',
  108 + 'options',
  109 + 'imagesUpload',
  110 + ],
  111 + 'safe',
  112 + ],
  113 + // [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif', 'maxFiles' => 50],
  114 + [
  115 + [
  116 + 'description',
  117 + 'video',
  118 + ],
  119 + 'safe',
  120 + ],
  121 + [
  122 + [
  123 + 'is_top',
  124 + 'is_new',
  125 + 'akciya',
  126 + ],
  127 + 'boolean',
  128 + ],
  129 + // [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
  130 + ];
223 131 }
224   - return $variants_type;
225   - }
226   -
227   - public function setVariants($variants) {
228   - $this->_variants = $variants;
229   - }
230   -
231   - public function getFullName()
232   - {
233   - $words = [
234   - 'Рюкзаки' => 'Рюкзак',
235   - 'Несессеры' => 'Несессер',
236   - 'Сумки' => 'Сумка',
237   - 'Чехлы' => 'Чехол',
238   - 'Кошельки' => 'Кошелек',
239   - 'Гермочехлы' => 'Гермочехол',
240   - ];
241   - return empty($this->brand) ? $this->name : $this->brand->name .' '. $this->name;
242   - }
243   -
244   - public function getFullNameWithCategory()
245   - {
246   - $words = [
247   - 'Рюкзаки' => 'Рюкзак',
248   - 'Несессеры' => 'Несессер',
249   - 'Сумки' => 'Сумка',
250   - 'Чехлы' => 'Чехол',
251   - 'Кошельки' => 'Кошелек',
252   - 'Гермочехлы' => 'Гермочехол',
253   - ];
254   - $name = empty($this->brand) ? $this->name : $this->brand->name .' '. $this->name;
255   - return empty($this->category->categoryName->value) ? $name : (isset($words[$this->category->categoryName->value])? $words[$this->category->categoryName->value]: '') .' '. $name;
256   - }
257   -
258   -
259   - public function getCategories() {
260   - return $this->hasMany(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']);
261   -// return $this->getRelations('product_categories');
262   - }
263   - public function getCategoriesWithName() {
264   - return $this->hasMany(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id'])->joinWith('categoryNames');
265   -// return $this->getRelations('product_categories');
266   - }
267   -
268   - public function getCategoriesNames() {
269   - $result = [];
270   - foreach($this->categories as $category) {
271   - $result[] = $category->name;
  132 +
  133 + /**
  134 + * @inheritdoc
  135 + */
  136 + public function attributeLabels()
  137 + {
  138 + return [
  139 + 'product_id' => Yii::t('product', 'ID'),
  140 + 'name' => Yii::t('product', 'Name'),
  141 + 'brand_id' => Yii::t('product', 'Brand'),
  142 + 'categories' => Yii::t('product', 'Categories'),
  143 + // relation behavior field
  144 + 'category' => Yii::t('product', 'Category'),
  145 + // relation behavior field
  146 + 'image' => Yii::t('product', 'Image'),
  147 + 'images' => Yii::t('product', 'Images'),
  148 + 'description' => Yii::t('product', 'Description'),
  149 + 'video' => Yii::t('product', 'Video embeded'),
  150 + 'variants' => Yii::t('product', 'Variants'),
  151 + 'is_top' => Yii::t('product', 'Is top'),
  152 + 'is_new' => Yii::t('product', 'Is new'),
  153 + 'akciya' => Yii::t('product', 'Is promo'),
  154 + ];
272 155 }
273   - return $result;
274   - }
275   -
276   - public function getVariantsWithFilters(){
277   - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->with('filters');
278   - }
279   -
280   - /**
281   - * @return ActiveQuery
282   - */
283   - public function getCategory() {
284   - return $this->hasOne(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']);
285   - }
286   -
287   - public function getOptions() {
288   - return $this->hasMany(TaxOption::className(), ['tax_option_id' => 'option_id'])->viaTable('product_option', ['product_id' => 'product_id']);
289   - }
290   -
291   - public function getProperties() {
292   - $groups = $options = [];
293   - foreach ($this->options as $option) {
294   - $options[$option->tax_group_id][] = $option;
295   - }
296   - foreach (TaxGroup::find()->where(['tax_group_id' => array_keys($options)])->all() as $group) {
297   - if (!empty($options[$group->tax_group_id])) {
298   - $group->_options = $options[$group->tax_group_id];
299   - $groups[] = $group;
300   - }
  156 +
  157 + public function withEventBanner()
  158 + {
  159 +
301 160 }
302   - return $groups;
303   - }
304   -
305   - public function getActiveProperties($category_id) {
306   - $groups = $options = [];
307   - foreach ($this->options as $option) {
308   - $options[$option->tax_group_id][] = $option;
309   - }
310   - foreach (TaxGroup::find()->joinWith('categories')->where(['tax_group.tax_group_id' => array_keys($options), 'tax_group.display' => TRUE, 'category.category_id' => $category_id])->all() as $group) {
311   - if (!empty($options[$group->tax_group_id])) {
312   - $group->_options = $options[$group->tax_group_id];
313   - $groups[] = $group;
314   - }
  161 +
  162 + public function getEvents()
  163 + {
  164 + return $this->hasMany(Event::className(), [ 'event_id' => 'event_id' ])
  165 + ->viaTable('events_to_products', [ 'product_id' => 'product_id' ]);
315 166 }
316   - return $groups;
317   - }
318   -
319   - public function getStocks() {
320   - return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_id' => 'product_id']);
321   - }
322   -
323   - /**
324   - * @inheritdoc
325   - * @return ProductQuery the active query used by this AR class.
326   - */
327   - public static function find()
328   - {
329   - return new ProductQuery(get_called_class());
330   - }
331   -
332   - public function getQuantity() {
333   - return ProductStock::find()
334   - ->where(['product_id' => $this->product_id])
335   - ->sum('quantity');
336   - }
337   -
338   - public function afterSave($insert, $changedAttributes)
339   - {
340   - parent::afterSave($insert, $changedAttributes);
341   -
342   -// $images = UploadedFile::getInstance($this, 'imagesUpload');
343   -// var_dump($images);exit;
344   -
345   -// if (!empty($this->imagesUpload)) {
346   -// if (!is_array($this->imagesUpload)) {
347   -// $this->imagesUpload = [$this->imagesUpload];
348   -// }
349   -// foreach($this->imagesUpload as $image) {
350   -// $image->saveAs((Yii::getAlias('@frontend/web/storage/products/original/' . $image->baseName .'_'. uniqid() . '.' . $image->extension)));
351   -// }
352   -//
353   -//
354   -// }
355   -
356   - if (!empty($this->_variants)) {
357   - $todel = [];
358   - foreach ($this->variants ?: [] as $_variant) {
359   - $todel[$_variant->product_variant_id] = $_variant->product_variant_id;
  167 +
  168 + public function getUrl()
  169 + {
  170 + return '/product/' . $this->alias;
  171 + }
  172 +
  173 + /**
  174 + * @return \yii\db\ActiveQuery
  175 + */
  176 + public function getBrand()
  177 + {
  178 + return $this->hasOne(Brand::className(), [ 'brand_id' => 'brand_id' ]);
  179 + }
  180 +
  181 + /**
  182 + * @return \yii\db\ActiveQuery
  183 + */
  184 + public function getImage()
  185 + {
  186 + return $this->hasOne(ProductImage::className(), [ 'product_id' => 'product_id' ]);
  187 + }
  188 +
  189 + /**
  190 + * fetch stored image url
  191 + *
  192 + * @return string
  193 + */
  194 + public function getImageUrl()
  195 + {
  196 + $image = empty( $this->variant ) ? null : $this->variant->image;
  197 + return !empty( $image ) ? $image->imageUrl : '/images/no_photo.png';
  198 + }
  199 +
  200 + /**
  201 + * @return \yii\db\ActiveQuery
  202 + */
  203 + public function getImages()
  204 + {
  205 + return $this->hasMany(ProductImage::className(), [ 'product_id' => 'product_id' ])
  206 + ->where([ 'product_variant_id' => null ]);
  207 + }
  208 +
  209 + /**
  210 + * @return \yii\db\ActiveQuery
  211 + */
  212 + public function getVariant()
  213 + {
  214 + return $this->hasOne(ProductVariant::className(), [ 'product_id' => 'product_id' ]);
  215 + }
  216 +
  217 + /**
  218 + * @return \yii\db\ActiveQuery
  219 + */
  220 + public function getEnabledVariant()
  221 + {
  222 + return $this->hasOne(ProductVariant::className(), [ 'product_id' => 'product_id' ])
  223 + ->andOnCondition(
  224 + [
  225 + '!=',
  226 + ProductVariant::tableName() . '.stock',
  227 + 0,
  228 + ]
  229 + );
  230 + }
  231 +
  232 + public function getVariantPrice()
  233 + {
  234 + return $this->variant->price;
  235 + }
  236 +
  237 + public function getEnabledVariantPrice()
  238 + {
  239 + return $this->enabledVariants[ 0 ]->price;
  240 + }
  241 +
  242 + /**
  243 + * @return \yii\db\ActiveQuery
  244 + */
  245 + public function getVariants()
  246 + {
  247 + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ]);
  248 + }
  249 +
  250 + public function getEnabledVariants()
  251 + {
  252 + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ])
  253 + ->andOnCondition(
  254 + [
  255 + '!=',
  256 + ProductVariant::tableName() . '.stock',
  257 + 0,
  258 + ]
  259 + )
  260 + ->joinWith('image');
  261 + }
  262 +
  263 + /*
  264 + * Get variants grouped by type
  265 + */
  266 + public function getEnabledVariantsGrouped()
  267 + {
  268 + $variants = [];
  269 + foreach ($this->enabledVariants as $variant) {
  270 + $variants[ $variant->product_variant_type_id ? $variant->product_variant_type_id : 1 ][] = $variant;
  271 + }
  272 + if (empty( $variants )) {
  273 + return [];
  274 + }
  275 + $ids = array_keys($variants);
  276 + $variants_type = [];
  277 + foreach (ProductVariantType::find()
  278 + ->select(
  279 + [
  280 + 'product_variant_type_id',
  281 + 'name2',
  282 + ]
  283 + )
  284 + ->where([ 'product_variant_type_id' => $ids ])
  285 + ->all() as $variant_type) {
  286 + $variant_type->_variants = $variants[ $variant_type->product_variant_type_id ];
  287 + $variants_type[] = $variant_type;
360 288 }
361   - foreach ($this->_variants as $_variant) {
362   - if (!is_array($_variant)) {
363   - return;
  289 + return $variants_type;
  290 + }
  291 +
  292 + public function setVariants($variants)
  293 + {
  294 + $this->_variants = $variants;
  295 + }
  296 +
  297 + public function Name()
  298 + {
  299 + $words = [
  300 + 'Рюкзаки' => 'Рюкзак',
  301 + 'Несессеры' => 'Несессер',
  302 + 'Сумки' => 'Сумка',
  303 + 'Чехлы' => 'Чехол',
  304 + 'Кошельки' => 'Кошелек',
  305 + 'Гермочехлы' => 'Гермочехол',
  306 + ];
  307 + return empty( $this->brand ) ? $this->name : $this->brand->name . ' ' . $this->name;
  308 + }
  309 +
  310 + public function getFullNameWithCategory()
  311 + {
  312 + $words = [
  313 + 'Рюкзаки' => 'Рюкзак',
  314 + 'Несессеры' => 'Несессер',
  315 + 'Сумки' => 'Сумка',
  316 + 'Чехлы' => 'Чехол',
  317 + 'Кошельки' => 'Кошелек',
  318 + 'Гермочехлы' => 'Гермочехол',
  319 + ];
  320 + $name = empty( $this->brand ) ? $this->name : $this->brand->name . ' ' . $this->name;
  321 + return empty( $this->category->categoryName->value ) ? $name : ( isset( $words[ $this->category->categoryName->value ] ) ? $words[ $this->category->categoryName->value ] : '' ) . ' ' . $name;
  322 + }
  323 +
  324 + public function getFullname()
  325 + {
  326 + $name = '';
  327 + $taxOption = TaxOption::find()
  328 + ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id')
  329 + ->where(
  330 + [
  331 + 'tax_group.use_in_name' => 1,
  332 + 'tax_option.tax_option_id' => ArrayHelper::getColumn(
  333 + $this->options,
  334 + 'tax_option_id'
  335 + ),
  336 + ]
  337 + )
  338 + ->one();
  339 + if ($taxOption) {
  340 + if (!empty( $taxOption->synonym )) {
  341 + $taxOption = $taxOption->synonym;
  342 + } else {
  343 + $taxOption = $taxOption->value;
364 344 }
365   - if (!empty($_variant['product_variant_id'])) {
366   - unset($todel[$_variant['product_variant_id']]);
367   - $model = ProductVariant::findOne($_variant['product_variant_id']);
  345 + $name .= $taxOption;
  346 + } else {
  347 + if (!empty( $this->category->synonym )) {
  348 + $name .= $this->category->synonym;
368 349 } else {
369   - $model = new ProductVariant();
  350 + $name .= $this->category->name;
370 351 }
371   - $_variant['product_id'] = $this->product_id;
372   - $model->load(['ProductVariant' => $_variant]);
373   - $model->product_id = $this->product_id;
374   - $model->save();
375 352 }
376   - if (!empty($todel)) {
377   - ProductVariant::deleteAll(['product_variant_id' => $todel]);
  353 +
  354 + if (!empty( $this->brand )) {
  355 + $name = $name . ' ' . $this->brand->name;
378 356 }
  357 + $name .= ' ' . $this->name;
  358 + return $name;
379 359 }
380   - }
381   -
382   - public function beforeDelete() {
383   - ProductImage::deleteAll(['product_id' => $this->product_id]);
384   - ProductCategory::deleteAll(['product_id' => $this->product_id]);
385   - ProductVariant::deleteAll(['product_id' => $this->product_id]);
386   - ProductOption::deleteAll(['product_id' => $this->product_id]);
387   - //ProductVariantOption::deleteAll(['product_id' => $this->product_id]);
388   - ProductStock::deleteAll(['product_id' => $this->product_id]);
389   - Share::deleteAll(['product_id' => $this->product_id]);
390   - return true;
391   - }
392   -
393   - public function imagesUpload()
394   - {
395   - if ($this->validate()) {
396   - $images = [];
397   - foreach ($this->imagesUpload as $image) {
398   - $imageName = $image->baseName .'.'. $image->extension;
399   - $i = 0;
400   - while(file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) {
401   - $i++;
402   - $imageName = $image->baseName .'_'. $i .'.'. $image->extension;
403   - }
404   -
405   - $image->saveAs(Yii::getAlias('@imagesDir/products/' .$imageName));
406   - $images[] = $imageName;
  360 +
  361 + public function getCategories()
  362 + {
  363 + return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ])
  364 + ->viaTable('product_category', [ 'product_id' => 'product_id' ]);
  365 + // return $this->getRelations('product_categories');
  366 + }
  367 + public function getCategoriesWithName()
  368 + {
  369 + return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ])
  370 + ->viaTable('product_category', [ 'product_id' => 'product_id' ])
  371 + ->joinWith('categoryNames');
  372 + // return $this->getRelations('product_categories');
  373 + }
  374 +
  375 + public function getCategoriesNames()
  376 + {
  377 + $result = [];
  378 + foreach ($this->categories as $category) {
  379 + $result[] = $category->name;
407 380 }
408   - return $images;
409   - } else {
410   - return false;
  381 + return $result;
411 382 }
412   - }
413   -
414   - public function getImagesHTML() {
415   - $op = [];
416   - if ($this->images) {
417   - foreach ($this->images as $image) {
418   - $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb');
  383 +
  384 + public function getVariantsWithFilters()
  385 + {
  386 + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ])
  387 + ->with('filters');
  388 + }
  389 +
  390 + /**
  391 + * @return ActiveQuery
  392 + */
  393 + public function getCategory()
  394 + {
  395 + return $this->hasOne(Category::className(), [ 'category_id' => 'category_id' ])
  396 + ->viaTable('product_category', [ 'product_id' => 'product_id' ]);
  397 + }
  398 +
  399 + public function getOptions()
  400 + {
  401 + return $this->hasMany(TaxOption::className(), [ 'tax_option_id' => 'option_id' ])
  402 + ->viaTable('product_option', [ 'product_id' => 'product_id' ]);
  403 + }
  404 +
  405 + public function getProperties()
  406 + {
  407 + $groups = $options = [];
  408 + foreach ($this->options as $option) {
  409 + $options[ $option->tax_group_id ][] = $option;
  410 + }
  411 + foreach (TaxGroup::find()
  412 + ->where([ 'tax_group_id' => array_keys($options) ])
  413 + ->all() as $group) {
  414 + if (!empty( $options[ $group->tax_group_id ] )) {
  415 + $group->_options = $options[ $group->tax_group_id ];
  416 + $groups[] = $group;
  417 + }
419 418 }
  419 + return $groups;
420 420 }
421   - return $op;
422   - }
423   -
424   - public function getImagesConfig() {
425   - $op = [];
426   - if ($this->images) {
427   - foreach ($this->images as $image) {
428   - $op[] = [
429   - 'caption' => $image->image,
430   - 'width' => '120px',
431   - 'url' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $image->product_image_id]),
432   - 'key' => $image->product_image_id,
433   - 'extra' => [
434   - 'id' => $image->product_image_id,
435   - ],
436   - ];
  421 +
  422 + public function getActiveProperties($category_id)
  423 + {
  424 + $groups = $options = [];
  425 + foreach ($this->options as $option) {
  426 + $options[ $option->tax_group_id ][] = $option;
437 427 }
  428 + foreach (TaxGroup::find()
  429 + ->joinWith('categories')
  430 + ->where(
  431 + [
  432 + 'tax_group.tax_group_id' => array_keys($options),
  433 + 'tax_group.display' => true,
  434 + 'category.category_id' => $category_id,
  435 + ]
  436 + )
  437 + ->all() as $group) {
  438 + if (!empty( $options[ $group->tax_group_id ] )) {
  439 + $group->_options = $options[ $group->tax_group_id ];
  440 + $groups[] = $group;
  441 + }
  442 + }
  443 + return $groups;
438 444 }
439   - return $op;
440   - }
441   -
442   - public function recalculateRating() {
  445 +
  446 + public function getStocks()
  447 + {
  448 + return $this->hasMany(Stock::className(), [ 'stock_id' => 'stock_id' ])
  449 + ->viaTable(ProductStock::tableName(), [ 'product_id' => 'product_id' ]);
  450 + }
  451 +
443 452 /**
444   - * @var ProductToRating $averageRating
  453 + * @inheritdoc
  454 + * @return ProductQuery the active query used by this AR class.
445 455 */
446   - $average = $this->getComments()->joinWith('rating')->select(['average' => 'avg(artbox_comment_rating.value)::float'])->scalar();
447   - if(!$average) {
448   - $average = 0;
  456 + public static function find()
  457 + {
  458 + return new ProductQuery(get_called_class());
449 459 }
450   - $averageRating = $this->averageRating;
451   - if(!empty($averageRating)) {
452   - $averageRating->value = $average;
453   - } else {
454   - $averageRating = new ProductToRating(['product_id' => $this->product_id, 'value' => $average]);
  460 +
  461 + public function getQuantity()
  462 + {
  463 + return ProductStock::find()
  464 + ->where([ 'product_id' => $this->product_id ])
  465 + ->sum('quantity');
455 466 }
456   - if($averageRating->save()) {
  467 +
  468 + public function afterSave($insert, $changedAttributes)
  469 + {
  470 + parent::afterSave($insert, $changedAttributes);
  471 +
  472 + // $images = UploadedFile::getInstance($this, 'imagesUpload');
  473 + // var_dump($images);exit;
  474 +
  475 + // if (!empty($this->imagesUpload)) {
  476 + // if (!is_array($this->imagesUpload)) {
  477 + // $this->imagesUpload = [$this->imagesUpload];
  478 + // }
  479 + // foreach($this->imagesUpload as $image) {
  480 + // $image->saveAs((Yii::getAlias('@frontend/web/storage/products/original/' . $image->baseName .'_'. uniqid() . '.' . $image->extension)));
  481 + // }
  482 + //
  483 + //
  484 + // }
  485 +
  486 + if (!empty( $this->_variants )) {
  487 + $todel = [];
  488 + foreach ($this->variants ? : [] as $_variant) {
  489 + $todel[ $_variant->product_variant_id ] = $_variant->product_variant_id;
  490 + }
  491 + foreach ($this->_variants as $_variant) {
  492 + if (!is_array($_variant)) {
  493 + return;
  494 + }
  495 + if (!empty( $_variant[ 'product_variant_id' ] )) {
  496 + unset( $todel[ $_variant[ 'product_variant_id' ] ] );
  497 + $model = ProductVariant::findOne($_variant[ 'product_variant_id' ]);
  498 + } else {
  499 + $model = new ProductVariant();
  500 + }
  501 + $_variant[ 'product_id' ] = $this->product_id;
  502 + $model->load([ 'ProductVariant' => $_variant ]);
  503 + $model->product_id = $this->product_id;
  504 + $model->save();
  505 + }
  506 + if (!empty( $todel )) {
  507 + ProductVariant::deleteAll([ 'product_variant_id' => $todel ]);
  508 + }
  509 + }
  510 + }
  511 +
  512 + public function beforeDelete()
  513 + {
  514 + ProductImage::deleteAll([ 'product_id' => $this->product_id ]);
  515 + ProductCategory::deleteAll([ 'product_id' => $this->product_id ]);
  516 + ProductVariant::deleteAll([ 'product_id' => $this->product_id ]);
  517 + ProductOption::deleteAll([ 'product_id' => $this->product_id ]);
  518 + //ProductVariantOption::deleteAll(['product_id' => $this->product_id]);
  519 + ProductStock::deleteAll([ 'product_id' => $this->product_id ]);
  520 + Share::deleteAll([ 'product_id' => $this->product_id ]);
457 521 return true;
458   - } else {
459   - return false;
  522 + }
  523 +
  524 + public function imagesUpload()
  525 + {
  526 + if ($this->validate()) {
  527 + $images = [];
  528 + foreach ($this->imagesUpload as $image) {
  529 + $imageName = $image->baseName . '.' . $image->extension;
  530 + $i = 0;
  531 + while (file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) {
  532 + $i++;
  533 + $imageName = $image->baseName . '_' . $i . '.' . $image->extension;
  534 + }
  535 +
  536 + $image->saveAs(Yii::getAlias('@imagesDir/products/' . $imageName));
  537 + $images[] = $imageName;
  538 + }
  539 + return $images;
  540 + } else {
  541 + return false;
  542 + }
  543 + }
  544 +
  545 + public function getImagesHTML()
  546 + {
  547 + $op = [];
  548 + if ($this->images) {
  549 + foreach ($this->images as $image) {
  550 + $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb');
  551 + }
  552 + }
  553 + return $op;
  554 + }
  555 +
  556 + public function getImagesConfig()
  557 + {
  558 + $op = [];
  559 + if ($this->images) {
  560 + foreach ($this->images as $image) {
  561 + $op[] = [
  562 + 'caption' => $image->image,
  563 + 'width' => '120px',
  564 + 'url' => \yii\helpers\Url::to(
  565 + [
  566 + '/product/manage/delimg',
  567 + 'id' => $image->product_image_id,
  568 + ]
  569 + ),
  570 + 'key' => $image->product_image_id,
  571 + 'extra' => [
  572 + 'id' => $image->product_image_id,
  573 + ],
  574 + ];
  575 + }
  576 + }
  577 + return $op;
  578 + }
  579 +
  580 + public function recalculateRating()
  581 + {
  582 + /**
  583 + * @var ProductToRating $averageRating
  584 + */
  585 + $average = $this->getComments()
  586 + ->joinWith('rating')
  587 + ->select([ 'average' => 'avg(artbox_comment_rating.value)::float' ])
  588 + ->scalar();
  589 + if (!$average) {
  590 + $average = 0;
  591 + }
  592 + $averageRating = $this->averageRating;
  593 + if (!empty( $averageRating )) {
  594 + $averageRating->value = $average;
  595 + } else {
  596 + $averageRating = new ProductToRating(
  597 + [
  598 + 'product_id' => $this->product_id,
  599 + 'value' => $average,
  600 + ]
  601 + );
  602 + }
  603 + if ($averageRating->save()) {
  604 + return true;
  605 + } else {
  606 + return false;
  607 + }
  608 + }
  609 +
  610 + public function getComments()
  611 + {
  612 + return $this->hasMany(CommentModel::className(), [ 'entity_id' => 'product_id' ])
  613 + ->where(
  614 + [
  615 + 'artbox_comment.entity' => self::className(),
  616 + 'artbox_comment.status' => CommentModel::STATUS_ACTIVE,
  617 + 'artbox_comment.artbox_comment_pid' => null,
  618 + ]
  619 + );
  620 + }
  621 +
  622 + public function getAverageRating()
  623 + {
  624 + return $this->hasOne(ProductToRating::className(), [ 'product_id' => 'product_id' ]);
  625 + }
  626 +
  627 + public function getTaxGroupsByLevel($level)
  628 + {
  629 + $categories = ArrayHelper::getColumn($this->categories, 'category_id');
  630 + return TaxGroup::find()
  631 + ->distinct()
  632 + ->innerJoin('relation', 'entity1_id = tax_group_id')
  633 + ->andWhere([ 'relation.entity2_id' => $categories ])
  634 + ->andWhere([ 'level' => $level ]);
460 635 }
461 636 }
462   -
463   - public function getComments() {
464   - return $this->hasMany(CommentModel::className(), ['entity_id' => 'product_id'])->where(['artbox_comment.entity' => self::className(), 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, 'artbox_comment.artbox_comment_pid' => NULL]);
465   - }
466   -
467   - public function getAverageRating() {
468   - return $this->hasOne(ProductToRating::className(), ['product_id' => 'product_id']);
469   - }
470   -
471   - public function getTaxGroupsByLevel($level)
472   - {
473   - $categories = ArrayHelper::getColumn($this->categories, 'category_id');
474   - return TaxGroup::find()->distinct()->innerJoin('relation', 'entity1_id = tax_group_id')->andWhere(['relation.entity2_id' => $categories])->andWhere(['level' => $level]);
475   - }
476   -}
... ...
common/modules/rubrication/models/TaxGroup.php
... ... @@ -20,6 +20,7 @@ use Yii;
20 20 * @property integer $level
21 21 * @property integer $sort
22 22 * @property boolean $display
  23 + * @property boolean $use_in_name
23 24 * @property TaxGroupToGroup[] $taxGroupToGroups
24 25 * @property TaxGroupToGroup[] $taxGroupToGroups0
25 26 * @property TaxOption[] $taxOptions
... ... @@ -65,7 +66,7 @@ class TaxGroup extends \yii\db\ActiveRecord
65 66 return [
66 67 [['name', 'module'], 'required'],
67 68 [['description', 'settings'], 'string'],
68   - [['hierarchical', 'is_filter', 'display'], 'boolean'],
  69 + [['hierarchical', 'is_filter', 'display', 'use_in_name'], 'boolean'],
69 70 [['level', 'sort'], 'integer'],
70 71 [['alias', 'module'], 'string', 'max' => 50],
71 72 [['name'], 'string', 'max' => 255],
... ... @@ -89,6 +90,7 @@ class TaxGroup extends \yii\db\ActiveRecord
89 90 'is_filter' => 'Use in filter',
90 91 'sort' => 'Sort',
91 92 'display' => 'Display',
  93 + 'use_in_name' => 'Использовать в названии',
92 94 ];
93 95 }
94 96  
... ...
common/modules/rubrication/models/TaxOption.php
... ... @@ -19,6 +19,7 @@ use yii\db\ActiveRecord;
19 19 * @property string $alias
20 20 * @property integer $sort
21 21 * @property integer $default_value
  22 + * @property string $synonym
22 23 *
23 24 * @property TaxEntityRelation[] $taxEntityRelations
24 25 * @property TaxGroup $taxGroup
... ... @@ -73,6 +74,7 @@ class TaxOption extends \yii\db\ActiveRecord
73 74 return [
74 75 [['tax_group_id','name'], 'required'],
75 76 [['tax_group_id', 'parent_id', 'sort', 'default_value'], 'integer'],
  77 + [['synonym'], 'string', 'max' => 255],
76 78 [['alias'], 'string', 'max' => 50],
77 79 [['tax_group_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxGroup::className(), 'targetAttribute' => ['tax_group_id' => 'tax_group_id']],
78 80 // [['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxOption::className(), 'targetAttribute' => ['parent_id' => 'tax_option_id']],
... ... @@ -91,6 +93,7 @@ class TaxOption extends \yii\db\ActiveRecord
91 93 'alias' => Yii::t('app', 'Alias'),
92 94 'sort' => Yii::t('app', 'Sort'),
93 95 'default_value' => Yii::t('app', 'Default Value'),
  96 + 'synonym' => Yii::t('product', 'Синоним'),
94 97 ];
95 98 }
96 99  
... ...
common/modules/rubrication/views/tax-group/_form.php
... ... @@ -38,6 +38,8 @@ use common\components\artboxtree\ArtboxTreeHelper;
38 38  
39 39 <?= $form->field($model, 'display')->checkbox() ?>
40 40  
  41 + <?= $form->field($model, 'use_in_name')->checkbox() ?>
  42 +
41 43 <?= $form->field($model, 'sort')->textInput() ?>
42 44  
43 45 <div class="form-group">
... ...
common/modules/rubrication/views/tax-option/_form.php
... ... @@ -30,8 +30,10 @@ use common\modules\rubrication\helpers\RubricationHelper;
30 30  
31 31 <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
32 32 <!-- --><?php //require(dirname(__FILE__) .'/value/_fields_'. $group->module .'.php')?>
33   -
  33 +
34 34 <?= $form->field($model, 'alias')->textInput(['maxlength' => true]) ?>
  35 +
  36 + <?= $form->field($model, 'synonym')->textInput(['maxlength' => true]) ?>
35 37  
36 38 <?php if ($group->hierarchical) :?>
37 39 <?php
... ...
console/migrations/m170303_130758_add_columns_synonym.php 0 → 100644
  1 +<?php
  2 +
  3 + use yii\db\Migration;
  4 +
  5 + class m170303_130758_add_columns_synonym extends Migration
  6 + {
  7 + public function up()
  8 + {
  9 + $this->addColumn('category', 'synonym', $this->string());
  10 + $this->addColumn(
  11 + 'tax_group',
  12 + 'use_in_name',
  13 + $this->boolean()
  14 + ->defaultValue(false)
  15 + );
  16 + $this->addColumn('tax_option', 'synonym', $this->string());
  17 + }
  18 +
  19 + public function down()
  20 + {
  21 + $this->dropColumn('tax_group', 'use_in_menu');
  22 + $this->dropColumn('tax_option', 'synonym');
  23 + $this->dropColumn('category', 'synonym');
  24 + }
  25 + }
... ...
frontend/views/catalog/product_item.php
... ... @@ -64,7 +64,7 @@
64 64 'product' => $product,
65 65 '#' => 'm' . $product->enabledVariants[ 0 ]->product_variant_id,
66 66 ]) ?>"
67   - class="name"><?= $product->fullnamewithcategory ?>
  67 + class="name"><?= $product->fullname ?>
68 68 </a></div>
69 69  
70 70 <?php
... ...