Skip to content

Commit 2b22350

Browse files
committed
Add a "date" filter to analytics
1 parent 811c0a7 commit 2b22350

7 files changed

Lines changed: 138 additions & 10 deletions

File tree

config/packages/twig.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
twig:
2+
default_path: '%kernel.project_dir%/templates'
3+
form_themes:
4+
- '@EasyAdmin/form/bootstrap_4.html.twig'
5+
- 'form/analytics_filter_form.html.twig'

src/Controller/AnalyticsController.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,50 @@
1313

1414
namespace App\Controller;
1515

16+
use App\DTO\AnalyticsFilters;
17+
use App\Form\Type\AnalyticsFiltersType;
1618
use App\Highcharts\Chart\TagAmountChart;
1719
use App\Highcharts\Chart\TagUsageChart;
1820
use App\Repository\OperationRepository;
1921
use App\Repository\TagRepository;
22+
use Symfony\Component\Form\FormFactoryInterface;
23+
use Symfony\Component\HttpFoundation\Request;
2024
use Symfony\Component\HttpFoundation\Response;
2125
use Symfony\Component\Routing\Annotation\Route;
2226
use Twig\Environment;
2327

2428
class AnalyticsController
2529
{
2630
private Environment $twig;
31+
private FormFactoryInterface $formFactory;
2732
private TagRepository $tagRepository;
2833
private OperationRepository $operationRepository;
2934

3035
public function __construct(
3136
Environment $twig,
37+
FormFactoryInterface $formFactory,
3238
TagRepository $tagRepository,
3339
OperationRepository $operationRepository
3440
) {
3541
$this->twig = $twig;
42+
$this->formFactory = $formFactory;
3643
$this->tagRepository = $tagRepository;
3744
$this->operationRepository = $operationRepository;
3845
}
3946

4047
/**
4148
* @Route("/admin/analytics", name="analytics")
4249
*/
43-
public function analytics(): Response
50+
public function analytics(Request $request): Response
4451
{
45-
$operations = $this->operationRepository->findWithTags();
52+
$filters = new AnalyticsFilters();
53+
$form = $this->formFactory->createNamed('', AnalyticsFiltersType::class, $filters);
54+
$form->handleRequest($request);
55+
56+
$operations = $this->operationRepository->findForAnalytics($filters);
4657

4758
return new Response($this->twig->render('analytics.html.twig', [
59+
'filters_form' => $form->createView(),
4860
'charts' => [
4961
new TagUsageChart($operations),
5062
new TagAmountChart($operations),

src/DTO/AnalyticsFilters.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Compotes package.
7+
*
8+
* (c) Alex "Pierstoval" Rock <pierstoval@gmail.com>.
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace App\DTO;
15+
16+
use DateTime;
17+
use Symfony\Component\Validator\Constraints as Assert;
18+
19+
class AnalyticsFilters
20+
{
21+
/**
22+
* @Assert\LessThanOrEqual(propertyPath="endDate")
23+
*/
24+
public ?DateTime $startDate = null;
25+
26+
/**
27+
* @Assert\GreaterThanOrEqual(propertyPath="startDate")
28+
*/
29+
public ?DateTime $endDate = null;
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Compotes package.
7+
*
8+
* (c) Alex "Pierstoval" Rock <pierstoval@gmail.com>.
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace App\Form\Type;
15+
16+
use App\DTO\AnalyticsFilters;
17+
use Symfony\Component\Form\AbstractType;
18+
use Symfony\Component\Form\Extension\Core\Type\DateType;
19+
use Symfony\Component\Form\FormBuilderInterface;
20+
use Symfony\Component\OptionsResolver\OptionsResolver;
21+
22+
class AnalyticsFiltersType extends AbstractType
23+
{
24+
public function buildForm(FormBuilderInterface $builder, array $options): void
25+
{
26+
$builder
27+
->add('startDate', DateType::class, [
28+
'widget' => 'single_text',
29+
'required' => false,
30+
])
31+
->add('endDate', DateType::class, [
32+
'widget' => 'single_text',
33+
'required' => false,
34+
])
35+
;
36+
}
37+
38+
public function configureOptions(OptionsResolver $resolver): void
39+
{
40+
$resolver
41+
->setDefaults([
42+
'data_class' => AnalyticsFilters::class,
43+
'method' => 'get',
44+
'csrf_protection' => false,
45+
'allow_extra_fields' => true,
46+
])
47+
;
48+
}
49+
50+
public function getBlockPrefix()
51+
{
52+
return 'analytics_filter';
53+
}
54+
}

src/Repository/OperationRepository.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace App\Repository;
1515

16+
use App\DTO\AnalyticsFilters;
1617
use App\Entity\Operation;
1718
use DateTimeImmutable;
1819
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
@@ -62,15 +63,28 @@ public function monthIsPopulated(DateTimeImmutable $month): bool
6263
/**
6364
* @return Operation[]
6465
*/
65-
public function findWithTags(): array
66+
public function findForAnalytics(AnalyticsFilters $filters): array
6667
{
67-
return $this->_em->createQuery(
68-
<<<DQL
69-
SELECT operation, tags
70-
FROM {$this->_entityName} as operation
71-
LEFT JOIN operation.tags as tags
72-
DQL
73-
)
68+
$qb = $this->createQueryBuilder('operation')
69+
->addSelect('tags')
70+
->leftJoin('operation.tags', 'tags')
71+
;
72+
73+
if ($filters->startDate) {
74+
$qb
75+
->andWhere('operation.operationDate >= :startDate')
76+
->setParameter('startDate', $filters->startDate)
77+
;
78+
}
79+
if ($filters->endDate) {
80+
$qb
81+
->andWhere('operation.operationDate <= :endDate')
82+
->setParameter('endDate', $filters->endDate)
83+
;
84+
}
85+
86+
return $qb
87+
->getQuery()
7488
->getResult()
7589
;
7690
}

templates/analytics.html.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
{% endblock %}
66

77
{% block main %}
8+
9+
{{ form(filters_form) }}
10+
811
<ul class="nav nav-tabs" role="tablist">
912
{% for chart in charts %}
1013
<li class="nav-item">
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
{% block analytics_filter_widget %}
3+
{{ form_start(form) }}
4+
<div class="row">
5+
<div class="col s6">{{ form_row(form.startDate) }}</div>
6+
<div class="col s6">{{ form_row(form.endDate) }}</div>
7+
</div>
8+
<button type="submit" class="btn">Submit</button>
9+
{{ form_end(form) }}
10+
{% endblock %}

0 commit comments

Comments
 (0)