Skip to content

Commit 601e0fd

Browse files
committed
Gestion des Super Apéros depuis le BO
1 parent 1310301 commit 601e0fd

File tree

21 files changed

+866
-49
lines changed

21 files changed

+866
-49
lines changed

app/config/packages/backoffice_menu.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ parameters:
100100
- admin_site_articles_list
101101
- admin_site_articles_add
102102
- admin_site_articles_edit
103+
super_apero_infos:
104+
nom: 'Super Apéro'
105+
niveau: 'ROLE_ADMIN'
106+
url: '/admin/super-apero'
107+
extra_routes:
108+
- admin_super_apero_list
109+
- admin_super_apero_add
110+
- admin_super_apero_edit
103111
forum:
104112
nom: 'Évènements'
105113
niveau: 'ROLE_FORUM'

app/config/packages/doctrine.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ doctrine:
2929
is_bundle: false
3030
dir: '%kernel.project_dir%/../sources/AppBundle/Site/Entity'
3131
prefix: 'AppBundle\Site\Entity'
32+
SuperApero:
33+
type: attribute
34+
is_bundle: false
35+
dir: '%kernel.project_dir%/../sources/AppBundle/SuperApero/Entity'
36+
prefix: 'AppBundle\SuperApero\Entity'
3237

3338
when@test:
3439
doctrine:

app/config/routing/admin.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ admin_antennes:
4545
resource: "admin_antennes.yml"
4646
prefix: /antennes
4747

