Blame view

common/models/Book.php 5.01 KB
8f340aa7   Anastasia   - main page
1
2
3
4
  <?php
      
      namespace common\models;
      
82d2fae2   Anastasia   - add alias to books
5
6
      use artbox\core\models\Alias;
      use frontend\behaviors\SlugBehavior;
8f340aa7   Anastasia   - main page
7
8
9
      use Yii;
      use yii\behaviors\TimestampBehavior;
      use yii\helpers\FileHelper;
8f340aa7   Anastasia   - main page
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
      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,
82d2fae2   Anastasia   - add alias to books
46
47
48
49
50
51
52
                  ],
                  [
                      'class' => SlugBehavior::className(),
                      'action' => 'book/view',
                      'params' => [
                          'id'   => 'id',
                      ],
8f340aa7   Anastasia   - main page
53
                  ]
82d2fae2   Anastasia   - add alias to books
54
              ];
8f340aa7   Anastasia   - main page
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
              
          }
          /**
           * {@inheritdoc}
           */
          public function rules()
          {
              return [
                  [
                      [
                          'author_id',
                          'created_at',
                          'status',
                      ],
                      'default',
                      'value' => null,
                  ],
                  [
                      [
                          'author_id',
                          'created_at',
                          'status',
82d2fae2   Anastasia   - add alias to books
77
                          'alias_id'
8f340aa7   Anastasia   - main page
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                      ],
                      '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;
          }
          
82d2fae2   Anastasia   - add alias to books
163
164
165
166
167
          public function getAlias(){
              return $this->hasOne(Alias::className(), ['id' => 'alias_id']);
          }
          
          public function getActiveComments(){
95f0b726   Anastasia   - comment answer ...
168
              return $this->hasMany(Comment::className(), ['book_id' => 'id'])->with('activeComments')->where(['status' => true, 'parent_id' => null])->orderBy('created_at');
82d2fae2   Anastasia   - add alias to books
169
170
          }
          
8f340aa7   Anastasia   - main page
171
      }