Skip to content

Commit

Permalink
admin groups created (#1117)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Havelka <[email protected]>
  • Loading branch information
GitHubJanHave and Jan Havelka authored Nov 22, 2023
1 parent 757dfa0 commit ae693f8
Show file tree
Hide file tree
Showing 42 changed files with 1,676 additions and 0 deletions.
119 changes: 119 additions & 0 deletions app/AdminModule/ConfigurationModule/Forms/GroupFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

namespace App\AdminModule\ConfigurationModule\Forms;

use App\AdminModule\Forms\BaseFormFactory;
use App\Model\Settings\Commands\SetSettingDateValue;
use App\Model\Settings\Commands\SetSettingStringValue;
use App\Model\Settings\Queries\SettingDateValueQuery;
use App\Model\Settings\Queries\SettingStringValueQuery;
use App\Model\Settings\Settings;
use App\Services\CommandBus;
use App\Services\QueryBus;
use Nette;
use Nette\Application\UI\Form;
use Nette\Utils\DateTime;
use Nextras\FormComponents\Controls\DateControl;
use Nextras\FormsRendering\Renderers\Bs4FormRenderer;
use stdClass;
use Throwable;

use function assert;

/**
* Formulář pro nastavení semináře.
*/
class GroupFormFactory
{
use Nette\SmartObject;

public function __construct(
private BaseFormFactory $baseFormFactory,
private CommandBus $commandBus,
private QueryBus $queryBus,
) {
}

/**
* Vytvoří formulář.
*
* @throws Throwable
*/
public function create(): Form
{
$form = $this->baseFormFactory->create();

$renderer = $form->getRenderer();
assert($renderer instanceof Bs4FormRenderer);
$renderer->wrappers['control']['container'] = 'div class="col-7"';
$renderer->wrappers['label']['container'] = 'div class="col-5 col-form-label"';

$form->addText('groupMinMembers', 'admin.configuration.group_min_members')
->addRule(Form::FILLED, 'admin.configuration.group_min_members_empty');

$form->addText('groupMaxMembers', 'admin.configuration.group_max_members')
->addRule(Form::FILLED, 'admin.configuration.group_max_members_empty');

$groupFillTerm = new DateControl('admin.configuration.group_fill_term');
$groupFillTerm->addRule(Form::FILLED, 'admin.configuration.group_fill_term_empty');
$form->addComponent($groupFillTerm, 'groupFillTerm');



$form->addSubmit('submit', 'admin.common.save');

$form->setDefaults([
'groupMinMembers' => $this->queryBus->handle(new SettingStringValueQuery(Settings::GROUP_MIN_MEMBERS)),
'groupMaxMembers' => $this->queryBus->handle(new SettingStringValueQuery(Settings::GROUP_MAX_MEMBERS)),
'groupFillTerm' => $this->queryBus->handle(new SettingDateValueQuery(Settings::GROUP_FILL_TERM)),
]);

$form->onSuccess[] = [$this, 'processForm'];

return $form;
}

/**
* Zpracuje formulář.
*
* @throws Throwable
*/
public function processForm(Form $form, stdClass $values): void
{
$this->commandBus->handle(new SetSettingStringValue(Settings::GROUP_MIN_MEMBERS, $values->groupMinMembers));
$this->commandBus->handle(new SetSettingStringValue(Settings::GROUP_MAX_MEMBERS, $values->groupMaxMembers));
$this->commandBus->handle(new SetSettingDateValue(Settings::GROUP_FILL_TERM, $values->groupFillTerm));
}

/**
* Ověří, že datum začátku semináře je dříve než konce.
*
* @param DateTime[] $args
*/
public function validateSeminarFromDate(DateControl $field, array $args): bool
{
return $args[0] <= $args[1];
}

/**
* Ověří, že datum konce semináře je později než začátku.
*
* @param DateTime[] $args
*/
public function validateSeminarToDate(DateControl $field, array $args): bool
{
return $args[0] >= $args[1];
}

/**
* Ověří, že datum uzavření registrace je dříve než začátek semináře.
*
* @param DateTime[] $args
*/
public function validateEditRegistrationTo(DateControl $field, array $args): bool
{
return $args[0] < $args[1];
}
}
37 changes: 37 additions & 0 deletions app/AdminModule/ConfigurationModule/Presenters/GroupPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\AdminModule\ConfigurationModule\Presenters;

use App\AdminModule\ConfigurationModule\Forms\GroupFormFactory;
use App\Model\Settings\Exceptions\SettingsItemNotFoundException;
use Nette\Application\UI\Form;
use Nette\DI\Attributes\Inject;
use stdClass;
use Throwable;

/**
* Presenter obsluhující nastavení skupin.
*/
class GroupPresenter extends ConfigurationBasePresenter
{
#[Inject]
public GroupFormFactory $groupFormFactory;

/**
* @throws SettingsItemNotFoundException
* @throws Throwable
*/
protected function createComponentGroupForm(): Form
{
$form = $this->groupFormFactory->create();

$form->onSuccess[] = function (Form $form, stdClass $values): void {
$this->flashMessage('admin.configuration.configuration_saved', 'success');
$this->redirect('this');
};

return $form;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{block main}
<h2>{_admin.configuration.group_heading}</h2>
<div class="card card-body bg-light pb-1 mb-3">
{control groupForm}
</div>
{/block}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<a n:href="SkautIs:default" n:class="nav-link, $presenter->isLinkCurrent(':Admin:Configuration:SkautIs:*') ? active">
{_admin.configuration.menu.skautis}
</a>
<a n:href="Group:default" n:class="nav-link, $presenter->isLinkCurrent(':Admin:Configuration:Group:*') ? active">
{_admin.configuration.menu.group}
</a>
<a n:href="Web:default" n:class="nav-link, $presenter->isLinkCurrent(':Admin:Configuration:Web:*') ? active">
{_admin.configuration.menu.web}
</a>
Expand Down
166 changes: 166 additions & 0 deletions app/AdminModule/GroupsModule/Components/GroupsGridControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

declare(strict_types=1);

namespace App\AdminModule\GroupsModule\Components;

use App\Model\Group\Commands\RemoveGroup;
use App\Model\Group\Commands\SaveGroup;
use App\Model\Group\Repositories\GroupRepository;
use App\Model\Group\Group;
use App\Services\CommandBus;
use App\Services\ExcelExportService;
use Exception;
use Nette\Application\AbortException;
use Nette\Application\UI\Control;
use Nette\Application\UI\Form;
use Nette\Forms\Container;
use Nette\Forms\Controls\TextInput;
use Nette\Http\Session;
use Nette\Http\SessionSection;
use Nette\Localization\Translator;
use stdClass;
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Exception\DataGridException;

use function assert;

/**
* Komponenta pro správu místností.
*/
class GroupsGridControl extends Control
{
private SessionSection $sessionSection;

Check failure on line 33 in app/AdminModule/GroupsModule/Components/GroupsGridControl.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Property App\AdminModule\GroupsModule\Components\GroupsGridControl::$sessionSection is never read, only written.

public function __construct(
private CommandBus $commandBus,
private Translator $translator,
private GroupRepository $groupRepository,
private ExcelExportService $excelExportService,

Check failure on line 39 in app/AdminModule/GroupsModule/Components/GroupsGridControl.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Property App\AdminModule\GroupsModule\Components\GroupsGridControl::$excelExportService is never read, only written.
private Session $session,

Check failure on line 40 in app/AdminModule/GroupsModule/Components/GroupsGridControl.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Property App\AdminModule\GroupsModule\Components\GroupsGridControl::$session is never read, only written.
) {
$this->sessionSection = $session->getSection('srs');
}

/**
* Vykreslí komponentu.
*/
public function render(): void
{
$this->template->setFile(__DIR__ . '/templates/groups_grid.latte');
$this->template->render();
}

/**
* Vytvoří komponentu.
*
* @throws DataGridException
*/
public function createComponentGroupsGrid(string $name): void
{
$grid = new DataGrid($this, $name);
$grid->setTranslator($this->translator);
$grid->setDataSource($this->groupRepository->createQueryBuilder('g'));
$grid->setDefaultSort(['name' => 'ASC']);
$grid->setPagination(false);


$grid->addColumnText('name', 'admin.program.groups.column.name');

$grid->addColumnText('leader_email', 'admin.program.groups.column.name');
$grid->addColumnText('places', 'admin.program.groups.column.name');
$grid->addColumnText('price', 'admin.program.groups.column.name');

$grid->addInlineAdd()->setPositionTop()->onControlAdd[] = function (Container $container): void {
$container->addText('name', '')
->addRule(Form::FILLED, 'admin.program.groups.column.name_empty')
->addRule(Form::IS_NOT_IN, 'admin.program.groups.column.name_exists', $this->groupRepository->findAll());

$container->addText('places', '')
->addCondition(Form::FILLED)
->addRule(Form::INTEGER, 'admin.program.groups.column.capacity_format');
};
$grid->getInlineAdd()->onSubmit[] = [$this, 'add'];

$grid->addInlineEdit()->onControlAdd[] = static function (Container $container): void {
$container->addText('name', '')
->addRule(Form::FILLED, 'admin.program.groups.column.name_empty');

$container->addText('places', '')
->addCondition(Form::FILLED)
->addRule(Form::INTEGER, 'admin.program.groups.column.capacity_format');
};
$grid->getInlineEdit()->onSetDefaults[] = function (Container $container, Group $item): void {
$nameText = $container['name'];
assert($nameText instanceof TextInput);
$nameText->addRule(Form::IS_NOT_IN, 'admin.program.groups.column.name_exists', $this->groupRepository->findOthersNames($item->getId()));

Check failure on line 96 in app/AdminModule/GroupsModule/Components/GroupsGridControl.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Call to an undefined method App\Model\Group\Repositories\GroupRepository::findOthersNames().

$container->setDefaults([
'name' => $item->getName(),
'places' => $item->getPlaces(),
]);
};
$grid->getInlineEdit()->onSubmit[] = [$this, 'edit'];

$grid->addAction('detail', 'admin.common.detail', 'Groups:detail')
->setClass('btn btn-xs btn-primary');

$grid->addAction('delete', '', 'delete!')
->setIcon('trash')
->setTitle('admin.common.delete')
->setClass('btn btn-xs btn-danger')
->addAttributes([
'data-toggle' => 'confirmation',
'data-content' => $this->translator->translate('admin.program.groups.action.delete_confirm'),
]);
}

/**
* Zpracuje přidání místnosti.
*/
public function add(stdClass $values): void
{
$group = new Group($values->name, $values->capacity !== '' ? $values->capacity : null);

Check failure on line 123 in app/AdminModule/GroupsModule/Components/GroupsGridControl.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Class App\Model\Group\Group constructor invoked with 2 parameters, 0 required.

$this->commandBus->handle(new SaveGroup($group));

$p = $this->getPresenter();
$p->flashMessage('admin.program.groups.message.save_success', 'success');
$p->redrawControl('flashes');
}

/**
* Zpracuje úpravu místnosti.
*/
public function edit(string $id, stdClass $values): void
{
$group = $this->groupRepository->findById((int) $id);

$group->setName($values->name);
$group->setCapacity($values->capacity !== '' ? $values->capacity : null);

Check failure on line 140 in app/AdminModule/GroupsModule/Components/GroupsGridControl.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis

Call to an undefined method App\Model\Group\Group::setCapacity().

$this->commandBus->handle(new SaveGroup($group));

$p = $this->getPresenter();
$p->flashMessage('admin.program.groups.message.save_success', 'success');
$p->redrawControl('flashes');
}

/**
* Odstraní místnost.
*
* @throws AbortException
*/
public function handleDelete(int $id): void
{
$group = $this->groupRepository->findById($id);

$this->commandBus->handle(new RemoveGroup($group));

$p = $this->getPresenter();
$p->flashMessage('admin.program.groups.message.delete_success', 'success');
$p->redirect('this');
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\AdminModule\GroupsModule\Components;

/**
* Factory komponenty pro správu místností.
*/
interface IGroupsGridControlFactory
{
/**
* Vytvoří komponentu.
*/
public function create(): GroupsGridControl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\AdminModule\GroupsModule\Components;

/**
* Factory komponenty pro správu kategorií.
*/
interface IStatusGridControlFactory
{
/**
* Vytvoří komponentu.
*/
public function create(): StatusGridControl;
}
Loading

0 comments on commit ae693f8

Please sign in to comment.