Skip to content

Commit

Permalink
added troops
Browse files Browse the repository at this point in the history
  • Loading branch information
Lung committed Sep 21, 2023
1 parent 4f06726 commit 29a4e5d
Show file tree
Hide file tree
Showing 27 changed files with 671 additions and 123 deletions.
15 changes: 15 additions & 0 deletions public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ li.step.is-active::after {
margin-bottom: 1rem;
}

.form-group-middle {
display: flex;
flex-flow: column;
align-items: center;
row-gap: 10px;
}

.form-control {
border: none;
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
Expand Down Expand Up @@ -306,10 +313,18 @@ li.step.is-active::after {
color: var(--color-accent);
}

.hightlight {
color: var(--color-accent);
}

.btn[type=submit] {
width: 100%;
}

.btn-small[type=submit] {
width: inherit;
}

.btn-mini[type=submit] {
width: inherit;
}
Expand Down
31 changes: 28 additions & 3 deletions src/Application/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use kissj\Event\EventController;
use kissj\Export\ExportController;
use kissj\Middleware\AdminsOnlyMiddleware;
use kissj\Middleware\CheckPatrolLeaderParticipants;
use kissj\Middleware\CheckLeaderParticipants;
use kissj\Middleware\ChoosedRoleOnlyMiddleware;
use kissj\Middleware\LoggedOnlyMiddleware;
use kissj\Middleware\NonChoosedRoleOnlyMiddleware;
use kissj\Middleware\NonLoggedOnlyMiddleware;
use kissj\Middleware\OpenStatusOnlyMiddleware;
use kissj\Middleware\PatrolLeadersOnlyMiddleware;
use kissj\Middleware\TroopLeadersOnlyMiddleware;
use kissj\Middleware\TroopParticipantsOnlyMiddleware;
use kissj\Participant\Admin\AdminController;
use kissj\Participant\ParticipantController;
use kissj\Participant\Patrol\PatrolController;
use kissj\Participant\Troop\TroopController;
use kissj\User\UserController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand Down Expand Up @@ -85,7 +88,7 @@ public function addRoutesInto(App $app): App

$app->group('/patrol', function (RouteCollectorProxy $app) {
$app->get('/participant/{participantId}/show', PatrolController::class . '::showParticipant')
->setName('p-show');
->setName('p-show'); // TODO check if CheckLeaderParticipants is needed here

$app->group('', function (RouteCollectorProxy $app) {
$app->get('/closeRegistration', PatrolController::class . '::showCloseRegistration')
Expand Down Expand Up @@ -115,9 +118,31 @@ public function addRoutesInto(App $app): App

$app->post('/delete', PatrolController::class . '::deleteParticipant')
->setName('p-delete');
})->add(CheckPatrolLeaderParticipants::class);
})->add(CheckLeaderParticipants::class);
})->add(OpenStatusOnlyMiddleware::class);
})->add(PatrolLeadersOnlyMiddleware::class);

$app->group('/troop', function (RouteCollectorProxy $app) {
$app->post('/tieParticipantToTroopByLeader', TroopController::class . '::tieParticipantToTroopByLeader')
->setName('tie-tp-by-tl')
->add(TroopLeadersOnlyMiddleware::class)
->add(OpenStatusOnlyMiddleware::class);

$app->post('/tieParticipantToTroopByParticipant', TroopController::class . '::tieParticipantToTroopByParticipant')
->setName('tie-tp-by-tp')
->add(TroopParticipantsOnlyMiddleware::class);

$app->group('/participant/{participantId}', function (RouteCollectorProxy $app) {
$app->get('/show', TroopController::class . '::showParticipant')
->setName('tp-show');

$app->get('/showUntie', TroopController::class . '::showUntieParticipant')
->setName('tp-showUntie');

$app->post('/untie', TroopController::class . '::untieParticipant')
->setName('tp-untie');
})->add(CheckLeaderParticipants::class);
});

