<?php

namespace backend\models;

use Yii;
use backend\models\Book;

/**
 * This is the model class for table "category".
 *
 * @property integer $id
 * @property string $title
 * @property integer $order
 * @property integer $status
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
	public $file;
    public static function tableName()
    {
        return 'category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order', 'status'], 'integer'],
			[['file'], 'file','extensions' => 'jpg,png,gif,jpeg'],
            [['title','image'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'order' => 'Order',
            'status' => 'Status',
			'image' => 'Image',
			'file' => 'File format: jpg,png,gif,jpeg',
        ];
    }

    public static function itemAlias($type,$code=NULL) {
        $_items = array(
            'status' => array(
                '0' => 'Inactive',
                '1' => 'Active',
            ),
        );
        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

    public static function countBook($id)
    {
        $book = Book::find()->where('categoryId ='.$id)->all();
        return count($book);
    }
	
	public static function deleteImage($oldImage) {
        if(strlen($oldImage)>0){
            $image = Yii::$app->basePath . '/web/images/category/' . $oldImage;
            if(is_file($image)){
                unlink($image);
            }
        }
        return false;
    }
}
