Completed
Branch master (99b5bb)
by WILMER
02:19
created

MainSidebar   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 67
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C renderItem() 0 32 15
1
<?php
2
3
/*
4
 * This file is part of the Cjt Terabyte LLC [yii2-extension].
5
 *
6
 * (c) Cjt Terabyte LLC [yii2-extension] <http://github.com/cjtterabytesoft>.
7
 * For the full copyright and license information, please view the LICENSE.md.
8
 * file that was distributed with this source code.
9
 *
10
 * @link http://www.cjtterabyte.com.
11
 * @author Original Agiel K. Saputra
12
 * @adaptation Wilmer Arámbula <[email protected]>.
13
 * @copyright (c) 2015 Cjt Terabyte LLC.
14
 * @Extension: [yii2-adminlte-basic].
15
 * @Widget [MainSidebar].
16
 * @since 1.0
17
 */
18
19
namespace cjtterabytesoft\adminlte\basic\widgets;
20
21
use Yii;
22
use yii\widgets\Menu;
23
use yii\helpers\Html;
24
use yii\helpers\Url;
25
use yii\helpers\ArrayHelper;
26
/**
27
 * Class MainSidebar.
28
 * Create main sidebar for admin-lte.
29
 * ```php
30
 * $admin_site_menu[0] = ['label' => Yii::t('app', 'MAIN NAVIGATION'), 'options' => ['class' => 'header'], 'template'
31
 * => '{label}'];
32
 * $admin_site_menu[1] = ['label' => Yii::t('app', 'Dashboard'), 'icon' => '<i class="fa fa-dashboard"></i>', 'options'
33
 * => ['class' => 'treeview'], 'items' => [
34
 *     ['icon' => '<i class="fa fa-circle-o"></i>', 'label' => Yii::t('app', 'Home'), 'url' => ['/site/index']],
35
 *     ['icon' => '<i class="fa fa-circle-o"></i>', 'label' => Yii::t('app', 'Update'), 'url' => '#'],
36
 * ]];
37
 * // Shorting menu by index
38
 * ksort($admin_site_menu);
39
 * echo MainSidebar::widget([
40
 *     'options'         => ['class' => 'sidebar-menu'],
41
 *     'linkTemplate'    => '<a href="{url}">{icon}<span>{label}</span>{right-icon}{badge}</a>',
42
 *     'submenuTemplate' => "\n<ul class=\"treeview-menu\">\n{items}\n</ul>\n",
43
 *     'activateParents' => true,
44
 *     'items'           => $admin_site_menu,
45
 * ]);
46
 * ```
47
 *
48
 * @author Agiel K. Saputra
49
 */
50
class MainSidebar extends Menu
51
{
52
    /**
53
     * @var string
54
     */
55
    public $linkTemplate = '<a href="{url}" {linkOptions}>\n{icon}\n{label}\n{right-icon}\n{badge}</a>';
56
    /**
57
     * @var string
58
     */
59
    public $labelTemplate = '{icon}\n{label}\n{badge}';
60
    /**
61
     * @var string
62
     */
63
    public $submenuTemplate = "\n<ul class=\"treeview-menu\">\n{items}\n</ul>\n";
64
    /**
65
     * @var string
66
     */
67
    public $badgeTag = 'small';
68
    /**
69
     * @var string
70
     */
71
    public $badgeClass = 'badge pull-right';
72
    /**
73
     * @var string
74
     */
75
    public $badgeBgClass = 'bg-green';
76
    /**
77
     * @var string
78
     */
79
    public $parentRightIcon = '<i class="fa fa-angle-left pull-right"></i>';
80
    /**
81
     * @inheritdoc
82
     */
83
84
    protected function renderItem($item)
85
    {
86
        $item['badgeOptions'] = isset($item['badgeOptions']) ? $item['badgeOptions'] : [];
87
        if (!ArrayHelper::getValue($item, 'badgeOptions.class')) {
88
            $bg = isset($item['badgeBgClass']) ? $item['badgeBgClass'] : $this->badgeBgClass;
89
            $item['badgeOptions']['class'] = $this->badgeClass . ' ' . $bg;
90
        }
91
        if (isset($item['items']) && !isset($item['right-icon'])) {
92
            $item['right-icon'] = $this->parentRightIcon;
93
        }
94
        if (isset($item['url'])) {
95
            $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
96
            return strtr($template, [
97
                '{badge}'          => isset($item['badge']) ? Html::tag('small', $item['badge'], $item['badgeOptions']) : '',
98
                '{icon}'           => isset($item['icon']) ? $item['icon'] : '',
99
                '{right-icon}'     => isset($item['right-icon']) ? $item['right-icon'] : '',
100
                '{url}'            => Url::to($item['url']),
101
                '{label}'          => $item['label'],
102
                '{linkOptions}'    => isset($item['linkOptions']) ? Html::renderTagAttributes($item['linkOptions']): '',
103
            ]);
104
        } else {
105
            $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
106
            return strtr($template, [
107
                '{badge}'          => isset($item['badge']) ? Html::tag('small', $item['badge'], $item['badgeOptions']) : '',
108
                '{icon}'           => isset($item['icon']) ? $item['icon'] : '',
109
                '{right-icon}'     => isset($item['right-icon']) ? $item['right-icon'] : '',
110
                '{label}'          => $item['label'],
111
                '{linkOptions}'    => isset($item['linkOptions']) ? Html::renderTagAttributes($item['linkOptions']): '',
112
            ]);
113
114
        }
115
    }
116
}