// TODO refactor for patrols
$app->group('/participant', function (RouteCollectorProxy $app) {
Expand Down
1 change: 1 addition & 0 deletions src/FlashMessages/FlashMessagesBySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace kissj\FlashMessages;

// TODO add translator to this class and make all messages translatable
class FlashMessagesBySession implements FlashMessagesInterface
{
public function info(string $message): void
Expand Down
71 changes: 71 additions & 0 deletions src/Middleware/CheckLeaderParticipants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace kissj\Middleware;

use kissj\FlashMessages\FlashMessagesInterface;
use kissj\Participant\ParticipantRepository;
use kissj\Participant\Patrol\PatrolLeader;
use kissj\Participant\Patrol\PatrolService;
use kissj\Participant\Troop\TroopLeader;
use kissj\Participant\Troop\TroopParticipantRepository;
use kissj\Participant\Troop\TroopService;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as ResponseHandler;
use Slim\Routing\RouteContext;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* participants actions are allowed only for their Patrol Leader or for their Troop Leader
*/
class CheckLeaderParticipants extends BaseMiddleware
{
public function __construct(
private readonly PatrolService $patrolService,
private readonly TroopService $troopService,
private readonly TroopParticipantRepository $troopParticipantRepository,
private readonly ParticipantRepository $participantRepository,
private readonly FlashMessagesInterface $flashMessages,
private readonly TranslatorInterface $translator,
) {
}

public function process(Request $request, ResponseHandler $handler): Response
{
$route = RouteContext::fromRequest($request)->getRoute();
if ($route === null) {
throw new \RuntimeException('Cannot access route in CheckLeaderParticipants middleware');
}

$participantId = (int)$route->getArgument('participantId');
$leader = $this->participantRepository->getParticipantFromUser($this->getUser($request));

if ($leader instanceof PatrolLeader) {
if (!$this->patrolService->patrolParticipantBelongsPatrolLeader(
$this->patrolService->getPatrolParticipant($participantId),
$leader,
)) {
$this->flashMessages->error($this->translator->trans('flash.error.wrongPatrol'));

return $this->createRedirectResponse($request, 'dashboard');
}
} elseif ($leader instanceof TroopLeader) {
if (!$this->troopService->troopParticipantBelongsTroopLeader(
$this->troopParticipantRepository->get($participantId),
$leader,
)) {
$this->flashMessages->error($this->translator->trans('flash.error.wrongTroop'));

return $this->createRedirectResponse($request, 'dashboard');
}
} else {
$this->flashMessages->error($this->translator->trans('flash.error.notLeader'));

return $this->createRedirectResponse($request, 'dashboard');
}

return $handler->handle($request);
}
}
46 changes: 0 additions & 46 deletions src/Middleware/CheckPatrolLeaderParticipants.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Middleware/PatrolLeadersOnlyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function process(Request $request, ResponseHandler $handler): Response
) {
$this->flashMessages->error($this->translator->trans('flash.error.plOnly'));

return $this->createRedirectResponse($request, 'loginAskEmail');
return $this->createRedirectResponse($request, 'landing');
}

return $handler->handle($request);
Expand Down
38 changes: 38 additions & 0 deletions src/Middleware/TroopLeadersOnlyMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace kissj\Middleware;

use kissj\FlashMessages\FlashMessagesInterface;
use kissj\Participant\ParticipantRepository;
use kissj\Participant\ParticipantRole;
use kissj\User\User;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as ResponseHandler;
use Symfony\Contracts\Translation\TranslatorInterface;

class TroopLeadersOnlyMiddleware extends BaseMiddleware
{
public function __construct(
private readonly FlashMessagesInterface $flashMessages,
private readonly TranslatorInterface $translator,
private readonly ParticipantRepository $participantRepository,
) {
}

public function process(Request $request, ResponseHandler $handler): Response
{
$user = $request->getAttribute('user');

if (
$user instanceof User
&& $this->participantRepository->getParticipantFromUser($user)->role !== ParticipantRole::TroopLeader
) {
$this->flashMessages->error($this->translator->trans('flash.error.tlOnly'));

return $this->createRedirectResponse($request, 'landing');
}

return $handler->handle($request);
}
}
38 changes: 38 additions & 0 deletions src/Middleware/TroopParticipantsOnlyMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace kissj\Middleware;

use kissj\FlashMessages\FlashMessagesInterface;
use kissj\Participant\ParticipantRepository;
use kissj\Participant\ParticipantRole;
use kissj\User\User;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as ResponseHandler;
use Symfony\Contracts\Translation\TranslatorInterface;

class TroopParticipantsOnlyMiddleware extends BaseMiddleware
{
public function __construct(
private readonly FlashMessagesInterface $flashMessages,
private readonly TranslatorInterface $translator,
private readonly ParticipantRepository $participantRepository,
) {
}

public function process(Request $request, ResponseHandler $handler): Response
{
$user = $request->getAttribute('user');

if (
$user instanceof User
&& $this->participantRepository->getParticipantFromUser($user)->role !== ParticipantRole::TroopParticipant
) {
$this->flashMessages->error($this->translator->trans('flash.error.tpOnly'));

return $this->createRedirectResponse($request, 'landing');
}

return $handler->handle($request);
}
}
8 changes: 0 additions & 8 deletions src/Participant/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,6 @@ public function showPayments(
$event,
$user,
),
'approvedTroopParticipants' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::TroopParticipant],
[UserStatus::Approved],
$event,
$user,
),
]);
}

