This repository was archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuListener.php
More file actions
executable file
·78 lines (64 loc) · 1.96 KB
/
MenuListener.php
File metadata and controls
executable file
·78 lines (64 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Statamic\Addons\Menu;
use Statamic\API\File;
use Statamic\API\Folder;
use Statamic\API\Nav;
use Statamic\API\YAML;
use Statamic\Events\Data\PageDeleted;
use Statamic\Extend\Listener;
class MenuListener extends Listener {
/**
* The events to be listened for, and the methods to call.
*
* @var array
*/
public $events = [
'cp.nav.created' => 'addNavItems',
'cp.add_to_head' => 'injectMenuStyles',
PageDeleted::class => 'removeItem',
];
public function addNavItems($nav) {
$menu = Nav::item('menu')
->title('Menu')
->route('menu.index')
->icon('list');
$nav->addTo('tools', $menu);
}
public function injectMenuStyles() {
$html = $this->css->tag('styles');
return $html;
}
public function removeItem(PageDeleted $event) {
$id = $event->id;
$files = Folder::disk('storage')->getFilesByType('addons/Menu', 'yaml');
collect($files)
->map(function ($item) {
return YAML::parse(File::disk('storage')->get($item));
})
->each(function ($item) use ($id) {
$values = $this->searchIds($item['items'], $id);
if (count($values) > 0) {
foreach ($values as $index => $value) {
$parent = preg_replace('/.(.[*id])$/x', '', $index);
array_forget($item['items'], $parent);
$path = preg_replace('/(.[*\d+])$/x', '', $parent);
$items = array_get($item['items'], $path);
if (is_null($items)) {
$item['items'] = array_values($item['items']);
} else {
$parent = array_values(array_get($item['items'], $path));
array_set($item['items'], $path, $parent);
}
}
$this->storage->putYAML($item['slug'].'.yaml', $item);
}
});
}
public function searchIds($tree, $id) {
return collect(array_dot($tree))
->filter(function ($item) use ($id) {
return $item == $id;
})
->toArray();
}
}