diff --git a/backend/views/category/_form.php b/backend/views/category/_form.php index ae21c57..6bde578 100755 --- a/backend/views/category/_form.php +++ b/backend/views/category/_form.php @@ -20,8 +20,10 @@ use kartik\select2\Select2; ]); ?> field($model, 'name')->textInput() ?> - + field($model, 'alias')->textInput(['maxlength' => true]) ?> + + field($model, 'synonym')->textInput(['maxlength' => true]) ?> field($model, 'parent_id')->dropDownList($categories, [ 'prompt' => Yii::t('rubrication', 'Root category'), diff --git a/common/modules/product/models/Category.php b/common/modules/product/models/Category.php index cae96b3..c5a225f 100755 --- a/common/modules/product/models/Category.php +++ b/common/modules/product/models/Category.php @@ -29,6 +29,7 @@ use yii\db\Query; * @property integer $product_unit_id * @property string $alias * @property boolean $populary + * @property string $synonym * * @property CategoryName $categoryName * @property Product[] $products @@ -89,7 +90,7 @@ class Category extends \yii\db\ActiveRecord [['name'], 'string'], [['parent_id', 'depth', 'category_name_id', 'product_unit_id'], 'integer'], [['path', 'meta_desc', 'h1', 'seo_text'], 'string'], - [['meta_title', 'image'], 'string', 'max' => 255], + [['meta_title', 'image', 'synonym'], 'string', 'max' => 255], [['meta_robots'], 'string', 'max' => 50], [['alias', 'name'], 'string', 'max' => 250], [['populary'], 'boolean'], @@ -122,6 +123,7 @@ class Category extends \yii\db\ActiveRecord 'populary' => Yii::t('product', 'Populary'), 'name' => Yii::t('product', 'Name'), 'remote_id' => Yii::t('product', 'Remote ID'), + 'synonym' => Yii::t('product', 'Синоним'), ]; } diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index d07d724..3f02a79 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -1,476 +1,636 @@ relationBehavior::className(), - 'relations' => [ - 'product_categories' => 'entity1', // Product category - 'product_option' => 'entity1' // Product category - ] - ], - [ - 'class' =>FilterBehavior::className(), - ], - [ - 'class' => Slug::className(), - 'in_attribute' => 'name', - 'out_attribute' => 'alias', - 'translit' => true - ] - ]; - } - - /** - * @inheritdoc - */ - public static function tableName() - { - return '{{%product}}'; - } - - /** - * @inheritdoc - */ - public function rules() - { - return [ -// [['categories'], 'required'], - [['brand_id'], 'integer'], - [['name'], 'string', 'max' => 150], - [['alias'], 'string', 'max' => 250], - [['categories', 'variants', 'options', 'imagesUpload'], 'safe'], -// [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif', 'maxFiles' => 50], - [['description', 'video'], 'safe'], - [['is_top', 'is_new', 'akciya'], 'boolean'], -// [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], - ]; - } - - /** - * @inheritdoc - */ - public function attributeLabels() - { - return [ - 'product_id' => Yii::t('product', 'ID'), - 'name' => Yii::t('product', 'Name'), - 'brand_id' => Yii::t('product', 'Brand'), - 'categories' => Yii::t('product', 'Categories'), // relation behavior field - 'category' => Yii::t('product', 'Category'), // relation behavior field - 'image' => Yii::t('product', 'Image'), - 'images' => Yii::t('product', 'Images'), - 'description' => Yii::t('product', 'Description'), - 'video' => Yii::t('product', 'Video embeded'), - 'variants' => Yii::t('product', 'Variants'), - 'is_top' => Yii::t('product', 'Is top'), - 'is_new' => Yii::t('product', 'Is new'), - 'akciya' => Yii::t('product', 'Is promo'), - ]; - } - - - public function withEventBanner(){ - - } - - - - public function getEvents(){ - return $this->hasMany(Event::className(), ['event_id' => 'event_id'])->viaTable('events_to_products', ['product_id' => 'product_id']); - } - - public function getUrl() { - return '/product/'. $this->alias; - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getBrand() - { - return $this->hasOne(Brand::className(), ['brand_id' => 'brand_id']); - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getImage() - { - return $this->hasOne(ProductImage::className(), ['product_id' => 'product_id']); - } - - /** - * fetch stored image url - * @return string - */ - public function getImageUrl() - { - $image = empty($this->variant) ? null : $this->variant->image; - return !empty($image) ? $image->imageUrl : '/images/no_photo.png'; - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getImages() - { - return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id'])->where(['product_variant_id' => null]); - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getVariant() - { - return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']); - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getEnabledVariant() - { - return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]); - } - - public function getVariantPrice() { - return $this->variant->price; - } - - public function getEnabledVariantPrice() { - return $this->enabledVariants[0]->price; - } - + + namespace common\modules\product\models; + + use common\behaviors\Slug; + use common\models\Event; + use common\models\ProductToRating; + use common\models\Share; + use common\modules\comment\models\CommentModel; + use common\modules\product\behaviors\FilterBehavior; + use common\modules\rubrication\models\TaxGroup; + use common\modules\rubrication\models\TaxOption; + use Yii; + use common\modules\relation\relationBehavior; + use yii\db\ActiveQuery; + use yii\db\ActiveRecord; + use yii\helpers\ArrayHelper; + /** - * @return \yii\db\ActiveQuery + * This is the model class for table "{{%product}}". + * + * @property string $name + * @property integer $brand_id + * @property integer $product_id + * @property Category $category + * @property array $categories + * @property array of ProductVariant $variants + * @property ProductVariant $variant + * @property ProductImage $image + * @property array $images + * @property boolean $is_top + * @property boolean $is_new + * @property boolean $akciya + * @property string $fullname + * @property ProductToRating $averageRating + * @property array $properties + * @property ProductVariant $enabledVariant + * @property array $enabledVariants */ - public function getVariants() - { - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']); - } - - public function getEnabledVariants() + class Product extends \yii\db\ActiveRecord { - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0])->joinWith('image'); - } - - /* - * Get variants grouped by type - */ - public function getEnabledVariantsGrouped() - { - $variants = []; - foreach ($this->enabledVariants as $variant) { - $variants[$variant->product_variant_type_id ? $variant->product_variant_type_id : 1][] = $variant; + /** @var array $_variants */ + public $_variants = []; + + /** @var array $_images */ + public $imagesUpload = []; + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + [ + 'class' => relationBehavior::className(), + 'relations' => [ + 'product_categories' => 'entity1', + // Product category + 'product_option' => 'entity1' + // Product category + ], + ], + [ + 'class' => FilterBehavior::className(), + ], + [ + 'class' => Slug::className(), + 'in_attribute' => 'name', + 'out_attribute' => 'alias', + 'translit' => true, + ], + ]; } - if (empty($variants)) { - return []; + + /** + * @inheritdoc + */ + public static function tableName() + { + return '{{%product}}'; } - $ids = array_keys($variants); - $variants_type = []; - foreach(ProductVariantType::find()->select(['product_variant_type_id', 'name2'])->where(['product_variant_type_id' => $ids])->all() as $variant_type) { - $variant_type->_variants = $variants[$variant_type->product_variant_type_id]; - $variants_type[] = $variant_type; + + /** + * @inheritdoc + */ + public function rules() + { + return [ + // [['categories'], 'required'], + [ + [ 'brand_id' ], + 'integer', + ], + [ + [ 'name' ], + 'string', + 'max' => 150, + ], + [ + [ 'alias' ], + 'string', + 'max' => 250, + ], + [ + [ + 'categories', + 'variants', + 'options', + 'imagesUpload', + ], + 'safe', + ], + // [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif', 'maxFiles' => 50], + [ + [ + 'description', + 'video', + ], + 'safe', + ], + [ + [ + 'is_top', + 'is_new', + 'akciya', + ], + 'boolean', + ], + // [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], + ]; } - return $variants_type; - } - - public function setVariants($variants) { - $this->_variants = $variants; - } - - public function getFullName() - { - $words = [ - 'Рюкзаки' => 'Рюкзак', - 'Несессеры' => 'Несессер', - 'Сумки' => 'Сумка', - 'Чехлы' => 'Чехол', - 'Кошельки' => 'Кошелек', - 'Гермочехлы' => 'Гермочехол', - ]; - return empty($this->brand) ? $this->name : $this->brand->name .' '. $this->name; - } - - public function getFullNameWithCategory() - { - $words = [ - 'Рюкзаки' => 'Рюкзак', - 'Несессеры' => 'Несессер', - 'Сумки' => 'Сумка', - 'Чехлы' => 'Чехол', - 'Кошельки' => 'Кошелек', - 'Гермочехлы' => 'Гермочехол', - ]; - $name = empty($this->brand) ? $this->name : $this->brand->name .' '. $this->name; - return empty($this->category->categoryName->value) ? $name : (isset($words[$this->category->categoryName->value])? $words[$this->category->categoryName->value]: '') .' '. $name; - } - - - public function getCategories() { - return $this->hasMany(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']); -// return $this->getRelations('product_categories'); - } - public function getCategoriesWithName() { - return $this->hasMany(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id'])->joinWith('categoryNames'); -// return $this->getRelations('product_categories'); - } - - public function getCategoriesNames() { - $result = []; - foreach($this->categories as $category) { - $result[] = $category->name; + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'product_id' => Yii::t('product', 'ID'), + 'name' => Yii::t('product', 'Name'), + 'brand_id' => Yii::t('product', 'Brand'), + 'categories' => Yii::t('product', 'Categories'), + // relation behavior field + 'category' => Yii::t('product', 'Category'), + // relation behavior field + 'image' => Yii::t('product', 'Image'), + 'images' => Yii::t('product', 'Images'), + 'description' => Yii::t('product', 'Description'), + 'video' => Yii::t('product', 'Video embeded'), + 'variants' => Yii::t('product', 'Variants'), + 'is_top' => Yii::t('product', 'Is top'), + 'is_new' => Yii::t('product', 'Is new'), + 'akciya' => Yii::t('product', 'Is promo'), + ]; } - return $result; - } - - public function getVariantsWithFilters(){ - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->with('filters'); - } - - /** - * @return ActiveQuery - */ - public function getCategory() { - return $this->hasOne(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']); - } - - public function getOptions() { - return $this->hasMany(TaxOption::className(), ['tax_option_id' => 'option_id'])->viaTable('product_option', ['product_id' => 'product_id']); - } - - public function getProperties() { - $groups = $options = []; - foreach ($this->options as $option) { - $options[$option->tax_group_id][] = $option; - } - foreach (TaxGroup::find()->where(['tax_group_id' => array_keys($options)])->all() as $group) { - if (!empty($options[$group->tax_group_id])) { - $group->_options = $options[$group->tax_group_id]; - $groups[] = $group; - } + + public function withEventBanner() + { + } - return $groups; - } - - public function getActiveProperties($category_id) { - $groups = $options = []; - foreach ($this->options as $option) { - $options[$option->tax_group_id][] = $option; - } - 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) { - if (!empty($options[$group->tax_group_id])) { - $group->_options = $options[$group->tax_group_id]; - $groups[] = $group; - } + + public function getEvents() + { + return $this->hasMany(Event::className(), [ 'event_id' => 'event_id' ]) + ->viaTable('events_to_products', [ 'product_id' => 'product_id' ]); } - return $groups; - } - - public function getStocks() { - return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_id' => 'product_id']); - } - - /** - * @inheritdoc - * @return ProductQuery the active query used by this AR class. - */ - public static function find() - { - return new ProductQuery(get_called_class()); - } - - public function getQuantity() { - return ProductStock::find() - ->where(['product_id' => $this->product_id]) - ->sum('quantity'); - } - - public function afterSave($insert, $changedAttributes) - { - parent::afterSave($insert, $changedAttributes); - -// $images = UploadedFile::getInstance($this, 'imagesUpload'); -// var_dump($images);exit; - -// if (!empty($this->imagesUpload)) { -// if (!is_array($this->imagesUpload)) { -// $this->imagesUpload = [$this->imagesUpload]; -// } -// foreach($this->imagesUpload as $image) { -// $image->saveAs((Yii::getAlias('@frontend/web/storage/products/original/' . $image->baseName .'_'. uniqid() . '.' . $image->extension))); -// } -// -// -// } - - if (!empty($this->_variants)) { - $todel = []; - foreach ($this->variants ?: [] as $_variant) { - $todel[$_variant->product_variant_id] = $_variant->product_variant_id; + + public function getUrl() + { + return '/product/' . $this->alias; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getBrand() + { + return $this->hasOne(Brand::className(), [ 'brand_id' => 'brand_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getImage() + { + return $this->hasOne(ProductImage::className(), [ 'product_id' => 'product_id' ]); + } + + /** + * fetch stored image url + * + * @return string + */ + public function getImageUrl() + { + $image = empty( $this->variant ) ? null : $this->variant->image; + return !empty( $image ) ? $image->imageUrl : '/images/no_photo.png'; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getImages() + { + return $this->hasMany(ProductImage::className(), [ 'product_id' => 'product_id' ]) + ->where([ 'product_variant_id' => null ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getVariant() + { + return $this->hasOne(ProductVariant::className(), [ 'product_id' => 'product_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getEnabledVariant() + { + return $this->hasOne(ProductVariant::className(), [ 'product_id' => 'product_id' ]) + ->andOnCondition( + [ + '!=', + ProductVariant::tableName() . '.stock', + 0, + ] + ); + } + + public function getVariantPrice() + { + return $this->variant->price; + } + + public function getEnabledVariantPrice() + { + return $this->enabledVariants[ 0 ]->price; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getVariants() + { + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ]); + } + + public function getEnabledVariants() + { + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ]) + ->andOnCondition( + [ + '!=', + ProductVariant::tableName() . '.stock', + 0, + ] + ) + ->joinWith('image'); + } + + /* + * Get variants grouped by type + */ + public function getEnabledVariantsGrouped() + { + $variants = []; + foreach ($this->enabledVariants as $variant) { + $variants[ $variant->product_variant_type_id ? $variant->product_variant_type_id : 1 ][] = $variant; + } + if (empty( $variants )) { + return []; + } + $ids = array_keys($variants); + $variants_type = []; + foreach (ProductVariantType::find() + ->select( + [ + 'product_variant_type_id', + 'name2', + ] + ) + ->where([ 'product_variant_type_id' => $ids ]) + ->all() as $variant_type) { + $variant_type->_variants = $variants[ $variant_type->product_variant_type_id ]; + $variants_type[] = $variant_type; } - foreach ($this->_variants as $_variant) { - if (!is_array($_variant)) { - return; + return $variants_type; + } + + public function setVariants($variants) + { + $this->_variants = $variants; + } + + public function Name() + { + $words = [ + 'Рюкзаки' => 'Рюкзак', + 'Несессеры' => 'Несессер', + 'Сумки' => 'Сумка', + 'Чехлы' => 'Чехол', + 'Кошельки' => 'Кошелек', + 'Гермочехлы' => 'Гермочехол', + ]; + return empty( $this->brand ) ? $this->name : $this->brand->name . ' ' . $this->name; + } + + public function getFullNameWithCategory() + { + $words = [ + 'Рюкзаки' => 'Рюкзак', + 'Несессеры' => 'Несессер', + 'Сумки' => 'Сумка', + 'Чехлы' => 'Чехол', + 'Кошельки' => 'Кошелек', + 'Гермочехлы' => 'Гермочехол', + ]; + $name = empty( $this->brand ) ? $this->name : $this->brand->name . ' ' . $this->name; + return empty( $this->category->categoryName->value ) ? $name : ( isset( $words[ $this->category->categoryName->value ] ) ? $words[ $this->category->categoryName->value ] : '' ) . ' ' . $name; + } + + public function getFullname() + { + $name = ''; + $taxOption = TaxOption::find() + ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') + ->where( + [ + 'tax_group.use_in_name' => 1, + 'tax_option.tax_option_id' => ArrayHelper::getColumn( + $this->options, + 'tax_option_id' + ), + ] + ) + ->one(); + if ($taxOption) { + if (!empty( $taxOption->synonym )) { + $taxOption = $taxOption->synonym; + } else { + $taxOption = $taxOption->value; } - if (!empty($_variant['product_variant_id'])) { - unset($todel[$_variant['product_variant_id']]); - $model = ProductVariant::findOne($_variant['product_variant_id']); + $name .= $taxOption; + } else { + if (!empty( $this->category->synonym )) { + $name .= $this->category->synonym; } else { - $model = new ProductVariant(); + $name .= $this->category->name; } - $_variant['product_id'] = $this->product_id; - $model->load(['ProductVariant' => $_variant]); - $model->product_id = $this->product_id; - $model->save(); } - if (!empty($todel)) { - ProductVariant::deleteAll(['product_variant_id' => $todel]); + + if (!empty( $this->brand )) { + $name = $name . ' ' . $this->brand->name; } + $name .= ' ' . $this->name; + return $name; } - } - - public function beforeDelete() { - ProductImage::deleteAll(['product_id' => $this->product_id]); - ProductCategory::deleteAll(['product_id' => $this->product_id]); - ProductVariant::deleteAll(['product_id' => $this->product_id]); - ProductOption::deleteAll(['product_id' => $this->product_id]); - //ProductVariantOption::deleteAll(['product_id' => $this->product_id]); - ProductStock::deleteAll(['product_id' => $this->product_id]); - Share::deleteAll(['product_id' => $this->product_id]); - return true; - } - - public function imagesUpload() - { - if ($this->validate()) { - $images = []; - foreach ($this->imagesUpload as $image) { - $imageName = $image->baseName .'.'. $image->extension; - $i = 0; - while(file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) { - $i++; - $imageName = $image->baseName .'_'. $i .'.'. $image->extension; - } - - $image->saveAs(Yii::getAlias('@imagesDir/products/' .$imageName)); - $images[] = $imageName; + + public function getCategories() + { + return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ]) + ->viaTable('product_category', [ 'product_id' => 'product_id' ]); + // return $this->getRelations('product_categories'); + } + public function getCategoriesWithName() + { + return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ]) + ->viaTable('product_category', [ 'product_id' => 'product_id' ]) + ->joinWith('categoryNames'); + // return $this->getRelations('product_categories'); + } + + public function getCategoriesNames() + { + $result = []; + foreach ($this->categories as $category) { + $result[] = $category->name; } - return $images; - } else { - return false; + return $result; } - } - - public function getImagesHTML() { - $op = []; - if ($this->images) { - foreach ($this->images as $image) { - $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb'); + + public function getVariantsWithFilters() + { + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ]) + ->with('filters'); + } + + /** + * @return ActiveQuery + */ + public function getCategory() + { + return $this->hasOne(Category::className(), [ 'category_id' => 'category_id' ]) + ->viaTable('product_category', [ 'product_id' => 'product_id' ]); + } + + public function getOptions() + { + return $this->hasMany(TaxOption::className(), [ 'tax_option_id' => 'option_id' ]) + ->viaTable('product_option', [ 'product_id' => 'product_id' ]); + } + + public function getProperties() + { + $groups = $options = []; + foreach ($this->options as $option) { + $options[ $option->tax_group_id ][] = $option; + } + foreach (TaxGroup::find() + ->where([ 'tax_group_id' => array_keys($options) ]) + ->all() as $group) { + if (!empty( $options[ $group->tax_group_id ] )) { + $group->_options = $options[ $group->tax_group_id ]; + $groups[] = $group; + } } + return $groups; } - return $op; - } - - public function getImagesConfig() { - $op = []; - if ($this->images) { - foreach ($this->images as $image) { - $op[] = [ - 'caption' => $image->image, - 'width' => '120px', - 'url' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $image->product_image_id]), - 'key' => $image->product_image_id, - 'extra' => [ - 'id' => $image->product_image_id, - ], - ]; + + public function getActiveProperties($category_id) + { + $groups = $options = []; + foreach ($this->options as $option) { + $options[ $option->tax_group_id ][] = $option; } + 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) { + if (!empty( $options[ $group->tax_group_id ] )) { + $group->_options = $options[ $group->tax_group_id ]; + $groups[] = $group; + } + } + return $groups; } - return $op; - } - - public function recalculateRating() { + + public function getStocks() + { + return $this->hasMany(Stock::className(), [ 'stock_id' => 'stock_id' ]) + ->viaTable(ProductStock::tableName(), [ 'product_id' => 'product_id' ]); + } + /** - * @var ProductToRating $averageRating + * @inheritdoc + * @return ProductQuery the active query used by this AR class. */ - $average = $this->getComments()->joinWith('rating')->select(['average' => 'avg(artbox_comment_rating.value)::float'])->scalar(); - if(!$average) { - $average = 0; + public static function find() + { + return new ProductQuery(get_called_class()); } - $averageRating = $this->averageRating; - if(!empty($averageRating)) { - $averageRating->value = $average; - } else { - $averageRating = new ProductToRating(['product_id' => $this->product_id, 'value' => $average]); + + public function getQuantity() + { + return ProductStock::find() + ->where([ 'product_id' => $this->product_id ]) + ->sum('quantity'); } - if($averageRating->save()) { + + public function afterSave($insert, $changedAttributes) + { + parent::afterSave($insert, $changedAttributes); + + // $images = UploadedFile::getInstance($this, 'imagesUpload'); + // var_dump($images);exit; + + // if (!empty($this->imagesUpload)) { + // if (!is_array($this->imagesUpload)) { + // $this->imagesUpload = [$this->imagesUpload]; + // } + // foreach($this->imagesUpload as $image) { + // $image->saveAs((Yii::getAlias('@frontend/web/storage/products/original/' . $image->baseName .'_'. uniqid() . '.' . $image->extension))); + // } + // + // + // } + + if (!empty( $this->_variants )) { + $todel = []; + foreach ($this->variants ? : [] as $_variant) { + $todel[ $_variant->product_variant_id ] = $_variant->product_variant_id; + } + foreach ($this->_variants as $_variant) { + if (!is_array($_variant)) { + return; + } + if (!empty( $_variant[ 'product_variant_id' ] )) { + unset( $todel[ $_variant[ 'product_variant_id' ] ] ); + $model = ProductVariant::findOne($_variant[ 'product_variant_id' ]); + } else { + $model = new ProductVariant(); + } + $_variant[ 'product_id' ] = $this->product_id; + $model->load([ 'ProductVariant' => $_variant ]); + $model->product_id = $this->product_id; + $model->save(); + } + if (!empty( $todel )) { + ProductVariant::deleteAll([ 'product_variant_id' => $todel ]); + } + } + } + + public function beforeDelete() + { + ProductImage::deleteAll([ 'product_id' => $this->product_id ]); + ProductCategory::deleteAll([ 'product_id' => $this->product_id ]); + ProductVariant::deleteAll([ 'product_id' => $this->product_id ]); + ProductOption::deleteAll([ 'product_id' => $this->product_id ]); + //ProductVariantOption::deleteAll(['product_id' => $this->product_id]); + ProductStock::deleteAll([ 'product_id' => $this->product_id ]); + Share::deleteAll([ 'product_id' => $this->product_id ]); return true; - } else { - return false; + } + + public function imagesUpload() + { + if ($this->validate()) { + $images = []; + foreach ($this->imagesUpload as $image) { + $imageName = $image->baseName . '.' . $image->extension; + $i = 0; + while (file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) { + $i++; + $imageName = $image->baseName . '_' . $i . '.' . $image->extension; + } + + $image->saveAs(Yii::getAlias('@imagesDir/products/' . $imageName)); + $images[] = $imageName; + } + return $images; + } else { + return false; + } + } + + public function getImagesHTML() + { + $op = []; + if ($this->images) { + foreach ($this->images as $image) { + $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb'); + } + } + return $op; + } + + public function getImagesConfig() + { + $op = []; + if ($this->images) { + foreach ($this->images as $image) { + $op[] = [ + 'caption' => $image->image, + 'width' => '120px', + 'url' => \yii\helpers\Url::to( + [ + '/product/manage/delimg', + 'id' => $image->product_image_id, + ] + ), + 'key' => $image->product_image_id, + 'extra' => [ + 'id' => $image->product_image_id, + ], + ]; + } + } + return $op; + } + + public function recalculateRating() + { + /** + * @var ProductToRating $averageRating + */ + $average = $this->getComments() + ->joinWith('rating') + ->select([ 'average' => 'avg(artbox_comment_rating.value)::float' ]) + ->scalar(); + if (!$average) { + $average = 0; + } + $averageRating = $this->averageRating; + if (!empty( $averageRating )) { + $averageRating->value = $average; + } else { + $averageRating = new ProductToRating( + [ + 'product_id' => $this->product_id, + 'value' => $average, + ] + ); + } + if ($averageRating->save()) { + return true; + } else { + return false; + } + } + + public function getComments() + { + 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, + ] + ); + } + + public function getAverageRating() + { + return $this->hasOne(ProductToRating::className(), [ 'product_id' => 'product_id' ]); + } + + public function getTaxGroupsByLevel($level) + { + $categories = ArrayHelper::getColumn($this->categories, 'category_id'); + return TaxGroup::find() + ->distinct() + ->innerJoin('relation', 'entity1_id = tax_group_id') + ->andWhere([ 'relation.entity2_id' => $categories ]) + ->andWhere([ 'level' => $level ]); } } - - public function getComments() { - 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]); - } - - public function getAverageRating() { - return $this->hasOne(ProductToRating::className(), ['product_id' => 'product_id']); - } - - public function getTaxGroupsByLevel($level) - { - $categories = ArrayHelper::getColumn($this->categories, 'category_id'); - return TaxGroup::find()->distinct()->innerJoin('relation', 'entity1_id = tax_group_id')->andWhere(['relation.entity2_id' => $categories])->andWhere(['level' => $level]); - } -} diff --git a/common/modules/rubrication/models/TaxGroup.php b/common/modules/rubrication/models/TaxGroup.php index 0bc8b42..fd1e881 100755 --- a/common/modules/rubrication/models/TaxGroup.php +++ b/common/modules/rubrication/models/TaxGroup.php @@ -20,6 +20,7 @@ use Yii; * @property integer $level * @property integer $sort * @property boolean $display + * @property boolean $use_in_name * @property TaxGroupToGroup[] $taxGroupToGroups * @property TaxGroupToGroup[] $taxGroupToGroups0 * @property TaxOption[] $taxOptions @@ -65,7 +66,7 @@ class TaxGroup extends \yii\db\ActiveRecord return [ [['name', 'module'], 'required'], [['description', 'settings'], 'string'], - [['hierarchical', 'is_filter', 'display'], 'boolean'], + [['hierarchical', 'is_filter', 'display', 'use_in_name'], 'boolean'], [['level', 'sort'], 'integer'], [['alias', 'module'], 'string', 'max' => 50], [['name'], 'string', 'max' => 255], @@ -89,6 +90,7 @@ class TaxGroup extends \yii\db\ActiveRecord 'is_filter' => 'Use in filter', 'sort' => 'Sort', 'display' => 'Display', + 'use_in_name' => 'Использовать в названии', ]; } diff --git a/common/modules/rubrication/models/TaxOption.php b/common/modules/rubrication/models/TaxOption.php index 601c61c..8db8795 100755 --- a/common/modules/rubrication/models/TaxOption.php +++ b/common/modules/rubrication/models/TaxOption.php @@ -19,6 +19,7 @@ use yii\db\ActiveRecord; * @property string $alias * @property integer $sort * @property integer $default_value + * @property string $synonym * * @property TaxEntityRelation[] $taxEntityRelations * @property TaxGroup $taxGroup @@ -73,6 +74,7 @@ class TaxOption extends \yii\db\ActiveRecord return [ [['tax_group_id','name'], 'required'], [['tax_group_id', 'parent_id', 'sort', 'default_value'], 'integer'], + [['synonym'], 'string', 'max' => 255], [['alias'], 'string', 'max' => 50], [['tax_group_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxGroup::className(), 'targetAttribute' => ['tax_group_id' => 'tax_group_id']], // [['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxOption::className(), 'targetAttribute' => ['parent_id' => 'tax_option_id']], @@ -91,6 +93,7 @@ class TaxOption extends \yii\db\ActiveRecord 'alias' => Yii::t('app', 'Alias'), 'sort' => Yii::t('app', 'Sort'), 'default_value' => Yii::t('app', 'Default Value'), + 'synonym' => Yii::t('product', 'Синоним'), ]; } diff --git a/common/modules/rubrication/views/tax-group/_form.php b/common/modules/rubrication/views/tax-group/_form.php index d2b44e7..a3a02ed 100755 --- a/common/modules/rubrication/views/tax-group/_form.php +++ b/common/modules/rubrication/views/tax-group/_form.php @@ -38,6 +38,8 @@ use common\components\artboxtree\ArtboxTreeHelper; field($model, 'display')->checkbox() ?> + field($model, 'use_in_name')->checkbox() ?> + field($model, 'sort')->textInput() ?>
diff --git a/common/modules/rubrication/views/tax-option/_form.php b/common/modules/rubrication/views/tax-option/_form.php index d73bcbd..517bb37 100755 --- a/common/modules/rubrication/views/tax-option/_form.php +++ b/common/modules/rubrication/views/tax-option/_form.php @@ -30,8 +30,10 @@ use common\modules\rubrication\helpers\RubricationHelper; field($model, 'name')->textInput(['maxlength' => true]) ?> module .'.php')?> - + field($model, 'alias')->textInput(['maxlength' => true]) ?> + + field($model, 'synonym')->textInput(['maxlength' => true]) ?> hierarchical) :?> addColumn('category', 'synonym', $this->string()); + $this->addColumn( + 'tax_group', + 'use_in_name', + $this->boolean() + ->defaultValue(false) + ); + $this->addColumn('tax_option', 'synonym', $this->string()); + } + + public function down() + { + $this->dropColumn('tax_group', 'use_in_menu'); + $this->dropColumn('tax_option', 'synonym'); + $this->dropColumn('category', 'synonym'); + } + } diff --git a/frontend/views/catalog/product_item.php b/frontend/views/catalog/product_item.php index b846b3e..0fea24c 100755 --- a/frontend/views/catalog/product_item.php +++ b/frontend/views/catalog/product_item.php @@ -64,7 +64,7 @@ 'product' => $product, '#' => 'm' . $product->enabledVariants[ 0 ]->product_variant_id, ]) ?>" - class="name">fullnamewithcategory ?> + class="name">fullname ?>