Skip to content

Commit

Permalink
Modification of the form for editing the fact of the camp. `IsAccepte…
Browse files Browse the repository at this point in the history
…d` is a newly required parameter
  • Loading branch information
Dorazil committed Oct 10, 2023
1 parent 6df0ab7 commit 6ae93b7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ protected function createComponentParticipantList(): ParticipantList
true,
$this->isAllowParticipantUpdate,
$this->isAllowParticipantDelete,
$this->event->isOnlineLogin(),
);

$control->onUpdate[] = function (array $updates): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class EditParticipantDialog extends Dialog
public array $onUpdate = [];

/** @param array<int, Participant> $participants */
public function __construct(private array $participants, private bool $isAllowedDaysUpdate, private bool $isAccountAllowed, private bool $isRepaymentAllowed)
public function __construct(private array $participants, private bool $isAllowedDaysUpdate, private bool $isAccountAllowed, private bool $isRepaymentAllowed, private bool $isOnlineLogin)
{
}

Expand Down
29 changes: 21 additions & 8 deletions app/AccountancyModule/Components/Participants/ParticipantList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Forms\BaseForm;
use Model\DTO\Participant\Participant;
use Model\DTO\Participant\UpdateParticipant;
use Model\Participant\ParticipantNotFound;
use Nette\Application\BadRequestException;
use Nette\Forms\Controls\SubmitButton;
use Nette\Http\IResponse;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function __construct(
protected bool $isAllowIsAccount,
protected bool $isAllowParticipantUpdate,
protected bool $isAllowParticipantDelete,
protected bool $isOnlineLogin,
) {
}

Expand Down Expand Up @@ -162,13 +164,13 @@ public function handleEdit(int $participantId): void

protected function createComponentEditDialog(): EditParticipantDialog
{
$dialog = new EditParticipantDialog($this->participantsById(), $this->isAllowDaysUpdate, $this->isAllowIsAccount, $this->isAllowRepayment);
$dialog = new EditParticipantDialog($this->participantsById(), $this->isAllowDaysUpdate, $this->isAllowIsAccount, $this->isAllowRepayment, $this->isOnlineLogin);

$dialog->onUpdate[] = function (int $participantId, array $fields): void {
$dialog->onUpdate[] = function (int $participantId, array $fields, bool $isAccepted): void {
$changes = [];

foreach ($fields as $field => $value) {
$changes[] = new UpdateParticipant($this->aid, $participantId, $field, (string) $value);
$changes[] = new UpdateParticipant($this->aid, $participantId, $field, (string) $value, $isAccepted);
}

$this->onUpdate($changes);
Expand Down Expand Up @@ -224,25 +226,36 @@ private function massEditSubmitted(SubmitButton $button): void

$values = $button->getForm()->getValues()['edit'];

$changes = [];
$changes = [];
$currentParticipants = [];
foreach ($this->currentParticipants as $key => $p) {
$currentParticipants[$p->id] = $p;
}

foreach ($button->getForm()->getValues()->participantIds as $participantId) {
$participant = $currentParticipants[$participantId] ?? throw new ParticipantNotFound('Cannot find participant from the given data');

if ($values['days'] !== null) {
$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_DAYS, $values['days']);
if ($this->isOnlineLogin && ! $participant->isAccepted()) {
$this->flashMessage(sprintf('Nelze upravit hodnotu dny na táboře. Účastník %s není na tábor přihlášen.', $participant->displayName), 'warning');
} else {
$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_DAYS, $values['days'], $participant->isAccepted());
}
}

if ($values['payment'] !== null) {
$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_PAYMENT, $values['payment']);
$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_PAYMENT, $values['payment'], $participant->isAccepted());
}

if ($values['repayment'] !== null) {
$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_REPAYMENT, $values['repayment']);
$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_REPAYMENT, $values['repayment'], $participant->isAccepted());
}

if (in_array($values['isAccount'], [self::NO_ACTION, null])) {
continue;
}

$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_IS_ACCOUNT, $values['isAccount']);
$changes[] = new UpdateParticipant($this->aid, $participantId, UpdateParticipant::FIELD_IS_ACCOUNT, $values['isAccount'], $participant->isAccepted());
}

$this->onUpdate($changes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ protected function createComponentParticipantList(): ParticipantList
true,
$this->isAllowParticipantUpdate,
false,
false,
);

$control->onUpdate[] = function (array $updates): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ protected function createComponentParticipantList(): ParticipantList
false,
$this->isAllowParticipantUpdate,
$this->isAllowParticipantDelete,
false,
);

$control->onUpdate[] = function (array $updates): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public function create(
bool $isAllowIsAccount,
bool $isAllowParticipantUpdate,
bool $isAllowParticipantDelete,
bool $isOnlineLogin,
): ParticipantList;
}
11 changes: 11 additions & 0 deletions app/model/Participant/Exception/ParticipantNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Model\Participant;

use Exception;

class ParticipantNotFound extends Exception
{
}

0 comments on commit 6ae93b7

Please sign in to comment.