Blame view

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

  

      namespace common\models;

  

004b2298   Yarik   test
5
      use common\behaviors\MapsBehavior;

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

      use common\modules\comment\models\Rating;

eb7e82fb   Administrator   29.02.16
8
9
10
      use Yii;

      use yii\behaviors\BlameableBehavior;

      use yii\behaviors\TimestampBehavior;

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

eb7e82fb   Administrator   29.02.16
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      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
32
       * @property User                      $user

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

       * @property Rating[]                  $rating

       * @property string                    $ratingValue

38ffb9db   Yarik   test
36
37
       * @method string minImg( string $dir, $width, $height = NULL ) Resizes image

       * @method array ShowGallery( string $array ) Splits string to image paths array

eb7e82fb   Administrator   29.02.16
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
65
66
67
       */

      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
68
                      'class' => 'common\behaviors\ShowImage',

eb7e82fb   Administrator   29.02.16
69
                  ],

004b2298   Yarik   test
70
71
72
73
74
75
76
77
78
79
80
                  'maps'       => [

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

                      'location_attributes' => [

                          'city',

                          'street',

                          'house',

                      ],

                      'required_attributes' => [

                          'city',

                      ],

                  ],

eb7e82fb   Administrator   29.02.16
81
82
83
84
85
86
87
88
89
90
91
92
93
              ];

          }

  

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [

                          'name',

                          'preview',

2293c233   Administrator   16.03.16
94
                          'city',

2f324895   Yarik   test
95
                          'cover',

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

                      '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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                  '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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
              ];

          }

  

          /**

           * @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
211
  

38ffb9db   Yarik   test
212
213
214
          /**

           * @return ActiveQuery

           */

fdc1c9de   Yarik   test
215
216
          public function getPortfolioUsers()

          {

2f324895   Yarik   test
217
218
219
220
221
222
223
              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
224
          }

877ea4b2   Yarik   test
225
226
227
228
229
230
231
  

          /**

           * @return ActiveQuery

           */

          public function getComments()

          {

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

38ffb9db   Yarik   test
232
233
234
                          ->andWhere([ 'comment.model'  => $this->className(),

                                       'comment.status' => Comment::STATUS_ACTIVE,

                          ]);

877ea4b2   Yarik   test
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
          }

  

          /**

           * @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
260
      }