diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index e515162..fbb029f 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -14,15 +14,111 @@ use App\Service\ActivityService; class SearchController extends AbstractController { public function module( - ?string $query, - ?string $type + Request $request, + UserService $userService, + ActivityService $activityService ): Response { + // Defaults + $locales = []; + $categories = []; + + // Extended search + if ($request->get('filter')) + { + // Init user + $user = $this->initUser( + $request, + $userService, + $activityService + ); + + // Locales + foreach (explode('|', $this->getParameter('app.locales')) as $locale) + { + if ($request->get('locales')) + { + $locales[] = + [ + 'value' => $locale, + 'checked' => in_array($locale, (array) $request->get('locales')) ? true : false, + ]; + } + + else + { + $locales[] = + [ + 'value' => $locale, + 'checked' => in_array($locale, $user->getLocales()) ? true : false, + ]; + } + } + + // Categories + foreach (explode('|', $this->getParameter('app.categories')) as $category) + { + if ($request->get('categories')) + { + $categories[] = + [ + 'value' => $category, + 'checked' => in_array($category, (array) $request->get('categories')) ? true : false, + ]; + } + + else + { + $categories[] = + [ + 'value' => $category, + 'checked' => in_array($category, $user->getCategories()) ? true : false, + ]; + } + } + } + return $this->render( 'default/search/module.html.twig', [ - 'query' => $query ? urldecode($query) : '', + 'query' => $request->get('query') ? urldecode($request->get('query')) : '', + 'filter' => $request->get('filter'), + 'sensitive' => $request->get('sensitive'), + 'locales' => $locales, + 'categories' => $categories, ] ); } + + private function initUser( + Request $request, + UserService $userService, + ActivityService $activityService + ): ?\App\Entity\User + { + // Init user + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $activityService->getEventCodes(), + $this->getParameter('app.theme'), + $this->getParameter('app.sensitive'), + $this->getParameter('app.yggdrasil'), + $this->getParameter('app.posters'), + $this->getParameter('app.approved') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } + + return $user; + } } \ No newline at end of file diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 31b7c10..0da8c26 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -265,22 +265,22 @@ class TorrentController extends AbstractController // Get total torrents $total = $torrentService->findTorrentsTotal( - $user->getId(), + $request->get('filter') ? 0 : $user->getId(), $query, - $user->getLocales(), - $user->getCategories(), - $user->isSensitive() ? false : null, + $request->get('filter') ? (array) $request->get('locales') : $user->getLocales(), + $request->get('filter') ? (array) $request->get('categories') : $user->getCategories(), + $request->get('filter') ? ($request->get('sensitive') ? null : false) : ($user->isSensitive() ? false : null), !$user->isModerator() ? true : null, !$user->isModerator() ? true : null, ); $torrents = []; foreach ($torrentService->findTorrents( - $user->getId(), + $request->get('filter') ? 0 : $user->getId(), $query, - $user->getLocales(), - $user->getCategories(), - $user->isSensitive() ? false : null, + $request->get('filter') ? (array) $request->get('locales') : $user->getLocales(), + $request->get('filter') ? (array) $request->get('categories') : $user->getCategories(), + $request->get('filter') ? ($request->get('sensitive') ? null : false) : ($user->isSensitive() ? false : null), !$user->isModerator() ? true : null, !$user->isModerator() ? true : null, $this->getParameter('app.pagination'), diff --git a/templates/default/layout.html.twig b/templates/default/layout.html.twig index 2e25d05..b4c9100 100644 --- a/templates/default/layout.html.twig +++ b/templates/default/layout.html.twig @@ -26,8 +26,7 @@ {{ render(controller( 'App\\Controller\\SearchController::module', { - query : app.request.get('query'), - type : app.request.get('type') + request: app.request } )) }} {% endblock %} diff --git a/templates/default/search/module.html.twig b/templates/default/search/module.html.twig index 6677ce5..6653baf 100644 --- a/templates/default/search/module.html.twig +++ b/templates/default/search/module.html.twig @@ -1,4 +1,62 @@
\ No newline at end of file