diff --git a/backend/views/category/_form.php b/backend/views/category/_form.php index 68cf4c8..626a793 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/config/main.php b/common/config/main.php index 0b034e2..d967c04 100755 --- a/common/config/main.php +++ b/common/config/main.php @@ -9,7 +9,7 @@ return [ ], 'components' => [ 'cache' => [ - 'class' => 'yii\caching\MemCache', + 'class' => 'yii\caching\DummyCache', 'keyPrefix' => 'linija_' ], 'sms' => [ diff --git a/common/modules/product/models/Category.php b/common/modules/product/models/Category.php index 51d9259..897ef29 100755 --- a/common/modules/product/models/Category.php +++ b/common/modules/product/models/Category.php @@ -30,6 +30,7 @@ use common\behaviors\Slug; * @property string $alias * @property boolean $populary * @property string $name + * @property string $synonym * * @property CategoryName $categoryName * @property Product[] $products @@ -77,7 +78,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'], @@ -108,6 +109,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 57b69c9..e984ab9 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -1,508 +1,667 @@ SaveMultipleImgBehavior::className(), - 'fields' => [ - ['name'=>'imagesUpload','directory' => 'products' ] - ] - ], - [ - 'class' => FilterBehavior::className(), - ], - [ - 'class' => Slug::className(), - 'in_attribute' => 'name', - 'out_attribute' => 'alias', - 'translit' => true - ] - ]; - } - - /** - * @inheritdoc - */ - public static function tableName() - { - return '{{%product}}'; - } - - - /** - * @return \yii\db\ActiveQuery - */ - public function getEvents(){ - return $this->hasMany(Event::className(), ['event_id' => 'event_id'])->viaTable('events_to_products', ['product_id' => 'product_id']); - } - - /** - * @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 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->image) ? $this->image : $this->variant->image; - return !empty($image) ? $image->imageUrl : '/storage/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 getVariants() - { - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']); - } + + namespace common\modules\product\models; + + use common\behaviors\SaveMultipleImgBehavior; + use common\behaviors\Slug; + 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 yii\db\ActiveQuery; + use yii\db\ActiveRecord; + use yii\helpers\ArrayHelper; + use common\models\Event; /** - * @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 ProductToRating $averageRating + * @property array $properties + * @property ProductVariant $enabledVariant + * @property array $enabledVariants */ - public function getEnabledVariant() + class Product extends \yii\db\ActiveRecord { - return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']) - ->andOnCondition(['!=', ProductVariant::tableName() .'.status', 1]); - } - - public function getVariantPrice() { - return $this->variant->price; - } - - - public function getPrice() { - return $this->variant->price; - } - - public function getEnabledVariantPrice() { - return $this->enabledVariants[0]->price; - } - - - - public function getEnabledVariants() - { - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.status', 1])->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; + public $_variants = []; + public $_categories = []; + public $_options = []; + + /** @var array $_images */ + public $imagesUpload = ''; + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + [ + 'class' => SaveMultipleImgBehavior::className(), + 'fields' => [ + [ + 'name' => 'imagesUpload', + 'directory' => 'products', + ], + ], + ], + [ + '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; + + /** + * @return \yii\db\ActiveQuery + */ + public function getEvents() + { + return $this->hasMany(Event::className(), [ 'event_id' => 'event_id' ]) + ->viaTable('events_to_products', [ 'product_id' => 'product_id' ]); } - return $variants_type; - } - - public function setVariants($variants) { - $this->_variants = $variants; - } - - public function getFullName() - { - return empty($this->brand) ? $this->name : $this->brand->name .' '. $this->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']); -// return $this->getRelations('product_categories'); - } - - public function getCategoriesNames() { - $result = []; - foreach($this->categories as $category) { - $result[] = $category->name; + + /** + * @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 $result; - } - - public function getVariantsWithFilters(){ - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->with(['filters','image']); - } - - /** - * @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; + + /** + * @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 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->image ) ? $this->image : $this->variant->image; + return !empty( $image ) ? $image->imageUrl : '/storage/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 getVariants() + { + return $this->hasMany(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() . '.status', + 1, + ] + ); + } + + public function getVariantPrice() + { + return $this->variant->price; + } + + public function getPrice() + { + return $this->variant->price; + } + + public function getEnabledVariantPrice() + { + return $this->enabledVariants[ 0 ]->price; + } + + public function getEnabledVariants() + { + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ]) + ->andOnCondition( + [ + '!=', + ProductVariant::tableName() . '.status', + 1, + ] + ) + ->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; + } + return $variants_type; } - return $groups; - } - - public function getActiveProperties($category_id) { - $cacheKey = ['ActiveProperties','id' => $category_id, 'options' =>$this->options, 'product_id' => $this->product_id ]; - - if(!$groups = Yii::$app->cache->get($cacheKey)){ - + + public function setVariants($variants) + { + $this->_variants = $variants; + } + + public function getFullName() + { + $name = ''; + $taxOption = TaxOption::find() + ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') + ->innerJoin('tax_group_to_category', 'tax_group.tax_group_id = tax_group_to_category.tax_group_id') + ->where( + [ + 'tax_group_to_category.category_id' => $this->category->category_id, + '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; + } + $name .= $taxOption; + } else { + if(!empty($this->category->synonym)) { + $name .= $this->category->synonym; + } else { + $name .= $this->category->name; + } + } + + if (!empty( $this->brand )) { + $name = $name . ' ' . $this->brand->name; + } + $name .= ' ' . $this->name; + return $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' ]); + // return $this->getRelations('product_categories'); + } + + public function getCategoriesNames() + { + $result = []; + foreach ($this->categories as $category) { + $result[] = $category->name; + } + return $result; + } + + public function getVariantsWithFilters() + { + return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'product_id' ]) + ->with( + [ + 'filters', + 'image', + ] + ); + } + + /** + * @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; + $options[ $option->tax_group_id ][] = $option; } - - $taxGroups = TaxGroup::find()->joinWith('categories')->where(['tax_group.tax_group_id' => array_keys($options), 'tax_group.display' => TRUE, 'category.category_id' => $category_id])->all(); - - foreach ($taxGroups as $group) { - if (!empty($options[$group->tax_group_id])) { - $group->_options = $options[$group->tax_group_id]; + 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; } } - Yii::$app->cache->set($cacheKey,$groups,3600*24); + return $groups; } - - return $groups; - } - - public function getStocks() { - return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_id' => 'product_id']); - } - - - public function getQuantity() { - return ProductStock::find() - ->where(['product_id' => $this->product_id]) - ->sum('quantity'); - } - - - public function beforeSave($insert) - { - - if(parent::beforeSave($insert)){ - - return true; - } - return false; - - - - } - - public function afterSave($insert, $changedAttributes) - { - parent::afterSave($insert, $changedAttributes); - - - if(!empty($this->categories)){ - $categories = Category::findAll($this->categories); - $this->unlinkAll('categories', true); - foreach($categories as $category){ - $this->link('categories', $category); + + public function getActiveProperties($category_id) + { + $cacheKey = [ + 'ActiveProperties', + 'id' => $category_id, + 'options' => $this->options, + 'product_id' => $this->product_id, + ]; + + if (!$groups = Yii::$app->cache->get($cacheKey)) { + + $groups = $options = []; + foreach ($this->options as $option) { + $options[ $option->tax_group_id ][] = $option; + } + + $taxGroups = TaxGroup::find() + ->joinWith('categories') + ->where( + [ + 'tax_group.tax_group_id' => array_keys($options), + 'tax_group.display' => true, + 'category.category_id' => $category_id, + ] + ) + ->all(); + + foreach ($taxGroups as $group) { + if (!empty( $options[ $group->tax_group_id ] )) { + $group->_options = $options[ $group->tax_group_id ]; + $groups[] = $group; + } + } + Yii::$app->cache->set($cacheKey, $groups, 3600 * 24); } + + return $groups; + } + + public function getStocks() + { + return $this->hasMany(Stock::className(), [ 'stock_id' => 'stock_id' ]) + ->viaTable(ProductStock::tableName(), [ 'product_id' => 'product_id' ]); } - - if(!empty($this->options)){ - $options = TaxOption::findAll($this->options); - $this->unlinkAll('options',true); - foreach($options as $option){ - $this->link('options', $option); + + public function getQuantity() + { + return ProductStock::find() + ->where([ 'product_id' => $this->product_id ]) + ->sum('quantity'); + } + + public function beforeSave($insert) + { + + if (parent::beforeSave($insert)) { + + return true; } + return false; + } - - - if (!empty($this->_variants)) { - $todel = []; - foreach ($this->variants ?: [] as $_variant) { - $todel[$_variant->product_variant_id] = $_variant->product_variant_id; + + public function afterSave($insert, $changedAttributes) + { + parent::afterSave($insert, $changedAttributes); + + if (!empty( $this->categories )) { + $categories = Category::findAll($this->categories); + $this->unlinkAll('categories', true); + foreach ($categories as $category) { + $this->link('categories', $category); + } } - foreach ($this->_variants as $_variant) { - if (!is_array($_variant)) { - return; + + if (!empty( $this->options )) { + $options = TaxOption::findAll($this->options); + $this->unlinkAll('options', true); + foreach ($options as $option) { + $this->link('options', $option); } - if (!empty($_variant['product_variant_id'])) { - unset($todel[$_variant['product_variant_id']]); - $model = ProductVariant::findOne($_variant['product_variant_id']); - } else { - $model = new ProductVariant(); + } + + 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 ]); } - $_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() + { + if (parent::beforeDelete()) { + $this->unlinkAll('categories', true); + $this->unlinkAll('options', true); + ProductImage::deleteAll([ 'product_id' => $this->product_id ]); + + ProductVariant::deleteAll([ 'product_id' => $this->product_id ]); + ProductStock::deleteAll([ 'product_id' => $this->product_id ]); + Share::deleteAll([ 'product_id' => $this->product_id ]); + return true; } + return false; + } - } - - public function beforeDelete() { - if(parent::beforeDelete()){ - $this->unlinkAll('categories', true); - $this->unlinkAll('options', true); - ProductImage::deleteAll(['product_id' => $this->product_id]); - - ProductVariant::deleteAll(['product_id' => $this->product_id]); - ProductStock::deleteAll(['product_id' => $this->product_id]); - Share::deleteAll(['product_id' => $this->product_id]); - return true; - } - 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; + + 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; + } + $imgDir = Yii::getAlias('@imagesDir/products/'); + if (!is_dir($imgDir)) { + mkdir($imgDir, 0755, true); + } + + $image->saveAs($imgDir . $imageName); + $images[] = $imageName; } - $imgDir =Yii::getAlias('@imagesDir/products/'); - if(!is_dir($imgDir)) { - mkdir($imgDir, 0755, true); + 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'); } - - - $image->saveAs($imgDir .$imageName); - $images[] = $imageName; } - return $images; - } else { - return false; + return $op; } - } - - public function getImagesHTML() { - $op = []; - if ($this->images) { - foreach ($this->images as $image) { - $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb'); + + public function getImagesConfig() + { + $op = []; + if ($this->images) { + foreach ($this->images as $image) { + $op[] = [ + 'caption' => $image->image, + '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; } - return $op; - } - - public function getImagesConfig() { - $op = []; - if ($this->images) { - foreach ($this->images as $image) { - $op[] = [ - 'caption' => $image->image, - '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 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; } } - 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( + 'tax_group_to_category', + 'tax_group_to_category.tax_group_id = tax_group.tax_group_id' + ) + ->andWhere([ 'tax_group_to_category.category_id' => $categories ]) + ->andWhere([ 'level' => $level ]); + } + + public function setCategories($values) + { + $this->categories = $values; + } + + public function setOptions($values) + { + $this->options = $values; + } + + public function getFilters() + { + + return $this->hasMany(TaxOption::className(), [ 'tax_option_id' => 'option_id' ]) + ->viaTable('product_option', [ 'product_id' => 'product_id' ]) + ->joinWith('taxGroup'); + } + } - - 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('tax_group_to_category', 'tax_group_to_category.tax_group_id = tax_group.tax_group_id')->andWhere(['tax_group_to_category.category_id' => $categories])->andWhere(['level' => $level]); - } - - public function setCategories($values) - { - $this->categories = $values; - } - - public function setOptions($values) - { - $this->options = $values; - } - - public function getFilters(){ - - return $this->hasMany(TaxOption::className(), ['tax_option_id' => 'option_id']) - ->viaTable('product_option',[ 'product_id'=> 'product_id']) - ->joinWith('taxGroup'); - } - -} diff --git a/common/modules/rubrication/models/TaxGroup.php b/common/modules/rubrication/models/TaxGroup.php index 096eaa7..857a20d 100755 --- a/common/modules/rubrication/models/TaxGroup.php +++ b/common/modules/rubrication/models/TaxGroup.php @@ -20,6 +20,7 @@ use Yii; * @property integer $sort * @property boolean $display * @property boolean $is_menu + * @property boolean $use_in_name * @property TaxGroupToGroup[] $taxGroupToGroups * @property TaxGroupToGroup[] $taxGroupToGroups0 * @property TaxOption[] $taxOptions @@ -59,11 +60,12 @@ class TaxGroup extends \yii\db\ActiveRecord return [ [['name', 'module'], 'required'], [['description', 'settings'], 'string'], - [['hierarchical', 'is_filter', 'display','is_menu'], 'boolean'], + [['hierarchical', 'is_filter', 'display','is_menu', 'use_in_name'], 'boolean'], [['level', 'sort'], 'integer'], [['alias', 'module'], 'string', 'max' => 50], [['name'], 'string', 'max' => 255], - [['categories'], 'safe'] + [['categories'], 'safe'], + [['use_in_name'], 'default', 'value' => false], ]; } @@ -84,6 +86,7 @@ class TaxGroup extends \yii\db\ActiveRecord 'sort' => 'Sort', 'display' => 'Display', 'is_menu' => 'Отображать в меню', + 'use_in_name' => 'Использовать в названии', ]; } diff --git a/common/modules/rubrication/models/TaxOption.php b/common/modules/rubrication/models/TaxOption.php index d977fd9..48a9b03 100755 --- a/common/modules/rubrication/models/TaxOption.php +++ b/common/modules/rubrication/models/TaxOption.php @@ -20,6 +20,7 @@ use yii\db\ActiveRecord; * @property integer $default_value * @property integer $name * @property array $image + * @property string $synonym * * @property TaxEntityRelation[] $taxEntityRelations * @property TaxGroup $taxGroup @@ -74,7 +75,7 @@ class TaxOption extends \yii\db\ActiveRecord return [ [['tax_group_id','name'], 'required'], [['tax_group_id', 'parent_id', 'sort', 'default_value'], 'integer'], - [['image','alias', 'value'], 'string', 'max' => 255], + [['image','alias', 'value', 'synonym'], 'string', 'max' => 255], [['tax_group_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxGroup::className(), 'targetAttribute' => ['tax_group_id' => 'tax_group_id']], ]; } @@ -92,6 +93,7 @@ class TaxOption extends \yii\db\ActiveRecord 'sort' => Yii::t('app', 'Sort'), 'default_value' => Yii::t('app', 'Default Value'), 'image' => Yii::t('product', 'Image'), + '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 4c3a784..f536d6a 100755 --- a/common/modules/rubrication/views/tax-group/_form.php +++ b/common/modules/rubrication/views/tax-group/_form.php @@ -37,8 +37,10 @@ use common\components\artboxtree\ArtboxTreeHelper; field($model, 'is_filter')->checkbox() ?> field($model, 'display')->checkbox() ?> - + field($model, 'is_menu')->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 f8084d5..ced022d 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) { $tree = TaxOption::find()->getTree($model->tax_group_id); diff --git a/console/migrations/yarik/m170302_170344_add_synonym_and_use_in_menu.php b/console/migrations/yarik/m170302_170344_add_synonym_and_use_in_menu.php new file mode 100644 index 0000000..ba45b54 --- /dev/null +++ b/console/migrations/yarik/m170302_170344_add_synonym_and_use_in_menu.php @@ -0,0 +1,18 @@ +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'); + } +} diff --git a/console/migrations/yarik/m170302_173310_add_category_synonym_column.php b/console/migrations/yarik/m170302_173310_add_category_synonym_column.php new file mode 100644 index 0000000..ae83ed9 --- /dev/null +++ b/console/migrations/yarik/m170302_173310_add_category_synonym_column.php @@ -0,0 +1,16 @@ +addColumn('category', 'synonym', $this->string()); + } + + public function down() + { + $this->dropColumn('category', 'synonym'); + } +} diff --git a/frontend/views/catalog/_product_item.php b/frontend/views/catalog/_product_item.php index 1847094..6b9b235 100755 --- a/frontend/views/catalog/_product_item.php +++ b/frontend/views/catalog/_product_item.php @@ -14,7 +14,7 @@ use yii\helpers\Url;
- name, Url::to(['catalog/product', 'product' => $model->alias]), ['class'=>'btn-product-details','data-pjax'=>0, 'itemprop' => 'name'] )?> + getFullName(), Url::to(['catalog/product', 'product' => $model->alias]), ['class'=>'btn-product-details','data-pjax'=>0, 'itemprop' => 'name'] )?>