Skip to content

Instantly share code, notes, and snippets.

@Insolita
Last active August 29, 2015 14:15
Show Gist options
  • Save Insolita/d593109463a6f92a5bc7 to your computer and use it in GitHub Desktop.
Save Insolita/d593109463a6f92a5bc7 to your computer and use it in GitHub Desktop.
MenuLTE
<?php
/**
* @example
* <?= \common\widgets\menu\MenuWidget::widget(
* [
* 'options' => ['class' => 'sidebar-menu'],
* 'items' =>
* [
* [
* 'label' => 'Система',
* 'options' => ['class'=>'treeview'],
* 'items'=>[
* [
* 'label' => 'Настройки',
* 'url' => ['/config/index'],
* 'icon' => 'cogs',
* ],
* [
* 'label' => 'Админы',
* 'url' => ['/admin/index'],
* 'icon' => 'th-list',
* ],
* [
* 'label' => 'Роли и права',
* 'url' => ['/simplerbac/rbac/index'],
* 'icon' => 'gevel',
* ],
* ]
* ],
* [
* 'label' => 'Контент',
* 'options' => ['class'=>'treeview'],
* 'items'=>[
* [
* 'label' => 'Новости',
* 'url' => ['/news/admin/index'],
* ],
* [
* 'label' => 'Статьи',
* 'url' => ['/article/admin/index'],
* ],
* [
* 'label' => 'Виджеты',
* 'url' => ['/widgetman/default/index'],
* ],
* ]
* ],
* [
* 'label' => Yii::t('backend', 'System Events'),
* 'url' => ['/system-event/index'],
* 'icon' => 'bell-o',
* 'badge' => \common\models\SystemEvent::getTodayCount(),
* 'badgeBgClass' => 'bg-green',
* 'visible'=>\Yii::$app->user->can('administrator')
* ],
* [
* 'label' => Yii::t('backend', 'Logs'),
* 'url' => ['/log/index'],
* 'icon' => 'angle-double-right',
* 'badge' => \backend\models\SystemLog::getTodayCount(),
* 'badgeBgClass' => 'bg-red',
* 'visible'=>\Yii::$app->user->can('administrator')
* ]
* ]
* ]); ?>
*
**/
namespace common\widgets\menu;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
class MenuWidget extends \yii\widgets\Menu
{
public $linkTemplate = '<a href="{url}"><i class="fa fa-{icon}"></i><span>{label}</span>{right-icon}{badge}</a>';
public $labelTemplate = '<a href="#">"><i class="fa fa-{icon}"></i><span>{label}</span>{right-icon}{badge}</a>';
public $badgeTag = 'small';
public $badgeClass = 'badge pull-right';
public $badgeBgClass = 'bg-green';
public $parentRightIcon = '<i class="fa fa-angle-left pull-right"></i>';
public $submenuTemplate = '\n<ul class=\"treeview-menu\">\n{items}\n</ul>\n';
public $activateParents = true;
/**
* @inheritdoc
*/
protected function renderItem($item)
{
$item['badgeOptions'] = isset($item['badgeOptions']) ? $item['badgeOptions'] : [];
if (!ArrayHelper::getValue($item, 'badgeOptions.class')) {
$bg = isset($item['badgeBgClass']) ? $item['badgeBgClass'] : $this->badgeBgClass;
$item['badgeOptions']['class'] = $this->badgeClass . ' ' . $bg;
}
if (isset($item['items']) && !isset($item['right-icon'])) {
$item['right-icon'] = $this->parentRightIcon;
}
if (isset($item['url'])) {
$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
return strtr(
$template, [
'{badge}' => isset($item['badge'])
? Html::tag('small', $item['badge'], $item['badgeOptions'])
: '',
'{icon}' => isset($item['icon']) ? $item['icon'] : '',
'{right-icon}' => isset($item['right-icon']) ? $item['right-icon'] : '',
'{url}' => Url::to($item['url']),
'{label}' => $item['label'],
]
);
} else {
$template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
return strtr(
$template, [
'{badge}' => isset($item['badge'])
? Html::tag('small', $item['badge'], $item['badgeOptions'])
: '',
'{icon}' => isset($item['icon']) ? $item['icon'] : '',
'{right-icon}' => isset($item['right-icon']) ? $item['right-icon'] : '',
'{label}' => $item['label'],
]
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment