<?php

namespace backend\models;

use Yii;
use common\models\Tools;

/**
 * This is the model class for table "termin".
 *
 * @property integer $termin_id
 * @property string $termin_name
 * @property integer $is_book
 *
 * @property Menu[] $menus
 * @property TerminLang[] $terminLangs
 * @property Language[] $langs
 * @property TerminStructure[] $terminStructures
 */
class Termin extends \yii\db\ActiveRecord
{
    var $termin_title;
    var $termin_pid;

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['is_book'], 'integer'],
            [['termin_pid'], 'safe'],
            [['termin_title'], 'string', 'max' => 250],
            [['termin_name'], 'string', 'max' => 250]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'termin_id' => Yii::t('app', 'termin'),
            'termin_name' => Yii::t('app', 'name').' (SYSTEM NAME)',
            'is_book' => Yii::t('app', 'book'),
        ];
    }

    /**
     * Выполняет поиск по параметрам
     * @param array $param принимает [termin_id, language_id, return_one, return_field, show_all]
     * @return array one | array all | string значение масива
     */
    public function finInfo (array $params = [])
    {
        Tools::ifNotExist ($params, array (
            'termin_id' => false,
            'termin_pid' => false,
            'language_id' => Yii::$app->params['language_id'],
            'return_one' => false,
            'return_field' => false,
            'show_all' => false,
            'to_array' => true,
            'pid_title' => false,
        ));

        $WHERE = $SELECT = array ();

        $model = new self();
        
        $SELECT[] = 'termin.*';
        $query = $model->find();
 
        if ($params['termin_id'])
        {
            $WHERE['termin.termin_id'] = $params['termin_id'];
        }
    
        // перевод
        $SELECT[] = 'termin_lang.*';
        $query->join(
            'INNER JOIN', 'termin_lang',
            'termin.termin_id = termin_lang.termin_id'
        );

        if ($params['language_id'])
        {
            $WHERE['termin_lang.language_id'] = $params['language_id'];
        }
        
        // структура
        $SELECT[] = 'termin_structure.*';
        $query->join(
            'INNER JOIN', 'termin_structure',
            'termin.termin_id = termin_structure.termin_id'
        );

        if ($params['termin_pid'] !== false)
        {
            $WHERE['termin_structure.termin_pid'] = $params['termin_pid'];
        }

        if ($params['pid_title'])
        {
            $SELECT[] = 'termin_pid_lang.termin_title as termin_pid_title';
            $query->join(
                'LEFT JOIN', 'termin_lang as termin_pid_lang',
                'termin_pid_lang.termin_id = termin_structure.termin_pid'
            );
        }
        
        // SELECT
        if (! empty ($SELECT))
        {
            $query->select($SELECT);
        }
        
        // WHERE
        if (! empty ($WHERE))
        {
            $query->where($WHERE);
        }
    
        if ($params['to_array'])
        {
            $query = $query->asArray();
        }
    
        if ($params['return_one'] || $params['return_field'])
        {
    
            $result = $params['return_field'] ? $query->one($params['return_field']) : $query->one();
        }
        else
        {
            $result = $query->all();
        }
    
        return $result;
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMenus()
    {
        return $this->hasMany(Menu::className(), ['termin_id' => 'termin_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTerminLangs()
    {
        return $this->hasMany(TerminLang::className(), ['termin_id' => 'termin_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLangs()
    {
        return $this->hasMany(Language::className(), ['language_id' => 'language_id'])
            ->viaTable('termin_lang', ['termin_id' => 'termin_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTerminStructures()
    {
        return $this->hasMany(TerminStructure::className(), ['termin_id' => 'termin_id']);
    }

}
