Blame view

common/models/Portfolio.php 8.08 KB
eb7e82fb   Administrator   29.02.16
1
2
3
4
  <?php

  

      namespace common\models;

  

877ea4b2   Yarik   test
5
6
      use common\modules\comment\models\Comment;

      use common\modules\comment\models\Rating;

eb7e82fb   Administrator   29.02.16
7
8
9
      use Yii;

      use yii\behaviors\BlameableBehavior;

      use yii\behaviors\TimestampBehavior;

877ea4b2   Yarik   test
10
      use yii\db\ActiveQuery;

eb7e82fb   Administrator   29.02.16
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
      use yii\db\Expression;

  

      /**

       * This is the model class for table "portfolio".

       * @property integer                   $portfolio_id

       * @property integer                   $user_id

       * @property string                    $name

       * @property string                    $link

       * @property string                    $date_add

       * @property integer                   $user_add_id Currently inactive attribute

       * @property integer                   $view_count

       * @property string                    $city

       * @property string                    $street

       * @property string                    $house

       * @property string                    $description

       * @property string                    $cover

       * @property integer                   $gallery_id

       * @property string                    $preview

       * @property PortfolioSpecialization[] $portfolioSpecializations

       * @property Specialization[]          $specializations

2f324895   Yarik   test
31
       * @property User                      $user

877ea4b2   Yarik   test
32
33
34
       * @property Comment[]                 $comment

       * @property Rating[]                  $rating

       * @property string                    $ratingValue

eb7e82fb   Administrator   29.02.16
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
       */

      class Portfolio extends \yii\db\ActiveRecord

      {

  

          /**

           * @inheritdoc

           */

          public static function tableName()

          {

              return 'portfolio';

          }

  

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

                  [

                      'class'              => BlameableBehavior::className(),

                      'createdByAttribute' => 'user_id',

                      'updatedByAttribute' => false,

                  ],

                  [

                      'class'              => TimestampBehavior::className(),

                      'createdAtAttribute' => 'date_add',

                      'updatedAtAttribute' => false,

                      'value'              => new Expression('NOW()'),

                  ],

                  [

2f324895   Yarik   test
65
                      'class' => 'common\behaviors\ShowImage',

eb7e82fb   Administrator   29.02.16
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                  ],

              ];

          }

  

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [

                          'name',

                          'preview',

2293c233   Administrator   16.03.16
80
                          'city',

2f324895   Yarik   test
81
                          'cover',

eb7e82fb   Administrator   29.02.16
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
                      ],

                      'required',

                  ],

                  [

                      [

                          'gallery_id',

                      ],

                      'integer',

                  ],

                  [

                      [

                          'description',

                          'cover',

                      ],

                      'string',

                  ],

                  [

                      [

                          'name',

                          'link',

                          'city',

                          'street',

                          'house',

                      ],

                      'string',

                      'max' => 255,

                  ],

                  [

                      [

                          'preview',

                      ],

                      'string',

                      'max' => 1000,

                  ],

                  [

                      [

                          'specializationInput',

                      ],

                      'safe',

                  ],

              ];

          }

  

          /**

           * @inheritdoc

           */

          public function attributeLabels()

          {

              return [

06ec2844   Administrator   28.03.16
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
                  'portfolio_id'         => Yii::t('app', 'portfolio_id'),

                  'user_id'              => Yii::t('app', 'user_id'),

                  'name'                 => Yii::t('app', 'name'),

                  'link'                 => Yii::t('app', 'link'),

                  'date_add'             => Yii::t('app', 'date_add'),

                  'user_add_id'          => Yii::t('app', 'user_add_id'),

                  'view_count'           => Yii::t('app', 'view_count'),

                  'city'                 => Yii::t('app', 'city'),

                  'street'               => Yii::t('app', 'street'),

                  'house'                => Yii::t('app', 'house'),

                  'description'          => Yii::t('app', 'description'),

                  'cover'                => Yii::t('app', 'cover_m'),

                  'gallery_id'           => Yii::t('app', 'gallery_id'),

                  'specializationString' => Yii::t('app', 'specializationString'),

                  'preview'              => Yii::t('app', 'preview'),

eb7e82fb   Administrator   29.02.16
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
              ];

          }

  

          /**

           * @return \yii\db\ActiveQuery

           */

          public function getPortfolioSpecializations()

          {

              return $this->hasMany(PortfolioSpecialization::className(), [ 'portfolio_id' => 'portfolio_id' ]);

          }

  

          public function getSpecializations()

          {

              return $this->hasMany(Specialization::className(), [ 'specialization_id' => 'specialization_id' ])

                          ->viaTable('portfolio_specialization', [ 'portfolio_id' => 'portfolio_id' ]);

          }

  

          public function getSpecializationInput()

          {

              return $this->getSpecializations()

                          ->asArray()

                          ->indexBy('specialization_id')

                          ->column();

          }

  

          public function setSpecializationInput($value)

          {

              $this->specializationInput = $value;

          }

  

          public function getSpecializationString()

          {

              return implode(', ', $this->getSpecializations()

                                        ->select([

                                            'specialization_name',

                                            'specialization_id',

                                        ])

                                        ->asArray()

                                        ->indexBy('specialization_id')

                                        ->column());

          }

  

          public function setSpecializationString($value)

          {

              $this->specializationString = $value;

          }

  

          public function getGallery()

          {

              return $this->hasOne(Gallery::className(), [ 'gallery_id' => 'gallery_id' ]);

          }

fdc1c9de   Yarik   test
197
198
199
  

          public function getPortfolioUsers()

          {

2f324895   Yarik   test
200
201
202
203
204
205
206
              return $this->hasMany(PortfolioUser::className(), [ 'portfolio_id' => 'portfolio_id' ])

                          ->with('user');

          }

  

          public function getUser()

          {

              return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);

fdc1c9de   Yarik   test
207
          }

877ea4b2   Yarik   test
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  

          /**

           * @return ActiveQuery

           */

          public function getComments()

          {

              return $this->hasMany(Comment::className(), [ 'model_id' => 'portfolio_id' ])

                          ->andWhere([ 'comment.model' => $this->className(), 'comment.status' => Comment::STATUS_ACTIVE ]);

          }

  

          /**

           * @return ActiveQuery

           */

          public function getRating()

          {

              return $this->hasMany(Rating::className(), [ 'model_id' => 'comment_id' ])

                          ->via('comments')

                          ->andWhere([

                              'not',

                              [ 'rating.value' => NULL ],

                          ]);

          }

  

          /**

           * @return string

           */

          public function getRatingValue()

          {

              return $this->getRating()

                          ->asArray()

                          ->select([ 'sum' => 'ROUND(SUM(rating.value)/COUNT(rating.value)::numeric, 1)' ])

                          ->scalar();

          }

eb7e82fb   Administrator   29.02.16
241
      }