ToolbarBehavior::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-grid-view-library project.
5
 * (c) 2amigOS! <http://2amigos.us/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace dosamigos\grid\behaviors;
11
12
use yii\base\Behavior;
13
use yii\bootstrap\ButtonGroup;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
17
class ToolbarBehavior extends Behavior
18
{
19
    /**
20
     * @var array $buttons the buttons that would be rendered within the toolbar. The buttons configuration are exactly the
21
     *            same as http://www.yiiframework.com/doc-2.0/yii-bootstrap-buttongroup.html to display them with one difference,
22
     *            multiple button groups can be displayed by providing a separator element:
23
     *
24
     * ```
25
     * 'buttons' => [
26
     *      ['label' => 'A'],
27
     *      ['label' => 'B', 'visible' => false],
28
     *      '-', // divider
29
     *      ['label' => 'C'], // this will belong to a different group
30
     * ]
31
     * ```
32
     * @see http://www.yiiframework.com/doc-2.0/yii-bootstrap-buttongroup.html#$buttons-detail
33
     */
34
    public $buttons = [];
35
    /**
36
     * @var boolean whether to HTML-encode the button labels of the button groups.
37
     */
38
    public $encodeLabels = true;
39
    /**
40
     * @var array $buttonGroupOptions the options to pass to the button groups. Currently are global. For example:
41
     *
42
     * ```
43
     * 'buttonGroupOptions' => ['class' => 'btn-group-lg']
44
     * ```
45
     */
46
    public $buttonGroupOptions = [];
47
    /**
48
     * @var array the options for the toolbar tag.
49
     */
50
    public $options = [];
51
    /**
52
     * @var array the options for the toolbar container.
53
     */
54
    public $containerOptions = [];
55
    /**
56
     * @var string toolbar alignment, defaults to right alignment.
57
     */
58
    public $alignRight = true;
59
    /**
60
     * @var array contains the grouped buttons
61
     */
62
    protected $groups = [];
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function init()
68
    {
69
        $this->initOptions();
70
        $this->initButtonGroups();
71
    }
72
73
    /**
74
     * Renders the toolbar.
75
     *
76
     * @return string
77
     */
78
    public function renderToolbar()
79
    {
80
        if (empty($this->groups)) {
81
            return '';
82
        }
83
        $content = [];
84
        foreach ($this->groups as $buttons) {
85
            $content[] = ButtonGroup::widget(
86
                ['buttons' => $buttons, 'encodeLabels' => $this->encodeLabels, 'options' => $this->buttonGroupOptions]
87
            );
88
        }
89
        $toolbar = Html::tag('div', implode("\n", $content), $this->options);
90
91
        $container = Html::tag('div', $toolbar, $this->containerOptions);
92
93
        if ($this->alignRight) {
94
            $container .= '<div class="clearfix" style="margin-top:5px"></div>';
95
        }
96
97
        return $container;
98
    }
99
100
    /**
101
     * Initializes toolbar options
102
     */
103
    protected function initOptions()
104
    {
105
        $this->options = ArrayHelper::merge($this->options, ['class' => 'btn-toolbar', 'role' => 'toolbar']);
106
        if ($this->alignRight === true) {
107
            Html::addCssClass($this->containerOptions, 'pull-right');
108
        }
109
    }
110
111
    /**
112
     * Parses the buttons to check for possible button group separations.
113
     */
114
    protected function initButtonGroups()
115
    {
116
        $group = [];
117
        foreach ($this->buttons as $button) {
118
            if (is_string($button) && $button == '-') {
119
                $this->groups[] = $group;
120
                $group = [];
121
                continue;
122
            }
123
            $group[] = $button;
124
        }
125
        if (!empty($group)) {
126
            $this->groups[] = $group;
127
        }
128
    }
129
}
130