/
BoxTrait.php
102 lines (80 loc) · 2.92 KB
/
BoxTrait.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
<?php
namespace machour\yii2\adminlte\widgets;
use yii\helpers\ArrayHelper;
trait BoxTrait {
public $box = false;
public static $boxConfig = [];
private static $defaultConfig = [
'type' => 'box-solid',
'noPadding' => false,
'header' => [
'title' => '',
'class' => '',
'tools' => '{collapse}{remove}',
'icon' => false,
],
'body' => [
'class' => ''
],
'footer' => '',
];
public static function boxBegin($boxConfig = []) {
self::$boxConfig = ArrayHelper::merge(self::$defaultConfig, $boxConfig);
echo Html::beginTag('div', ['class' => 'box ' . self::$boxConfig['type']]);
if (!empty(self::$boxConfig['header']['title'])) {
$header = self::$boxConfig['header'];
echo Html::beginTag('div', ['class' => 'box-header ' . $header['class']]);
if ($header['icon']) {
echo $header['icon'];
}
if (!empty($header['title'])) {
echo Html::tag('h3', Html::encode($header['title']), ['class' => 'box-title']);
}
if (trim($header['tools'])) {
echo Html::beginTag('div', ['class' => 'box-tools pull-right']);
foreach (['collapse' => 'minus', 'remove' => 'times'] as $tool => $icon) {
$header['tools'] = str_replace('{' . $tool . '}', self::boxTool($tool, $icon), $header['tools']);
}
echo $header['tools'];
echo Html::endTag('div');
}
echo Html::endTag('div');
}
$class = 'box-body ';
$class .= self::$boxConfig['body']['class'];
if (self::$boxConfig['noPadding']) {
$class .= ' no-padding';
}
echo Html::beginTag('div', ['class' => $class]);
}
static $footerUsed = false;
public static function footer() {
self::$footerUsed = true;
echo Html::endTag('div');
echo Html::beginTag('div', ['class' => 'box-footer']);
if (!empty(self::$boxConfig['footer'])) {
echo self::$boxConfig['footer'];
echo Html::endTag('div');
}
}
public static function boxEnd() {
if (!self::$footerUsed) {
echo Html::endTag('div');
if (!empty(self::$boxConfig['footer'])) {
echo Html::beginTag('div', ['class' => 'box-footer']);
echo self::$boxConfig['footer'];
echo Html::endTag('div');
}
} elseif (empty(self::$boxConfig['footer'])) {
echo Html::endTag('div');
}
echo Html::endTag('div');
}
private static function boxTool($widget, $icon) {
return Html::tag(
'a',
Html::tag('i', null, ['class' => 'fa fa-' . $icon]),
['data-widget' => $widget, 'class' => 'btn btn-box-tool']
);
}
}