<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "quote".
 *
 * @property integer $id
 * @property integer $quoteIndex
 * @property string $content
 * @property integer $chapterId
 * @property string $image
 * @property Chapter $chapter
 */
class Quote extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public $file;
    public $bookid;
    public static function tableName()
    {
        return 'quote';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['quoteIndex', 'chapterId'], 'integer'],
            [['content'], 'string'],
            [['image'], 'string', 'max' => 255],
            [['file'], 'file'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'quoteIndex' => 'Pages Index',
            //'type' => 'Type',
            'content' => 'Content',
            'chapterId' => 'Chapter',
			'image' => 'Image',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getChapter()
    {
        return $this->hasOne(Chapter::className(), ['id' => 'chapterId']);
    }
    public static function checkType($name_file)
    {
        $image = explode(',','jpg,png,gif,tiff');
        //var_dump($image);exit;
        foreach($image as $item)
        {
            $check = substr_count($name_file,$item,0);
            if($check > 0)
                return 'image';
        }

        $video = explode(',','mp4,avi,flv,mpeg,mkv,3gp,wmv,swf');
        foreach($video as $item1)
        {
            $check = substr_count($name_file,$item1,0);
            if($check > 0)
                return 'video';
        }

        $audio = explode(',','mp3,ogg,wav,wma,m4a');
        foreach($audio as $item2)
        {
            $check = substr_count($name_file,$item2,0);
            if($check > 0)
                return 'audio';
        }

    }
    public function deleteImage() {
        if(strlen($this->image) != 0){
            $image = Yii::$app->basePath . '/web/images/quote/' . $this->image;
            if (unlink($image)) {
                $this->image = null;
                $this->save();
                return true;
            }
        }
        return false;
    }
}
