<?php

/**
 * This is the model class for table "sport_event".
 *
 * The followings are the available columns in table 'sport_event':
 * @property integer $id
 * @property integer $sport_event_category_id
 * @property string $start_date
 * @property string $end_date
 * @property integer $country_id
 *
 * The followings are the available model relations:
 * @property SportEventCategory $sportEventCategory
 * @property SportEventI18n[] $i18ns
 * @property SportEventI18n $i18n
 */
class SportEvent extends CActiveRecord implements INodeCrudModel
{
    public $calendar_section_id;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return SportEvent the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'sport_event';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('sport_event_category_id', 'required'),
            array('sport_event_category_id, country_id', 'numerical', 'integerOnly' => true),
            array('startDateRu, endDateRu', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, sport_event_category_id, start_date, end_date, country_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'sportEventCategory' => array(self::BELONGS_TO, 'SportEventCategory', 'sport_event_category_id'),
            'i18ns' => array(self::HAS_MANY, 'SportEventI18n', 'id', 'index' => 'lang'),
            'i18n' => array(self::HAS_ONE, 'SportEventI18n', 'id', 'condition' => 'lang=\'' . Yii::app()->language . '\''),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'start_date' => 'Начало',
            'end_date' => 'Конец',

            'sport_event_category_id' => 'Категория',
            'country_id' => 'Страна',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

//        $criteria->compare('id', $this->id);
//        $criteria->compare('sport_event_category_id', $this->sport_event_category_id);

        if (isset($this->sport_event_category_id))
            $criteria->compare('sport_event_category_id', $this->sport_event_category_id);
        else
            $criteria->addInCondition('sport_event_category_id', array_keys($this->getSportEventCategories()));
        $criteria->compare('start_date', $this->start_date, true);
        $criteria->compare('end_date', $this->end_date, true);
        $criteria->compare('country_id', $this->country_id);

        $sort = new CSort();
        $sort->defaultOrder = array(
            'start_date' => true,
        );

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => $sort,
        ));
    }

    public static function getCountries()
    {
        return array(
            '2' => Yii::t('site', 'Украина'),
            '3' => Yii::t('site', 'Россия'),
            '1' => Yii::t('site', 'Мир'),
        );
    }

    public function getCountry()
    {
        if ($this->country_id == '1') return Yii::t('site', 'Мир');
        elseif ($this->country_id == '2') return Yii::t('site', 'Украина'); else /* if ($this->country_id == '3') */
            return Yii::t('site', 'Россия');
    }


    public function getStartDateRu()
    {
        return ($this->start_date !== null)
            ? date('d.m.Y', CDateTimeParser::parse($this->start_date, 'yyyy-MM-dd'))
            : date('d.m.Y');
    }

    public function setStartDateRu($value)
    {
        $this->start_date = date('Y-m-d', CDateTimeParser::parse($value, 'dd.MM.yyyy'));
    }


    public function getEndDateRu()
    {
        return ($this->end_date !== null)
            ? date('d.m.Y', CDateTimeParser::parse($this->end_date, 'yyyy-MM-dd'))
            : date('d.m.Y');
    }

    public function setEndDateRu($value)
    {
        $this->end_date = date('Y-m-d', CDateTimeParser::parse($value, 'dd.MM.yyyy'));
    }


    public function getSportEventCategories()
    {
        $criteria = new CDbCriteria();
        $criteria->compare('calendar_section_id', $this->calendar_section_id);

        return CHtml::listData(SportEventCategory::model()->findAll($criteria), 'id', 'i18n.name');
    }

    /**
     * @param Node $node
     * @return void
     */
    public function setNode($node)
    {
        $this->calendar_section_id = $node->data_id;
    }
}