Skip to content

Commit

Permalink
Vylepšení vlastních polí (#739)
Browse files Browse the repository at this point in the history
* odstraneni prijezdu a odjezdu

* úprava modelu - customdate, customdatetime

* migrate

* multiselect

* vlastni pole typu multiselect, date, datetime

* oprava coding standard

* nastaveni roli pro vlastni pole

* zobrazeni poli podle roli

* oprava CS

* prepinani zobrazovani poli

* prepinani viditelnosti a povinnosti poli

* filtrovani podle select a multiselect

* coding standard fix

* coding standard fix

* build fix
  • Loading branch information
jan-stanek authored May 28, 2020
1 parent a5ba383 commit 609db07
Show file tree
Hide file tree
Showing 38 changed files with 959 additions and 342 deletions.
69 changes: 52 additions & 17 deletions app/AdminModule/Components/UsersGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
use App\Model\Enums\PaymentType;
use App\Model\Enums\SkautIsEventType;
use App\Model\Settings\CustomInput\CustomCheckbox;
use App\Model\Settings\CustomInput\CustomDate;
use App\Model\Settings\CustomInput\CustomDateTime;
use App\Model\Settings\CustomInput\CustomInputRepository;
use App\Model\Settings\CustomInput\CustomMultiSelect;
use App\Model\Settings\CustomInput\CustomSelect;
use App\Model\Settings\CustomInput\CustomText;
use App\Model\Settings\Settings;
use App\Model\Settings\SettingsException;
use App\Model\User\CustomInputValue\CustomCheckboxValue;
use App\Model\User\CustomInputValue\CustomFileValue;
use App\Model\User\CustomInputValue\CustomSelectValue;
use App\Model\User\CustomInputValue\CustomTextValue;
use App\Model\User\User;
use App\Model\User\UserRepository;
Expand Down Expand Up @@ -48,7 +50,6 @@
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Exception\DataGridColumnStatusException;
use Ublaboo\DataGrid\Exception\DataGridException;
use function array_merge;
use function array_slice;
use function array_values;
use function explode;
Expand Down Expand Up @@ -351,8 +352,6 @@ public function createComponentUsersGrid(string $name) : void
return $customInputValue->getValue()
? $this->translator->translate('admin.common.yes')
: $this->translator->translate('admin.common.no');
case $customInputValue instanceof CustomSelectValue:
return $customInputValue->getValueOption();
case $customInputValue instanceof CustomFileValue:
return $customInputValue->getValue()
? Html::el('a')
Expand All @@ -365,6 +364,8 @@ public function createComponentUsersGrid(string $name) : void
Html::el('span')->setAttribute('class', 'fa fa-download')
)
: '';
default:
return $customInputValue->getValueText();
}
}

Expand Down Expand Up @@ -405,22 +406,56 @@ public function createComponentUsersGrid(string $name) : void
break;

