<?php

    namespace common\models;

    use Yii;
    use yii\behaviors\BlameableBehavior;
    use yii\behaviors\TimestampBehavior;
    use yii\db\Expression;

    /**
     * This is the model class for table "blog".
     * @property integer $blog_id
     * @property integer $user_id
     * @property string  $name
     * @property string  $link
     * @property string  $date_add
     * @property integer $user_add_id
     * @property integer $view_count
     * @property string  $description
     * @property string  $cover
     */
    class Blog extends \yii\db\ActiveRecord
    {
        public $imageUpload;

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


        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class'              => BlameableBehavior::className(),
                    'createdByAttribute' => 'user_id',
                    'updatedByAttribute' => false,
                ],
                [
                    'class'              => TimestampBehavior::className(),
                    'createdAtAttribute' => 'date_add',
                    'updatedAtAttribute' => false,
                    'value'              => new Expression('NOW()'),
                ],
                'slug' => [
                    'class' => 'common\behaviors\Slug',
                    'in_attribute' => 'name',
                    'out_attribute' => 'link',
                    'translit' => true
                ]
            ];
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'name', 'description' ],
                    'required',
                ],
                [
                    [ 'description' ],
                    'string',
                ],
                [
                    [
                        'name',
                        'link',
                        'cover',
                    ],
                    'string',
                    'max' => 255,
                ],
                [['imageUpload'], 'safe'],
                [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],
            ];
        }

        public function getDateCreate(){
            return date('Y-m-d',strtotime($this->date_add));
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'blog_id'     => Yii::t('app', 'blog_id'),
                'user_id'     => Yii::t('app', 'user_id'),
                'name'        => Yii::t('app', 'name'),
                'link'        => Yii::t('app', 'link'),
                'date_add'    => Yii::t('app', 'date_add'),
                'user_add_id' => Yii::t('app', 'user_add_id'),
                'view_count'  => Yii::t('app', 'view_count'),
                'description' => Yii::t('app', 'description'),
                'cover'       => Yii::t('app', 'cover'),
                'image'       => Yii::t('app', 'cover'),
                'imageUrl'    => Yii::t('app', 'cover'),
            ];
        }

        public function getImageFile() {
            return empty($this->image) ? null : Yii::getAlias('@imagesDir/brands/'. $this->image);
        }

        public function getImageUrl()
        {
            return empty($this->image) ? null : Yii::getAlias('@imagesUrl/brands/' . $this->image);
        }
    }
