Products.php
6.41 KB
1
2
3
4
5
6
7
8
9
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
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
71
72
73
74
75
76
77
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
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
<?php
namespace app\modules\admin\models;
use Yii;
use app\components\Translite;
use app\components\resize;
use yii\web\UploadedFile;
class Products extends \yii\db\ActiveRecord
{
public $old_image;
public $catalog_parent;
public $catalog;
public $catalog_parent_image;
public $catalog_image;
public static function tableName()
{
return 'products';
}
public function rules()
{
return [
[
[
'name','art'
],
'required'
],
[
[
'fasovka','type','brends','catalog_id','old_image',
'body','char','new','top','akciya', 'out_of_stock','translit',
'meta_title','meta_keywords','meta_description','similar','featured'
],
'safe'
],
[
['image'],
'file',
'extensions'=>'jpg, gif, png',
'skipOnEmpty'=>true
],
];
}
public function attributeLabels()
{
return [
'name'=>'Название',
'body'=>'Описание',
'char'=>'Характеристики',
'sort'=>'Сорт.',
'image'=>'Изображения',
'fasovka'=>'Фасовка',
'type'=>'Типы',
'brends'=>'Производители',
'top'=>'Топ',
'new'=>'Новинка',
'akciya'=>'Акция',
'out_of_stock'=>'Нет в наличии',
'similar'=>'Похожие товары',
'featured'=>'Рекоммендуемые товары',
];
}
public function beforeSave($insert) {
if (!$this->translit)
$this->translit = Translite::rusencode($this->name);
if($image = UploadedFile::getInstance($this,'image')){
$this->deleteImage($this->old_image);
//$this->image = $image;
$this->image = time() . '_' . rand(1, 1000) . '.' . $image->extension;
$image->saveAs('upload/products/'.$this->image);
$resizeObj = new resize('upload/products/'.$this->image);
$resizeObj -> resizeImage(180, 240, 'landscape');
$resizeObj -> saveImage('upload/products/ico/'.$this->image, 100);
$resizeObj -> resizeImage(400, 400, 'landscape');
$resizeObj -> saveImage('upload/products/big/'.$this->image, 100);
}else $this->image = $this->old_image;
return parent::beforeSave($insert);
}
public function afterSave($insert, $changedAttributes) {
if(!$insert) {
ProductsFasovka::deleteAll(['product_id' => $this->id]);
}
if(is_array($this->fasovka)) {
foreach ($this->fasovka as $fasovka) {
$ProductsFasovka = new ProductsFasovka();
$ProductsFasovka->product_id = $this->id;
$ProductsFasovka->fasovka_id = $fasovka;
$ProductsFasovka->save();
}
}
if(!$insert) {
ProductsType::deleteAll(['product_id' => $this->id]);
}
if(is_array($this->type)) {
foreach ($this->type as $type) {
$ProductsType = new ProductsType();
$ProductsType->product_id = $this->id;
$ProductsType->type_id = $type;
$ProductsType->save();
}
}
if(!$insert) {
ProductsBrends::deleteAll(['product_id' => $this->id]);
}
if(is_array($this->brends)) {
foreach ($this->brends as $brend) {
$ProductsBrends = new ProductsBrends();
$ProductsBrends->product_id = $this->id;
$ProductsBrends->brend_id = $brend;
$ProductsBrends->save();
}
}
return parent::afterSave($insert, $changedAttributes);
}
public function beforeDelete() {
$this->deleteImage($this->image);
return parent::beforeDelete();
}
public function getFasovka()
{
return $this->hasMany(Fasovka::className(), ['id' => 'fasovka_id'])->viaTable(ProductsFasovka::tableName(), ['product_id' => 'id']);
}
public function setFasovka($fasovka)
{
$this->fasovka = $fasovka;
}
public function getType()
{
return $this->hasMany(Type::className(), ['id' => 'type_id'])->viaTable(ProductsType::tableName(), ['product_id' => 'id']);
}
public function setType($type)
{
$this->type = $type;
}
public function getBrends()
{
return $this->hasMany(Brends::className(), ['id' => 'brend_id'])->viaTable(ProductsBrends::tableName(), ['product_id' => 'id']);
}
public function setBrends($brends)
{
$this->brends = $brends;
}
public function getCatalog()
{
return $this->hasOne(Catalog::className(), ['id' => 'catalog_id']);
}
public function getMods()
{
return $this->hasMany(Mod::className(), ['product_id' => 'id']);
}
public function getFotos()
{
return $this->hasMany(Fotos::className(), ['product_id' => 'id']);
}
public function deleteImage($file){
if(!empty($file)){
@unlink('upload/products/'.$file);
@unlink('upload/products/ico/'.$file);
@unlink('upload/products/big/'.$file);
}
}
}