Layout   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 5 1
A afterValidate() 0 7 2
A getOptions() 0 8 2
A getRegions() 0 4 1
A regionPanels() 0 17 3
A render() 0 5 1
1
<?php
2
3
namespace cornernote\dashboard;
4
5
use cornernote\dashboard\models\Dashboard;
6
use cornernote\dashboard\models\DashboardPanel;
7
use Yii;
8
use yii\base\Model;
9
use yii\bootstrap\ActiveForm;
10
11
/**
12
 * Panel
13
 * @package cornernote\dashboard
14
 */
15
class Layout extends Model
16
{
17
    /**
18
     * @var string
19
     */
20
    public $id;
21
22
    /**
23
     * @var string
24
     */
25
    public $viewPath;
26
27
    /**
28
     * @var Dashboard
29
     */
30
    public $dashboard;
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function load($data, $formName = null)
36
    {
37
        $this->dashboard->load($data);
38
        return parent::load($data, $formName);
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function afterValidate()
45
    {
46
        if (!$this->dashboard->validate()) {
47
            $this->addError(null);
48
        }
49
        parent::afterValidate();
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getOptions()
56
    {
57
        $attributes = [];
58
        foreach ($this->safeAttributes() as $attribute) {
59
            $attributes[$attribute] = $this->$attribute;
60
        }
61
        return $attributes;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getRegions()
68
    {
69
        return [];
70
    }
71
72
    /**
73
     * @param DashboardPanel[] $dashboardPanels
74
     * @param string $view
75
     * @return array
76
     */
77
    public function regionPanels($dashboardPanels, $view = 'view')
78
    {
79
        $regionPanels = [];
80
        foreach (array_keys($this->getRegions()) as $region) {
81
            $regionPanels[$region] = [];
82
        }
83
        foreach ($dashboardPanels as $dashboardPanel) {
84
            $regionPanels[$dashboardPanel->region][] = [
85
                'options' => [
86
                    'id' => 'dashboard-panel-' . $dashboardPanel->id,
87
                    'class' => 'dashboard-panel',
88
                ],
89
                'content' => $dashboardPanel->panel->render($view),
90
            ];
91
        }
92
        return $regionPanels;
93
    }
94
95
    /**
96
     * @param string $view
97
     * @param array $params
98
     * @return string
99
     */
100
    public function render($view, $params = [])
101
    {
102
        $params['layout'] = $this;
103
        return \Yii::$app->view->render($this->viewPath . '/' . $view, $params);
104
    }
105
106
}
107