48+
admin_super_apero:
49+
resource: "admin_super_apero.yml"
50+
prefix: /super-apero
51+
4852
admin_members_reporting:
4953
path: /members/reporting
5054
defaults: {_controller: AppBundle\Controller\MembershipAdmin\ReportingAction}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
admin_super_apero_list:
2+
path: /
3+
defaults: {_controller: AppBundle\Controller\Admin\SuperApero\ListAction}
4+
5+
admin_super_apero_add:
6+
path: /add
7+
defaults: {_controller: AppBundle\Controller\Admin\SuperApero\AddAction}
8+
9+
admin_super_apero_edit:
10+
path: /edit/{id}
11+
defaults: {_controller: AppBundle\Controller\Admin\SuperApero\EditAction}
12+
requirements:
13+
id: '\d+'
14+
15+
admin_super_apero_delete:
16+
path: /delete/{id}
17+
defaults: {_controller: AppBundle\Controller\Admin\SuperApero\DeleteAction}
18+
methods: [POST]
19+
requirements:
20+
id: '\d+'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Phinx\Migration\AbstractMigration;
6+
7+
final class CreateSuperAperoTables extends AbstractMigration
8+
{
9+
public function change(): void
10+
{
11+
$this->table('super_apero')
12+
->addColumn('date', 'date', ['null' => false])
13+
->create();
14+
15+
$this->table('super_apero_meetup')
16+
->addColumn('super_apero_id', 'integer', [
17+
'null' => false,
18+
'signed' => false,
19+
])
20+
->addColumn('antenne', 'string', ['limit' => 255, 'null' => false])
21+
->addColumn('meetup_id', 'integer', ['null' => true])
22+
->addColumn('description', 'text', ['null' => true])
23+
->addForeignKey('super_apero_id', 'super_apero', 'id', ['delete' => 'CASCADE', 'update' => 'NO_ACTION'])
24+
->create();
25+
}
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Controller\Admin\SuperApero;
6+
7+
use AppBundle\AuditLog\Audit;
8+
use AppBundle\SuperApero\Entity\SuperApero;
9+
use AppBundle\SuperApero\Entity\Repository\SuperAperoRepository;
10+
use AppBundle\SuperApero\Form\SuperAperoType;
11+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
final class AddAction extends AbstractController
16+
{
17+
public function __construct(
18+
private readonly SuperAperoRepository $superAperoRepository,
19+
private readonly Audit $audit,
20+
) {}
21+
22+
public function __invoke(Request $request): Response
23+
{
24+
$superApero = new SuperApero();
25+
$form = $this->createForm(SuperAperoType::class, $superApero);
26+
27+
$form->handleRequest($request);
28+
29+
if ($form->isSubmitted() && $form->isValid()) {
30+
$this->superAperoRepository->save($superApero);
31+
$this->audit->log('Ajout du Super Apéro ' . $superApero->annee());
32+
$this->addFlash('notice', 'Le Super Apéro ' . $superApero->annee() . ' a été ajouté');
33+
34+
return $this->redirectToRoute('admin_super_apero_list');
35+
}
36+
37+
return $this->render('admin/super_apero/form.html.twig', [
38+
'form' => $form->createView(),
39+
'formTitle' => 'Ajouter un Super Apéro',
40+
'submitLabel' => 'Ajouter',
41+
]);
42+
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Controller\Admin\SuperApero;
6+
7+
use AppBundle\AuditLog\Audit;
8+
use AppBundle\SuperApero\Entity\Repository\SuperAperoRepository;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\RedirectResponse;
11+
use Symfony\Component\HttpFoundation\Request;
12+
13+
final class DeleteAction extends AbstractController
14+
{
15+
public function __construct(
16+
private readonly SuperAperoRepository $superAperoRepository,
17+
private readonly Audit $audit,
18+
) {}
19+
20+
public function __invoke(int $id, Request $request): RedirectResponse
21+
{
22+
if (!$this->isCsrfTokenValid('super_apero_delete', $request->request->getString('_token'))) {
23+
$this->addFlash('error', 'Token invalide');
24+
25+
return $this->redirectToRoute('admin_super_apero_list');
26+
}
27+
28+
$superApero = $this->superAperoRepository->find($id);
29+
30+
if ($superApero === null) {
31+
throw $this->createNotFoundException('Super apéro non trouvé');
32+
}
33+
34+
$annee = $superApero->annee();
35+
$this->superAperoRepository->delete($superApero);
36+
$this->audit->log('Suppression du Super Apéro ' . $annee);
37+
$this->addFlash('notice', 'Le Super Apéro ' . $annee . ' a été supprimé');
38+
39+
return $this->redirectToRoute('admin_super_apero_list');
40+
}
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Controller\Admin\SuperApero;
6+
7+
use AppBundle\AuditLog\Audit;
8+
use AppBundle\SuperApero\Entity\Repository\SuperAperoRepository;
9+
use AppBundle\SuperApero\Entity\SuperApero;
10+
use AppBundle\SuperApero\Form\SuperAperoType;
11+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
final class EditAction extends AbstractController
16+
{
17+
public function __construct(
18+
private readonly SuperAperoRepository $superAperoRepository,
19+
private readonly Audit $audit,
20+
) {}
21+
22+
public function __invoke(int $id, Request $request): Response
23+
{
24+
$superApero = $this->superAperoRepository->find($id);
25+
26+
if (!$superApero instanceof SuperApero) {
27+
throw $this->createNotFoundException('Super apéro non trouvé');
28+
}
29+
30+
$form = $this->createForm(SuperAperoType::class, $superApero);
31+
$form->handleRequest($request);
32+
33+
if ($form->isSubmitted() && $form->isValid()) {
34+
$this->superAperoRepository->save($superApero);
35+
$this->audit->log('Modification du Super Apéro ' . $superApero->annee());
36+
$this->addFlash('notice', 'Le Super Apéro ' . $superApero->annee() . ' a été modifié');
37+
38+
return $this->redirectToRoute('admin_super_apero_list');
39+
}
40+
41+
return $this->render('admin/super_apero/form.html.twig', [
42+
'form' => $form->createView(),
43+
'formTitle' => 'Modifier le Super Apéro ' . $superApero->annee(),
44+
'submitLabel' => 'Modifier',
45+
]);
46+
}
47+
}
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+
namespace AppBundle\Controller\Admin\SuperApero;
6+
7+
use AppBundle\SuperApero\Entity\Repository\SuperAperoRepository;
8+
use AppBundle\SuperApero\Entity\SuperApero;
9+
use Psr\Clock\ClockInterface;
10+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11+
use Symfony\Component\HttpFoundation\Response;
12+
13+
final class ListAction extends AbstractController
14+
{
15+
public function __construct(
16+
private readonly SuperAperoRepository $superAperoRepository,
17+
private readonly ClockInterface $clock,
18+
) {}
19+
20+
public function __invoke(): Response
21+
{
22+
$currentYear = (int) $this->clock->now()->format('Y');
23+
24+
return $this->render('admin/super_apero/index.html.twig', [
25+
'aperos' => $this->superAperoRepository->getAllSortedByYear(),
26+
'currentYear' => $currentYear,
27+
'hasSuperAperoForCurrentYear' => $this->superAperoRepository->findOneByYear($currentYear) instanceof SuperApero,
28+
]);
29+
}
30+
}

sources/AppBundle/Controller/Website/Static/SuperAperoAction.php

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,30 @@
44

55
namespace AppBundle\Controller\Website\Static;
66

7+
use AppBundle\SuperApero\Entity\Repository\SuperAperoRepository;
78
use AppBundle\Twig\ViewRenderer;
8-
use Symfony\Component\DependencyInjection\Attribute\Autowire;
9+
use Symfony\Component\HttpFoundation\RedirectResponse;
910
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1012

1113
final readonly class SuperAperoAction
1214
{
1315
public function __construct(
1416
private ViewRenderer $view,
15-
#[Autowire('%super_apero_csv_url%')]
16-
private string $superAperoCsvUrl,
17+
private SuperAperoRepository $superAperoRepository,
18+
private UrlGeneratorInterface $urlGenerator,
1719
) {}
1820

1921
public function __invoke(): Response
2022
{
21-
return $this->view->render('site/superapero.html.twig', [
22-
'aperos' => $this->getAperos($this->superAperoCsvUrl),
23-
]);
24-
}
23+
$superApero = $this->superAperoRepository->findActive();
2524

26-
/**
27-
* @return array{code: (string | null), content: (string | null), meetup_id?: (string | null)}[]
28-
*/
29-
protected function getAperos(string $url): array
30-
{
31-
$fp = fopen($url, 'rb');
32-
if (!$fp) {
33-
throw new \RuntimeException("Error opening spreadsheet");
25+
if ($superApero === null) {
26+
return new RedirectResponse($this->urlGenerator->generate('home'));
3427
}
3528

36-
$aperos = [];
37-
38-
while (false !== ($row = fgetcsv($fp))) {
39-
if (trim((string) $row[0]) === '') {
40-
continue;
41-
}
42-
43-
[$code, $meeetupId, $content] = $row;
44-
45-
$apero = [
46-
'code' => mb_strtolower((string) $code),
47-
'content' => $content,
48-
];
49-
50-
if (strlen(trim((string) $meeetupId)) !== 0) {
51-
$apero['meetup_id'] = $meeetupId;
52-
}
53-
54-
$aperos[] = $apero;
55-
}
56-
57-
return $aperos;
29+
return $this->view->render('site/superapero.html.twig', [
30+
'superApero' => $superApero,
31+
]);
5832
}
5933
}

0 commit comments

Comments
 (0)