From 73e49e99cbe257f414bc795ab7682820a078138a Mon Sep 17 00:00:00 2001 From: Nicolas Debrigode Date: Mon, 22 Jan 2024 16:30:59 +0100 Subject: [PATCH] add filter tag --- config/packages/twig.yaml | 2 ++ public/assets/custom.css | 5 +++++ src/Controller/IndexController.php | 25 ++++++++++++++++++++++--- src/Form/LegendForm.php | 5 ++++- src/Repository/GetAsDataRepository.php | 4 ++-- src/Repository/KnowlinksRepository.php | 12 ++++++++++++ templates/core/form/_theme.html.twig | 11 +++++++++++ templates/core/legend.html.twig | 24 +++++++++++++----------- 8 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 templates/core/form/_theme.html.twig diff --git a/config/packages/twig.yaml b/config/packages/twig.yaml index e6b6370..3111490 100644 --- a/config/packages/twig.yaml +++ b/config/packages/twig.yaml @@ -7,3 +7,5 @@ twig: decimals: 2 decimal_point: ',' thousands_separator: ' ' + form_themes: + - 'core/form/_theme.html.twig' diff --git a/public/assets/custom.css b/public/assets/custom.css index fe5716f..37c8126 100644 --- a/public/assets/custom.css +++ b/public/assets/custom.css @@ -24,3 +24,8 @@ .sticky-top-legend { top: 5px !important; } + +.form-check { + min-height: unset !important; + margin-bottom: unset !important; +} diff --git a/src/Controller/IndexController.php b/src/Controller/IndexController.php index 339b1ed..ece2118 100644 --- a/src/Controller/IndexController.php +++ b/src/Controller/IndexController.php @@ -44,9 +44,15 @@ class IndexController extends BaseController $form = $this->createForm(LegendForm::class); $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $data = $asDataRepository::get($this->base_data['top'], null, (array) $form->getData()); + } else { + $data = $asDataRepository::get($this->base_data['top']); + } + return $this->render('pages/index.html.twig', [ 'base_data' => $this->base_data, - 'data' => $asDataRepository::get($this->base_data['top']), + 'data' => $data, 'knownlinks' => KnowlinksRepository::get(), 'form' => [ 'legend' => $form->createView(), @@ -57,9 +63,10 @@ class IndexController extends BaseController #[Route( path: '/{topinterval}', name: 'index_topinterval', - methods: ['GET'], + methods: ['GET|POST'], )] public function indexTopInterval( + Request $request, ConfigApplication $Config, GetAsDataRepository $asDataRepository, string $topinterval, @@ -70,11 +77,23 @@ class IndexController extends BaseController $Config::getAsStatsConfigTopInterval()[$topinterval]['label'] ); + $form = $this->createForm(LegendForm::class); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $data = $asDataRepository::get($this->base_data['top'], $topinterval, (array) $form->getData()); + } else { + $data = $asDataRepository::get($this->base_data['top'], $topinterval); + } + return $this->render('pages/index.html.twig', [ 'base_data' => $this->base_data, - 'data' => $asDataRepository::get($this->base_data['top'], $topinterval), + 'data' => $data, 'hours' => $Config::getAsStatsConfigTopInterval()[$topinterval]['label'], 'knownlinks' => KnowlinksRepository::get(), + 'form' => [ + 'legend' => $form->createView(), + ], ]); } } diff --git a/src/Form/LegendForm.php b/src/Form/LegendForm.php index 76b2bbd..ed9ffa7 100644 --- a/src/Form/LegendForm.php +++ b/src/Form/LegendForm.php @@ -20,9 +20,12 @@ class LegendForm extends AbstractType foreach (KnowlinksRepository::get() as $knowlink) { $builder ->add(\sprintf('%s', $knowlink['tag']), CheckboxType::class, [ - 'label' => false, + 'label' => $knowlink['descr'], 'translation_domain' => false, 'required' => false, + 'label_attr' => [ + 'class' => 'small float-left', + ], ]) ; } diff --git a/src/Repository/GetAsDataRepository.php b/src/Repository/GetAsDataRepository.php index 9cb1e80..599306e 100644 --- a/src/Repository/GetAsDataRepository.php +++ b/src/Repository/GetAsDataRepository.php @@ -16,7 +16,7 @@ class GetAsDataRepository * @throws Exception * @throws DbErrorException */ - public static function get(int $top, ?string $topInterval = null): array + public static function get(int $top, ?string $topInterval = null, array $selectedLinks = []): array { if (0 === $top) { return []; @@ -33,7 +33,7 @@ class GetAsDataRepository $data = new DbAsStatsRepository($dbName); $asInfoRepository = new DbAsInfoRepository(); - foreach ($data->getASStatsTop($top, []) as $as => $nbytes) { + foreach ($data->getASStatsTop($top, KnowlinksRepository::select($selectedLinks)) as $as => $nbytes) { $return['asinfo'][$as]['info'] = $asInfoRepository->getAsInfo($as); $return['asinfo'][$as]['v4'] = [ diff --git a/src/Repository/KnowlinksRepository.php b/src/Repository/KnowlinksRepository.php index a60a736..7f58438 100644 --- a/src/Repository/KnowlinksRepository.php +++ b/src/Repository/KnowlinksRepository.php @@ -60,4 +60,16 @@ class KnowlinksRepository return $knownlinks; } + + public static function select(array $selectedLink): array + { + $selected_links = []; + + foreach ($selectedLink as $tag => $check) { + if ($check) { + $selected_links[] = $tag; + } + } + return $selected_links; + } } diff --git a/templates/core/form/_theme.html.twig b/templates/core/form/_theme.html.twig new file mode 100644 index 0000000..7d21615 --- /dev/null +++ b/templates/core/form/_theme.html.twig @@ -0,0 +1,11 @@ +{% use "bootstrap_5_layout.html.twig" %} + +{# Checkbox #} + +{% block checkbox_row -%} + {#--#} + {{- form_widget(form) -}} + {{- form_help(form) -}} + {{- form_errors(form) -}} + +{%- endblock checkbox_row %} diff --git a/templates/core/legend.html.twig b/templates/core/legend.html.twig index 03b91db..b622772 100644 --- a/templates/core/legend.html.twig +++ b/templates/core/legend.html.twig @@ -1,17 +1,24 @@
+ {{ form_start(form.legend) }}
- {{ form_start(form.legend) }}

Legend

- +
{% if knownlinks is defined %} {% for link in knownlinks %} + - - {% endfor %} {% endif %} @@ -39,7 +38,10 @@
+ {% for field in form.legend %} + {% if field.vars.name == link.tag %} + {{ form_row(field) }} + {% endif %} + {% endfor %} + @@ -24,14 +31,6 @@
{{ link.descr }} - {% for field in form.legend %} - {% if field.vars.name == link.tag %} - {{ form_widget(field) }} - {% endif %} - {% endfor %} -
- {{ form_end(form.legend) }} +
+ {{ form_end(form.legend) }}