<?php
namespace common\models;
use Yii;
use yii\helpers\Url;
use yii\web\UploadedFile;
class Book extends \yii\db\ActiveRecord
{
const IMAGES_SIZE = [
['50', '50'],
];
public $file;
public $bookfile;
public static function tableName()
{
return 'book';
}
public function rules()
{
return [
[['category_id', 'author_id', 'name', 'language', 'year', 'status'], 'required'],
[['category_id', 'year', 'status'], 'integer'],
[['content'], 'string'],
[['name', 'language', 'keywords', 'description', 'bookfile'], 'string', 'max' => 255],
[['author_id'], 'string', 'max' => 50],
[['image'], 'string', 'max' => 100],
[['file'], 'image'],
[['bookfile'], 'file', 'extensions' => 'txt,docx,pdf,fb2'],
];
}
public function attributeLabels()
{
return [
'id' => '№',
'category_id' => 'Категория',
'author_id' => 'Автор',
'name' => 'Название',
'content' => 'Текс',
'language' => 'Язык',
'year' => 'Год',
'keywords' => 'Ключевые слова',
'description' => 'Описание',
'bookfile' => 'Книга',
'smallImage' => 'Картинка',
'image' => 'Картинка',
'file' => 'Картинка',
'statusName' => 'Статус',
'status' => 'Статус',
];
}
public function getAuthor()
{
return $this->hasOne(Author::className(), ['id' => 'author_id']);
}
public function getCategory() {
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
public function getFirstLetter() {
return mb_substr($this->name, 0, 1, 'utf-8');
}
public static function getStatusList()
{
return ['Выкл', 'Вкл'];
}
public function getStatusName()
{
$list = self::getStatusList();
return $list[$this->status];
}
public function getSmallImage() {
if($this->image) {
$path = str_replace('admin/', '', Url::home(true)).'uploads/image/book/50x50/'.$this->image;
}
else {
$path = str_replace('admin/', '', Url::home(true)).'uploads/image/no-image.svg';
}
return $path;
}
public function beforeSave($insert)
{
if($file = UploadedFile::getInstance($this, 'file')){
$dir = Yii::getAlias('@images').'/book/';
if(file_exists($dir.$this->image)){
@unlink($dir.$this->image);
}
if(file_exists($dir.'50x50/'.$this->image)){
@unlink($dir.'50x50/'.$this->image);
}
$this->image = strtotime('now').'_'.Yii::$app->getSecurity()->generateRandomString(6) . '.' . $file->extension;
$file->saveAs($dir.$this->image);
$imag = Yii::$app->image->load($dir.$this->image);
$imag->background('#fff',0);
$imag->resize('50','50', Yii\image\drivers\Image::INVERSE);
$imag->crop('50','50');
$imag->save($dir.'50x50/'.$this->image, 90);
}
if($file = UploadedFile::getInstance($this, 'bookfile')){
$dir = Yii::getAlias('@books');
if(file_exists($dir.$this->bookfile)){
@unlink($dir.$this->bookfile);
}
$this->bookfile = strtotime('now').'_'.Yii::$app->getSecurity()->generateRandomString(6) . '.' . $file->extension;
$file->saveAs($dir.$this->bookfile);
}
return parent::beforeSave($insert);
}
public function beforeDelete()
{
if (parent::beforeDelete()) {
$dir = Yii::getAlias('@images').'/book/';
if(file_exists($dir.$this->image)){
@unlink($dir.$this->image);
}
foreach (self::IMAGES_SIZE as $size){
$size_dir = $size[0].'x';
if($size[1] !== null)
$size_dir .= $size[1];
if(file_exists($dir.$this->image)){
@unlink($dir.$size_dir.'/'.$this->image);
}
}
return true;
} else {
return false;
}
}
}