case $customInput instanceof CustomSelect:
$columnCustomInput->setFilterSelect(array_merge(['' => 'admin.common.all'], $customInput->getSelectOptions()))
->setCondition(static function (QueryBuilder $qb, string $value) use ($customInput) : void {
if ($value === '') {
return;
} else {
$qb->leftJoin('u.customInputValues', 'uCIV3')
->leftJoin('uCIV3.input', 'uCIVI3')
->leftJoin('App\Model\User\CustomInputValue\CustomSelectValue', 'uCSV', 'WITH', 'uCIV3.id = uCSV.id')
->andWhere('uCIVI3.id = :iid3 OR uCIVI3.id IS NULL')
->andWhere('uCSV.value = :ivalue3')
->setParameter('iid3', $customInput->getId())
->setParameter('ivalue3', $value);
}
$columnCustomInput->setFilterMultiSelect($customInput->getFilterOptions())
->setCondition(static function (QueryBuilder $qb, ArrayHash $values) use ($customInput) : void {
$qb->leftJoin('u.customInputValues', 'uCIV3')
->leftJoin('uCIV3.input', 'uCIVI3')
->leftJoin('App\Model\User\CustomInputValue\CustomSelectValue', 'uCSV', 'WITH', 'uCIV3.id = uCSV.id')
->andWhere('uCIVI3.id = :iid3 OR uCIVI3.id IS NULL')
->andWhere('uCSV.value in (:ivalues3)')
->setParameter('iid3', $customInput->getId())
->setParameter('ivalues3', (array) $values);
})
->setTranslateOptions();
break;

case $customInput instanceof CustomMultiSelect:
$columnCustomInput->setFilterMultiSelect($customInput->getSelectOptions())
->setCondition(static function (QueryBuilder $qb, ArrayHash $values) use ($customInput) : void {
$qb->leftJoin('u.customInputValues', 'uCIV4')
->leftJoin('uCIV4.input', 'uCIVI4')
->leftJoin('App\Model\User\CustomInputValue\CustomMultiSelectValue', 'uCMSV', 'WITH', 'uCIV4.id = uCMSV.id')
->andWhere('uCIVI4.id = :iid4 OR uCIVI4.id IS NULL')
->andWhere('uCMSV.value in (:ivalues4)')
->setParameter('iid4', $customInput->getId())
->setParameter('ivalues4', (array) $values);
})
->setTranslateOptions();
break;

case $customInput instanceof CustomDate:
$columnCustomInput->setSortable()
->setSortableCallback(static function (QueryBuilder $qb, array $sort) use ($customInput, $columnCustomInputName) : void {
$qb->leftJoin('u.customInputValues', 'uCIV5')
->leftJoin('uCIV5.input', 'uCIVI5')
->leftJoin('App\Model\User\CustomInputValue\CustomDateValue', 'uCDV', 'WITH', 'uCIV5.id = uCDV.id')
->andWhere('uCIVI5.id = :iid5 OR uCIVI5.id IS NULL')
->setParameter('iid5', $customInput->getId())
->orderBy('uCDV.value', $sort[$columnCustomInputName]);
});
break;

case $customInput instanceof CustomDateTime:
$columnCustomInput->setSortable()
->setSortableCallback(static function (QueryBuilder $qb, array $sort) use ($customInput, $columnCustomInputName) : void {
$qb->leftJoin('u.customInputValues', 'uCIV6')
->leftJoin('uCIV6.input', 'uCIVI6')
->leftJoin('App\Model\User\CustomInputValue\CustomDateTimeValue', 'uCDTV', 'WITH', 'uCIV6.id = uCDTV.id')
->andWhere('uCIVI6.id = :iid6 OR uCIVI6.id IS NULL')
->setParameter('iid6', $customInput->getId())
->orderBy('uCDTV.value', $sort[$columnCustomInputName]);
});
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace App\AdminModule\ConfigurationModule\Components;

use App\Model\Acl\Role;
use App\Model\Settings\CustomInput\CustomInput;
use App\Model\Settings\CustomInput\CustomInputRepository;
use App\Model\Settings\CustomInput\CustomMultiSelect;
use App\Model\Settings\CustomInput\CustomSelect;
use App\Services\AclService;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\ORMException;
use Nette\Application\AbortException;
Expand All @@ -15,6 +18,7 @@
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Exception\DataGridColumnStatusException;
use Ublaboo\DataGrid\Exception\DataGridException;
use function count;

/**
* Komponenta pro správu vlastních polí přihlášky.
Expand All @@ -27,10 +31,16 @@ class CustomInputsGridControl extends Control

private CustomInputRepository $customInputRepository;

public function __construct(ITranslator $translator, CustomInputRepository $customInputRepository)
{
private AclService $aclService;

public function __construct(
ITranslator $translator,
CustomInputRepository $customInputRepository,
AclService $aclService
) {
$this->translator = $translator;
$this->customInputRepository = $customInputRepository;
$this->aclService = $aclService;
}

/**
Expand Down Expand Up @@ -59,6 +69,13 @@ public function createComponentCustomInputsGrid(string $name) : void

$grid->addColumnText('name', 'admin.configuration.custom_inputs_name');

$grid->addColumnText('roles', 'admin.configuration.custom_inputs_roles', 'rolesText')
->setRendererOnCondition(function () {
return $this->translator->translate('admin.configuration.custom_inputs_roles_all');
}, function (CustomInput $input) {
return count($this->aclService->getRolesWithoutRolesOptions([Role::GUEST, Role::UNAPPROVED, Role::NONREGISTERED])) === $input->getRoles()->count();
});

$grid->addColumnText('type', 'admin.configuration.custom_inputs_type')
->setRenderer(function (CustomInput $input) {
return $this->translator->translate('admin.common.custom_' . $input->getType());
Expand All @@ -76,7 +93,7 @@ public function createComponentCustomInputsGrid(string $name) : void

$grid->addColumnText('options', 'admin.configuration.custom_inputs_options')
->setRenderer(static function (CustomInput $input) {
return $input instanceof CustomSelect ? $input->getOptions() : null;
return $input instanceof CustomSelect || $input instanceof CustomMultiSelect ? $input->getOptionsText() : null;
});

$grid->addToolbarButton('Application:add')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
namespace App\AdminModule\ConfigurationModule\Forms;

use App\AdminModule\Forms\BaseFormFactory;
use App\Model\Acl\Role;
use App\Model\Acl\RoleRepository;
use App\Model\Settings\CustomInput\CustomCheckbox;
use App\Model\Settings\CustomInput\CustomDate;
use App\Model\Settings\CustomInput\CustomDateTime;
use App\Model\Settings\CustomInput\CustomFile;
use App\Model\Settings\CustomInput\CustomInput;
use App\Model\Settings\CustomInput\CustomInputRepository;
use App\Model\Settings\CustomInput\CustomMultiSelect;
use App\Model\Settings\CustomInput\CustomSelect;
use App\Model\Settings\CustomInput\CustomText;
use App\Services\AclService;
use App\Utils\Helpers;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\ORMException;
use Nette;
use Nette\Application\UI\Form;
use stdClass;
use function array_keys;
use function array_map;
use function explode;
use function implode;
use function trim;

/**
Expand All @@ -38,10 +46,20 @@ class CustomInputFormFactory

private CustomInputRepository $customInputRepository;

public function __construct(BaseFormFactory $baseFormFactory, CustomInputRepository $customInputRepository)
{
private AclService $aclService;

private RoleRepository $roleRepository;

public function __construct(
BaseFormFactory $baseFormFactory,
CustomInputRepository $customInputRepository,
AclService $aclService,
RoleRepository $roleRepository
) {
$this->baseFormFactory = $baseFormFactory;
$this->customInputRepository = $customInputRepository;
$this->aclService = $aclService;
$this->roleRepository = $roleRepository;
}

/**
Expand All @@ -58,8 +76,13 @@ public function create(int $id) : Form
$form->addText('name', 'admin.configuration.custom_inputs_name')
->addRule(Form::FILLED, 'admin.configuration.custom_inputs_name_empty');

$rolesOptions = $this->aclService->getRolesWithoutRolesOptions([Role::GUEST, Role::UNAPPROVED, Role::NONREGISTERED]);
$rolesSelect = $form->addMultiSelect('roles', 'admin.configuration.custom_inputs_roles', $rolesOptions)
->addRule(Form::FILLED, 'admin.configuration.custom_inputs_roles_empty');

$typeSelect = $form->addSelect('type', 'admin.configuration.custom_inputs_type', $this->prepareCustomInputTypesOptions());
$typeSelect->addCondition($form::EQUAL, CustomInput::SELECT)->toggle('custom-input-select');
$typeSelect->addCondition($form::EQUAL, CustomInput::MULTISELECT)->toggle('custom-input-select');

$form->addCheckbox('mandatory', 'admin.configuration.custom_inputs_edit_mandatory');

Expand All @@ -79,14 +102,17 @@ public function create(int $id) : Form
$form->setDefaults([
'id' => $id,
'name' => $this->customInput->getName(),
'roles' => Helpers::getIds($this->customInput->getRoles()),
'type' => $this->customInput->getType(),
'mandatory' => $this->customInput->isMandatory(),
]);

if ($this->customInput instanceof CustomSelect) {
if ($this->customInput instanceof CustomSelect || $this->customInput instanceof CustomMultiSelect) {
$customInput = $this->customInput;
$optionsText->setDefaultValue($customInput->getOptions());
$optionsText->setDefaultValue($customInput->getOptionsText());
}
} else {
$rolesSelect->setDefaultValue(array_keys($rolesOptions));
}

$form->onSuccess[] = [$this, 'processForm'];
Expand Down Expand Up @@ -118,24 +144,42 @@ public function processForm(Form $form, stdClass $values) : void

case CustomInput::SELECT:
$this->customInput = new CustomSelect();
$options = array_map(
static function (string $o) {
return trim($o);
},
explode(',', $values->options)
);
$this->customInput->setOptions($options);
break;

$options = explode(',', $values->options);
$optionsTrimmed = [];
foreach ($options as $option) {
$optionsTrimmed[] = trim($option);
}

$this->customInput->setOptions(implode(', ', $optionsTrimmed));

case CustomInput::MULTISELECT:
$this->customInput = new CustomMultiSelect();
$options = array_map(
static function (string $o) {
return trim($o);
},
explode(',', $values->options)
);
$this->customInput->setOptions($options);
break;

case CustomInput::FILE:
$this->customInput = new CustomFile();
break;

case CustomInput::DATE:
$this->customInput = new CustomDate();
break;

case CustomInput::DATETIME:
$this->customInput = new CustomDateTime();
break;
}
}

$this->customInput->setName($values->name);
$this->customInput->setRoles($this->roleRepository->findRolesByIds($values->roles));
$this->customInput->setMandatory($values->mandatory);

$this->customInputRepository->save($this->customInput);
Expand Down
1 change: 0 additions & 1 deletion app/AdminModule/Forms/AddRoleFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public function processForm(Form $form, stdClass $values) : void
$role->setRegisterable($parent->isRegisterable());
$role->setRegisterableFrom($parent->getRegisterableFrom());
$role->setRegisterableTo($parent->getRegisterableTo());
$role->setDisplayArrivalDeparture($parent->isDisplayArrivalDeparture());
} else {
$nonregistered = $this->roleRepository->findBySystemName(Role::NONREGISTERED);
foreach ($nonregistered->getPages() as $page) {
Expand Down
4 changes: 0 additions & 4 deletions app/AdminModule/Forms/EditRoleFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ public function create(int $id) : Form

// $form->addCheckbox('syncedWithSkautIs', 'admin.acl.roles_synced_with_skaut_is');

$form->addCheckbox('displayArrivalDeparture', 'admin.acl.roles_display_arrival_departure');

$form->addCheckbox('feeFromSubevents', 'admin.acl.roles_fee_from_subevents_checkbox')
->addCondition(Form::EQUAL, false)
->toggle('fee');
Expand Down Expand Up @@ -189,7 +187,6 @@ public function create(int $id) : Form
'capacity' => $this->role->getCapacity(),
'approvedAfterRegistration' => $this->role->isApprovedAfterRegistration(),
// 'syncedWithSkautIs' => $this->role->isSyncedWithSkautIS(),
'displayArrivalDeparture' => $this->role->isDisplayArrivalDeparture(),
'feeFromSubevents' => $this->role->getFee() === null,
'fee' => $this->role->getFee(),
'minimumAge' => $this->role->getMinimumAge(),
Expand Down Expand Up @@ -226,7 +223,6 @@ public function processForm(Form $form, stdClass $values) : void
$this->role->setCapacity($capacity);
$this->role->setApprovedAfterRegistration($values->approvedAfterRegistration);
// $this->role->setSyncedWithSkautIS($values->syncedWithSkautIs);
$this->role->setDisplayArrivalDeparture($values->displayArrivalDeparture);
$this->role->setMinimumAge($values->minimumAge);
$this->role->setPermissions($this->permissionRepository->findPermissionsByIds($values->permissions));
$this->role->setPages($this->pageRepository->findPagesBySlugs($values->pages));
Expand Down
Loading

0 comments on commit 609db07

Please sign in to comment.