Browse Source

init extended search feature #35

main
ghost 1 year ago
parent
commit
5a0342a998
  1. 102
      src/Controller/SearchController.php
  2. 16
      src/Controller/TorrentController.php
  3. 3
      templates/default/layout.html.twig
  4. 58
      templates/default/search/module.html.twig

102
src/Controller/SearchController.php

@ -14,15 +14,111 @@ use App\Service\ActivityService;
class SearchController extends AbstractController class SearchController extends AbstractController
{ {
public function module( public function module(
?string $query, Request $request,
?string $type UserService $userService,
ActivityService $activityService
): Response ): 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( return $this->render(
'default/search/module.html.twig', '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;
}
} }

16
src/Controller/TorrentController.php

@ -265,22 +265,22 @@ class TorrentController extends AbstractController
// Get total torrents // Get total torrents
$total = $torrentService->findTorrentsTotal( $total = $torrentService->findTorrentsTotal(
$user->getId(), $request->get('filter') ? 0 : $user->getId(),
$query, $query,
$user->getLocales(), $request->get('filter') ? (array) $request->get('locales') : $user->getLocales(),
$user->getCategories(), $request->get('filter') ? (array) $request->get('categories') : $user->getCategories(),
$user->isSensitive() ? false : null, $request->get('filter') ? ($request->get('sensitive') ? null : false) : ($user->isSensitive() ? false : null),
!$user->isModerator() ? true : null, !$user->isModerator() ? true : null,
!$user->isModerator() ? true : null, !$user->isModerator() ? true : null,
); );
$torrents = []; $torrents = [];
foreach ($torrentService->findTorrents( foreach ($torrentService->findTorrents(
$user->getId(), $request->get('filter') ? 0 : $user->getId(),
$query, $query,
$user->getLocales(), $request->get('filter') ? (array) $request->get('locales') : $user->getLocales(),
$user->getCategories(), $request->get('filter') ? (array) $request->get('categories') : $user->getCategories(),
$user->isSensitive() ? false : null, $request->get('filter') ? ($request->get('sensitive') ? null : false) : ($user->isSensitive() ? false : null),
!$user->isModerator() ? true : null, !$user->isModerator() ? true : null,
!$user->isModerator() ? true : null, !$user->isModerator() ? true : null,
$this->getParameter('app.pagination'), $this->getParameter('app.pagination'),

3
templates/default/layout.html.twig

@ -26,8 +26,7 @@
{{ render(controller( {{ render(controller(
'App\\Controller\\SearchController::module', 'App\\Controller\\SearchController::module',
{ {
query : app.request.get('query'), request: app.request
type : app.request.get('type')
} }
)) }} )) }}
{% endblock %} {% endblock %}

58
templates/default/search/module.html.twig

@ -1,4 +1,62 @@
<form class="margin-t-16-px" name="search" method="get" action="{{ path('torrent_search') }}"> <form class="margin-t-16-px" name="search" method="get" action="{{ path('torrent_search') }}">
<input class="min-width-200-px" type="text" name="query" value="{{ query }}" placeholder="{{ 'Keyword, file, hash...' | trans }}" /> <input class="min-width-200-px" type="text" name="query" value="{{ query }}" placeholder="{{ 'Keyword, file, hash...' | trans }}" />
<input {% if query %}class="button-green"{% endif %} type="submit" value="{{ 'Search' | trans }}" /> <input {% if query %}class="button-green"{% endif %} type="submit" value="{{ 'Search' | trans }}" />
{% if filter %}
<input type="hidden" name="filter" value="true" />
<div class="text-left">
<div class="margin-t-16-px margin-b-4-px text-right">
<h2 class="margin-x-4-px">
{{ 'Locales' | trans }}
</h2>
</div>
<div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default">
{% for locale in locales | sort %}
<div class="margin-t-4-px margin-b-8-px margin-r-8-px display-inline-block min-width-120-px">
{% if locale.checked %}
<input name="locales[]" id="{{ locale.value }}" type="checkbox" value="{{ locale.value }}" checked="checked" />
{% else %}
<input name="locales[]" id="{{ locale.value }}" type="checkbox" value="{{ locale.value }}" />
{% endif %}
<label class="margin-x-4-px" for="{{ locale.value }}">
{{ locale.value | locale_name(locale.value) | u.title }}
</label>
</div>
{% endfor %}
</div>
<div class="margin-b-4-px text-right">
<h2 class="margin-x-4-px">
{{ 'Categories' | trans }}
</h2>
</div>
<div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default">
{% for category in categories | sort %}
<div class="margin-t-4-px margin-b-8-px margin-r-8-px display-inline-block min-width-120-px">
{% if category.checked %}
<input name="categories[]" id="{{ category.value }}" type="checkbox" value="{{ category.value }}" checked="checked" />
{% else %}
<input name="categories[]" id="{{ category.value }}" type="checkbox" value="{{ category.value }}" />
{% endif %}
<label class="margin-x-4-px" for="{{ category.value }}">
{{ category.value | trans_category | u.title }}
</label>
</div>
{% endfor %}
</div>
<div class="margin-b-4-px text-right">
<h2 class="margin-x-4-px">
{{ 'other' | trans | u.title }}
</h2>
</div>
<div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default">
{% if sensitive %}
<input name="sensitive" id="sensitive" type="checkbox" value="true" checked="checked" />
{% else %}
<input name="sensitive" id="sensitive" type="checkbox" value="true" />
{% endif %}
<label class="margin-x-4-px" for="sensitive">
{{ 'Sensitive' | trans }}
</label>
</div>
</div>
{% endif %}
</form> </form>
Loading…
Cancel
Save