<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "chapter".
 *
 * @property integer $id
 * @property integer $bookId
 * @property string $title
 * @property string $bookMarkIndex
 * @property string $image
 * @property integer $type
 * @property BookMark[] $bookMarks
 * @property Book $book
 * @property Quote[] $quotes
 * @property string $time
 * @property string $attachment
 */
class Chapter extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public $file;
	public $attach;
    public static function tableName()
    {
        return 'chapter';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title','chapterIndex', 'type'], 'required'],
            [['bookId','type','chapterIndex'], 'integer'],
            [['title', 'bookMarkIndex'], 'string', 'max' => 255],
            [['image','directURL'], 'string'],
            [['time','attachment'], 'safe'],
            [['file'], 'file'],
			[['attach'], 'file','extensions' => 'pdf,epub,mp4,avi,flv,mpeg,mkv,3gp,wmv,swf,mp3'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'bookId' => 'Book',
            'title' => 'Title',
            'bookMarkIndex' => 'Book Mark Index',
            'image' => 'Image',
            'time' => 'Time',
            'chapterIndex' => 'Index',
			'attach' => 'File ( Input file with type: pdf,epub,video or mp3 )',
			'directURL'=> 'Direct URL (priority)'
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBookMarks()
    {
        return $this->hasMany(BookMark::className(), ['chapterId' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBook()
    {
        return $this->hasOne(Book::className(), ['id' => 'bookId']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getQuotes()
    {
        return $this->hasMany(Quote::className(), ['chapterId' => 'id']);
    }

    public static function checkType($typeFile)
    {
        if($typeFile === 'pdf')
            //return \Constants::FILE_PDF; //pdf
			return 1;
        if($typeFile === 'epub')
            //return \Constants::FILE_EPUB; // Epub
			return 2;
        $video = explode(',','mp4,avi,flv,mpeg,mkv,3gp,wmv,swf');
        if(in_array($typeFile,$video))
            //return \Constants::FILE_VIDEO; //video
			return 3;
		if($typeFile == 'mp4')
			return 4;
    }

    public static function deleteImage($oldImage) {
        if(strlen($oldImage) != 0){
            $image = Yii::$app->basePath . '/web/images/chapter/' . $oldImage;
			if(is_file($image)){			
            if (unlink($image)) {
					//$this->image = null;
					//$this->save();
					return true;
				}
			}
        }
        return false;
    }
	
	public static function deleteAttach($oldAttach) {
        if(strlen($oldAttach) != 0){
            $attach = Yii::$app->basePath . '/uploads/attachment/' . $oldAttach;
			if(is_file($attach)){			
            if (unlink($attach)) {
					//$this->image = null;
					//$this->save();
					return true;
				}
			}
        }
        return false;
    }
}
