Completed
Push — master ( 89011d...ce8f51 )
by Klochok
03:44
created

UiOptionsBehavior::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hipanel\behaviors;
4
5
use hipanel\models\IndexPageUiOptions;
6
use Yii;
7
use yii\base\Behavior;
8
use yii\helpers\Inflector;
9
use yii\web\Controller;
10
11
class UiOptionsBehavior extends Behavior
12
{
13
    /**
14
     * @var mixed
15
     */
16
    public $modelClass;
17
18
    /**
19
     * @var IndexPageUiOptions
20
     */
21
    private $_model;
22
23
    public function events()
24
    {
25
        return [Controller::EVENT_BEFORE_ACTION => 'ensureUiOptions'];
26
    }
27
28
    public function ensureUiOptions($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
        if ($this->isRouteAllowed()) {
31
            $options = [];
32
            $params = Yii::$app->request->get();
33
            $model = $this->getModel();
34
            $model->attributes = $this->getUiOptionsStorage()->get($this->getRoute());
35
            $model->availableRepresentations = $this->findRepresentations();
36
            if ($params) {
37
                foreach ($params as $key => $value) {
38
                    if (in_array($key, array_keys($model->toArray()))) {
39
                        $options[$key] = $value;
40
                    }
41
                }
42
                $model->attributes = $options;
43
                if ($model->validate()) {
44
                    $this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray());
45
                } else {
46
                    $errors = json_encode($model->getErrors());
47
                    Yii::warning('UiOptionsBehavior - IndexPageUiModel validation errors: ' . $errors);
48
                }
49
            }
50
        }
51
    }
52
53
    protected function getModel()
54
    {
55
        if ($this->_model === null) {
56
            $this->_model = $this->findModel();
57
        }
58
59
        return $this->_model;
60
    }
61
62
    protected function findModel()
63
    {
64
        return $this->owner->indexPageUiOptionsModel;
65
    }
66
67
    protected function isRouteAllowed()
68
    {
69
        return $this->owner->action->id === 'index';
70
    }
71
72
    protected function getUiOptionsStorage()
73
    {
74
        return Yii::$app->get('uiOptionsStorage');
75
    }
76
77
    protected function getRoute()
78
    {
79
        return Yii::$app->request->pathInfo;
80
    }
81
82
    protected function findRepresentations()
83
    {
84
        $out = [];
85
        $module = $this->owner->module->id;
86
        $owner = Inflector::id2camel($this->owner->id);
87
        $gridClass =  sprintf('\hipanel\modules\%s\grid\%sGridView', $module, $owner);
88
        if (class_exists($gridClass)) {
89
            $out = array_keys($gridClass::getRepresentations());
90
        }
91
92
        return $out;
93
    }
94
}
95