Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit a8c8d76

Browse files
committed
more cleanup and fix nesting issue
1 parent 922c8e3 commit a8c8d76

15 files changed

+298
-163
lines changed

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,3 @@ return [
125125

126126
### Crud Views
127127
[Wiki](https://github.com/ctf0/SimpleMenu/wiki/Crud-Views)
128-
129-
## Todo
130-
- find a way to add new nesting levels in [vuedraggable](https://github.com/SortableJS/Vue.Draggable)

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"extra": {
3232
"laravel": {
3333
"providers": [
34-
"ctf0\\SimpleMenu\\SimpleMenuServiceProvidor"
34+
"ctf0\\SimpleMenu\\SimpleMenuServiceProvider"
3535
],
3636
"aliases": {
3737
"SimpleMenu": "ctf0\\SimpleMenu\\Facade\\SimpleMenu"

src/Controllers/Admin/MenusController.php

-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function store(Request $request)
4949
]);
5050

5151
$menu = Menu::create($request->all());
52-
$menu->cleanData();
5352

5453
return redirect()->route($this->crud_prefix . '.menus.index');
5554
}
@@ -105,7 +104,6 @@ public function update($id, Request $request)
105104

106105
// update and trigger events
107106
$menu->update($request->except('saveList'));
108-
$menu->cleanData();
109107

110108
return back();
111109
}
@@ -122,7 +120,6 @@ public function destroy($id, Request $request)
122120
$menu = Menu::find($id);
123121
$menu->pages()->detach();
124122
$menu->delete();
125-
$menu->cleanData();
126123

127124
if ($request->expectsJson()) {
128125
return response()->json(['done'=>true]);

src/Controllers/Admin/PagesController.php

-5
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public function store(Request $request)
5959
$page->givePermissionTo($permissions);
6060
$page->assignToMenus($menus);
6161

62-
$page->cleanData();
63-
6462
return redirect()->route($this->crud_prefix . '.pages.index');
6563
}
6664

@@ -103,8 +101,6 @@ public function update($id, Request $request)
103101
$page->syncPermissions($permissions);
104102
$page->syncMenus($menus);
105103

106-
$page->cleanData();
107-
108104
return redirect()->route($this->crud_prefix . '.pages.index');
109105
}
110106

@@ -119,7 +115,6 @@ public function destroy($id, Request $request)
119115
{
120116
$page = Page::find($id);
121117
$page->delete();
122-
$page->cleanData();
123118

124119
if ($request->expectsJson()) {
125120
return response()->json(['done'=>true]);

src/Controllers/Admin/Traits/MenuOps.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function removePage($id, Request $request)
4141
// remove page from menu
4242
$menu = Menu::find($id);
4343
$menu->pages()->detach($request->page_id);
44-
$menu->cleanData();
44+
$menu->touch();
4545

4646
// clear page nesting
4747
if (config('simpleMenu.clearRootDescendants')) {
@@ -78,10 +78,14 @@ protected function saveListToDb($list)
7878
$child = $this->findPage($one->id);
7979
$parent = $this->findPage($one->parent_id);
8080

81+
if ($child == $parent) {
82+
return;
83+
}
84+
8185
$child->makeChildOf($parent);
8286

83-
$child->cleanData();
84-
$parent->cleanData();
87+
$child->touch();
88+
$parent->touch();
8589

8690
if ($one->children) {
8791
$this->saveListToDb($one->children);
@@ -103,7 +107,9 @@ protected function clearNests($id)
103107

104108
protected function clearSelfAndNests($id)
105109
{
106-
return $this->findPage($id)->clearSelfAndDescendants();
110+
$page = $this->findPage($id);
111+
112+
return $page->clearSelfAndDescendants();
107113
}
108114

109115
protected function findPage($id)

src/Models/Menu.php

-7
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@
66

77
class Menu extends Model
88
{
9-
use ClearCacheTrait;
10-
119
protected $guarded = ['id'];
1210

1311
public function pages()
1412
{
1513
return $this->belongsToMany(Page::class)->withPivot('order');
1614
}
17-
18-
public function cleanData()
19-
{
20-
return $this->clearCache('Menu');
21-
}
2215
}

src/Models/Page.php

+13-46
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,24 @@
33
namespace ctf0\SimpleMenu\Models;
44

55
use Baum\Node;
6-
use Illuminate\Support\Facades\File;
76
use Illuminate\Support\Facades\Cache;
87
use Spatie\Permission\Traits\HasRoles;
98
use Spatie\Translatable\HasTranslations;
109
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
1110

1211
class Page extends Node
1312
{
14-
use HasRoles, HasTranslations, ClearCacheTrait;
13+
use HasRoles, HasTranslations;
1514

1615
protected $with = ['roles', 'permissions', 'menus'];
1716
protected $appends = ['nests'];
18-
public $translatable = ['title', 'body', 'desc', 'prefix', 'url'];
1917
protected $guard_name = 'web';
2018
protected $hidden = [
2119
'children', 'roles', 'permissions',
2220
'menus', 'pivot', 'parent_id',
2321
'lft', 'rgt', 'depth',
2422
];
25-
26-
// protected static function boot()
27-
// {
28-
// parent::boot();
29-
// static::addGlobalScope('url', function (Illuminate\Database\Eloquent\Builder $builder) {
30-
// $builder->where('url->' . LaravelLocalization::getCurrentLocale(), '!=', '');
31-
// });
32-
// }
23+
public $translatable = ['title', 'body', 'desc', 'prefix', 'url'];
3324

3425
public function menus()
3526
{
@@ -66,12 +57,14 @@ public function getAttributeValue($key)
6657
*/
6758
public function assignToMenus($menus)
6859
{
69-
return $this->menus()->attach($menus);
60+
$this->menus()->attach($menus);
61+
$this->touch();
7062
}
7163

7264
public function syncMenus($menus)
7365
{
74-
return $this->menus()->sync($menus);
66+
$this->menus()->sync($menus);
67+
$this->touch();
7568
}
7669

7770
/**
@@ -91,12 +84,15 @@ public function getAncestors($columns = ['*'])
9184
public function getNestsAttribute()
9285
{
9386
return Cache::rememberForever($this->getCrntLocale() . "-{$this->route_name}_nests", function () {
94-
$childs = array_flatten(current($this->getDescendants()->toHierarchy()));
95-
96-
return count($childs) ? $childs : null;
87+
return $childs = array_flatten(current($this->getDescendants()->toHierarchy()));
9788
});
9889
}
9990

91+
/**
92+
* clear Nesting.
93+
*
94+
* @return [type] [description]
95+
*/
10096
public function destroyDescendants()
10197
{
10298
if (config('simpleMenu.deletePageAndNests')) {
@@ -108,10 +104,7 @@ public function destroyDescendants()
108104

109105
public function clearSelfAndDescendants()
110106
{
111-
// self
112107
$this->makeRoot();
113-
114-
// childs
115108
$this->clearNests();
116109
}
117110

@@ -131,32 +124,6 @@ protected function clearNests()
131124
$one->makeRoot();
132125
});
133126

134-
$this->cleanData();
135-
}
136-
137-
/**
138-
* clear cacheing and stuff.
139-
*
140-
* @param [type] $page [description]
141-
*
142-
* @return [type] [description]
143-
*/
144-
public function cleanData()
145-
{
146-
$route_name = $this->route_name;
147-
148-
// clear page session
149-
session()->forget($route_name);
150-
151-
// remove the route file
152-
File::delete(config('simpleMenu.routeListPath'));
153-
154-
// clear page cache
155-
$this->clearCache($route_name);
156-
$this->clearCache('_ancestors');
157-
$this->clearCache('_nests');
158-
159-
// clear menu cache
160-
$this->clearCache('Menu');
127+
$this->touch();
161128
}
162129
}

src/Models/ClearCacheTrait.php src/Observers/ClearCacheTrait.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace ctf0\SimpleMenu\Models;
3+
namespace ctf0\SimpleMenu\Observers;
44

55
use Illuminate\Support\Facades\Cache;
66

@@ -13,8 +13,15 @@ protected function clearCache($key_name)
1313
foreach ($keys as $key) {
1414
$redis->del($key);
1515
}
16+
}
1617

18+
protected function clearPagesCache()
19+
{
1720
Cache::forget('sm-pages');
21+
}
22+
23+
protected function clearMenusCache()
24+
{
1825
Cache::forget('sm-menus');
1926
}
2027
}

src/Observers/MenuObserver.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace ctf0\SimpleMenu\Observers;
4+
5+
use ctf0\SimpleMenu\Models\Menu;
6+
7+
class MenuObserver
8+
{
9+
use ClearCacheTrait;
10+
11+
/**
12+
* Listen to the User saved event.
13+
*/
14+
public function saved(Menu $menu)
15+
{
16+
return $this->cleanData($menu);
17+
}
18+
19+
/**
20+
* Listen to the User deleted event.
21+
*/
22+
public function deleted(Menu $menu)
23+
{
24+
return $this->cleanData($menu);
25+
}
26+
27+
/**
28+
* helpers.
29+
*
30+
* @param [type] $menu [description]
31+
* @param mixed $page
32+
*
33+
* @return [type] [description]
34+
*/
35+
protected function cleanData($menu)
36+
{
37+
$this->clearCache("{$menu->name}Menu");
38+
$this->clearMenusCache();
39+
}
40+
}

src/Observers/PageObserver.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace ctf0\SimpleMenu\Observers;
4+
5+
use ctf0\SimpleMenu\Models\Page;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Facades\Cache;
8+
9+
class PageObserver
10+
{
11+
use ClearCacheTrait;
12+
13+
/**
14+
* Listen to the User saving event.
15+
*/
16+
public function saving(Page $page)
17+
{
18+
return $this->cleanData($page);
19+
}
20+
21+
/**
22+
* Listen to the User deleting event.
23+
*/
24+
public function deleting(Page $page)
25+
{
26+
return $this->cleanData($page);
27+
}
28+
29+
/**
30+
* helpers.
31+
*
32+
* @param [type] $menu [description]
33+
* @param mixed $page
34+
*
35+
* @return [type] [description]
36+
*/
37+
protected function cleanData($page)
38+
{
39+
$route_name = $page->route_name;
40+
41+
// clear page session
42+
session()->forget($route_name);
43+
44+
// remove the route file
45+
File::delete(config('simpleMenu.routeListPath'));
46+
47+
// clear page cache
48+
$this->clearCache($route_name);
49+
$this->clearCache('_ancestors');
50+
$this->clearCache('_nests');
51+
52+
// clear menu cache
53+
foreach ($page->menus->pluck('name') as $menu) {
54+
$this->clearCache("{$menu}Menu");
55+
}
56+
57+
$this->clearPagesCache();
58+
}
59+
}

src/SimpleMenuServiceProvider.php

+6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace ctf0\SimpleMenu;
44

5+
use ctf0\SimpleMenu\Models\Menu;
6+
use ctf0\SimpleMenu\Models\Page;
57
use Illuminate\Support\Facades\URL;
68
use Illuminate\Foundation\AliasLoader;
79
use Illuminate\Support\ServiceProvider;
10+
use ctf0\SimpleMenu\Observers\MenuObserver;
11+
use ctf0\SimpleMenu\Observers\PageObserver;
812
use ctf0\SimpleMenu\Observers\UserObserver;
913
use ctf0\SimpleMenu\Middleware\RoleMiddleware;
1014
use ctf0\SimpleMenu\Middleware\PermissionMiddleware;
@@ -72,6 +76,8 @@ public function boot()
7276
protected function observers()
7377
{
7478
if (!app()->runningInConsole()) {
79+
Page::observe(PageObserver::class);
80+
Menu::observe(MenuObserver::class);
7581
app(config('simpleMenu.userModel'))->observe(UserObserver::class);
7682
}
7783
}

0 commit comments

Comments
 (0)