XEditableAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 3
1
<?php
2
/**
3
 * X-editable extension for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-x-editable
6
 * @package   yii2-x-editable
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
/**
12
 * {@inheritdoc}
13
 */
14
15
namespace hiqdev\xeditable;
16
17
use hiqdev\xeditable\traits\XEditableTrait;
18
use yii\base\Action;
19
use yii\web\NotFoundHttpException;
20
21
/**
22
 * usage:
23
 * in your controller you may write this
24
 * public function actions()
25
 *   {
26
 *       return [
27
 *           'editable' => [
28
 *           'class' => 'hiqdev\xeditable\XEditableAction',
29
 *           //'scenario'=>'editable',  //optional
30
 *           'modelclass' => Model::class,
31
 *           ],
32
 *       ];
33
 *   }.
34
 *
35
 * Class XEditableAction
36
 */
37
class XEditableAction extends Action
38
{
39
    public $modelclass;
40
    public $scenario = '';
41
42
    /**
43
     * @throws NotFoundHttpException
44
     */
45
    public function run()
46
    {
47
        if (\Yii::$app->request->isAjax) {
48
            $pk         = $_POST['pk'];
49
            $name       = $_POST['name'];
50
            $value      = $_POST['value'];
51
            $modelclass = $this->modelclass;
52
            $model      = $modelclass::findOne($pk);
53
            if ($this->scenario) {
54
                $model->setScenario($this->scenario);
55
            }
56
            XEditableTrait::saveAction([
57
                'name'  => $name,
58
                'value' => $value,
59
                'model' => $model,
60
            ]);
61
        }
62
    }
63
}
64