<?php

namespace common\models;

use common\behaviors\SaveImgBehavior;

class Bg extends \yii\db\ActiveRecord
{
	public $old_image;
        
	public static function tableName()
    {
        return 'bg';
    }

	/**
	 * @inheritdoc
	 */
	public function behaviors()
	{
		return [
			[
				'class'     => SaveImgBehavior::className(),
				'fields' => [
					['name'=>'image','directory' => 'bg' ]
				]
			],
		];
	}

	public function rules()
	{
		return [
			[['title'], 'required'],
			[['old_image','url'], 'safe'],
                        [['image'], 'file', 'extensions'=>'jpg, gif, png', 'skipOnEmpty'=>true],

                    ];
	}	
	
	public function attributeLabels()
	{
		return [
			'title'=>'Название',
			'body'=>'Описание',
			'date'=>'Дата',
                        'image'=>'Изображения',
		];
	}
        

        
	public function beforeDelete() {
		$this->deleteImage($this->image);
		return parent::beforeDelete();
	}

	public function deleteImage($file){
					if(!empty($file)){
						@unlink('upload/bg/'.$file);
					}
	}
	

}

