forked from 2amigos/yii2-editable-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditableAction.php
97 lines (91 loc) · 3.02 KB
/
EditableAction.php
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
<?php
/**
* @copyright Copyright (c) 2013 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\editable;
use Yii;
use yii\base\Action;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;
/**
* EditableAction is a server side Action that helps to update the record in db.
*
* @author Antonio Ramirez <[email protected]>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\editable
*/
class EditableAction extends Action
{
/**
* @var string the class name to handle
*/
public $modelClass;
/**
* @var string the scenario to be used (optional)
*/
public $scenario;
/**
* @var \Closure a function to be called previous saving model. The anonymous function is preferable to have the
* model passed by reference. This is useful when we need to set model with extra data previous update.
*/
public $preProcess;
/**
* @var bool whether to create a model if a primary key parameter was not found.
*/
public $forceCreate = true;
/**
* @inheritdoc
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
if ($this->modelClass === null) {
throw new InvalidConfigException("'modelClass' cannot be empty.");
}
}
/**
* Runs the action
* @return bool
* @throws BadRequestHttpException
*/
public function run()
{
$class = $this->modelClass;
$pk = Yii::$app->request->post('pk');
$pk = unserialize(base64_decode($pk));
$attribute = Yii::$app->request->post('name');
$value = Yii::$app->request->post('value');
if ($attribute === null) {
throw new BadRequestHttpException("'name' parameter cannot be empty.");
}
if ($value === null) {
throw new BadRequestHttpException("'value' parameter cannot be empty.");
}
/** @var \Yii\db\ActiveRecord $model */
$model = $class::findOne($pk);
if (!$model) {
if ($this->forceCreate) { // only useful for models with one editable attribute or no validations
$model = new $class;
} else {
throw new BadRequestHttpException('Entity not found by primary key ' . $pk);
}
}
// do we have a preProcess function
if ($this->preProcess && is_callable($this->preProcess, true)) {
call_user_func($this->preProcess, $model);
}
if ($this->scenario !== null) {
$model->setScenario($this->scenario);
}
$model->$attribute = $value;
if ($model->validate([$attribute])) {
// no need to specify which attributes as Yii2 handles that via [[BaseActiveRecord::getDirtyAttributes]]
return $model->save(false);
} else {
throw new BadRequestHttpException($model->getFirstError($attribute));
}
}
}