|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\shop\models; |
|
4
|
|
|
|
|
5
|
|
|
use yii\db\ActiveRecord; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use app\modules\user\models\User; |
|
8
|
|
|
use app\modules\shop\models\WishlistProduct; |
|
9
|
|
|
use app\modules\shop\models\Currency; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is the model class for table "{{%wishlist}}". |
|
13
|
|
|
* Model fields: |
|
14
|
|
|
* @property integer $id |
|
15
|
|
|
* @property integer $user_id |
|
16
|
|
|
* @property string $title |
|
17
|
|
|
* @property bool $default |
|
18
|
|
|
* Relations: |
|
19
|
|
|
* @property User $user |
|
20
|
|
|
* @property WishlistProduct[] $items |
|
21
|
|
|
*/ |
|
22
|
|
|
class Wishlist extends ActiveRecord |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function tableName() |
|
29
|
|
|
{ |
|
30
|
|
|
return '{{%wishlist}}'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
|
View Code Duplication |
public function rules() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
[ |
|
40
|
|
|
[ |
|
41
|
|
|
'user_id', |
|
42
|
|
|
'title', |
|
43
|
|
|
'default', |
|
44
|
|
|
], |
|
45
|
|
|
'required' |
|
46
|
|
|
], |
|
47
|
|
|
[ |
|
48
|
|
|
[ |
|
49
|
|
|
'user_id', |
|
50
|
|
|
], |
|
51
|
|
|
'integer' |
|
52
|
|
|
], |
|
53
|
|
|
[ |
|
54
|
|
|
[ |
|
55
|
|
|
'default', |
|
56
|
|
|
], |
|
57
|
|
|
'boolean' |
|
58
|
|
|
], |
|
59
|
|
|
[['default'], 'default', 'value' => true], |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function attributeLabels() |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
'id' => Yii::t('app', 'ID'), |
|
70
|
|
|
'user_id' => Yii::t('app', 'User'), |
|
71
|
|
|
'title' => Yii::t('app', 'Title'), |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return User|null |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getUser() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->hasOne(User::className(), ['id' => 'user_id']); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return WishlistProduct[]|null |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getItems() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->hasMany(WishlistProduct::className(), ['wishlist_id' => 'id']); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $user_id |
|
93
|
|
|
* @param array $wishlist_ids |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function getWishlist($user_id, $wishlist_ids) |
|
97
|
|
|
{ |
|
98
|
|
|
if ($user_id != 0) { |
|
99
|
|
|
return self::findAll(['user_id' => $user_id]); |
|
100
|
|
|
} |
|
101
|
|
|
return self::findAll([ |
|
102
|
|
|
'id' => $wishlist_ids, |
|
103
|
|
|
'user_id' => $user_id, |
|
104
|
|
|
]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $title |
|
109
|
|
|
* @param $user_id |
|
110
|
|
|
* @param array $wishlist_ids |
|
111
|
|
|
* @param bool $default |
|
112
|
|
|
* @return Wishlist|null |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function createWishlist($title, $user_id, $wishlist_ids, $default = true) |
|
115
|
|
|
{ |
|
116
|
|
|
$model = new static; |
|
117
|
|
|
$model->user_id = $user_id; |
|
118
|
|
|
$model->title = $title; |
|
119
|
|
|
$model->default = $default; |
|
120
|
|
|
if ($model->save(true)) { |
|
121
|
|
|
$sessionWishlists = $wishlist_ids; |
|
122
|
|
|
$sessionWishlists[] = $model->id; |
|
123
|
|
|
Yii::$app->session->set('wishlists', $sessionWishlists); |
|
124
|
|
|
return $model; |
|
125
|
|
|
} |
|
126
|
|
|
Yii::error('Failed to save wishlist'); |
|
127
|
|
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param $productId |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function addToWishlist($productId) |
|
135
|
|
|
{ |
|
136
|
|
|
if ($productId !== null) { |
|
137
|
|
|
foreach ($this->items as $item) { |
|
138
|
|
|
if ($item->product_id == $productId) { |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
$model = new WishlistProduct(); |
|
143
|
|
|
$model->wishlist_id = $this->id; |
|
144
|
|
|
$model->product_id = $productId; |
|
145
|
|
|
if ($model->save(true)) { |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
Yii::error('Failed to save item in wishlist'); |
|
149
|
|
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
Yii::error('Incorrect product_id'); |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param $user_id |
|
157
|
|
|
* @param array $wishlist_ids |
|
158
|
|
|
* @param $wishlistId |
|
159
|
|
|
* @return int |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function countItems($user_id, $wishlist_ids, $wishlistId = null) |
|
162
|
|
|
{ |
|
163
|
|
|
$query = Wishlist::find(); |
|
|
|
|
|
|
164
|
|
|
$query->where(['user_id' => $user_id]) |
|
165
|
|
|
->joinWith('items', true, 'INNER JOIN'); |
|
166
|
|
|
if (null !== $wishlistId) { |
|
167
|
|
|
$query->where([ |
|
168
|
|
|
self::tableName() . '.id' => $wishlistId, |
|
169
|
|
|
]); |
|
170
|
|
|
} |
|
171
|
|
|
if ($user_id == 0) { |
|
172
|
|
|
$query->andWhere([ |
|
173
|
|
|
self::tableName() . '.id' => $wishlist_ids, |
|
174
|
|
|
]); |
|
175
|
|
|
} |
|
176
|
|
|
return $query->count(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param $id |
|
181
|
|
|
* @param $user_id |
|
182
|
|
|
* @param array $wishlist_ids |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function setDefaultWishlist($id, $user_id, $wishlist_ids) |
|
186
|
|
|
{ |
|
187
|
|
|
if (null !== $wishlists = Wishlist::getWishlist($user_id, $wishlist_ids)) { |
|
|
|
|
|
|
188
|
|
|
foreach ($wishlists as $wishlist) { |
|
189
|
|
|
/** @var Wishlist $wishlist */ |
|
190
|
|
|
$wishlist->default = false; |
|
191
|
|
|
if ($wishlist->id == $id) { |
|
192
|
|
|
$wishlist->default = true; |
|
193
|
|
|
} |
|
194
|
|
|
$wishlist->save(); |
|
195
|
|
|
} |
|
196
|
|
|
return true; |
|
197
|
|
|
} |
|
198
|
|
|
Yii::error('Failed to set default wishlist'); |
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param string $title |
|
204
|
|
|
* @return bool |
|
205
|
|
|
*/ |
|
206
|
|
|
public function renameWishlist($title) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->title = $title; |
|
209
|
|
|
return $this->save(true, ['title']); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param $itemId |
|
214
|
|
|
* @return bool |
|
215
|
|
|
*/ |
|
216
|
|
|
public function removeItem($itemId) |
|
217
|
|
|
{ |
|
218
|
|
|
/** @var WishlistProduct $item */ |
|
219
|
|
|
|
|
220
|
|
|
$item = WishlistProduct::findOne([ |
|
221
|
|
|
'wishlist_id' => $this->id, |
|
222
|
|
|
'product_id' => $itemId |
|
223
|
|
|
]); |
|
224
|
|
|
return $item->delete(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return bool |
|
229
|
|
|
*/ |
|
230
|
|
|
public function clearWishlist() |
|
231
|
|
|
{ |
|
232
|
|
|
WishlistProduct::deleteAll(['wishlist_id' => $this->id]); |
|
233
|
|
|
return true; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param $user_id |
|
238
|
|
|
* @param array $wishlist_ids |
|
239
|
|
|
* @param $wishlistId |
|
240
|
|
|
* @param array $selections |
|
|
|
|
|
|
241
|
|
|
* @return string |
|
242
|
|
|
*/ |
|
243
|
|
|
public static function getTotalPrice($user_id, $wishlist_ids, $wishlistId = null, $selections = null) |
|
244
|
|
|
{ |
|
245
|
|
|
/** @var WishlistProduct $item */ |
|
246
|
|
|
$total_price = 0; |
|
247
|
|
|
if (null !== $wishlistId) { |
|
248
|
|
|
/** @var Wishlist $wishlist */ |
|
249
|
|
|
$wishlist = static::findOne([ |
|
250
|
|
|
'id' => $wishlistId, |
|
251
|
|
|
'user_id' => $user_id |
|
252
|
|
|
]); |
|
253
|
|
|
if (null !== $selections) { |
|
254
|
|
|
foreach ($wishlist->items as $item) { |
|
255
|
|
|
if (in_array($item->product_id, $selections)) { |
|
256
|
|
|
$total_price += $item->product->convertedPrice(null, false); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
} else { |
|
260
|
|
|
foreach ($wishlist->items as $item) { |
|
261
|
|
|
$total_price += $item->product->convertedPrice(null, false); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
} else { |
|
266
|
|
|
$wishlists = static::getWishlist($user_id, $wishlist_ids); |
|
267
|
|
|
foreach ($wishlists as $wishlist) { |
|
268
|
|
|
foreach ($wishlist->items as $item) { |
|
269
|
|
|
$total_price += $item->product->convertedPrice(null, false); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
return Currency::getMainCurrency()->format($total_price); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.