d8c1a2e0
Yarik
Big commit artbox
|
1
|
<?php
|
d55d2fe0
Yarik
Multilanguage
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
namespace common\modules\product\controllers;
use common\modules\product\models\Product;
use common\modules\product\models\ProductImage;
use common\modules\product\models\ProductStock;
use common\modules\product\models\ProductVariant;
use common\modules\product\models\ProductVariantListSearch;
use common\modules\product\models\Stock;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
|
d8c1a2e0
Yarik
Big commit artbox
|
18
|
/**
|
d55d2fe0
Yarik
Multilanguage
|
19
|
* ManageController implements the CRUD actions for ProductVariant model.
|
d8c1a2e0
Yarik
Big commit artbox
|
20
|
*/
|
d55d2fe0
Yarik
Multilanguage
|
21
|
class VariantController extends Controller
|
d8c1a2e0
Yarik
Big commit artbox
|
22
|
{
|
d55d2fe0
Yarik
Multilanguage
|
23
24
25
26
27
28
29
30
31
32
33
34
|
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => [ 'POST' ],
],
|
d8c1a2e0
Yarik
Big commit artbox
|
35
|
],
|
d55d2fe0
Yarik
Multilanguage
|
36
|
];
|
d8c1a2e0
Yarik
Big commit artbox
|
37
|
}
|
36d1807a
Yarik
Big commit.
|
38
|
|
d55d2fe0
Yarik
Multilanguage
|
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/**
* Lists all ProductVariant models.
* @return mixed
*/
public function actionIndex($product_id)
{
$searchModel = new ProductVariantListSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $product_id);
if(( $product = Yii::$app->request->get('product_id') ) !== NULL) {
$product = Product::findOne($product);
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'product' => $product,
'product_id' => $product_id,
]);
}
|
36d1807a
Yarik
Big commit.
|
59
|
|
d55d2fe0
Yarik
Multilanguage
|
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/**
* Displays a single ProductVariant model.
*
* @param integer $id
*
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
|
36d1807a
Yarik
Big commit.
|
73
|
|
d55d2fe0
Yarik
Multilanguage
|
74
75
76
77
78
79
80
81
82
|
/**
* Creates a new ProductVariant model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate($product_id)
{
$model = new ProductVariant();
$model->product_id = $product_id;
|
72a992f5
Yarik
Import browser v1.0
|
83
|
$model->generateLangs();
|
d55d2fe0
Yarik
Multilanguage
|
84
|
if($model->load(Yii::$app->request->post())) {
|
72a992f5
Yarik
Import browser v1.0
|
85
|
$model->loadLangs(\Yii::$app->request);
|
d55d2fe0
Yarik
Multilanguage
|
86
|
$model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload');
|
72a992f5
Yarik
Import browser v1.0
|
87
|
if($model->save() && $model->transactionStatus) {
|
d55d2fe0
Yarik
Multilanguage
|
88
89
90
91
|
if(( $image = UploadedFile::getInstance($model, 'image') )) {
$imageModel = ProductImage::find()
->where([ 'product_variant_id' => $model->product_variant_id ])
->one();
|
d55d2fe0
Yarik
Multilanguage
|
92
93
94
95
96
97
98
99
100
101
102
103
|
if($imageModel instanceof ProductImage) {
$imageModel->product_id = $model->product_id;
$imageModel->product_variant_id = $model->product_variant_id;
$imageModel->image = $image->name;
$imageModel->save();
} else {
$imageModel = new ProductImage();
$imageModel->product_id = $model->product_id;
$imageModel->product_variant_id = $model->product_variant_id;
$imageModel->image = $image->name;
$imageModel->save();
}
|
d55d2fe0
Yarik
Multilanguage
|
104
|
$imgDir = Yii::getAlias('@storage/products/');
|
d55d2fe0
Yarik
Multilanguage
|
105
106
107
|
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
|
d55d2fe0
Yarik
Multilanguage
|
108
|
$image->saveAs(Yii::getAlias('@storage/products/' . $image->name));
|
36d1807a
Yarik
Big commit.
|
109
|
}
|
d55d2fe0
Yarik
Multilanguage
|
110
|
$ProductStocks = Yii::$app->request->post('ProductStock');
|
d55d2fe0
Yarik
Multilanguage
|
111
|
$total_quantity = 0;
|
d55d2fe0
Yarik
Multilanguage
|
112
113
114
115
116
117
118
119
120
121
|
if(!empty( $ProductStocks ) && is_array($ProductStocks)) {
$model->unlinkAll('stocks', true);
$sorted_array = [];
foreach($ProductStocks as $subArray) {
if(!empty( $subArray[ 'name' ] ) && !empty( $subArray[ 'quantity' ] )) {
if(!empty( $sorted_array[ $subArray[ 'name' ] ] )) {
$sorted_array[ $subArray[ 'name' ] ] += $subArray[ 'quantity' ];
} else {
$sorted_array[ $subArray[ 'name' ] ] = $subArray[ 'quantity' ];
}
|
36d1807a
Yarik
Big commit.
|
122
123
|
}
}
|
d55d2fe0
Yarik
Multilanguage
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
$ProductStocks = $sorted_array;
$stock_names = array_keys($ProductStocks);
$stocks = Stock::find()
->where([ 'name' => $stock_names ])
->indexBy('name')
->all();
foreach($ProductStocks as $name => $quantity) {
$quantity = (int) $quantity;
if(!array_key_exists($name, $stocks)) {
$stock = new Stock([
'name' => $name,
]);
if(!$stock->save()) {
continue;
}
} else {
$stock = $stocks[ $name ];
}
$psModel = new ProductStock([
'product_id' => $model->product_id,
'product_variant_id' => $model->product_variant_id,
'stock_id' => $stock->stock_id,
'quantity' => $quantity,
|
36d1807a
Yarik
Big commit.
|
147
|
]);
|
d55d2fe0
Yarik
Multilanguage
|
148
149
|
if($psModel->save()) {
$total_quantity += $quantity;
|
36d1807a
Yarik
Big commit.
|
150
|
}
|
36d1807a
Yarik
Big commit.
|
151
|
}
|
d55d2fe0
Yarik
Multilanguage
|
152
153
154
155
156
|
} else {
$model->unlinkAll('stocks', true);
}
$model->stock = $total_quantity;
|
72a992f5
Yarik
Import browser v1.0
|
157
|
if($model->save() && $model->transactionStatus) {
|
d55d2fe0
Yarik
Multilanguage
|
158
159
160
161
|
return $this->redirect([
'index',
'product_id' => $product_id,
]);
|
36d1807a
Yarik
Big commit.
|
162
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
163
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
164
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
165
|
$groups = $model->getTaxGroupsByLevel(1);
|
d8c1a2e0
Yarik
Big commit artbox
|
166
|
return $this->render('create', [
|
d55d2fe0
Yarik
Multilanguage
|
167
|
'model' => $model,
|
72a992f5
Yarik
Import browser v1.0
|
168
|
'model_langs' => $model->model_langs,
|
d55d2fe0
Yarik
Multilanguage
|
169
170
|
'groups' => $groups,
'stocks' => [ new ProductStock() ],
|
d8c1a2e0
Yarik
Big commit artbox
|
171
172
|
]);
}
|
d55d2fe0
Yarik
Multilanguage
|
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/**
* Updates an existing ProductVariant model.
* If update is successful, the browser will be redirected to the 'view' page.
*
* @param integer $product_id
* @param integer $id
*
* @return mixed
*/
public function actionUpdate($product_id, $id)
{
$model = $this->findModel($id);
|
72a992f5
Yarik
Import browser v1.0
|
186
|
$model->generateLangs();
|
d55d2fe0
Yarik
Multilanguage
|
187
|
if($model->load(Yii::$app->request->post())) {
|
72a992f5
Yarik
Import browser v1.0
|
188
189
|
$model->loadLangs(\Yii::$app->request);
if($model->save() && $model->transactionStatus) {
|
d55d2fe0
Yarik
Multilanguage
|
190
191
192
193
|
if(( $image = UploadedFile::getInstance($model, 'image') )) {
$imageModel = ProductImage::find()
->where([ 'product_variant_id' => $model->product_variant_id ])
->one();
|
d55d2fe0
Yarik
Multilanguage
|
194
195
196
197
198
199
200
201
202
203
204
205
|
if($imageModel instanceof ProductImage) {
$imageModel->product_id = $model->product_id;
$imageModel->product_variant_id = $model->product_variant_id;
$imageModel->image = $image->name;
$imageModel->save();
} else {
$imageModel = new ProductImage();
$imageModel->product_id = $model->product_id;
$imageModel->product_variant_id = $model->product_variant_id;
$imageModel->image = $image->name;
$imageModel->save();
}
|
d55d2fe0
Yarik
Multilanguage
|
206
|
$imgDir = Yii::getAlias('@storage/products/');
|
d55d2fe0
Yarik
Multilanguage
|
207
208
209
|
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
|
d55d2fe0
Yarik
Multilanguage
|
210
|
$image->saveAs(Yii::getAlias('@storage/products/' . $image->name));
|
d8c1a2e0
Yarik
Big commit artbox
|
211
|
}
|
d55d2fe0
Yarik
Multilanguage
|
212
|
$ProductStocks = Yii::$app->request->post('ProductStock');
|
d55d2fe0
Yarik
Multilanguage
|
213
|
$total_quantity = 0;
|
d55d2fe0
Yarik
Multilanguage
|
214
215
216
217
218
219
220
221
222
223
|
if(!empty( $ProductStocks ) && is_array($ProductStocks)) {
$model->unlinkAll('stocks', true);
$sorted_array = [];
foreach($ProductStocks as $subArray) {
if(!empty( $subArray[ 'name' ] ) && !empty( $subArray[ 'quantity' ] )) {
if(!empty( $sorted_array[ $subArray[ 'name' ] ] )) {
$sorted_array[ $subArray[ 'name' ] ] += $subArray[ 'quantity' ];
} else {
$sorted_array[ $subArray[ 'name' ] ] = $subArray[ 'quantity' ];
}
|
36d1807a
Yarik
Big commit.
|
224
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
225
|
}
|
d55d2fe0
Yarik
Multilanguage
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
$ProductStocks = $sorted_array;
$stock_names = array_keys($ProductStocks);
$stocks = Stock::find()
->where([ 'name' => $stock_names ])
->indexBy('name')
->all();
foreach($ProductStocks as $name => $quantity) {
$quantity = (int) $quantity;
if(!array_key_exists($name, $stocks)) {
$stock = new Stock([
'name' => $name,
]);
if(!$stock->save()) {
continue;
}
} else {
$stock = $stocks[ $name ];
}
$psModel = new ProductStock([
'product_id' => $model->product_id,
'product_variant_id' => $model->product_variant_id,
'stock_id' => $stock->stock_id,
'quantity' => $quantity,
|
36d1807a
Yarik
Big commit.
|
249
|
]);
|
d55d2fe0
Yarik
Multilanguage
|
250
251
|
if($psModel->save()) {
$total_quantity += $quantity;
|
36d1807a
Yarik
Big commit.
|
252
|
}
|
36d1807a
Yarik
Big commit.
|
253
|
}
|
d55d2fe0
Yarik
Multilanguage
|
254
255
256
|
} else {
$model->unlinkAll('stocks', true);
}
|
d55d2fe0
Yarik
Multilanguage
|
257
|
$model->stock = $total_quantity;
|
72a992f5
Yarik
Import browser v1.0
|
258
|
if($model->save() && $model->transactionStatus) {
|
d55d2fe0
Yarik
Multilanguage
|
259
260
261
|
return $this->redirect([
'index',
'product_id' => $product_id,
|
36d1807a
Yarik
Big commit.
|
262
|
]);
|
d8c1a2e0
Yarik
Big commit artbox
|
263
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
264
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
265
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
266
|
$groups = $model->getTaxGroupsByLevel(1);
|
d8c1a2e0
Yarik
Big commit artbox
|
267
|
return $this->render('update', [
|
d55d2fe0
Yarik
Multilanguage
|
268
|
'model' => $model,
|
72a992f5
Yarik
Import browser v1.0
|
269
|
'model_langs' => $model->model_langs,
|
d55d2fe0
Yarik
Multilanguage
|
270
271
|
'groups' => $groups,
'stocks' => ( !empty( $model->variantStocks ) ) ? $model->variantStocks : [ new ProductStock ],
|
d8c1a2e0
Yarik
Big commit artbox
|
272
|
]);
|
d8c1a2e0
Yarik
Big commit artbox
|
273
|
}
|
d55d2fe0
Yarik
Multilanguage
|
274
275
276
277
278
|
/**
* Deletes an existing ProductVariant model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
|
72a992f5
Yarik
Import browser v1.0
|
279
|
* @param integer $product_id
|
d55d2fe0
Yarik
Multilanguage
|
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
* @param integer $id
*
* @return mixed
*/
public function actionDelete($product_id, $id)
{
$this->findModel($id)
->delete();
return $this->redirect([
'index',
'product_id' => $product_id,
]);
|
d8c1a2e0
Yarik
Big commit artbox
|
294
|
}
|
d55d2fe0
Yarik
Multilanguage
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
public function actionDelimg($id)
{
$image = ProductImage::findOne($id);
if($image) {
$image->delete();
}
print '1';
exit;
}
/**
* Finds the ProductVariant model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id
*
* @return ProductVariant the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if(( $model = ProductVariant::findOne($id) ) !== NULL) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
|
d8c1a2e0
Yarik
Big commit artbox
|
324
325
|
}
}
|