1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Cart module for Yii2 |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/yii2-cart |
7
|
|
|
* @package yii2-cart |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\yii2\cart\actions; |
13
|
|
|
|
14
|
|
|
use hipanel\modules\finance\cart\AbstractCartPosition; |
15
|
|
|
use hiqdev\hiart\Collection; |
16
|
|
|
use hiqdev\yii2\cart\CartPositionInterface; |
17
|
|
|
use hiqdev\yii2\cart\ImmutableQuantityInterface; |
18
|
|
|
use hiqdev\yii2\cart\Module as CartModule; |
19
|
|
|
use Yii; |
20
|
|
|
|
21
|
|
|
class AddToCartAction extends \yii\base\Action |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var CartPositionInterface The class for new product |
25
|
|
|
*/ |
26
|
|
|
public $productClass; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var boolean whether the action expects bulk models load using `selection` |
30
|
|
|
*/ |
31
|
|
|
public $bulkLoad = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool whether client should be redirected to the cart in case of success item adding |
35
|
|
|
*/ |
36
|
|
|
public $redirectToCart = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool whether any errors occurred during save |
40
|
|
|
*/ |
41
|
|
|
protected $hasErrors = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the cart module. |
45
|
|
|
* @return CartModule |
46
|
|
|
*/ |
47
|
|
|
public function getCartModule() |
48
|
|
|
{ |
49
|
|
|
return CartModule::getInstance(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function run() |
53
|
|
|
{ |
54
|
|
|
$data = null; |
|
|
|
|
55
|
|
|
$request = Yii::$app->request; |
56
|
|
|
/** @var CartPositionInterface $model */ |
57
|
|
|
$model = Yii::createObject($this->productClass); |
|
|
|
|
58
|
|
|
$collection = new Collection(); // TODO: drop dependency |
59
|
|
|
$collection->setModel($model); |
60
|
|
|
|
61
|
|
|
if (!$this->bulkLoad) { |
62
|
|
|
$data = [$request->post() ?: $request->get()]; |
63
|
|
|
$collection->load($data); |
64
|
|
|
} else { |
65
|
|
|
$collection->load(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($collection->models as $position) { |
69
|
|
|
/** @var CartPositionInterface|AbstractCartPosition $position */ |
70
|
|
|
if (!$position->validate()) { |
|
|
|
|
71
|
|
|
$this->hasErrors = true; |
72
|
|
|
$error = $collection->getFirstError(); |
73
|
|
|
if (empty($error)) { |
74
|
|
|
$error = Yii::t('cart', 'Failed to add item to the cart'); |
75
|
|
|
} |
76
|
|
|
Yii::$app->session->addFlash('warning', $error); |
77
|
|
|
Yii::warning('Failed to add item to cart', 'cart'); |
78
|
|
|
|
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->putPositionsToCart($collection->models); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function putPositionsToCart($positions) |
87
|
|
|
{ |
88
|
|
|
$cart = $this->getCartModule()->getCart(); |
89
|
|
|
$cart->putPositions($positions); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function afterRun() |
93
|
|
|
{ |
94
|
|
|
$this->ensureBehaviors(); |
95
|
|
|
if ($this->hasEventHandlers('afterAction')) { |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$request = Yii::$app->request; |
100
|
|
|
|
101
|
|
|
if ($request->isAjax) { |
102
|
|
|
Yii::$app->end(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($this->redirectToCart && !$this->hasErrors) { |
106
|
|
|
return $this->controller->redirect('@cart'); |
|
|
|
|
107
|
|
|
} else { |
108
|
|
|
return $this->controller->redirect($request->referrer ?: $this->controller->goHome()); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.