Blame view

common/modules/product/models/Product.php 8.69 KB
a8370482   Alexander Karnovsky   init project
1
2
3
4
  <?php
  
  namespace common\modules\product\models;
  
5aa7418e   Karnovsky A   Base-product#3 fu...
5
  use common\behaviors\Slug;
a8370482   Alexander Karnovsky   init project
6
7
8
9
  use common\modules\rubrication\models\TaxOption;
  use Yii;
  use common\modules\relation\relationBehavior;
  use yii\db\ActiveQuery;
e2128676   Karnovsky A   Karnovsky 0505201...
10
11
  use yii\helpers\Html;
  use yii\web\UploadedFile;
a8370482   Alexander Karnovsky   init project
12
13
14
15
16
  
  /**
   * This is the model class for table "{{%product}}".
   *
   * @property string $name
b519af22   Karnovsky A   Base-product func...
17
   * @property integer $brand_id
a8370482   Alexander Karnovsky   init project
18
   * @property integer $product_id
b519af22   Karnovsky A   Base-product func...
19
20
21
22
23
   * @property Category $category
   * @property array $categories
   * @property ProductVariant $variant
   * @property ProductImage $image
   * @property array $images
550f051a   Karnovsky A   Karnovsky-0505201...
24
25
   * @property boolean $is_top
   * @property boolean $is_new
a8370482   Alexander Karnovsky   init project
26
27
28
   */
  class Product extends \yii\db\ActiveRecord
  {
14eadb86   Karnovsky A   Eager loading for...
29
      /** @var array $_variants */
b519af22   Karnovsky A   Base-product func...
30
      public $_variants = [];
5aa7418e   Karnovsky A   Base-product#3 fu...
31
32
33
  
      /** @var array $_images */
      public $imagesUpload = [];
a8370482   Alexander Karnovsky   init project
34
35
36
37
38
39
40
41
42
      /**
       * @inheritdoc
       */
      public function behaviors()
      {
          return [
              [
                  'class' => relationBehavior::className(),
                  'relations' => [
5aa7418e   Karnovsky A   Base-product#3 fu...
43
44
                      'product_categories' => 'entity1', // Product category
                      'product_option' => 'entity1' // Product category
a8370482   Alexander Karnovsky   init project
45
46
                  ]
              ],
5aa7418e   Karnovsky A   Base-product#3 fu...
47
48
49
50
51
52
              [
                  'class' => Slug::className(),
                  'in_attribute' => 'name',
                  'out_attribute' => 'alias',
                  'translit' => true
              ]
a8370482   Alexander Karnovsky   init project
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
          ];
      }
  
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return '{{%product}}';
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
b519af22   Karnovsky A   Base-product func...
70
              [['brand_id'], 'integer'],
a8370482   Alexander Karnovsky   init project
71
              [['name'], 'string', 'max' => 150],
5aa7418e   Karnovsky A   Base-product#3 fu...
72
              [['alias'], 'string', 'max' => 250],
e2128676   Karnovsky A   Karnovsky 0505201...
73
              [['categories', 'variants', 'options', 'imagesUpload'], 'safe'],
55b984ad   Karnovsky A   ---
74
  //            [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif', 'maxFiles' => 50],
5aa7418e   Karnovsky A   Base-product#3 fu...
75
              [['description', 'video'], 'safe'],
550f051a   Karnovsky A   Karnovsky-0505201...
76
              [['is_top', 'is_new'], 'boolean'],
a8370482   Alexander Karnovsky   init project
77
78
79
80
81
82
83
84
85
86
  //            [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
b519af22   Karnovsky A   Base-product func...
87
              'product_id' => Yii::t('product', 'ID'),
a8370482   Alexander Karnovsky   init project
88
              'name' => Yii::t('product', 'Name'),
b519af22   Karnovsky A   Base-product func...
89
              'brand_id' => Yii::t('product', 'Brand'),
a8370482   Alexander Karnovsky   init project
90
              'categories' => Yii::t('product', 'Categories'), // relation behavior field
b519af22   Karnovsky A   Base-product func...
91
              'category' => Yii::t('product', 'Category'), // relation behavior field
5aa7418e   Karnovsky A   Base-product#3 fu...
92
93
              'image' => Yii::t('product', 'Image'),
              'images' => Yii::t('product', 'Images'),
550f051a   Karnovsky A   Karnovsky-0505201...
94
95
96
97
98
              'description' => Yii::t('product', 'Description'),
              'video' => Yii::t('product', 'Video embeded'),
              'variants' => Yii::t('product', 'Variants'),
              'is_top' => Yii::t('product', 'Is top'),
              'is_new' => Yii::t('product', 'Is new'),
a8370482   Alexander Karnovsky   init project
99
100
101
102
103
104
105
106
          ];
      }
  
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getBrand()
      {
b519af22   Karnovsky A   Base-product func...
107
          return $this->hasOne(Brand::className(), ['brand_id' => 'brand_id']);
a8370482   Alexander Karnovsky   init project
108
109
      }
  
