MainSidebar::renderItem()   C
last analyzed

Complexity

Conditions 15
Paths 24

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
rs 5.0504
cc 15
eloc 24
nc 24
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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