Expand Down Expand Up @@ -331,8 +325,6 @@ public function confirmPayment(int $paymentId, User $user, Request $request, Res
$this->logger->info('Payment ID ' . $paymentId
. ' cannot be confirmed from admin with event id ' . $user->event->id);
} else {
$participant->registrationCloseDate = DateTimeUtils::getDateTime();
$this->participantRepository->persist($participant);
$this->paymentService->confirmPayment($payment);
$this->flashMessages->success($this->translator->trans('flash.success.confirmPayment'));
$this->logger->info('Payment ID ' . $paymentId . ' manually confirmed as paid');
Expand Down
6 changes: 3 additions & 3 deletions src/Participant/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*
* @property int $id
* @property User|null $user m:hasOne
* @property ParticipantRole|null $role m:passThru(roleFromString|roleToString) needed for DB working (see Mapper.php)
* @property string|null $patrolName
* @property ParticipantRole|null $role m:passThru(roleFromString|roleToString) #needed for DB working (see Mapper.php)
* @property string|null $patrolName #used for troops too # TODO move to PatrolLeader + TroopLeader
* @property string|null $contingent
* @property string|null $firstName
* @property string|null $lastName
Expand Down Expand Up @@ -71,7 +71,7 @@ class Participant extends EntityDatetime
protected function initDefaults(): void
{
parent::initDefaults();
$this->tieCode = $this->generateTieCode(6); // TODO check if another code exists
$this->tieCode = $this->generateTieCode(6); // TODO check if another code exists in DB
}

public function setUser(User $user): void
Expand Down
5 changes: 5 additions & 0 deletions src/Participant/ParticipantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use kissj\Participant\Patrol\PatrolLeader;
use kissj\Participant\Patrol\PatrolParticipant;
use kissj\Participant\Patrol\PatrolParticipantRepository;
use kissj\Participant\Troop\TroopLeader;
use kissj\Participant\Troop\TroopParticipantRepository;
use kissj\User\User;
use kissj\User\UserStatus;
use Psr\Http\Message\ResponseInterface as Response;
Expand All @@ -20,6 +22,7 @@ public function __construct(
private readonly ParticipantService $participantService,
private readonly ParticipantRepository $participantRepository,
private readonly PatrolParticipantRepository $patrolParticipantRepository,
private readonly TroopParticipantRepository $troopParticipantRepository,
) {
}

Expand Down Expand Up @@ -96,6 +99,8 @@ private function getTemplateData(Participant $participant): array
$participants = [];
if ($participant instanceof PatrolLeader) {
$participants = $this->patrolParticipantRepository->findAllPatrolParticipantsForPatrolLeader($participant);
} elseif ($participant instanceof TroopLeader) {
$participants = $this->troopParticipantRepository->findAllTroopParticipantsForTroopLeader($participant);
}

return [
Expand Down
Loading

0 comments on commit 29a4e5d

Please sign in to comment.