twigEngine; } protected function sessions(): SessionManager { return $this->sessionManager; } public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { $redirect = $this->requireUser($request, $response); if ($redirect !== null) { return $redirect; } $user = $this->sessionManager->getUser(); \assert($user !== null); try { $list = $this->admin->listReporters($user->userId); } catch (ApiException $e) { $list = ['data' => [], 'total' => 0]; $this->flashFromException($e); } return $this->twigEngine->render($response, 'pages/reporters/index.twig', [ 'active_section' => 'reporters', 'list' => $list, 'can_write' => $this->userIs($user, 'admin'), ]); } /** * @param array{id: string} $args */ public function edit(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $redirect = $this->requireUser($request, $response); if ($redirect !== null) { return $redirect; } $user = $this->sessionManager->getUser(); \assert($user !== null); $id = $this->parseId($args['id']); if ($id === null) { return $this->twigEngine->render($response->withStatus(404), 'pages/error.twig', [ 'status' => 404, 'is_client_error' => true, 'message' => 'Reporter not found', ]); } try { $reporter = $this->admin->getReporter($user->userId, $id); } catch (ApiNotFoundException) { return $this->twigEngine->render($response->withStatus(404), 'pages/error.twig', [ 'status' => 404, 'is_client_error' => true, 'message' => 'Reporter not found', ]); } catch (ApiException $e) { $this->flashFromException($e); return $response->withStatus(303)->withHeader('Location', '/app/reporters'); } return $this->twigEngine->render($response, 'pages/reporters/edit.twig', [ 'active_section' => 'reporters', 'reporter' => $reporter, 'can_write' => $this->userIs($user, 'admin'), ]); } public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { $redirect = $this->requireUser($request, $response); if ($redirect !== null) { return $redirect; } $user = $this->sessionManager->getUser(); \assert($user !== null); $body = $this->formBody($request); $payload = ['name' => isset($body['name']) && is_string($body['name']) ? trim($body['name']) : '']; if (isset($body['description']) && is_string($body['description'])) { $payload['description'] = trim($body['description']) === '' ? null : trim($body['description']); } if (isset($body['trust_weight']) && is_numeric($body['trust_weight'])) { $payload['trust_weight'] = (float) $body['trust_weight']; } try { $created = $this->admin->createReporter($user->userId, $payload); $this->sessionManager->flash('success', 'Reporter created.'); $newId = (int) ($created['id'] ?? 0); return $response->withStatus(303)->withHeader('Location', $newId > 0 ? '/app/reporters/' . $newId : '/app/reporters'); } catch (ApiException $e) { $this->flashFromException($e); } return $response->withStatus(303)->withHeader('Location', '/app/reporters'); } /** * @param array{id: string} $args */ public function update(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $redirect = $this->requireUser($request, $response); if ($redirect !== null) { return $redirect; } $user = $this->sessionManager->getUser(); \assert($user !== null); $id = $this->parseId($args['id']); if ($id === null) { return $response->withStatus(303)->withHeader('Location', '/app/reporters'); } $body = $this->formBody($request); $payload = []; if (isset($body['name']) && is_string($body['name'])) { $payload['name'] = trim($body['name']); } if (isset($body['description']) && is_string($body['description'])) { $payload['description'] = trim($body['description']) === '' ? null : trim($body['description']); } if (isset($body['trust_weight']) && is_numeric($body['trust_weight'])) { $payload['trust_weight'] = (float) $body['trust_weight']; } if (array_key_exists('is_active', $body)) { $payload['is_active'] = $this->formBool($body['is_active']); } try { $this->admin->updateReporter($user->userId, $id, $payload); $this->sessionManager->flash('success', 'Reporter saved.'); } catch (ApiException $e) { $this->flashFromException($e); } return $response->withStatus(303)->withHeader('Location', '/app/reporters/' . $id); } /** * @param array{id: string} $args */ public function delete(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $redirect = $this->requireUser($request, $response); if ($redirect !== null) { return $redirect; } $user = $this->sessionManager->getUser(); \assert($user !== null); $id = $this->parseId($args['id']); if ($id === null) { return $response->withStatus(303)->withHeader('Location', '/app/reporters'); } try { $this->admin->deleteReporter($user->userId, $id); $this->sessionManager->flash('success', 'Reporter deactivated.'); } catch (ApiException $e) { $this->flashFromException($e); } return $response->withStatus(303)->withHeader('Location', '/app/reporters'); } }