Browse Source

add search filters counter #35

main
ghost 1 year ago
parent
commit
5523034307
  1. 75
      src/Controller/SearchController.php
  2. 15
      templates/default/search/module.html.twig

75
src/Controller/SearchController.php

@ -9,6 +9,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService; use App\Service\UserService;
use App\Service\TorrentService;
use App\Service\ActivityService; use App\Service\ActivityService;
class SearchController extends AbstractController class SearchController extends AbstractController
@ -16,6 +17,7 @@ class SearchController extends AbstractController
public function module( public function module(
Request $request, Request $request,
UserService $userService, UserService $userService,
TorrentService $torrentService,
ActivityService $activityService ActivityService $activityService
): Response ): Response
{ {
@ -23,8 +25,12 @@ class SearchController extends AbstractController
$locales = []; $locales = [];
$categories = []; $categories = [];
// Request
$query = $request->get('query') ? urldecode($request->get('query')) : '';
$filter = $request->get('filter') ? true : false;
// Extended search // Extended search
if ($request->get('filter')) if ($filter)
{ {
// Init user // Init user
$user = $this->initUser( $user = $this->initUser(
@ -33,57 +39,90 @@ class SearchController extends AbstractController
$activityService $activityService
); );
// Keywords
$keywords = explode(' ', $query);
// Locales // Locales
foreach (explode('|', $this->getParameter('app.locales')) as $locale) foreach (explode('|', $this->getParameter('app.locales')) as $locale)
{ {
if ($request->get('locales')) if ($request->get('locales'))
{ {
$locales[] = $checked = in_array($locale, (array) $request->get('locales'));
[
'value' => $locale,
'checked' => in_array($locale, (array) $request->get('locales')) ? true : false,
];
} }
else else
{ {
$checked = in_array($locale, $user->getLocales());
}
$locales[] = $locales[] =
[ [
'value' => $locale, 'value' => $locale,
'checked' => in_array($locale, $user->getLocales()) ? true : false, 'checked' => $checked,
'total' => $torrentService->findTorrentsTotal(
0,
$keywords,
[$locale],
$request->get('categories') ? $request->get('categories') : $user->getCategories(),
$sensitive,
!$user->isModerator() ? true : null,
!$user->isModerator() ? true : null,
)
]; ];
} }
}
// Categories // Categories
foreach (explode('|', $this->getParameter('app.categories')) as $category) foreach (explode('|', $this->getParameter('app.categories')) as $category)
{ {
if ($request->get('categories')) if ($request->get('categories'))
{ {
$categories[] = $checked = in_array($category, (array) $request->get('categories'));
[
'value' => $category,
'checked' => in_array($category, (array) $request->get('categories')) ? true : false,
];
} }
else else
{ {
$checked = in_array($category, $user->getCategories());
}
$categories[] = $categories[] =
[ [
'value' => $category, 'value' => $category,
'checked' => in_array($category, $user->getCategories()) ? true : false, 'checked' => $checked,
'total' => $torrentService->findTorrentsTotal(
0,
$keywords,
$request->get('locales') ? $request->get('locales') : $user->getLocales(),
[$category],
$sensitive,
!$user->isModerator() ? true : null,
!$user->isModerator() ? true : null,
)
]; ];
} }
}
// Sensitive
$sensitive =
[
'checked' => $request->get('sensitive'),
'total' => $torrentService->findTorrentsTotal(
0,
$keywords,
$request->get('locales') ? $request->get('locales') : $user->getLocales(),
$request->get('categories') ? $request->get('categories') : $user->getCategories(),
true,
!$user->isModerator() ? true : null,
!$user->isModerator() ? true : null,
)
];
} }
return $this->render( return $this->render(
'default/search/module.html.twig', 'default/search/module.html.twig',
[ [
'query' => $request->get('query') ? urldecode($request->get('query')) : '', 'query' => $query,
'filter' => $request->get('filter'), 'filter' => $filter,
'sensitive' => $request->get('sensitive'), 'sensitive' => $sensitive,
'locales' => $locales, 'locales' => $locales,
'categories' => $categories, 'categories' => $categories,
] ]

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

@ -9,6 +9,7 @@
</div> </div>
<div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default"> <div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default">
{% for locale in locales | sort %} {% for locale in locales | sort %}
{#{% if locale.total %}#}
<div class="margin-t-4-px margin-b-8-px margin-r-8-px display-inline-block min-width-120-px"> <div class="margin-t-4-px margin-b-8-px margin-r-8-px display-inline-block min-width-120-px">
{% if locale.checked %} {% if locale.checked %}
<input name="locales[]" id="{{ locale.value }}" type="checkbox" value="{{ locale.value }}" checked="checked" /> <input name="locales[]" id="{{ locale.value }}" type="checkbox" value="{{ locale.value }}" checked="checked" />
@ -17,8 +18,12 @@
{% endif %} {% endif %}
<label class="margin-x-4-px" for="{{ locale.value }}"> <label class="margin-x-4-px" for="{{ locale.value }}">
{{ locale.value | locale_name(locale.value) | u.title }} {{ locale.value | locale_name(locale.value) | u.title }}
<span class="text-color-night">
({{ locale.total }})
</span>
</label> </label>
</div> </div>
{#{% endif %}#}
{% endfor %} {% endfor %}
</div> </div>
<div class="margin-b-4-px padding-x-4-px text-right"> <div class="margin-b-4-px padding-x-4-px text-right">
@ -26,6 +31,7 @@
</div> </div>
<div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default"> <div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default">
{% for category in categories | sort %} {% for category in categories | sort %}
{#{% if category.total %}#}
<div class="margin-t-4-px margin-b-8-px margin-r-8-px display-inline-block min-width-120-px"> <div class="margin-t-4-px margin-b-8-px margin-r-8-px display-inline-block min-width-120-px">
{% if category.checked %} {% if category.checked %}
<input name="categories[]" id="{{ category.value }}" type="checkbox" value="{{ category.value }}" checked="checked" /> <input name="categories[]" id="{{ category.value }}" type="checkbox" value="{{ category.value }}" checked="checked" />
@ -34,21 +40,28 @@
{% endif %} {% endif %}
<label class="margin-x-4-px" for="{{ category.value }}"> <label class="margin-x-4-px" for="{{ category.value }}">
{{ category.value | trans_category | u.title }} {{ category.value | trans_category | u.title }}
<span class="text-color-night">
({{ category.total }})
</span>
</label> </label>
</div> </div>
{#{% endif %}#}
{% endfor %} {% endfor %}
</div> </div>
<div class="margin-b-4-px padding-x-4-px text-right"> <div class="margin-b-4-px padding-x-4-px text-right">
{{ 'other' | trans | u.title }} {{ 'other' | trans | u.title }}
</div> </div>
<div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default"> <div class="padding-t-16-px padding-b-8-px padding-x-24-px border-top-default">
{% if sensitive %} {% if sensitive.checked %}
<input name="sensitive" id="sensitive" type="checkbox" value="true" checked="checked" /> <input name="sensitive" id="sensitive" type="checkbox" value="true" checked="checked" />
{% else %} {% else %}
<input name="sensitive" id="sensitive" type="checkbox" value="true" /> <input name="sensitive" id="sensitive" type="checkbox" value="true" />
{% endif %} {% endif %}
<label class="margin-x-4-px" for="sensitive"> <label class="margin-x-4-px" for="sensitive">
{{ 'Sensitive' | trans }} {{ 'Sensitive' | trans }}
<span class="text-color-night">
({{ sensitive.total }})
</span>
</label> </label>
</div> </div>
</div> </div>

Loading…
Cancel
Save