From 4a447a249a69b9f9f2db47dc2ba93f41b6c5db18 Mon Sep 17 00:00:00 2001 From: yarik Date: Fri, 11 Nov 2016 20:01:35 +0200 Subject: [PATCH] Catalog begin --- CatalogUrlManager.php | 3 +++ models/ProductFrontendSearch.php | 18 ++++++++++-------- models/TaxVariantGroup.php | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/TaxVariantGroupLang.php | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/TaxVariantGroupSearch.php | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/TaxVariantGroupToCategory.php | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/TaxVariantOption.php | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/TaxVariantOptionLang.php | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/TaxVariantOptionSearch.php | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 960 insertions(+), 8 deletions(-) create mode 100755 models/TaxVariantGroup.php create mode 100755 models/TaxVariantGroupLang.php create mode 100755 models/TaxVariantGroupSearch.php create mode 100755 models/TaxVariantGroupToCategory.php create mode 100755 models/TaxVariantOption.php create mode 100755 models/TaxVariantOptionLang.php create mode 100755 models/TaxVariantOptionSearch.php diff --git a/CatalogUrlManager.php b/CatalogUrlManager.php index 83c410e..1cff897 100644 --- a/CatalogUrlManager.php +++ b/CatalogUrlManager.php @@ -141,6 +141,9 @@ case 'catalog/product': + $product_alias = ''; + $variant_sku = ''; + if (!empty( $params[ 'product' ] )) { $product_alias = strtolower($params[ 'product' ]); unset( $params[ 'product' ] ); diff --git a/models/ProductFrontendSearch.php b/models/ProductFrontendSearch.php index 00a5d1c..af4213c 100644 --- a/models/ProductFrontendSearch.php +++ b/models/ProductFrontendSearch.php @@ -53,7 +53,7 @@ class ProductFrontendSearch extends Product { * * @param array $params * - * @return ActiveDataProvider + * @return ArrayDataProvider */ public function search($category = null, $params = [], $in_stock = true) { @@ -61,7 +61,6 @@ class ProductFrontendSearch extends Product { $dataProvider = new ArrayDataProvider([ 'allModels' => $this->getSearchQuery($category, $params, $in_stock)->with([ 'images', -// 'events', 'variant', 'variant.image', 'comments', @@ -72,11 +71,17 @@ class ProductFrontendSearch extends Product { ], 'sort' => [ 'attributes' => [ - 'name' => [ + 'name_asc' => [ 'asc' => ['name' => SORT_ASC], + 'desc' => ['name' => SORT_ASC], + 'default' => SORT_ASC, + 'label' => 'имени от А до Я', + ], + 'name_desc' => [ + 'asc' => ['name' => SORT_DESC], 'desc' => ['name' => SORT_DESC], 'default' => SORT_DESC, - 'label' => 'имени', + 'label' => 'имени от Я до А', ], 'price' => [ 'asc' => ['price' => SORT_ASC], @@ -87,10 +92,7 @@ class ProductFrontendSearch extends Product { ], ] ]); - - - - + return $dataProvider; } diff --git a/models/TaxVariantGroup.php b/models/TaxVariantGroup.php new file mode 100755 index 0000000..ecd88d5 --- /dev/null +++ b/models/TaxVariantGroup.php @@ -0,0 +1,227 @@ + [ + 'class' => LanguageBehavior::className(), + ], + ]; + } + + /** + * @inheritdoc + */ + public static function tableName() + { + return 'tax_variant_group'; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'is_filter', + 'display', + 'is_menu', + ], + 'boolean', + ], + [ + [ + 'sort', + ], + 'integer', + ], + [ + [ 'categories' ], + 'safe', + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'Tax Group ID', + 'is_filter' => 'Use in filter', + 'sort' => 'Sort', + 'display' => 'Display', + 'is_menu' => 'Отображать в меню', + ]; + } + + /** + * @return ActiveQuery + */ + public function getCategories() + { + return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) + ->viaTable('tax_variant_group_to_category', [ 'tax_variant_group_id' => 'id' ]); + } + + /** + * Set categories to override tax_variant_group_to_category table data + * + * @param int[] $values + */ + public function setCategories(array $values) + { + $this->categories = $values; + } + + /** + * @inheritdoc + */ + public function afterSave($insert, $changedAttributes) + { + parent::afterSave($insert, $changedAttributes); + $this->unlinkAll('categories', true); + $categories = []; + if (!empty( $this->categories )) { + $categories = Category::findAll($this->categories); + } + foreach ($categories as $category) { + $this->link('categories', $category); + } + } + + public function getTaxVariantOptions() + { + return $this->hasMany(TaxVariantOption::className(), [ 'tax_variant_group_id' => 'id' ]) + ->inverseOf('taxVariantGroup'); + } + + /** + * @return ActiveQuery + */ + public function getTaxOptions() + { + return $this->getTaxVariantOptions(); + } + + /** + * Synonim for getTaxOptions() + * + * @see TaxGroup::getTaxOptions() + * @return \yii\db\ActiveQuery + */ + public function getOptions() + { + return $this->getTaxOptions(); + } + + /** + * Get customOptins that were filled dynamically. + * If $fillDefault is true then fill $customOptions with TaxOptions for current TaxGroup + * + * @param bool $fillDefault + * + * @return TaxOption[] + */ + public function getCustomOptions(bool $fillDefault = false): array + { + if ($fillDefault && empty( $this->custom_options )) { + $this->customOptions = $this->getTaxVariantOptions() + ->with('lang') + ->all(); + } + return $this->customOptions; + } + + /** + * Set customOptions + * + * @param TaxOption[] $value + */ + public function setCustomOptions(array $value) + { + foreach ($value as $item) { + if (!( $item instanceof TaxVariantOption )) { + throw new InvalidValueException( + 'All elements must be instances of ' . TaxVariantOption::className() + ); + } + } + $this->customOptions = $value; + } + + /** + * Get default lang alias + * + * @return string + */ + public function getAlias() + { + $default_lang = Language::getDefaultLanguage(); + /** + * @var TaxVariantGroupLang $lang + */ + $lang = $this->getLang($default_lang->id) + ->one(); + return $lang->alias; + } + } diff --git a/models/TaxVariantGroupLang.php b/models/TaxVariantGroupLang.php new file mode 100755 index 0000000..c2f7202 --- /dev/null +++ b/models/TaxVariantGroupLang.php @@ -0,0 +1,128 @@ + [ + 'class' => 'artweb\artbox\behaviors\Slug', + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ 'title' ], + 'required', + ], + [ + [ 'description' ], + 'string', + ], + [ + [ + 'title', + 'alias', + ], + 'string', + 'max' => 255, + ], + [ + [ + 'tax_variant_group_id', + 'language_id', + ], + 'unique', + 'targetAttribute' => [ + 'tax_variant_group_id', + 'language_id', + ], + 'message' => 'The combination of Tax Variant Group ID and Language ID has already been taken.', + ], + [ + [ 'language_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Language::className(), + 'targetAttribute' => [ 'language_id' => 'id' ], + ], + [ + [ 'tax_variant_group_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => TaxVariantGroup::className(), + 'targetAttribute' => [ 'tax_variant_group_id' => 'id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'tax_variant_group_id' => Yii::t('app', 'Tax Variant Group ID'), + 'language_id' => Yii::t('app', 'Language ID'), + 'title' => Yii::t('app', 'Name'), + 'description' => Yii::t('app', 'Description'), + 'alias' => Yii::t('app', 'Alias'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), [ 'id' => 'language_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaxVariantGroup() + { + return $this->hasOne(TaxVariantGroup::className(), [ 'id' => 'tax_variant_group_id' ]); + } + } diff --git a/models/TaxVariantGroupSearch.php b/models/TaxVariantGroupSearch.php new file mode 100755 index 0000000..3c399cc --- /dev/null +++ b/models/TaxVariantGroupSearch.php @@ -0,0 +1,114 @@ +joinWith('lang'); + + $dataProvider = new ActiveDataProvider( + [ + 'query' => $query, + 'sort' => [ + 'attributes' => [ + 'id', + 'is_filter', + 'groupName' => [ + 'asc' => [ 'tax_variant_group_lang.title' => SORT_ASC ], + 'desc' => [ 'tax_variant_group_lang.title' => SORT_DESC ], + ], + ], + ], + ] + ); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + $query->andFilterWhere( + [ + 'id' => $this->id, + 'is_filter' => $this->is_filter, + ] + ) + ->andFilterWhere( + [ + 'ilike', + 'tax_variant_group_lang.title', + $this->groupName, + ] + ); + + return $dataProvider; + } + } diff --git a/models/TaxVariantGroupToCategory.php b/models/TaxVariantGroupToCategory.php new file mode 100755 index 0000000..db1a864 --- /dev/null +++ b/models/TaxVariantGroupToCategory.php @@ -0,0 +1,100 @@ + true, + 'targetClass' => Category::className(), + 'targetAttribute' => [ 'category_id' => 'id' ], + ], + [ + [ 'tax_variant_group_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => TaxVariantGroup::className(), + 'targetAttribute' => [ 'tax_variant_group_id' => 'id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'Tax Variant Group To Category ID', + 'tax_variant_group_id' => 'Tax Variant Group ID', + 'category_id' => 'Category ID', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getCategory() + { + return $this->hasOne(Category::className(), [ 'id' => 'category_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getVariantTaxGroup() + { + return $this->hasOne(TaxVariantGroup::className(), [ 'id' => 'tax_variant_group_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaxGroup() + { + return $this->getVariantTaxGroup(); + } + } diff --git a/models/TaxVariantOption.php b/models/TaxVariantOption.php new file mode 100755 index 0000000..1754a4a --- /dev/null +++ b/models/TaxVariantOption.php @@ -0,0 +1,154 @@ + SaveImgBehavior::className(), + 'fields' => [ + [ + 'name' => 'image', + 'directory' => 'tax_option', + ], + ], + ], + 'language' => [ + 'class' => LanguageBehavior::className(), + ], + ]; + } + + /** + * @inheritdoc + */ + public static function tableName() + { + return '{{%tax_variant_option}}'; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'tax_variant_group_id', + 'sort', + ], + 'integer', + ], + [ + [ 'tax_variant_group_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => TaxVariantGroup::className(), + 'targetAttribute' => [ 'tax_variant_group_id' => 'id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'Tax Variant Option ID'), + 'tax_variant_group_id' => Yii::t('app', 'Tax Variant Group ID'), + 'sort' => Yii::t('app', 'Sort'), + 'image' => Yii::t('product', 'Image'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaxVariantGroup() + { + return $this->hasOne(TaxVariantGroup::className(), [ 'id' => 'tax_variant_group_id' ]) + ->inverseOf('taxVariantOptions'); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaxGroup() + { + return $this->getTaxVariantGroup(); + } + + /** + * Synonim for TaxOption::getTaxGroup() + * + * @see TaxOption::getTaxGroup() + * @return \yii\db\ActiveQuery + */ + public function getGroup() + { + return $this->getTaxGroup(); + } + + /** + * @return ActiveQuery + */ + public function getProductVariants() + { + return $this->hasMany(ProductVariant::className(), [ 'id' => 'product_variant_id' ]) + ->viaTable('product_variant_option', [ 'option_id' => 'id' ]); + } + } diff --git a/models/TaxVariantOptionLang.php b/models/TaxVariantOptionLang.php new file mode 100755 index 0000000..5fe5455 --- /dev/null +++ b/models/TaxVariantOptionLang.php @@ -0,0 +1,123 @@ + [ + 'class' => 'artweb\artbox\behaviors\Slug', + 'inAttribute' => 'value', + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ 'value' ], + 'required', + ], + [ + [ + 'value', + 'alias', + ], + 'string', + 'max' => 255, + ], + [ + [ + 'tax_variant_option_id', + 'language_id', + ], + 'unique', + 'targetAttribute' => [ + 'tax_variant_option_id', + 'language_id', + ], + 'message' => 'The combination of Tax Variant Option ID and Language ID has already been taken.', + ], + [ + [ 'language_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Language::className(), + 'targetAttribute' => [ 'language_id' => 'id' ], + ], + [ + [ 'tax_variant_option_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => TaxVariantOption::className(), + 'targetAttribute' => [ 'tax_variant_option_id' => 'id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'tax_variant_option_id' => Yii::t('app', 'Tax Variant Option ID'), + 'language_id' => Yii::t('app', 'Language ID'), + 'value' => Yii::t('app', 'Value'), + 'alias' => Yii::t('app', 'Alias'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), [ 'id' => 'language_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaxVariantOption() + { + return $this->hasOne(TaxVariantOption::className(), [ 'id' => 'tax_variant_option_id' ]); + } + } diff --git a/models/TaxVariantOptionSearch.php b/models/TaxVariantOptionSearch.php new file mode 100755 index 0000000..ea9d7cc --- /dev/null +++ b/models/TaxVariantOptionSearch.php @@ -0,0 +1,101 @@ +joinWith('lang'); + + $dataProvider = new ActiveDataProvider( + [ + 'query' => $query, + 'sort' => [ + 'attributes' => [ + 'id', + 'value' => [ + 'asc' => [ 'tax_variant_option_lang.value' => SORT_ASC ], + 'desc' => [ 'tax_variant_option_lang.value' => SORT_DESC ], + ], + ], + ], + ] + ); + + $this->load($params); + + // if (!$this->validate()) { + // return $dataProvider; + // } + + // grid filtering conditions + $query->andFilterWhere( + [ + 'id' => $this->id, + ] + ) + ->andFilterWhere( + [ + 'like', + 'tax_variant_option_lang.value', + $this->value, + ] + ); + + return $dataProvider; + } + } -- libgit2 0.21.4