b519af22   Karnovsky A   Base-product func...
110
111
112
113
114
115
116
117
118
119
120
121
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getImage()
      {
          return $this->hasOne(ProductImage::className(), ['product_id' => 'product_id']);
      }
  
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getImages()
a8370482   Alexander Karnovsky   init project
122
      {
b519af22   Karnovsky A   Base-product func...
123
          return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id']);
a8370482   Alexander Karnovsky   init project
124
125
      }
  
5aa7418e   Karnovsky A   Base-product#3 fu...
126
127
128
129
      public function setImages($images) {
          $this->_images = $images;
      }
  
b519af22   Karnovsky A   Base-product func...
130
131
132
133
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getVariant()
a8370482   Alexander Karnovsky   init project
134
      {
b519af22   Karnovsky A   Base-product func...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
          return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']);
      }
  
      public function getVariantPrice() {
          return $this->variant->price;
      }
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getVariants()
      {
          return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']);
      }
  
      public function setVariants($variants) {
          $this->_variants = $variants;
      }
  
      public function getFullName()
      {
7cdb0894   Karnovsky A   Some fixes
155
          return empty($this->brand) ? $this->name : $this->brand->name .' '. $this->name;
b519af22   Karnovsky A   Base-product func...
156
157
158
      }
  
      public function getCategories() {
550f051a   Karnovsky A   Karnovsky-0505201...
159
160
          return $this->hasMany(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']);
  //        return $this->getRelations('product_categories');
b519af22   Karnovsky A   Base-product func...
161
162
163
164
165
166
167
168
      }
  
      public function getCategoriesNames() {
          $result = [];
          foreach($this->categories as $category) {
              $result[] = $category->name;
          }
          return $result;
a8370482   Alexander Karnovsky   init project
169
170
171
      }
  
      public function getCategory() {
550f051a   Karnovsky A   Karnovsky-0505201...
172
          return $this->hasOne(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']);
a8370482   Alexander Karnovsky   init project
173
174
      }
  
5aa7418e   Karnovsky A   Base-product#3 fu...
175
176
177
178
      public function getOptions() {
          return $this->getRelations('product_option');
      }
  
a8370482   Alexander Karnovsky   init project
179
180
181
182
183
184
185
186
      /**
       * @inheritdoc
       * @return ProductQuery the active query used by this AR class.
       */
      public static function find()
      {
          return new ProductQuery(get_called_class());
      }
b519af22   Karnovsky A   Base-product func...
187
188
189
190
191
  
      public function afterSave($insert, $changedAttributes)
      {
          parent::afterSave($insert, $changedAttributes);
  
e2128676   Karnovsky A   Karnovsky 0505201...
192
193
194
195
196
197
198
199
  //        $images = UploadedFile::getInstance($this, 'imagesUpload');
  //        var_dump($images);exit;
  
  //        if (!empty($this->imagesUpload)) {
  //            if (!is_array($this->imagesUpload)) {
  //                $this->imagesUpload = [$this->imagesUpload];
  //            }
  //            foreach($this->imagesUpload as $image) {
1f90c5f4   Karnovsky A   ArtboxImage resizer
200
  //                $image->saveAs((Yii::getAlias('@storage/products/original/' . $image->baseName .'_'. uniqid() . '.' . $image->extension)));
e2128676   Karnovsky A   Karnovsky 0505201...
201
202
203
  //            }
  //
  //
0cc2a675   Karnovsky A   Base-product#4 fu...
204
  //        }
5aa7418e   Karnovsky A   Base-product#3 fu...
205
  
e136edc8   Karnovsky A   ---
206
207
208
209
          if (!empty($this->_variants)) {
              $todel = [];
              foreach ($this->variants ?: [] as $_variant) {
                  $todel[$_variant->product_variant_id] = $_variant->product_variant_id;
5aa7418e   Karnovsky A   Base-product#3 fu...
210
              }
e136edc8   Karnovsky A   ---
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
              foreach ($this->_variants as $_variant) {
                  if (!is_array($_variant)) {
                      return;
                  }
                  if (!empty($_variant['product_variant_id'])) {
                      unset($todel[$_variant['product_variant_id']]);
                      $model = ProductVariant::findOne($_variant['product_variant_id']);
                  } else {
                      $model = new ProductVariant();
                  }
                  $_variant['product_id'] = $this->product_id;
                  $model->load(['ProductVariant' => $_variant]);
                  $model->save();
              }
              if (!empty($todel)) {
                  ProductVariant::deleteAll(['product_variant_id' => $todel]);
b519af22   Karnovsky A   Base-product func...
227
              }
b519af22   Karnovsky A   Base-product func...
228
229
          }
      }
e2128676   Karnovsky A   Karnovsky 0505201...
230
  
1c1c6545   Karnovsky A   ---
231
232
233
234
235
236
237
      public function beforeDelete() {
          ProductImage::deleteAll(['product_id' => $this->product_id]);
          ProductCategory::deleteAll(['product_id' => $this->product_id]);
          ProductVariant::deleteAll(['product_id' => $this->product_id]);
          return true;
      }
  
e2128676   Karnovsky A   Karnovsky 0505201...
238
239
240
      public function imagesUpload()
      {
          if ($this->validate()) {
cf07505d   Karnovsky A   Karnovsky 0605201...
241
              $images = [];
e2128676   Karnovsky A   Karnovsky 0505201...
242
              foreach ($this->imagesUpload as $image) {
cf07505d   Karnovsky A   Karnovsky 0605201...
243
244
                  $imageName = $image->baseName .'.'. $image->extension;
                  $i = 0;
1b9687f3   Karnovsky A   All resizer
245
                  while(file_exists(Yii::getAlias('@storage/products/' . $imageName))) {
cf07505d   Karnovsky A   Karnovsky 0605201...
246
247
248
249
                      $i++;
                      $imageName = $image->baseName .'_'. $i .'.'. $image->extension;
                  }
  
1b9687f3   Karnovsky A   All resizer
250
                  $image->saveAs(Yii::getAlias('@storage/products/' .$imageName));
cf07505d   Karnovsky A   Karnovsky 0605201...
251
                  $images[] = $imageName;
e2128676   Karnovsky A   Karnovsky 0505201...
252
              }
cf07505d   Karnovsky A   Karnovsky 0605201...
253
              return $images;
e2128676   Karnovsky A   Karnovsky 0505201...
254
255
256
257
258
          } else {
              return false;
          }
      }
  
e136edc8   Karnovsky A   ---
259
260
261
      public function getImageUrl()
      {
          $image = empty($this->variant) ? null : $this->variant->image;
1b9687f3   Karnovsky A   All resizer
262
          return !empty($image) ? $image->imageUrl : '/storage/no_photo.png';
e136edc8   Karnovsky A   ---
263
264
      }
  
e2128676   Karnovsky A   Karnovsky 0505201...
265
266
267
268
269
270
271
272
273
      public function getImagesHTML() {
          $op = [];
          if ($this->images) {
              foreach ($this->images as $image) {
                  $op[] = Html::img($image->imageUrl);
              }
          }
          return $op;
      }
cf07505d   Karnovsky A   Karnovsky 0605201...
274
275
276
277
278
279
280
281
  
      public function getImagesConfig() {
          $op = [];
          if ($this->images) {
              foreach ($this->images as $image) {
                  $op[] = [
                      'caption' => $image->image,
                      'width' => '120px',
dc50f607   Karnovsky A   Some fixes
282
283
284
285
286
                      'url' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $image->product_image_id]),
                      'key' => $image->product_image_id,
                      'extra' => [
                          'id' => $image->product_image_id,
                      ],
cf07505d   Karnovsky A   Karnovsky 0605201...
287
288
289
290
291
                  ];
              }
          }
          return $op;
      }
a8370482   Alexander Karnovsky   init project
292
  }