mein-sterntours/app/Http/Controllers/NavigationTreeController.php
2026-01-23 17:34:40 +01:00

165 lines
5.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\NavigationTreeService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class NavigationTreeController extends Controller
{
protected $navigationService;
public function __construct(NavigationTreeService $navigationService)
{
$this->navigationService = $navigationService;
}
/**
* Zeigt die Navigationsbaum-Übersicht
*
* @return \Illuminate\View\View
*/
public function index()
{
return view('navigation.index');
}
/**
* Gibt die Navigationsbaum-Daten als JSON zurück (Frontend-Struktur)
*
* @param Request $request
* @return JsonResponse
*/
public function getData(Request $request): JsonResponse
{
try {
$includeHidden = $request->get('include_hidden', true);
$tree = $this->navigationService->getFrontendNavigationTree($includeHidden);
return response()->json([
'success' => true,
'data' => $tree,
'meta' => [
'total_nodes' => $this->navigationService->countNodes($tree),
'include_hidden' => $includeHidden,
'structure' => 'frontend'
]
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage()
], 500);
}
}
/**
* Sucht im Navigationsbaum
*
* @param Request $request
* @return JsonResponse
*/
public function search(Request $request): JsonResponse
{
try {
$query = $request->get('query', '');
$flatList = $this->navigationService->getFlatNavigationList();
// Filtere nach Suchbegriff
$results = array_filter($flatList, function ($node) use ($query) {
return stripos($node['title'], $query) !== false
|| stripos($node['slug'], $query) !== false
|| stripos($node['url'], $query) !== false;
});
return response()->json([
'success' => true,
'data' => array_values($results),
'meta' => [
'query' => $query,
'total_results' => count($results)
]
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage()
], 500);
}
}
/**
* Exportiert den Navigationsbaum als JSON-Datei
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function export(Request $request)
{
try {
$includeHidden = $request->get('include_hidden', true);
$tree = $this->navigationService->getFrontendNavigationTree($includeHidden);
$filename = 'navigation-tree-frontend-' . date('Y-m-d-His') . '.json';
$json = json_encode($tree, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return response($json)
->header('Content-Type', 'application/json')
->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
} catch (\Exception $e) {
return back()->with('error', 'Export fehlgeschlagen: ' . $e->getMessage());
}
}
/**
* Löscht den Cache
*
* @return \Illuminate\Http\RedirectResponse
*/
public function clearCache()
{
try {
$this->navigationService->clearCache();
return back()->with('success', 'Navigation-Cache erfolgreich gelöscht');
} catch (\Exception $e) {
return back()->with('error', 'Cache-Löschung fehlgeschlagen: ' . $e->getMessage());
}
}
/**
* Zeigt die Statistiken
*
* @return JsonResponse
*/
public function stats(): JsonResponse
{
try {
$allTree = $this->navigationService->getNavigationTree(false);
$activeTree = $this->navigationService->getNavigationTree(true);
$flatList = $this->navigationService->getFlatNavigationList();
// Zähle verschiedene Typen
$stats = [
'total_pages' => count($flatList),
'total_nodes' => $this->navigationService->countNodes($allTree),
'active_nodes' => $this->navigationService->countNodes($activeTree),
'inactive_nodes' => $this->navigationService->countNodes($allTree) - $this->navigationService->countNodes($activeTree),
'travel_programs' => count(array_filter($flatList, fn($n) => $n['is_travel_program'])),
'fewo_lodgings' => count(array_filter($flatList, fn($n) => $n['is_fewo_lodging'])),
'country_pages' => count(array_filter($flatList, fn($n) => $n['is_country_page'])),
];
return response()->json([
'success' => true,
'data' => $stats
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage()
], 500);
}
}
}