-
Notifications
You must be signed in to change notification settings - Fork 0
/
Panel.php
122 lines (104 loc) · 3.44 KB
/
Panel.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* @author chervand <chervand@gmail.com>
*/
namespace chervand\bootstrap;
use yii\base\InvalidConfigException;
use yii\bootstrap\Widget;
use yii\helpers\ArrayHelper;
/**
* Class \chervand\bootstrap\Panel implements Bootstrap 3 Panel widget.
*
* @package chervand\bootstrap
* @see http://getbootstrap.com/components/#panels
* @see http://www.yiiframework.com/doc-2.0/yii-bootstrap-widget.html
*/
class Panel extends Widget
{
/**
* inheritdoc
*/
public $options = [];
/**
* @var array|string heading section
* @see \chervand\bootstrap\Panel::prepareSection()
*/
public $heading;
/**
* @var array|string body section
* @see \chervand\bootstrap\Panel::prepareSection()
*/
public $body;
/**
* @var array|string footer section
* @see \chervand\bootstrap\Panel::prepareSection()
*/
public $footer;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->clientOptions = false;
if (empty($this->options['class'])) {
Html::addCssClass($this->options, ['panel', 'panel-default']);
} else {
Html::addCssClass($this->options, ['widget' => 'panel']);
}
$this->heading = static::prepareSection('heading', $this->heading);
$this->body = static::prepareSection('body', $this->body);
$this->footer = static::prepareSection('footer', $this->footer);
echo Html::beginTag('div', $this->options);
if (isset($this->heading['content'])) {
echo Html::tag('div', $this->heading['content'], $this->heading['options']);
}
if (isset($this->body['content'])) {
echo Html::tag('div', $this->body['content'], $this->body['options']);
} else {
echo Html::beginTag('div', $this->body['options']);
}
}
/**
* @param string $name section name, e.g. 'heading', 'body' or 'footer'
* @param array|string $section todo: section description
* @return array
* @throws InvalidConfigException
*/
public function prepareSection($name, &$section)
{
if (is_string($section)) {
$section = ['content' => $section];
} elseif (is_callable($section)) {
$section = ['view' => $section];
}
if (is_array($section) && isset($section['view'])) {
if (is_string($section['view'])) {
$params = ArrayHelper::getValue($section, 'params', []);
$section['content'] = $this->getView()
->render($section['view'], $params);
} elseif (is_callable($section['view'])) {
$section['content'] = call_user_func($section['view'], $this);
}
}
if (isset($section['content']) && !is_string($section['content'])) {
throw new InvalidConfigException('Invalid attribute type, string expected.');
}
$section['options'] = ArrayHelper::getValue($section, 'options', []);
Html::addCssClass($section['options'], ['widget' => 'panel-' . $name]);
return $section;
}
/**
* @inheritdoc
*/
public function run()
{
if (!isset($this->body['content'])) {
echo Html::endTag('div');
}
if (isset($this->footer['content'])) {
echo Html::tag('div', $this->footer['content'], $this->footer['options']);
}
echo Html::endTag('div');
}
}