Skip to content

Commit 75daf11

Browse files
committed
Menu from database
1 parent e15f1a2 commit 75daf11

File tree

9 files changed

+404
-27
lines changed

9 files changed

+404
-27
lines changed

coreui/src/containers/TheSidebar.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export default {
114114
axios.get('/api/menu?token=' + localStorage.getItem("api_token") )
115115
.then(function (response) {
116116
self.nav = self.rebuildData(response.data);
117+
console.log(self.rebuildData(response.data));
117118
}).catch(function (error) {
118119
console.log(error);
119120
self.$router.push({ path: '/login' });

laravel/app/Http/Controllers/MenuController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6-
use App\Http\Menus\Menus;
6+
use App\Http\Menus\GetSidebarMenu;
77

88
class MenuController extends Controller
99
{
@@ -24,7 +24,7 @@ public function index()
2424
} catch (Exception $e) {
2525
$roles = '';
2626
}
27-
$menus = new Menus();
27+
$menus = new GetSidebarMenu();
2828
return response()->json( $menus->get( $roles ) );
2929
}
3030

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* 07.11.2019
4+
* MenusMenu.php
5+
*/
6+
namespace App\Http\Menus;
7+
8+
use App\MenuBuilder\MenuBuilder;
9+
use Illuminate\Support\Facades\DB;
10+
use App\Menus;
11+
use App\MenuBuilder\RenderFromDatabaseData;
12+
13+
class GetSidebarMenu implements MenuInterface{
14+
15+
private $mb; //menu builder
16+
private $menu;
17+
18+
public function __construct(){
19+
$this->mb = new MenuBuilder();
20+
}
21+
22+
private function getMenuFromDB($menuId, $menuName){
23+
$this->menu = Menus::where('menu_id', '=', $menuId)->where('menu_name', '=', $menuName)->orderBy('sequence', 'asc')->get();
24+
}
25+
26+
private function getGuestMenu(){
27+
$this->getMenuFromDB(1, 'guest');
28+
}
29+
30+
private function getUserMenu(){
31+
$this->getMenuFromDB(1, 'user');
32+
}
33+
34+
public function get($roles){
35+
$roles = explode(',', $roles);
36+
if(empty($roles)){
37+
$this->getGuestMenu();
38+
}elseif(in_array('user', $roles)){
39+
$this->getUserMenu();
40+
}else{
41+
$this->getGuestMenu();
42+
}
43+
$rfd = new RenderFromDatabaseData;
44+
return $rfd->render($this->menu);
45+
}
46+
47+
}

laravel/app/MenuBuilder/MenuBuilder.php

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,32 @@ class MenuBuilder{
1010

1111
private $menu;
1212
private $dropdown;
13+
private $dropdownDeep;
1314

1415
public function __construct(){
1516
$this->menu = array();
1617
$this->dropdown = false;
18+
$this->dropdownDeep = 0;
19+
}
20+
21+
private function innerAddElementToMenuLastPosition(&$menu, $element, $offset){
22+
$z = 1;
23+
$result = false;
24+
$menu = &$menu[count($menu)-1];
25+
while(is_array($menu)){
26+
if($z == $this->dropdownDeep - $offset){
27+
array_push($menu['elements'], $element);
28+
$result = true;
29+
break;
30+
}
31+
$menu = &$menu['elements'][count($menu['elements'])-1];
32+
$z++;
33+
}
34+
return $result;
35+
}
36+
37+
private function addElementToMenuLastPosition($element, $offset = 0){
38+
return $this->innerAddElementToMenuLastPosition($this->menu, $element, $offset);
1739
}
1840

1941
private function addRegularLink($name, $href, $icon, $iconType){
@@ -41,16 +63,16 @@ private function addDropdownLink($name, $href, $icon, $iconType){
4163
$num = count($this->menu);
4264
$hasIcon = $icon === false ? false : true;
4365
if($hasIcon){
44-
array_push($this->menu[$num-1]['elements'], array(
66+
$this->addElementToMenuLastPosition(array(
4567
'slug' => 'link',
4668
'name' => $name,
4769
'href' => $href,
4870
'hasIcon' => $hasIcon,
4971
'icon' => $icon,
5072
'iconType' => $iconType
5173
));
52-
}else{
53-
array_push($this->menu[$num-1]['elements'], array(
74+
}else{
75+
$this->addElementToMenuLastPosition(array(
5476
'slug' => 'link',
5577
'name' => $name,
5678
'href' => $href,
@@ -87,34 +109,62 @@ public function addTitle($name, $icon = false, $iconType = 'coreui'){
87109
}
88110

89111
public function beginDropdown($name, $href, $icon = false, $iconType = 'coreui'){
90-
if($this->dropdown === true){
91-
throw new Exception('Starting dropdown inside dropdown');
92-
}
112+
//if($this->dropdown === true){
113+
// throw new Exception('Starting dropdown inside dropdown');
114+
//}
93115
$this->dropdown = true;
116+
$this->dropdownDeep++;
94117
$hasIcon = $icon === false ? false : true;
95-
if($hasIcon){
96-
array_push($this->menu, array(
97-
'slug' => 'dropdown',
98-
'name' => $name,
99-
'hasIcon' => $hasIcon,
100-
'icon' => $icon,
101-
'iconType' => $iconType,
102-
'elements' => array(),
103-
'href' => $href,
104-
));
118+
if($this->dropdownDeep === 1){
119+
if($hasIcon){
120+
array_push($this->menu, array(
121+
'slug' => 'dropdown',
122+
'name' => $name,
123+
'hasIcon' => $hasIcon,
124+
'icon' => $icon,
125+
'iconType' => $iconType,
126+
'elements' => array(),
127+
'href' => $href,
128+
));
129+
}else{
130+
array_push($this->menu, array(
131+
'slug' => 'dropdown',
132+
'name' => $name,
133+
'hasIcon' => $hasIcon,
134+
'elements' => array(),
135+
'href' => $href,
136+
));
137+
}
105138
}else{
106-
array_push($this->menu, array(
107-
'slug' => 'dropdown',
108-
'name' => $name,
109-
'hasIcon' => $hasIcon,
110-
'elements' => array(),
111-
'href' => $href,
112-
));
139+
if($hasIcon){
140+
$this->addElementToMenuLastPosition(array(
141+
'slug' => 'dropdown',
142+
'name' => $name,
143+
'hasIcon' => $hasIcon,
144+
'icon' => $icon,
145+
'iconType' => $iconType,
146+
'elements' => array(),
147+
'href' => $href,
148+
), 1);
149+
}else{
150+
$this->addElementToMenuLastPosition(array(
151+
'slug' => 'dropdown',
152+
'name' => $name,
153+
'hasIcon' => $hasIcon,
154+
'elements' => array(),
155+
'href' => $href,
156+
), 1);
157+
}
113158
}
159+
114160
}
115161

116162
public function endDropdown(){
117163
$this->dropdown = false;
164+
$this->dropdownDeep--;
165+
if($this->dropdownDeep < 0){
166+
$this->dropdownDeep = 0;
167+
}
118168
}
119169

120170
public function getResult(){
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/*
3+
* 08.11.2019
4+
* RenderFromDatabaseData
5+
*/
6+
7+
namespace App\MenuBuilder;
8+
use App\MenuBuilder\MenuBuilder;
9+
10+
class RenderFromDatabaseData{
11+
12+
private $mb; // MenuBuilder
13+
private $data;
14+
15+
public function __construct(){
16+
$this->mb = new MenuBuilder();
17+
}
18+
19+
private function addTitle($data){
20+
$this->mb->addTitle($data['name']);
21+
}
22+
23+
private function addLink($data){
24+
if($data['parent_id'] === NULL){
25+
$this->mb->addLink($data['name'], $data['href'], $data['icon']);
26+
}
27+
}
28+
29+
30+
31+
private function dropdownLoop($id){
32+
for($i = 0; $i<count($this->data); $i++){
33+
if($this->data[$i]['parent_id'] == $id){
34+
if($this->data[$i]['slug'] === 'dropdown'){
35+
$this->addDropdown($this->data[$i]);
36+
}elseif($this->data[$i]['slug'] === 'link'){
37+
$this->mb->addLink($this->data[$i]['name'], $this->data[$i]['href']);
38+
}else{
39+
$this->addTitle($this->data[$i]);
40+
}
41+
}
42+
}
43+
}
44+
45+
private function addDropdown($data){
46+
$this->mb->beginDropdown($data['name'], $data['href'], $data['icon']);
47+
$this->dropdownLoop($data['id']);
48+
$this->mb->endDropdown();
49+
}
50+
51+
private function mainLoop(){
52+
for($i = 0; $i<count($this->data); $i++){
53+
switch($this->data[$i]['slug']){
54+
case 'title':
55+
$this->addTitle($this->data[$i]);
56+
break;
57+
case 'link':
58+
$this->addLink($this->data[$i]);
59+
break;
60+
case 'dropdown':
61+
if($this->data[$i]['parent_id'] == null){
62+
$this->addDropdown($this->data[$i]);
63+
}
64+
break;
65+
}
66+
}
67+
}
68+
69+
/***
70+
* $data - array
71+
* return - array
72+
*/
73+
public function render($data){
74+
if(!empty($data)){
75+
$this->data = $data;
76+
$this->mainLoop();
77+
}
78+
return $this->mb->getResult();
79+
}
80+
81+
}

laravel/app/Menus.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Menus extends Model
8+
{
9+
protected $table = 'menus';
10+
public $timestamps = false;
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateMenusTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('menus', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('name');
19+
$table->string('href')->nullable();
20+
$table->string('icon')->nullable();
21+
$table->string('slug');
22+
$table->integer('parent_id')->unsigned()->nullable();
23+
$table->string('menu_name');
24+
$table->integer('menu_id')->unsigned();
25+
$table->integer('sequence');
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists('menus');
37+
}
38+
}

laravel/database/seeds/DatabaseSeeder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Illuminate\Database\Seeder;
4-
use database\seeds\NotesTableSeeder;
4+
//use database\seeds\NotesTableSeeder;
55

66
class DatabaseSeeder extends Seeder
77
{
@@ -12,6 +12,9 @@ class DatabaseSeeder extends Seeder
1212
*/
1313
public function run()
1414
{
15-
$this->call(UsersAndNotesSeeder::class);
15+
//$this->call(MenusTableSeeder::class);
16+
//$this->call(UsersAndNotesSeeder::class);
17+
$this->call('MenusTableSeeder');
18+
$this->call('UsersAndNotesSeeder');
1619
}
1720
}

0 commit comments

Comments
 (0)