[ 'class' => ArtboxTreeBehavior::className(), 'keyNameGroup' => null, 'keyNamePath' => 'path', ], 'artboxsynonym' => [ 'class' => ArtboxSynonymBehavior::className(), 'keyNameValue' => 'category_name_id', 'valueModel' => CategoryName::className(), 'valueOptionId' => 'category_id', 'valueFields' => [ // postKey => DBFieldName 'name' => 'value' ], 'slug' => [ 'valueKeyName' => 'value', 'slugKeyName' => 'alias', 'translit' => true ] ], [ 'class' => relationBehavior::className(), 'relations' => [ 'product_categories' => 'entity2', // Products of category 'tax_group_to_category' => 'entity2', ] ], ]; } /** * @inheritdoc */ public static function tableName() { return 'category'; } /** * @inheritdoc */ public function rules() { return [ [['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_robots'], 'string', 'max' => 50], [['alias', 'name'], 'string', 'max' => 250], [['populary'], 'boolean'], [['group_to_category', 'remote_category'], 'safe'], [['category_name_id'], 'exist', 'skipOnError' => true, 'targetClass' => CategoryName::className(), 'targetAttribute' => ['category_name_id' => 'category_name_id']], [['imageUpload'], 'safe'], [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'category_id' => Yii::t('product', 'Category ID'), 'parent_id' => Yii::t('product', 'Parent ID'), 'path' => Yii::t('product', 'Path'), 'depth' => Yii::t('product', 'Depth'), 'image' => Yii::t('product', 'Image'), 'imageUrl' => Yii::t('product', 'Image'), 'meta_title' => Yii::t('product', 'Meta Title'), 'meta_desc' => Yii::t('product', 'Meta Desc'), 'meta_robots' => Yii::t('product', 'Meta Robots'), 'h1' => Yii::t('product', 'h1'), 'seo_text' => Yii::t('product', 'Seo Text'), 'product_unit_id' => Yii::t('product', 'Product Unit ID'), 'alias' => Yii::t('product', 'Alias'), 'populary' => Yii::t('product', 'Populary'), 'name' => Yii::t('product', 'Name'), 'remote_id' => Yii::t('product', 'Remote ID'), ]; } public static function find() { return new CategoryQuery(get_called_class()); } /** * @return \yii\db\ActiveQuery */ public function getProductUnit() { return $this->hasOne(ProductUnit::className(), ['product_unit_id' => 'product_unit_id']); } public function getProducts() { return $this->hasMany(Product::className(), ['product_id' => 'product_id']) ->viaTable('product_category', ['category_id' => 'category_id']); // return $this->getRelations('product_categories'); } /** * @return \yii\db\ActiveQuery */ public function getCategoryNames() { return $this->hasMany(CategoryName::className(), ['category_id' => 'category_id']); } /** * @return \yii\db\ActiveQuery */ public function getProductCategories() { return $this->hasMany(ProductCategory::className(), ['category_id' => 'category_id']); } public function getTaxGroups() { return $this->getRelations('tax_group_to_category'); } public function getTaxGroupsByLevel($level) { return $this->hasMany(TaxGroup::className(), ['tax_group_id' => 'entity1_id']) ->viaTable('relation', ['entity2_id' => 'category_id']) ->andWhere(['level' => $level]); } public function getRemote_category() { return ArtboxTreeHelper::getArrayField($this->remote_id); } public function setRemote_category($value) { if (!empty($value) && is_array($value)) { $this->remote_id = ArtboxTreeHelper::setArrayField($value, false); } } public function getCategoryName() { return $this->hasOne(CategoryName::className(), ['category_name_id' => 'category_name_id']); } public function getName() { return empty($this->categoryName) ? null : $this->categoryName->value; } public function getImageFile() { return empty($this->image) ? '/images/no_photo.png' : Yii::getAlias('@imagesDir/categories/'. $this->image); } public function getImageUrl() { return empty($this->image) ? '/images/no_photo.png' : Yii::getAlias('@imagesUrl/categories/' . $this->image); } public function beforeSave($insert) { if (parent::beforeSave($insert)) { if (empty($this->parent_id)) $this->parent_id = 0; return true; } return false; } public function beforeDelete() { if(!empty($this->products)){ foreach($this->products as $product){ $product->delete(); } } ProductCategory::deleteAll(['category_id' => $this->category_id]); CategoryName::deleteAll(['category_id' => $this->category_id]); return true; } public function getActiveFilters() { $query1 = (new Query()) ->distinct() ->select([ 'option_id', 'tax_option.*', 'tax_group.*', 'tax_option.alias as option_alias', 'tax_group.alias as group_alias', 'tax_value_string.value as value', 'tax_option.sort AS tax_option_sort', 'tax_group.sort AS tax_group_sort', ]) ->from('tax_option') ->innerJoin('product_variant_option', 'tax_option.tax_option_id = product_variant_option.option_id') ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') ->innerJoin('product_variant', 'product_variant.product_variant_id = product_variant_option.product_variant_id') ->innerJoin('product', 'product.product_id = product_variant.product_id') ->innerJoin('product_category', 'product_category.product_id = product.product_id') ->innerJoin('tax_value_string', 'tax_value_string.tax_option_id = tax_option.tax_option_id') ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE]) ->andWhere(['!=', 'product_variant.stock', 0]); $query2 = (new Query()) ->distinct() ->select([ 'option_id', 'tax_option.*', 'tax_group.*', 'tax_option.alias as option_alias', 'tax_group.alias as group_alias', 'tax_value_string.value as value', 'tax_option.sort AS tax_option_sort', 'tax_group.sort AS tax_group_sort', ]) ->from('tax_option') ->innerJoin('product_option', 'tax_option.tax_option_id = product_option.option_id') ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') ->innerJoin('product', 'product.product_id = product_option.product_id') ->innerJoin('product_category', 'product_category.product_id = product.product_id') ->innerJoin('product_variant', 'product_variant.product_id = product.product_id') ->innerJoin('tax_value_string', 'tax_value_string.tax_option_id = tax_option.tax_option_id') ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE]) ->andWhere(['!=', 'product_variant.stock', 0]); $query3 = (new Query()) ->select('*') ->from(['subquery' => $query1->union($query2)]) ->orderBy('tax_group_sort, tax_option_sort'); return $query3; } public function getTaxGroupsByAlias($alias) { $connection = Yii::$app->getDb(); $command = $connection->createCommand(' SELECT ton.alias as option_alias, * FROM tax_option as ton LEFT JOIN tax_value_string as tvs ON ton.tax_option_id = tvs.tax_option_id RIGHT JOIN tax_group ON ton.tax_group_id = tax_group.tax_group_id WHERE ton.tax_option_id IN ( SELECT po.option_id FROM product_option as po WHERE po.product_id IN ( SELECT product_id FROM product_category WHERE category_id = :category_id ) ) AND tax_group.alias = :alias', [ ':alias' => $alias, ':category_id' => $this->category_id ]); return $command->queryAll(); } }