TaxOption.php 5.09 KB
<?php
    
    namespace common\modules\rubrication\models;
    
    use common\modules\product\models\Product;
    use Yii;
    use common\components\artboxtree\ArtboxTreeBehavior;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "{{%tax_option}}".
     * @property string      $tax_option_id
     * @property integer     $tax_group_id
     * @property integer     $parent_id
     * @property integer     $tree
     * @property string      $path_int
     * @property integer     $depth
     * @property string      $alias
     * @property integer     $sort
     * @property integer     $value
     * @property array       $image
     * @property TaxGroup    $taxGroup
     * @property TaxOption   $parent
     * @property TaxOption[] $taxOptions
     * @property Product[]   $products
     */
    class TaxOption extends \yii\db\ActiveRecord
    {
        
        public $_items_count;
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'artboxtree' => [
                    'class'        => ArtboxTreeBehavior::className(),
                    'keyNameGroup' => 'tax_group_id',
                ],
                'slug'       => [
                    'class'         => 'common\behaviors\Slug',
                    'in_attribute'  => 'value',
                    'out_attribute' => 'alias',
                    'translit'      => true,
                ],
            
            ];
        }
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return '{{%tax_option}}';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'tax_group_id',
                        'value',
                    ],
                    'required',
                ],
                [
                    [
                        'tax_group_id',
                        'parent_id',
                        'sort',
                    ],
                    'integer',
                ],
                [
                    [
                        'image',
                        'alias',
                        'value',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'tax_group_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => TaxGroup::className(),
                    'targetAttribute' => [ 'tax_group_id' => 'tax_group_id' ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'tax_option_id' => Yii::t('app', 'Tax Option ID'),
                'tax_group_id'  => Yii::t('app', 'Tax Group ID'),
                'parent_id'     => Yii::t('app', 'Parent ID'),
                'alias'         => Yii::t('app', 'Alias'),
                'sort'          => Yii::t('app', 'Sort'),
                'image'         => Yii::t('product', 'Image'),
            ];
        }
        
        public static function find()
        {
            return new TaxOptionQuery(get_called_class());
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getGroup()
        {
            return $this->getTaxGroup();
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getTaxGroup()
        {
            return $this->hasOne(TaxGroup::className(), [ 'tax_group_id' => 'tax_group_id' ])
                        ->inverseOf('taxOptions');
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getTaxOptions()
        {
            return $this->hasMany(TaxOption::className(), [ 'parent_id' => 'tax_option_id' ])
                        ->inverseOf('parent');
        }
        
        public function beforeSave($insert)
        {
            if(parent::beforeSave($insert)) {
                if(empty( $this->parent_id )) {
                    $this->parent_id = 0;
                }
                return true;
            }
            return false;
        }
        
        public function getImageFile()
        {
            return empty( $this->image ) ? NULL : Yii::getAlias('@imagesDir/tax_option/' . $this->image);
        }
        
        public function getImageUrl()
        {
            return empty( $this->image ) ? NULL : Yii::getAlias('@imagesUrl/tax_option/' . $this->image);
        }
        
        public function getProducts()
        {
            return $this->hasMany(Product::className(), [ 'product_id' => 'product_id' ])
                        ->viaTable('product_option', [ 'option_id' => 'tax_option_id' ]);
        }
    }