<?php

namespace common\modules\rubrication\models;

use common\modules\relation\relationBehavior;
use Yii;

/**
 * This is the model class for table "{{%tax_group}}".
 *
 * @property integer $tax_group_id
 * @property string $alias
 * @property string $name
 * @property string $description
 * @property string $module
 * @property boolean $hierarchical
 * @property string $settings
 * @property boolean is_filter
 *
 * @property TaxGroupToGroup[] $taxGroupToGroups
 * @property TaxGroupToGroup[] $taxGroupToGroups0
 * @property TaxOption[] $taxOptions
 * @property TaxOptionToGroup[] $taxOptionToGroups
 */
class TaxGroup extends \yii\db\ActiveRecord
{
    public $_options = [];
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => relationBehavior::className(),
                'relations' => [
                    'tax_group_to_category' => 'entity1',
                ]
            ],
            'slug' => [
                'class' => 'common\behaviors\Slug',
                'in_attribute' => 'name',
                'out_attribute' => 'alias',
                'translit' => true
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tax_group}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'module'], 'required'],
            [['description', 'settings'], 'string'],
            [['hierarchical'], 'boolean'],
            [['alias', 'module'], 'string', 'max' => 50],
            [['name'], 'string', 'max' => 255],
            [['group_to_category'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'tax_group_id' => 'Tax Group ID',
            'alias' => 'Alias',
            'name' => 'Name',
            'description' => 'Description',
            'module' => 'Module',
            'hierarchical' => 'Hierarchical',
//            'settings' => 'Settings',
            'is_filter' => 'Use in filter',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxGroupToGroups()
    {
        return $this->hasMany(TaxGroupToGroup::className(), ['tax_group1_id' => 'tax_group_id'])->inverseOf('taxGroup1');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxGroupToGroups0()
    {
        return $this->hasMany(TaxGroupToGroup::className(), ['tax_group2_id' => 'tax_group_id'])->inverseOf('taxGroup2');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOptions() {
        return $this->getTaxOptions();
    }
    public function getTaxOptions()
    {
        return $this->hasMany(TaxOption::className(), ['tax_group_id' => 'tax_group_id'])->inverseOf('taxGroup');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptionToGroups()
    {
        return $this->hasMany(TaxOptionToGroup::className(), ['tax_group_id' => 'tax_group_id'])->inverseOf('taxGroup');
    }

    public function getValueModelName($full_path = true) {
        $valueClass = 'TaxValue'. ucfirst($this->module);
        $fullClass = '\common\modules\rubrication\models\\'. $valueClass;
        return class_exists($fullClass) ? $full_path ? $fullClass : $valueClass : FALSE;
    }
}
