repository = new SemanticExplorerRepository(); } /** * GET /semantic-explorer/taxonomie * Hierarchische Kategorisierung */ public function index(): void { $terms = $this->repository->getTaxonomyTerms(); $hierarchy = $this->buildTaxonomyTree($terms); $stats = $this->repository->getTaxonomyStats(); $this->view('semantic-explorer.taxonomie', [ 'title' => 'Taxonomie', 'terms' => $terms, 'hierarchy' => $hierarchy, 'stats' => $stats, ]); } /** * GET /semantic-explorer/taxonomie/new */ public function create(): void { $this->view('semantic-explorer.taxonomie.new', [ 'title' => 'Neuer Taxonomie-Begriff', 'terms' => $this->repository->getTaxonomyTermsForSelect(), ]); } /** * POST /semantic-explorer/taxonomie */ public function store(): void { $input = $this->getJsonInput(); $name = trim($input['name'] ?? ''); $parentId = isset($input['parent_id']) && $input['parent_id'] !== '' ? (int) $input['parent_id'] : null; if ($name === '') { $this->json(['success' => false, 'error' => 'Name ist erforderlich'], 400); return; } try { $id = $this->repository->createTaxonomyTerm($name, $parentId); $this->json(['success' => true, 'id' => $id]); } catch (\Exception $e) { $this->json(['success' => false, 'error' => $e->getMessage()], 500); } } /** * GET /semantic-explorer/taxonomie/{id}/edit */ public function edit(int $id): void { $term = $this->repository->getTaxonomyTerm($id); if ($term === null) { $this->notFound('Begriff nicht gefunden'); } $this->view('semantic-explorer.taxonomie.edit', [ 'title' => 'Begriff bearbeiten', 'term' => $term, 'terms' => $this->repository->getTaxonomyTermsForSelect(), ]); } /** * POST /semantic-explorer/taxonomie/{id} */ public function update(int $id): void { $input = $this->getJsonInput(); $name = trim($input['name'] ?? ''); $parentId = isset($input['parent_id']) && $input['parent_id'] !== '' ? (int) $input['parent_id'] : null; if ($name === '') { $this->json(['success' => false, 'error' => 'Name ist erforderlich'], 400); return; } try { $this->repository->updateTaxonomyTerm($id, $name, $parentId); $this->json(['success' => true]); } catch (\Exception $e) { $this->json(['success' => false, 'error' => $e->getMessage()], 500); } } /** * POST /semantic-explorer/taxonomie/{id}/delete */ public function delete(int $id): void { try { $success = $this->repository->deleteTaxonomyTerm($id); if ($success) { $this->json(['success' => true]); } else { $this->json(['success' => false, 'error' => 'Begriff hat noch Unterbegriffe'], 400); } } catch (\Exception $e) { $this->json(['success' => false, 'error' => $e->getMessage()], 500); } } /** * Baut Baum aus flacher Liste */ private function buildTaxonomyTree(array $items, ?int $parentId = null): array { $tree = []; foreach ($items as $item) { if ($item['parent_id'] == $parentId) { $item['children'] = $this->buildTaxonomyTree($items, $item['id']); $tree[] = $item; } } return $tree; } }