- Added standard Laravel directory structure and configuration. - Included Svelte and Tailwind configuration for the admin interface. - Added core PHPUnit and testing scripts.
92 lines
2.5 KiB
PHP
92 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\NavigationItem;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class NavigationService
|
|
{
|
|
/**
|
|
* Get all navigation items for management.
|
|
*/
|
|
public function getManagementItems(): Collection
|
|
{
|
|
return NavigationItem::with(['page', 'children.page'])
|
|
->whereNull('parent_id')
|
|
->orderBy('order')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get current top-level navigation items for selection.
|
|
*/
|
|
public function getParentItems(): Collection
|
|
{
|
|
return NavigationItem::whereNull('parent_id')
|
|
->select('id', 'label')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Synchronize a page's presence in the navigation.
|
|
*
|
|
* @param \App\Models\Page $page The page to sync.
|
|
* @param bool $include Whether the page should be included in the navigation.
|
|
* @return void
|
|
*/
|
|
public function syncPageNavigation(\App\Models\Page $page, bool $include): void
|
|
{
|
|
if ($include) {
|
|
$navigationItem = NavigationItem::where('page_id', $page->id)->first();
|
|
if (!$navigationItem) {
|
|
NavigationItem::create([
|
|
'label' => $page->title,
|
|
'page_id' => $page->id,
|
|
'order' => NavigationItem::max('order') + 1,
|
|
'target' => '_self',
|
|
]);
|
|
} else {
|
|
$navigationItem->update(['label' => $page->title]);
|
|
}
|
|
} else {
|
|
NavigationItem::where('page_id', $page->id)->delete();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Store a new navigation item.
|
|
*/
|
|
public function store(array $data): NavigationItem
|
|
{
|
|
$maxOrder = NavigationItem::where('parent_id', $data['parent_id'] ?? null)->max('order');
|
|
$data['order'] = ($maxOrder !== null) ? $maxOrder + 1 : 0;
|
|
|
|
return NavigationItem::create($data);
|
|
}
|
|
|
|
/**
|
|
* Reorder navigation items.
|
|
*/
|
|
public function reorder(array $items): void
|
|
{
|
|
foreach ($items as $index => $itemData) {
|
|
$item = NavigationItem::find($itemData['id']);
|
|
if ($item) {
|
|
$item->update([
|
|
'order' => $index,
|
|
'parent_id' => $itemData['parent_id'] ?? null,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a navigation item.
|
|
*/
|
|
public function delete(NavigationItem $navigationItem): bool
|
|
{
|
|
return $navigationItem->delete();
|
|
}
|
|
}
|