Refactor Menu system and core config data system

This commit is contained in:
jitendra 2018-12-13 16:28:28 +05:30
parent 15135d64d7
commit 0bdd9e1a2a
26 changed files with 557 additions and 371 deletions

View File

@ -95,30 +95,10 @@ return [
], [
'key' => 'configuration',
'name' => 'Configure',
'route' => 'admin.account.edit',
'route' => 'admin.configuration.index',
'sort' => 5,
'icon-class' => 'configuration-icon',
], [
'key' => 'configuration.account',
'name' => 'My Account',
'route' => 'admin.account.edit',
'sort' => 1,
'icon-class' => '',
],
[
'key' => 'configuration.sales',
'name' => 'Sales',
'route' => 'admin.configuration.sales.shipping_methods',
'sort' => 1,
'icon-class' => '',
], [
'key' => 'configuration.sales.shipping_method',
'name' => 'Shipping Methods',
'route' => 'admin.configuration.sales.shipping_methods',
'sort' => 1,
'icon-class' => '',
],
[
'key' => 'settings',
'name' => 'Settings',
'route' => 'admin.locales.index',

View File

@ -51,6 +51,9 @@ class ConfigurationController extends Controller
*/
public function index()
{
// if(!request()->route('slug'))
// return redirect()->route('admin.configuration.index', ['slug' => 'marketplace']);
return view($this->_config['view']);
}

View File

@ -69,6 +69,10 @@ Route::group(['middleware' => ['web']], function () {
])->name('admin.customer.review.index');
Route::get('configuration/{slug?}/{slug2?}', 'Webkul\Admin\Http\Controllers\ConfigurationController@index')->defaults('_config', [
'view' => 'admin::configuration.index'
])->name('admin.configuration.index');
//Shipping Methods Routes
Route::get('configuration/sales/shipping-methods', 'Webkul\Admin\Http\Controllers\ConfigurationController@index')->defaults('_config', [
'view' => 'admin::configuration.sales.shipping-method'

View File

@ -16,7 +16,7 @@ class ProductFormAccordian {
public static function create($callback) {
$accordian = new ProductFormAccordian();
$callback($accordian);
$accordian->items = $accordian->sortItems($accordian->items);
$accordian->items = core()->sortItems($accordian->items);
return $accordian;
}
@ -38,21 +38,4 @@ class ProductFormAccordian {
'sort' => $sort
]);
}
/**
* Method to sort through the acl items and put them in order
*
* @return void
*/
public function sortItems($items) {
usort($items, function($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return $items;
}
}

View File

@ -67,24 +67,17 @@ class AdminServiceProvider extends ServiceProvider
view()->composer(['admin::layouts.nav-left', 'admin::layouts.nav-aside', 'admin::layouts.tabs'], function ($view) {
$menu = current(Event::fire('admin.menu.create'));
$keys = explode('.', $menu->currentKey);
$menu->items = core()->sortItems($menu->items);
$subMenus = $tabs = [];
if (count($keys) > 1) {
$subMenus = [
'items' => $menu->sortItems(array_get($menu->items, current($keys) . '.children')),
];
$view->with('menu', $menu);
});
if (count($keys) > 2) {
$tabs = [
'items' => $menu->sortItems(array_get($menu->items, implode('.children.', array_slice($keys, 0, 2)) . '.children')),
];
}
}
view()->composer(['admin::layouts.nav-aside', 'admin::layouts.tabs', 'admin::configuration.index'], function ($view) {
$tree = current(Event::fire('admin.config.create'));
$menu->items = $menu->sortItems($menu->items);
$tree->items = core()->sortItems($tree->items);
$view->with('menu', $menu)->with('subMenus', $subMenus)->with('tabs', $tabs);
$view->with('config', $tree);
});
}

View File

@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\View;
use Webkul\Ui\Menu;
use Webkul\Core\Tree;
use Webkul\Admin\ProductFormAccordian;
class EventServiceProvider extends ServiceProvider
@ -19,6 +20,8 @@ class EventServiceProvider extends ServiceProvider
{
$this->createAdminMenu();
$this->createAdminConfig();
$this->buildACL();
$this->registerACL();
@ -42,7 +45,7 @@ class EventServiceProvider extends ServiceProvider
public function createAdminMenu()
{
Event::listen('admin.menu.create', function () {
return Menu::create(function ($menu) {
return Tree::create(function ($menu) {
Event::fire('admin.menu.build', $menu);
});
});
@ -50,12 +53,32 @@ class EventServiceProvider extends ServiceProvider
Event::listen('admin.menu.build', function ($menu) {
foreach(config('menu.admin') as $item) {
if (bouncer()->hasPermission($item['key'])) {
$menu->add($item['key'], $item['name'], $item['route'], $item['sort'], $item['icon-class']);
$menu->add($item, 'menu');
}
}
});
}
/**
* This method fires an event for config creation, any package can add their config item by listening to the admin.config.build event
*
* @return void
*/
public function createAdminConfig()
{
Event::listen('admin.config.create', function () {
return Tree::create(function ($tree) {
Event::fire('admin.config.build', $tree);
});
});
Event::listen('admin.config.build', function ($config) {
foreach(config('core') as $item) {
$config->add($item);
}
});
}
/**
* Build route based ACL
*
@ -65,7 +88,7 @@ class EventServiceProvider extends ServiceProvider
{
Event::listen('admin.acl.build', function ($acl) {
foreach(config('acl') as $item) {
$acl->add($item['key'], $item['name'], $item['route'], $item['sort']);
$acl->add($item);
}
});
}

View File

@ -308,6 +308,9 @@ return [
],
'configuration' => [
'title' => 'Configuration',
'save-btn-title' => 'Save',
'tax-categories' => [
'title' => 'Tax Categories',
'add-title' => 'Add Tax Category',

View File

@ -1,11 +1,11 @@
@extends('admin::layouts.content')
@extends('admin::layouts.master')
@section('page_title')
{{ __('admin::app.account.title') }}
@stop
@section('content')
<div class="content">
@section('content-wrapper')
<div class="content full-page">
<form method="POST" action="" @submit.prevent="onSubmit">
<div class="page-header">
<div class="page-title">

View File

@ -0,0 +1,75 @@
<?php
$validations = [];
$disabled = false;
if (isset($field['validation'])) {
array_push($validations, $field['validation']);
}
$validations = implode('|', array_filter($validations));
$name = $field['title'];
?>
<div class="control-group {{ $field['type'] }}" :class="[errors.has('{{ $name }}') ? 'has-error' : '']">
<label for="{{ $name }}" {{ !isset($field['validation']) || strpos('required', $field['validation']) < 0 ? '' : 'class=required' }}>
{{ $field['title'] }}
<?php
$channel_locale = [];
if(isset($field['channel_based']) && $field['channel_based'])
{
array_push($channel_locale, $channel);
}
if(isset($field['locale_based']) && $field['locale_based']) {
array_push($channel_locale, $locale);
}
?>
@if(count($channel_locale))
<span class="locale">[{{ implode(' - ', $channel_locale) }}]</span>
@endif
</label>
<?php
$configData = core()->getConfigData($name, current($channel_locale), next($channel_locale));
?>
@if ($field['type'] == 'text')
<input type="text" v-validate="'{{ $validations }}'" class="control" id="{{ $name }}" name="{{ $name }}" value="{{ old($name) ?: core()->getConfigData($name) }}" data-vv-as="&quot;{{ $field['name'] }}&quot;">
@elseif ($field['type'] == 'textarea')
<textarea v-validate="'{{ $validations }}'" class="control" id="{{ $name }}" name="{{ $name }}" data-vv-as="&quot;{{ $field['name'] }}&quot;">{{ old($name) ?: core()->getConfigData($name) }}</textarea>
@elseif ($field['type'] == 'select')
<select v-validate="'{{ $validations }}'" class="control" id="{{ $name }}" name="{{ $name }}" data-vv-as="&quot;{{ $field['name'] }}&quot;" >
@foreach($field['options'] as $option)
<?php
if($option['value']) {
$value = 1;
}else {
$value = 0;
}
$selectedOption = core()->getConfigData($name) ?? '';
?>
<option value="{{ $value }}" {{ $value == $selectedOption ? 'selected' : ''}}>
{{ $option['title'] }}
</option>
@endforeach
</select>
@endif
<span class="control-error" v-if="errors.has('{{ $name }}')">@{{ errors.first('{!! $name !!}') }}</span>
</div>

View File

@ -0,0 +1,95 @@
@extends('admin::layouts.content')
@section('page_title')
{{ __('admin::app.configuration.title') }}
@stop
@section('content')
<div class="content">
<?php $locale = request()->get('locale') ?: app()->getLocale(); ?>
<?php $channel = request()->get('channel') ?: core()->getDefaultChannelCode(); ?>
<form method="POST" action="" @submit.prevent="onSubmit" enctype="multipart/form-data">
<div class="page-header">
<div class="page-title">
<h1>
{{ __('admin::app.configuration.title') }}
</h1>
<div class="control-group">
<select class="control" id="channel-switcher" name="channel">
@foreach(core()->getAllChannels() as $channelModel)
<option value="{{ $channelModel->code }}" {{ ($channelModel->code) == $channel ? 'selected' : '' }}>
{{ $channelModel->name }}
</option>
@endforeach
</select>
</div>
<div class="control-group">
<select class="control" id="locale-switcher" name="locale">
@foreach(core()->getAllLocales() as $localeModel)
<option value="{{ $localeModel->code }}" {{ ($localeModel->code) == $locale ? 'selected' : '' }}>
{{ $localeModel->name }}
</option>
@endforeach
</select>
</div>
</div>
<div class="page-action">
<button type="submit" class="btn btn-lg btn-primary">
{{ __('admin::app.configuration.save-btn-title') }}
</button>
</div>
</div>
<div class="page-content">
<div class="form-container">
@csrf()
@if ($groups = array_get($config->items, request()->route('slug') . '.children.' . request()->route('slug2') . '.children'))
@foreach ($groups as $key => $item)
<accordian :title="'{{ __($item['name']) }}'" :active="true">
<div slot="body">
@foreach ($item['fields'] as $field)
@include ('admin::configuration.field-type', ['field' => $field])
@endforeach
</div>
</accordian>
@endforeach
@endif
</div>
</div>
</form>
</div>
@stop
@push('scripts')
<script>
$(document).ready(function () {
$('#channel-switcher, #locale-switcher').on('change', function (e) {
$('#channel-switcher').val()
var query = '?channel=' + $('#channel-switcher').val() + '&locale=' + $('#locale-switcher').val();
window.location.href = "{{ route('admin.configuration.sales.shipping_methods') }}" + query;
})
});
</script>
@endpush

View File

@ -1,17 +1,37 @@
@if(count($subMenus))
<div class="aside-nav">
<ul>
@foreach($subMenus['items'] as $menuItem)
<li class="{{ $menu->getActive($menuItem) }}">
<a href="{{ $menuItem['url'] }}">
{{ $menuItem['name'] }}
<div class="aside-nav">
<ul>
@if (request()->route()->getName() != 'admin.configuration.index')
@if ($menu->getActive($menuItem))
<?php $keys = explode('.', $menu->currentKey); ?>
@foreach(array_get($menu->items, current($keys) . '.children') as $item)
<li class="{{ $menu->getActive($item) }}">
<a href="{{ $item['url'] }}">
{{ $item['name'] }}
@if ($menu->getActive($item))
<i class="angle-right-icon"></i>
@endif
</a>
</li>
@endforeach
</ul>
</div>
@endif
@else
@foreach($config->items as $key => $item)
<li class="{{ $item['key'] == request()->route('slug') ? 'active' : '' }}">
<a href="{{ route('admin.configuration.index', $item['key']) }}">
{{ isset($item['name']) ? $item['name'] : '' }}
@if ($item['key'] == request()->route('slug'))
<i class="angle-right-icon"></i>
@endif
</a>
</li>
@endforeach
@endif
</ul>
</div>

View File

@ -1,8 +1,19 @@
<?php
return [
'carriers' => [
'free' => [
[
'key' => 'sales',
'name' => 'Sales',
'sort' => 1
], [
'key' => 'sales.carriers',
'name' => 'Shipping Methods',
'sort' => 1,
], [
'key' => 'sales.carriers.free',
'name' => 'Free Shipping',
'sort' => 1,
'fields' => [
[
'name' => 'title',
'title' => 'Title',
@ -31,8 +42,12 @@ return [
],
'validation' => 'required'
]
],
'flatrate' => [
]
], [
'key' => 'sales.carriers.flatrate',
'name' => 'Flat Rate Shipping',
'sort' => 2,
'fields' => [
[
'name' => 'title',
'title' => 'Title',

View File

@ -1,13 +1,34 @@
@if(count($tabs))
<div class="tabs">
<ul>
@foreach($tabs['items'] as $tab)
<li class="{{ $menu->getActive($tab) }}">
<a href="{{ $tab['url'] }}">
{{ $tab['name'] }}
<div class="tabs">
<ul>
@if (request()->route()->getName() != 'admin.configuration.index')
<?php $keys = explode('.', $menu->currentKey); ?>
@foreach(array_get($menu->items, implode('.children.', array_slice($keys, 0, 2)) . '.children') as $item)
<li class="{{ $menu->getActive($item) }}">
<a href="{{ $item['url'] }}">
{{ $item['name'] }}
</a>
</li>
@endforeach
</ul>
</div>
@endif
@else
@if (array_get($config->items, request()->route('slug') . '.children'))
@foreach (array_get($config->items, request()->route('slug') . '.children') as $key => $item)
<li class="{{ $key == request()->route('slug2') ? 'active' : '' }}">
<a href="{{ route('admin.configuration.index', (request()->route('slug') . '/' . $key)) }}">
{{ $item['name'] }}
</a>
</li>
@endforeach
@endif
@endif
</ul>
</div>

View File

@ -608,4 +608,99 @@ class Core
return date('Y-m-d', $end);
}
}
/**
* Method to sort through the acl items and put them in order
*
* @return void
*/
public function sortItems($items) {
foreach ($items as &$item) {
if(count($item['children'])) {
$item['children'] = $this->sortItems($item['children']);
}
}
usort($items, function($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return $this->convertToAssociativeArray($items);
}
public function convertToAssociativeArray($items)
{
foreach ($items as $key1 => $level1) {
unset($items[$key1]);
$items[$level1['key']] = $level1;
if(count($level1['children'])) {
foreach ($level1['children'] as $key2 => $level2) {
$temp2 = explode('.', $level2['key']);
$finalKey2 = end($temp2);
unset($items[$level1['key']]['children'][$key2]);
$items[$level1['key']]['children'][$finalKey2] = $level2;
if(count($level2['children'])) {
foreach ($level2['children'] as $key3 => $level3) {
$temp3 = explode('.', $level3['key']);
$finalKey3 = end($temp3);
unset($items[$level1['key']]['children'][$finalKey2]['children'][$key3]);
$items[$level1['key']]['children'][$finalKey2]['children'][$finalKey3] = $level3;
}
}
}
}
}
return $items;
}
public function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
$count = count($keys);
while (count($keys) > 1) {
$key = array_shift($keys);
if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$finalKey = array_shift($keys);
if(isset($array[$finalKey])) {
$array[$finalKey] = $this->arrayMerge($array[$finalKey], $value);
} else {
$array[$finalKey] = $value;
}
return $array;
}
protected function arrayMerge(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMerge($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace Webkul\Core;
use Illuminate\Support\Facades\Request;
class Tree {
public $items = [];
public $current;
public $currentKey;
public function __construct() {
$this->current = Request::url();
}
/**
* Shortcut method for create a Config with a callback.
* This will allow you to do things like fire an event on creation.
*
* @param callable $callback Callback to use after the Config creation
* @return object
*/
public static function create($callback) {
$tree = new Tree();
$callback($tree);
return $tree;
}
/**
* Add a Config item to the item stack
*
* @param string $item Dot seperated heirarchy
*/
public function add($item, $type = '')
{
$item['children'] = [];
if ($type == 'menu') {
$item['url'] = route($item['route']);
if (strpos($this->current, $item['url']) !== false) {
$this->currentKey = $item['key'];
}
}
$children = str_replace('.', '.children.', $item['key']);
core()->array_set($this->items, $children, $item);
}
/**
* Method to find the active links
*
* @param array $item Item that needs to be checked if active
* @return string
*/
public function getActive($item)
{
$url = trim($item['url'], '/');
if ((strpos($this->current, $url) !== false) || (strpos($this->currentKey, $item['key']) === 0)) {
return 'active';
}
}
}

View File

@ -0,0 +1,81 @@
<?php
return [
[
'key' => 'sales',
'name' => 'Sales',
'sort' => 1
], [
'key' => 'sales.carriers',
'name' => 'Shipping Methods',
'sort' => 1,
], [
'key' => 'sales.carriers.free',
'name' => 'Free Shipping',
'sort' => 1,
'fields' => [
[
'name' => 'title',
'title' => 'Title',
'type' => 'text',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true
], [
'name' => 'description',
'title' => 'Description',
'type' => 'textarea',
'channel_based' => false,
'locale_based' => true
], [
'name' => 'active',
'title' => 'Status',
'type' => 'select',
'options' => [
[
'title' => 'Active',
'value' => true
], [
'title' => 'Inactive',
'value' => false
]
],
'validation' => 'required'
]
]
], [
'key' => 'sales.carriers.flatrate',
'name' => 'Flat Rate Shipping',
'sort' => 2,
'fields' => [
[
'name' => 'title',
'title' => 'Title',
'type' => 'text',
'validation' => 'required',
'channel_based' => true,
'locale_based' => true
], [
'name' => 'description',
'title' => 'Description',
'type' => 'textarea',
'channel_based' => true,
'locale_based' => false
], [
'name' => 'active',
'title' => 'Status',
'type' => 'select',
'options' => [
[
'title' => 'Active',
'value' => true
], [
'title' => 'Inactive',
'value' => false
]
],
'validation' => 'required'
]
]
]
];

View File

@ -60,7 +60,7 @@ class ShippingServiceProvider extends ServiceProvider
);
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/fields.php', 'core'
dirname(__DIR__) . '/Config/system.php', 'core'
);
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,4 @@
{
<<<<<<< HEAD
"/js/shop.js": "/js/shop.js?id=c7fe085a512d913b6670",
"/css/shop.css": "/css/shop.css?id=77c5b3c611d7a0e05d7e"
}
=======
"/js/shop.js": "/js/shop.js?id=c6ba9f43bd31f175a665",
"/css/shop.css": "/css/shop.css?id=637386a2fb6b1de9c4c4"
}
>>>>>>> 5f5249abe4a1447708683968edd5ea610d7d25dc
"/css/shop.css": "/css/shop.css?id=40d2e5be9b9dd886e1e4"
}

View File

@ -10,7 +10,7 @@ use Webkul\Shop\Http\Middleware\Locale;
use Webkul\Shop\Http\Middleware\Theme;
use Webkul\Shop\Http\Middleware\Currency;
use Webkul\Shop\Providers\ComposerServiceProvider;
use Webkul\Ui\Menu;
use Webkul\Core\Tree;
class ShopServiceProvider extends ServiceProvider
{
@ -80,14 +80,14 @@ class ShopServiceProvider extends ServiceProvider
public function createCustomerMenu()
{
Event::listen('customer.menu.create', function () {
return Menu::create(function ($menu) {
Event::fire('customer.menu.build', $menu);
return Tree::create(function ($tree) {
Event::fire('customer.menu.build', $tree);
});
});
Event::listen('customer.menu.build', function ($menu) {
Event::listen('customer.menu.build', function ($tree) {
foreach(config('menu.customer') as $item) {
$menu->add($item['key'], $item['name'], $item['route'], $item['sort']);
$tree->add($item, 'menu');
}
});
}

View File

@ -1,8 +1,8 @@
const { mix } = require("laravel-mix");
require("laravel-mix-merge-manifest");
// var publicPath = 'publishable/assets';
var publicPath = "../../../public/themes/default/assets";
var publicPath = 'publishable/assets';
// var publicPath = "../../../public/themes/default/assets";
mix.setPublicPath(publicPath).mergeManifest();
mix.disableNotifications();

View File

@ -1,143 +0,0 @@
<?php
namespace Webkul\Ui;
use Illuminate\Support\Facades\HTML;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\URL;
class Menu {
public $items;
public $current;
public $currentKey;
public function __construct() {
$this->current = Request::url();
}
/**
* Shortcut method for create a menu with a callback.
* This will allow you to do things like fire an event on creation.
*
* @param callable $callback Callback to use after the menu creation
* @return object
*/
public static function create($callback) {
$menu = new Menu();
$callback($menu);
return $menu;
}
/**
* Add a menu item to the item stack
*
* @param string $key Dot seperated heirarchy
* @param string $name Text for the anchor
* @param string $route Route for the menu
* @param integer $sort Sorting index for the items
* @param string $iconClass Icon Class name
*/
public function add($key, $name, $route, $sort = 0, $iconClass = null)
{
$url = route($route);
$item = [
'key' => $key,
'name' => $name,
'url' => $url,
'route' => $route,
'sort' => $sort,
'icon-class' => $iconClass,
'children' => []
];
if (strpos($this->current, $url) !== false) {
$this->currentKey = $key;
}
$children = str_replace('.', '.children.', $key);
$this->array_set($this->items, $children, $item);
}
/**
* Method to sort through the menu items and put them in order
*
* @return void
*/
public function sortItems($items) {
foreach ($items as &$item) {
if(count($item['children'])) {
$item['children'] = $this->sortItems($item['children']);
}
}
usort($items, function($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return $items;
}
public function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
$count = count($keys);
while (count($keys) > 1) {
$key = array_shift($keys);
if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$finalKey = array_shift($keys);
if(isset($array[$finalKey])) {
$array[$finalKey] = $this->arrayMerge($array[$finalKey], $value);
} else {
$array[$finalKey] = $value;
}
return $array;
}
protected function arrayMerge(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMerge($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
/**
* Method to find the active links
*
* @param array $item Item that needs to be checked if active
* @return string
*/
public function getActive($item)
{
$url = trim($item['url'], '/');
if ((strpos($this->current, $url) !== false) || (strpos($this->currentKey, $item['key']) === 0)) {
return 'active';
}
}
}

View File

@ -1,115 +0,0 @@
<?php
namespace Webkul\User;
class ACLCreator {
public $items = [];
public $roles = [];
/**
* Shortcut method for create a acl with a callback.
* This will allow you to do things like fire an event on creation.
*
* @param callable $callback Callback to use after the acl creation
* @return object
*/
public static function create($callback) {
$acl = new ACLCreator();
$callback($acl);
$acl->items = $acl->sortItems($acl->items);
return $acl;
}
/**
* Add a acl item to the item stack
*
* @param string $key Dot seperated heirarchy
* @param string $name Text for the anchor
* @param string $route Route for the acl
* @param integer $sort Sorting index for the items
*/
public function add($key, $name, $route, $sort = 0)
{
$item = [
'key' => $key,
'name' => $name,
'route' => $route,
'sort' => $sort,
'children' => []
];
$children = str_replace('.', '.children.', $key);
$this->array_set($this->items, $children, $item);
$this->roles[$route] = $key;
}
/**
* Method to sort through the acl items and put them in order
*
* @return void
*/
public function sortItems($items) {
foreach ($items as &$item) {
if(count($item['children'])) {
$item['children'] = $this->sortItems($item['children']);
}
}
usort($items, function($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return $items;
}
public function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
$count = count($keys);
while (count($keys) > 1) {
$key = array_shift($keys);
if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$finalKey = array_shift($keys);
if(isset($array[$finalKey])) {
$array[$finalKey] = $this->arrayMerge($array[$finalKey], $value);
} else {
$array[$finalKey] = $value;
}
return $array;
}
protected function arrayMerge(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMerge($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
}

View File

@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Event;
use Webkul\User\Bouncer;
use Webkul\User\Facades\Bouncer as BouncerFacade;
use Webkul\User\Http\Middleware\Bouncer as BouncerMiddleware;
use Webkul\User\ACLCreator;
use Webkul\Core\Tree;
class UserServiceProvider extends ServiceProvider
{
@ -37,7 +37,7 @@ class UserServiceProvider extends ServiceProvider
public function createACL()
{
Event::listen('admin.acl.create', function () {
return ACLCreator::create(function ($acl) {
return Tree::create(function ($acl) {
Event::fire('admin.acl.build', $acl);
});
});