Blame view

common/models/Portfolio.php 9 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
      use yii\db\Expression;

55bd0bb9   Yarik   Commit
13
      use yii\base\Component;

eb7e82fb   Administrator   29.02.16
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  

      /**

       * 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
33
       * @property User                      $user

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

       * @property Rating[]                  $rating

       * @property string                    $ratingValue

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

55bd0bb9   Yarik   Commit
38
39
       * @method Component watermark(string $path) Add watermark to image

       * @method string getWatermark(string $path, mixed $width, mixed $height) Get watermarked image

38ffb9db   Yarik   test
40
       * @method array ShowGallery( string $array ) Splits string to image paths array

eb7e82fb   Administrator   29.02.16
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
68
69
70
       */

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

eb7e82fb   Administrator   29.02.16
72
                  ],

004b2298   Yarik   test
73
74
75
76
77
78
79
80
81
82
83
                  'maps'       => [

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

                      'location_attributes' => [

                          'city',

                          'street',

                          'house',

                      ],

                      'required_attributes' => [

                          'city',

                      ],

                  ],

eb7e82fb   Administrator   29.02.16
84
85
86
87
88
89
90
91
92
93
94
95
96
              ];

          }

  

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [

                          'name',

                          'preview',

2293c233   Administrator   16.03.16
97
                          'city',

2f324895   Yarik   test
98
                          'cover',

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

                      '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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                  '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
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
211
212
213
              ];

          }

  

          /**

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

38ffb9db   Yarik   test
215
216
217
          /**

           * @return ActiveQuery

           */

fdc1c9de   Yarik   test
218
219
          public function getPortfolioUsers()

          {

2f324895   Yarik   test
220
221
222
223
224
225
226
              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
227
          }

877ea4b2   Yarik   test
228
229
230
231
232
233
234
  

          /**

           * @return ActiveQuery

           */

          public function getComments()

          {

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

38ffb9db   Yarik   test
235
236
237
                          ->andWhere([ 'comment.model'  => $this->className(),

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

                          ]);

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

  

          /**

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