diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 883e876..b7ffdd8 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -59,6 +59,14 @@ class TorrentController extends AbstractController 'added' => $torrent->getAdded(), 'locales' => $torrentService->findLastTorrentLocales($torrent->getId()), 'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(), + 'bookmark' => + [ + 'active' => $torrentService->findUserLastTorrentBookmarkValue( + $torrent->getId(), + $user->getId() + ), + 'total' => 0, + ], 'pages' => [] ], 'file' => @@ -225,6 +233,62 @@ class TorrentController extends AbstractController ); } + // Torrent bookmark + #[Route( + '/{_locale}/torrent/{torrentId}/bookmark/toggle', + name: 'torrent_bookmark_toggle', + requirements: + [ + 'torrentId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function toggleBookmark( + Request $request, + TranslatorInterface $translator, + UserService $userService, + TorrentService $torrentService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + if (!$user->isStatus()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init torrent + if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) + { + throw $this->createNotFoundException(); + } + + // Update + $torrentService->toggleTorrentBookmark( + $torrent->getId(), + $user->getId(), + time() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'torrent_info', + [ + '_locale' => $request->get('_locale'), + 'torrentId' => $torrent->getId() + ] + ); + } + // Torrent locales #[Route( '/{_locale}/torrent/{torrentId}/edit/locales/{torrentLocalesId}', @@ -271,16 +335,21 @@ class TorrentController extends AbstractController } // Init torrent locales - $torrentLocalesValue = []; + $torrentLocalesCurrent = [ + 'userId' => null, + 'value' => [] + ]; // Get from edition version requested if ($request->get('torrentLocalesId')) { if ($torrentLocales = $torrentService->getTorrentLocales($request->get('torrentLocalesId'))) { + $torrentLocalesCurrent['userId'] = $torrentLocales->getUserId(); + foreach ($torrentLocales->getValue() as $value) { - $torrentLocalesValue[] = $value; + $torrentLocalesCurrent['value'][] = $value; } } @@ -295,9 +364,11 @@ class TorrentController extends AbstractController { if ($torrentLocales = $torrentService->findLastTorrentLocales($torrent->getId())) { + $torrentLocalesCurrent['userId'] = $torrentLocales->getUserId(); + foreach ($torrentLocales->getValue() as $value) { - $torrentLocalesValue[] = $value; + $torrentLocalesCurrent['value'][] = $value; } // Update active locale @@ -306,26 +377,26 @@ class TorrentController extends AbstractController else { - $torrentLocalesValue[] = $request->get('_locale'); + $torrentLocalesCurrent['value'][] = $request->get('_locale'); } } // Init edition history $editions = []; - foreach ($torrentService->findTorrentLocales($torrent->getId()) as $torrentLocales) + foreach ($torrentService->findTorrentLocales($torrent->getId()) as $torrentLocalesEdition) { $editions[] = [ - 'id' => $torrentLocales->getId(), - 'added' => $torrentLocales->getAdded(), - 'approved' => $torrentLocales->isApproved(), - 'active' => $torrentLocales->getId() == $request->get('torrentLocalesId'), + 'id' => $torrentLocalesEdition->getId(), + 'added' => $torrentLocalesEdition->getAdded(), + 'approved' => $torrentLocalesEdition->isApproved(), + 'active' => $torrentLocalesEdition->getId() == $request->get('torrentLocalesId'), 'user' => [ - 'id' => $torrentLocales->getUserId(), + 'id' => $torrentLocalesEdition->getUserId(), 'identicon' => $userService->identicon( $userService->get( - $torrentLocales->getUserId() + $torrentLocalesEdition->getUserId() )->getAddress() ), ] @@ -340,7 +411,7 @@ class TorrentController extends AbstractController 'error' => [], 'attribute' => [ - 'value' => $request->get('locales') ? $request->get('locales') : $torrentLocalesValue, + 'value' => $request->get('locales') ? $request->get('locales') : $torrentLocalesCurrent['value'], 'placeholder' => $translator->trans('Content language') ] ] @@ -402,7 +473,7 @@ class TorrentController extends AbstractController 'session' => [ 'moderator' => $user->isModerator(), - 'owner' => $user->getId() === $torrentLocales->getUserId(), + 'owner' => $torrentLocalesCurrent['userId'] === $user->getId(), ] ] ); @@ -582,10 +653,11 @@ class TorrentController extends AbstractController { if ($torrentSensitive = $torrentService->getTorrentSensitive($request->get('torrentSensitiveId'))) { - $sensitive = + $torrentSensitiveCurrent = [ - 'id' => $torrentSensitive->getId(), - 'value' => $torrentSensitive->isValue(), + 'id' => $torrentSensitive->getId(), + 'userId' => $torrentSensitive->getUserId(), + 'value' => $torrentSensitive->isValue(), ]; } @@ -598,38 +670,41 @@ class TorrentController extends AbstractController { if ($torrentSensitive = $torrentService->findLastTorrentSensitive($request->get('torrentId'))) { - $sensitive = + $torrentSensitiveCurrent = [ - 'id' => $torrentSensitive->getId(), - 'value' => $torrentSensitive->isValue(), - ]; } + 'id' => $torrentSensitive->getId(), + 'userId' => $torrentSensitive->getUserId(), + 'value' => $torrentSensitive->isValue(), + ]; + } else { - $sensitive = + $torrentSensitiveCurrent = [ - 'id' => null, - 'value' => false, + 'id' => null, + 'userId' => null, + 'value' => false, ]; } } // Init edition history $editions = []; - foreach ($torrentService->findTorrentSensitive($torrent->getId()) as $torrentSensitive) + foreach ($torrentService->findTorrentSensitive($torrent->getId()) as $torrentSensitiveEdition) { $editions[] = [ - 'id' => $torrentSensitive->getId(), - 'added' => $torrentSensitive->getAdded(), - 'approved' => $torrentSensitive->isApproved(), - 'active' => $torrentSensitive->getId() == $sensitive['id'], + 'id' => $torrentSensitiveEdition->getId(), + 'added' => $torrentSensitiveEdition->getAdded(), + 'approved' => $torrentSensitiveEdition->isApproved(), + 'active' => $torrentSensitiveEdition->getId() == $torrentSensitiveCurrent['id'], 'user' => [ - 'id' => $torrentSensitive->getUserId(), + 'id' => $torrentSensitiveEdition->getUserId(), 'identicon' => $userService->identicon( $userService->get( - $torrentSensitive->getUserId() + $torrentSensitiveEdition->getUserId() )->getAddress() ), ] @@ -644,7 +719,7 @@ class TorrentController extends AbstractController 'error' => [], 'attribute' => [ - 'value' => $sensitive['value'], + 'value' => $torrentSensitiveCurrent['value'], 'placeholder' => $translator->trans('Apply sensitive filters to publication') ] ] @@ -682,7 +757,7 @@ class TorrentController extends AbstractController 'session' => [ 'moderator' => $user->isModerator(), - 'owner' => $user->getId() === $torrentSensitive->getUserId(), + 'owner' => $torrentSensitiveCurrent['userId'] === $user->getId(), ] ] ); diff --git a/src/Entity/TorrentBookmark.php b/src/Entity/TorrentBookmark.php new file mode 100644 index 0000000..2447d0d --- /dev/null +++ b/src/Entity/TorrentBookmark.php @@ -0,0 +1,80 @@ +id; + } + + public function getTorrentId(): ?int + { + return $this->torrentId; + } + + public function setTorrentId(int $torrentId): static + { + $this->torrentId = $torrentId; + + return $this; + } + + public function getUserId(): ?int + { + return $this->userId; + } + + public function setUserId(int $userId): static + { + $this->userId = $userId; + + return $this; + } + + public function getAdded(): ?int + { + return $this->added; + } + + public function setAdded(int $added): static + { + $this->added = $added; + + return $this; + } + + public function isValue(): ?bool + { + return $this->value; + } + + public function setValue(bool $value): static + { + $this->value = $value; + + return $this; + } +} diff --git a/src/Repository/TorrentBookmarkRepository.php b/src/Repository/TorrentBookmarkRepository.php new file mode 100644 index 0000000..e4b502a --- /dev/null +++ b/src/Repository/TorrentBookmarkRepository.php @@ -0,0 +1,40 @@ + + * + * @method TorrentBookmark|null find($id, $lockMode = null, $lockVersion = null) + * @method TorrentBookmark|null findOneBy(array $criteria, array $orderBy = null) + * @method TorrentBookmark[] findAll() + * @method TorrentBookmark[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class TorrentBookmarkRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, TorrentBookmark::class); + } + + public function findUserLastTorrentBookmark( + int $torrentId, + int $userId + ): ?TorrentBookmark + { + return $this->createQueryBuilder('tb') + ->where('tb.torrentId = :torrentId') + ->andWhere('tb.userId = :userId') + ->setParameter('torrentId', $torrentId) + ->setParameter('userId', $userId) + ->orderBy('tb.id', 'DESC') // same to ts.added + ->setMaxResults(1) + ->getQuery() + ->getOneOrNullResult() + ; + } +} diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index 9cc62eb..ec08834 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -5,10 +5,12 @@ namespace App\Service; use App\Entity\Torrent; use App\Entity\TorrentLocales; use App\Entity\TorrentSensitive; +use App\Entity\TorrentBookmark; use App\Repository\TorrentRepository; use App\Repository\TorrentLocalesRepository; use App\Repository\TorrentSensitiveRepository; +use App\Repository\TorrentBookmarkRepository; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Filesystem\Filesystem; @@ -158,6 +160,19 @@ class TorrentService ->findTorrentSensitive($torrentId); } + /// Bookmark + public function findUserLastTorrentBookmarkValue(int $torrentId, int $userId): bool + { + if ($torrentBookmark = $this->entityManagerInterface + ->getRepository(TorrentBookmark::class) + ->findUserLastTorrentBookmark($torrentId, $userId)) + { + return $torrentBookmark->isValue(); + } + + return false; + } + // Update public function toggleTorrentLocalesApproved( int $id @@ -204,6 +219,31 @@ class TorrentService return $torrentLocales; } + public function toggleTorrentBookmark( + int $torrentId, + int $userId, + int $added + ): ?TorrentBookmark + { + $torrentBookmark = new TorrentBookmark(); + + $torrentBookmark->setTorrentId($torrentId); + $torrentBookmark->setUserId($userId); + $torrentBookmark->setAdded($added); + + $torrentBookmark->setValue( + !$this->findUserLastTorrentBookmarkValue( + $torrentId, + $userId + ) + ); + + $this->entityManagerInterface->persist($torrentBookmark); + $this->entityManagerInterface->flush(); + + return $torrentBookmark; + } + public function deleteTorrentSensitive( int $id ): ?TorrentSensitive diff --git a/templates/default/torrent/info.html.twig b/templates/default/torrent/info.html.twig index 119be69..e4ec236 100644 --- a/templates/default/torrent/info.html.twig +++ b/templates/default/torrent/info.html.twig @@ -27,10 +27,16 @@