Box::init()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 11
cp 0
rs 9.7333
cc 4
nc 8
nop 0
crap 20
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets;
12
13
use yii\base\Widget;
14
use yii\helpers\Html;
15
16
class Box extends Widget
17
{
18
    /**
19
     * @var string|null Widget title
20
     */
21
    public $title = null;
22
23
    public $collapsed = false;
24
25
    public $collapsable = false;
26
27
    /**
28
     * Small helper for title.
29
     * @var null
30
     */
31
    public $small = null;
32
33
    /**
34
     * @var array the HTML attributes for the widget container tag
35
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
36
     */
37
    public $options = [];
38
39
    /**
40
     * @var array Body box widget options
41
     */
42
    public $bodyOptions = [];
43
44
    /**
45
     * @var array Body box widget options
46
     */
47
    public $footerOptions = [];
48
49
    /**
50
     * @var array Header box widget options
51
     */
52
    public $headerOptions = [];
53
54
    /**
55
     * @var boolean Whether to render the box body immediately.
56
     * Should be switched off if you prefer to call [[beginBody]] and [[endBody]] manually
57
     * @see beginBody()
58
     * @see endBody()
59
     */
60
    public $renderBody = true;
61
62
    /**
63
     * @var string content to be placed between opening and closing tags
64
     */
65
    public $body = null;
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function init()
71
    {
72
        if ($this->collapsed) {
73
            $this->collapsable = true;
74
        }
75
        $this->initOptions();
76
        echo Html::beginTag('div', $this->options) . "\n";
77
        // Begin box
78
        if ($this->title !== null) {
79
            $this->beginHeader();
80
            $this->endHeader();
81
        }
82
        if ($this->renderBody) {
83
            $this->beginBody();
84
        }
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function run()
91
    {
92
        $this->getView()->registerCss('
93
        .box-title-helper {
94
            padding: 0;
95
            margin: 0;
96
            line-height: 13px;
97
            color: #9eacb4;
98
            font-size: 13px;
99
            font-weight: 400;
100
        }
101
        ');
102
        if ($this->body !== null) {
103
            echo $this->body;
104
        }
105
        if ($this->renderBody) {
106
            $this->endBody();
107
        }
108
        echo "\n" . Html::endTag('div'); // End box
109
    }
110
111
    /**
112
     * @param $title
113
     * @param null $small
114
     * @return string
115
     */
116
    public function renderTitle($title, $small = null)
117
    {
118
        $resultTitle = Html::tag('h3', $title, ['class' => 'box-title']);
119
        $resultSmall = ($small === null) ? '' : Html::tag('span', $small, ['class' => 'box-title-helper']);
120
121
        return sprintf('%s %s', $resultTitle, $resultSmall);
122
    }
123
124
    /**
125
     * Start header section, render title if not exist.
126
     */
127
    public function beginHeader()
128
    {
129
        echo Html::beginTag('div', $this->headerOptions) . "\n";
130
        if ($this->title !== null) {
131
            echo Html::tag('h3', $this->title, ['class' => 'box-title']);
132
        }
133
        if ($this->collapsable) {
134
            echo '<div class="box-tools pull-right">
135
                <button class="btn btn-box-tool" data-widget="collapse">
136
                    <i class="fa fa-' . ($this->collapsed ? 'plus' : 'minus') . '"></i>
137
                </button>
138
            </div>';
139
        }
140
    }
141
142
    /**
143
     * End of header section.
144
     */
145
    public static function endHeader()
146
    {
147
        echo "\n" . Html::endTag('div');
148
    }
149
150
    /**
151
     * Begin box body container.
152
     */
153
    public function beginBody()
154
    {
155
        echo Html::beginTag('div', $this->bodyOptions) . "\n";
156
    }
157
158
    /**
159
     * End box body container.
160
     */
161
    public function endBody()
162
    {
163
        echo "\n" . Html::endTag('div');
164
    }
165
166
    /**
167
     * Begin box footer container.
168
     */
169
    public function beginFooter()
170
    {
171
        echo Html::beginTag('div', $this->footerOptions) . "\n";
172
    }
173
174
    /**
175
     * End box footer container.
176
     */
177
    public function endFooter()
178
    {
179
        echo "\n" . Html::endTag('div');
180
    }
181
182
    /**
183
     * Initializes the widget options.
184
     * This method sets the default values for various options.
185
     */
186
    protected function initOptions()
187
    {
188
        $this->options['class'] = isset($this->options['class']) ? 'box ' . $this->options['class'] : 'box';
189
        $this->headerOptions['class'] = isset($this->headerOptions['class']) ? 'box-header with-border ' . $this->headerOptions['class'] : 'box-header with-border';
190
        $this->bodyOptions['class'] = isset($this->bodyOptions['class']) ? 'box-body ' . $this->bodyOptions['class'] : 'box-body';
191
        $this->footerOptions['class'] = isset($this->footerOptions['class']) ? 'box-footer ' . $this->footerOptions['class'] : 'box-footer';
192
        if ($this->collapsed) {
193
            $this->options['class'] .= ' collapsed-box';
194
        }
195
    }
196
197
    /**
198
     * Start box-container
199
     * Only in header container!
200
     * Examples:
201
     * <button class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="" data-original-title="Collapse"><i class="fa fa-minus"></i></button>
202
     * <button class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="" data-original-title="Remove"><i class="fa fa-times"></i></button>.
203
     * @param array $options
204
     */
205
    public function beginTools(array $options = [])
206
    {
207
        echo "\n" . Html::beginTag('div', array_merge(['class' => 'box-tools pull-right btn-group'], $options));
208
    }
209
210
    /**
211
     * End box-tools container.
212
     */
213
    public function endTools()
214
    {
215
        echo "\n" . Html::endTag('div');
216
    }
217
}
218