|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\backend\actions; |
|
4
|
|
|
|
|
5
|
|
|
use app; |
|
6
|
|
|
use yii; |
|
7
|
|
|
use yii\base\Action; |
|
8
|
|
|
use yii\base\InvalidConfigException; |
|
9
|
|
|
use yii\helpers\Json; |
|
10
|
|
|
/** |
|
11
|
|
|
* Universal action for editable updates |
|
12
|
|
|
* |
|
13
|
|
|
* How to use: |
|
14
|
|
|
* |
|
15
|
|
|
* Add to your controller's action: |
|
16
|
|
|
* |
|
17
|
|
|
* ``` |
|
18
|
|
|
* |
|
19
|
|
|
* 'update-editable' => [ |
|
20
|
|
|
* 'class' => UpdateEditable::className(), |
|
21
|
|
|
* 'modelName' => Product::className(), |
|
22
|
|
|
* 'allowedAttributes' => [ |
|
23
|
|
|
* 'currency_id' => function(Product $model, $attribute) { |
|
24
|
|
|
* if ($model === null || $model->currency === null || $model->currency_id ===0) { |
|
25
|
|
|
* return null; |
|
26
|
|
|
* } |
|
27
|
|
|
* return \yii\helpers\Html::tag('div', $model->currency->name, ['class' => $model->currency->name]); |
|
28
|
|
|
* }, |
|
29
|
|
|
* 'price', |
|
30
|
|
|
* 'old_price', |
|
31
|
|
|
* ], |
|
32
|
|
|
* ], |
|
33
|
|
|
* |
|
34
|
|
|
* |
|
35
|
|
|
* ``` |
|
36
|
|
|
* |
|
37
|
|
|
* Allowed attributes is the array of attribute name as key and callable as value. |
|
38
|
|
|
* Callable is the function that returns the result of editable change. |
|
39
|
|
|
* |
|
40
|
|
|
* @package app\backend\actions |
|
41
|
|
|
*/ |
|
42
|
|
|
class UpdateEditable extends Action |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var string Model name, ie. `Product::className()` |
|
46
|
|
|
*/ |
|
47
|
|
|
public $modelName = null; |
|
48
|
|
|
|
|
49
|
|
|
public $allowedAttributes = []; |
|
50
|
|
|
|
|
51
|
|
|
public function init() |
|
52
|
|
|
{ |
|
53
|
|
|
if (!isset($this->modelName)) { |
|
54
|
|
|
throw new InvalidConfigException("Model name should be set in controller actions"); |
|
55
|
|
|
} |
|
56
|
|
|
if (!class_exists($this->modelName)) { |
|
57
|
|
|
throw new InvalidConfigException("Model class does not exists"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$newAllowedAttributes = []; |
|
61
|
|
|
foreach ($this->allowedAttributes as $key => $value) { |
|
62
|
|
|
if (is_callable($value) === true) { |
|
63
|
|
|
$newAllowedAttributes[$key] = $value; |
|
64
|
|
|
|
|
65
|
|
|
} else { |
|
66
|
|
|
$newAllowedAttributes[$value] = |
|
67
|
|
|
function(yii\db\ActiveRecord $model, $attribute) { |
|
68
|
|
|
return $model->getAttribute($attribute); |
|
69
|
|
|
}; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
$this->allowedAttributes = $newAllowedAttributes; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function run() |
|
79
|
|
|
{ |
|
80
|
|
|
/** @var \yii\db\ActiveRecord $modelName fake type for PHPStorm (: */ |
|
81
|
|
|
$modelName = $this->modelName; |
|
82
|
|
|
|
|
83
|
|
|
if (Yii::$app->request->post('hasEditable')) { |
|
84
|
|
|
$modelId = Yii::$app->request->post('editableKey'); |
|
85
|
|
|
$model = $modelName::findOne($modelId); |
|
86
|
|
|
|
|
87
|
|
|
if ($model === null) { |
|
88
|
|
|
throw new yii\web\NotFoundHttpException; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$formName = $model->formName(); |
|
92
|
|
|
|
|
93
|
|
|
$out = Json::encode(['output'=>'', 'message'=>'']); |
|
94
|
|
|
|
|
95
|
|
|
$post = []; |
|
96
|
|
|
$posted = current($_POST[$formName]); |
|
97
|
|
|
$post[$formName] = $posted; |
|
98
|
|
|
|
|
99
|
|
|
// load model like any single model validation |
|
100
|
|
|
if ($model->load($post)) { |
|
101
|
|
|
// can save model or do something before saving model |
|
102
|
|
|
$model->save(); |
|
103
|
|
|
if ($model->hasMethod('invalidateTags')) { |
|
104
|
|
|
$model->invalidateTags(); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$output = ''; |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
foreach ($this->allowedAttributes as $attribute=>$callable) { |
|
111
|
|
|
if (isset($posted[$attribute])) { |
|
112
|
|
|
$output = call_user_func($callable, $model, $attribute); |
|
113
|
|
|
|
|
114
|
|
|
break; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$out = Json::encode(['output'=>$output, 'message'=>'']); |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
echo $out; |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.