|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\AdminModule\Forms; |
| 6 | + |
| 7 | +use App\AdminModule\Presenters\AdminBasePresenter; |
| 8 | +use App\Model\User\Repositories\UserRepository; |
| 9 | +use App\Model\User\User; |
| 10 | +use App\Services\ApplicationService; |
| 11 | +use Contributte\Translation\Translator; |
| 12 | +use Nette; |
| 13 | +use Nette\Application\UI\Form; |
| 14 | +use Nette\Utils\ImageException; |
| 15 | +use stdClass; |
| 16 | + |
| 17 | +use function assert; |
| 18 | + |
| 19 | +/** |
| 20 | + * Formulář pro předání registrace jinému uživateli. |
| 21 | + */ |
| 22 | +class EditUserTransferFormFactory |
| 23 | +{ |
| 24 | + use Nette\SmartObject; |
| 25 | + |
| 26 | + /** |
| 27 | + * Upravovaný uživatel. |
| 28 | + */ |
| 29 | + private User|null $user = null; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + private readonly BaseFormFactory $baseFormFactory, |
| 33 | + private readonly UserRepository $userRepository, |
| 34 | + private readonly ApplicationService $applicationService, |
| 35 | + private readonly Translator $translator, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + public function create(int $id): Form |
| 40 | + { |
| 41 | + $this->user = $this->userRepository->findById($id); |
| 42 | + |
| 43 | + $form = $this->baseFormFactory->create(); |
| 44 | + |
| 45 | + $form->addSelect('targetUser', 'admin.users.users_target_user', $this->userRepository->getUsersOptions(true)) |
| 46 | + ->addRule(Form::NOT_EQUAL, 'admin.users.users_target_user_empty', 0) |
| 47 | + ->addRule(Form::NOT_EQUAL, 'admin.users.users_target_user_same', $this->user->getId()) |
| 48 | + ->setHtmlAttribute('data-live-search', 'true'); |
| 49 | + |
| 50 | + $form->addSubmit('submit', 'admin.users.users_transfer') |
| 51 | + ->setDisabled(! $this->user->isRegistered() || ! $this->user->hasPaidAnyApplication()) |
| 52 | + ->setHtmlAttribute('class', 'btn btn-danger') |
| 53 | + ->setHtmlAttribute('data-toggle', 'confirmation') |
| 54 | + ->setHtmlAttribute('data-content', $this->translator->translate('admin.users.users_transfer_confirm')); |
| 55 | + |
| 56 | + $form->onSuccess[] = [$this, 'processForm']; |
| 57 | + |
| 58 | + return $form; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Zpracuje formulář. |
| 63 | + * |
| 64 | + * @throws Nette\Utils\UnknownImageFileException |
| 65 | + * @throws ImageException |
| 66 | + */ |
| 67 | + public function processForm(Form $form, stdClass $values): void |
| 68 | + { |
| 69 | + $presenter = $form->getPresenter(); |
| 70 | + assert($presenter instanceof AdminBasePresenter); |
| 71 | + |
| 72 | + $loggedUser = $presenter->getDbUser(); |
| 73 | + |
| 74 | + $targetUser = $this->userRepository->findById($values->targetUser); |
| 75 | + |
| 76 | + $this->applicationService->transferRegistration($this->user, $targetUser, $loggedUser); |
| 77 | + } |
| 78 | +} |
0 commit comments