Book.php 5.09 KB
<?php
    
    namespace common\models;
    
    use artbox\core\models\Alias;
    use frontend\behaviors\SlugBehavior;
    use Yii;
    use yii\behaviors\TimestampBehavior;
    use yii\helpers\FileHelper;
    use yii\web\UploadedFile;

    /**
     * This is the model class for table "book".
     *
     * @property int    $id
     * @property int    $author_id
     * @property string $title
     * @property string $image
     * @property string $preview
     * @property string $description
     * @property int    $created_at
     * @property int    $status
     * @property Author $author
     */
    class Book extends \yii\db\ActiveRecord
    {
        const STATUS_ACTIVE = 1;
        const STATUS_MODERATION = 0;
        const STATUS_DELETED = 2;
        /**
         * {@inheritdoc}
         */
        public static function tableName()
        {
            return 'book';
        }
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class'              => TimestampBehavior::className(),
                    'updatedAtAttribute' => false,
                ],
                [
                    'class' => SlugBehavior::className(),
                    'action' => 'book/view',
                    'params' => [
                        'id'   => 'id',
                    ],
                ]
            ];
            
        }
        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                [
                    [
                        'author_id',
                        'created_at',
                        'status',
                    ],
                    'default',
                    'value' => null,
                ],
                [
                    [
                        'author_id',
                        'created_at',
                        'status',
                        'alias_id',
                        'price'
                    ],
                    'integer',
                ],
                [
                    [
                        'preview',
                        'description',
                    ],
                    'string',
                ],
                [
                    [
                        'title',
                        'image',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'author_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Author::className(),
                    'targetAttribute' => [ 'author_id' => 'id' ],
                ],
                [
                   ['title'], 'required'
                ],
                [
                    ['on_main'], 'boolean'
                ]
            ];
        }
        
        /**
         * {@inheritdoc}
         */
        public function attributeLabels()
        {
            return [
                'id'          => Yii::t('app', 'ID'),
                'author_id'   => Yii::t('app', 'Author ID'),
                'title'       => Yii::t('app', 'Title'),
                'image'       => Yii::t('app', 'Image'),
                'preview'     => Yii::t('app', 'Preview'),
                'description' => Yii::t('app', 'Description'),
                'created_at'  => Yii::t('app', 'Created At'),
                'status'      => Yii::t('app', 'Status'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getAuthor()
        {
            return $this->hasOne(Author::className(), [ 'id' => 'author_id' ]);
        }
        
        public function getStatuses(): array{
            return [
              self::STATUS_ACTIVE => 'Активный',
              self::STATUS_MODERATION => 'На модерации',
              self::STATUS_DELETED => 'На удалении',
            ];
        }
        
        public function saveImage($file){
            /**
             * @var UploadedFile $file;
             */
            if (!empty($file)){
                if (!file_exists(\Yii::getAlias('@storage/books/') . $this->id)) {
                    FileHelper::createDirectory(\Yii::getAlias('@storage/books/') . $this->id);
                }
                if ($file->saveAs(\Yii::getAlias('@storage/books/').$this->id.'/'.$this->id.'.'.$file->extension)){
                    $this->image = $this->id.'.'.$file->extension;
                    return $this->save();
                }
                return false;
                
            }
            return true;
        }
        
        public function getAlias(){
            return $this->hasOne(Alias::className(), ['id' => 'alias_id']);
        }
        
        public function getActiveComments(){
            return $this->hasMany(Comment::className(), ['entity_id' => 'id'])->andWhere(['entity' => Book::className()])->with('activeComments')->where(['status' => true, 'parent_id' => null])->orderBy('created_at');
        }
        
    }