diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 2fdd907..a93b5ba 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -104,7 +104,6 @@ class TorrentController extends AbstractController 'POST' ] )] - public function editLocales( Request $request, TranslatorInterface $translator, @@ -256,10 +255,135 @@ class TorrentController extends AbstractController return $this->render( 'default/torrent/edit/locales.html.twig', [ + 'torrentId' => $torrent->getId(), + 'moderator' => $user->isModerator(), + 'locales' => explode('|', $this->getParameter('app.locales')), + 'editions' => $editions, + 'form' => $form, + ] + ); + } + + #[Route( + '/{_locale}/torrent/{torrentId}/approve/locales/{torrentLocalesId}', + name: 'torrent_approve_locales', + requirements: + [ + 'torrentId' => '\d+', + 'torrentLocalesId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function approveLocales( + Request $request, + TranslatorInterface $translator, + UserService $userService, + TorrentService $torrentService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + // Check permissions + if (!$user->isModerator()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init torrent + if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) + { + throw $this->createNotFoundException(); + } + + // Init torrent locales + if (!$torrentLocales = $torrentService->getTorrentLocales($request->get('torrentLocalesId'))) + { + throw $this->createNotFoundException(); + } + + // Update approved + $torrentService->toggleTorrentLocalesApproved( + $torrentLocales->getId() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'torrent_edit_locales', + [ + '_locale' => $request->get('_locale'), + 'torrentId' => $torrent->getId(), + 'torrentLocalesId' => $torrentLocales->getId(), + ] + ); + } + + #[Route( + '/{_locale}/torrent/{torrentId}/delete/locales/{torrentLocalesId}', + name: 'torrent_delete_locales', + requirements: + [ + 'torrentId' => '\d+', + 'torrentLocalesId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function deleteLocales( + Request $request, + TranslatorInterface $translator, + UserService $userService, + TorrentService $torrentService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + // Check permissions + if (!$user->isModerator()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init torrent + if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) + { + throw $this->createNotFoundException(); + } + + // Init torrent locales + if (!$torrentLocales = $torrentService->getTorrentLocales($request->get('torrentLocalesId'))) + { + throw $this->createNotFoundException(); + } + + // Update approved + $torrentService->deleteTorrentLocales( + $torrentLocales->getId() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'torrent_edit_locales', + [ + '_locale' => $request->get('_locale'), 'torrentId' => $torrent->getId(), - 'locales' => explode('|', $this->getParameter('app.locales')), - 'editions' => $editions, - 'form' => $form, + 'torrentLocalesId' => $torrentLocales->getId(), ] ); } diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index 5bac3e4..dd0314a 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -158,6 +158,36 @@ class TorrentService ->findTorrentSensitive($torrentId); } + // Update + public function toggleTorrentLocalesApproved( + int $id + ): ?TorrentLocales + { + $torrentLocales = $this->getTorrentLocales($id); + + $torrentLocales->setApproved( + !$torrentLocales->isApproved() // toggle current value + ); + + $this->entityManagerInterface->persist($torrentLocales); + $this->entityManagerInterface->flush(); + + return $torrentLocales; + } + + // Delete + public function deleteTorrentLocales( + int $id + ): ?TorrentLocales + { + $torrentLocales = $this->getTorrentLocales($id); + + $this->entityManagerInterface->remove($torrentLocales); + $this->entityManagerInterface->flush(); + + return $torrentLocales; + } + // Setters public function add( string $filepath, diff --git a/templates/default/torrent/edit/locales.html.twig b/templates/default/torrent/edit/locales.html.twig index 005ce1a..4b41b0c 100644 --- a/templates/default/torrent/edit/locales.html.twig +++ b/templates/default/torrent/edit/locales.html.twig @@ -1,5 +1,5 @@ {% extends 'default/layout.html.twig' %} -{% block title %}{{'Edit torrent locales'|trans }} - {{ name }}{% endblock %} +{% block title %}{{'Torrent'|trans }} #{{ torrentId }} - {{'Edit locales'|trans }} - {{ name }}{% endblock %} {% block main_content %}