BacLuc | a125477 | 2020-03-29 12:34:34 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace BaclucEventPackage; |
| 5 | |
| 6 | |
| 7 | use BaclucC5Crud\Controller\ActionProcessor; |
| 8 | use BaclucC5Crud\Controller\ActionRegistryFactory; |
| 9 | use BaclucC5Crud\Controller\Renderer; |
| 10 | use BaclucC5Crud\Controller\Validation\ValidationResultItem; |
| 11 | use BaclucC5Crud\Controller\Validation\Validator; |
| 12 | use BaclucC5Crud\Controller\ValuePersisters\FieldPersistor; |
| 13 | use BaclucC5Crud\Controller\ValuePersisters\PersistorConfiguration; |
| 14 | use BaclucC5Crud\Controller\VariableSetter; |
| 15 | use BaclucC5Crud\Entity\Repository; |
| 16 | use BaclucC5Crud\FormViewAfterValidationFailedService; |
| 17 | use function BaclucC5Crud\Lib\collect as collect; |
| 18 | |
| 19 | class PostCancelEventForm implements ActionProcessor |
| 20 | { |
| 21 | const FORM_VIEW = "view/form"; |
| 22 | /** |
| 23 | * @var Validator |
| 24 | */ |
| 25 | private $validator; |
| 26 | /** |
| 27 | * @var FormViewAfterValidationFailedService |
| 28 | */ |
| 29 | private $formViewAfterValidationFailedService; |
| 30 | /** |
| 31 | * @var Repository |
| 32 | */ |
| 33 | private $repository; |
| 34 | /** |
| 35 | * @var PersistorConfiguration |
| 36 | */ |
| 37 | private $peristorConfiguration; |
| 38 | /** |
| 39 | * @var VariableSetter |
| 40 | */ |
| 41 | private $variableSetter; |
| 42 | /** |
| 43 | * @var Renderer |
| 44 | */ |
| 45 | private $renderer; |
| 46 | /** |
| 47 | * @var NoEditIdFallbackActionProcessor |
| 48 | */ |
| 49 | private $noEditIdFallbackActionProcessor; |
| 50 | |
| 51 | /** |
| 52 | * PostFormActionProcessor constructor. |
| 53 | * @param Validator $validator |
| 54 | * @param FormViewAfterValidationFailedService $formViewAfterValidationFailedService |
| 55 | * @param Repository $repository |
| 56 | * @param PersistorConfiguration $peristorConfiguration |
| 57 | * @param VariableSetter $variableSetter |
| 58 | * @param Renderer $renderer |
| 59 | * @param NoEditIdFallbackActionProcessor $noEditIdFallbackActionProcessor |
| 60 | */ |
| 61 | public function __construct( |
| 62 | Validator $validator, |
| 63 | FormViewAfterValidationFailedService $formViewAfterValidationFailedService, |
| 64 | Repository $repository, |
| 65 | PersistorConfiguration $peristorConfiguration, |
| 66 | VariableSetter $variableSetter, |
| 67 | Renderer $renderer, |
| 68 | NoEditIdFallbackActionProcessor $noEditIdFallbackActionProcessor |
| 69 | ) { |
| 70 | $this->validator = $validator; |
| 71 | $this->formViewAfterValidationFailedService = $formViewAfterValidationFailedService; |
| 72 | $this->repository = $repository; |
| 73 | $this->peristorConfiguration = $peristorConfiguration; |
| 74 | $this->variableSetter = $variableSetter; |
| 75 | $this->renderer = $renderer; |
| 76 | $this->noEditIdFallbackActionProcessor = $noEditIdFallbackActionProcessor; |
| 77 | } |
| 78 | |
| 79 | function getName(): string |
| 80 | { |
| 81 | return ActionRegistryFactory::POST_FORM; |
| 82 | } |
| 83 | |
| 84 | function process(array $get, array $post, ...$additionalParameters) |
| 85 | { |
| 86 | $editId = null; |
| 87 | if (count($additionalParameters) == 1 && $additionalParameters[0] != null) { |
| 88 | $editId = $additionalParameters[0]; |
| 89 | } |
| 90 | if ($editId == null) { |
| 91 | return call_user_func_array([$this->noEditIdFallbackActionProcessor, "process"], func_get_args()); |
| 92 | } |
| 93 | |
| 94 | $validationResult = $this->validator->validate($post); |
| 95 | |
| 96 | if (!$validationResult->isError()) { |
| 97 | $postValues = collect($validationResult) |
| 98 | ->keyBy(function (ValidationResultItem $validationResultItem) { |
| 99 | return $validationResultItem->getName(); |
| 100 | }) |
| 101 | ->map(function (ValidationResultItem $validationResultItem) { |
| 102 | return $validationResultItem->getPostValue(); |
| 103 | }); |
| 104 | |
| 105 | $postValues['event'] = $editId; |
| 106 | $entity = $this->repository->create(); |
| 107 | /** |
| 108 | * @var FieldPersistor $persistor |
| 109 | */ |
| 110 | foreach ($this->peristorConfiguration as $persistor) { |
| 111 | $persistor->persist($postValues, $entity); |
| 112 | } |
| 113 | $this->repository->persist($entity); |
| 114 | } else { |
| 115 | $formView = $this->formViewAfterValidationFailedService->getFormView($validationResult); |
| 116 | $this->variableSetter->set("fields", $formView->getFields()); |
| 117 | $this->variableSetter->set("editId", $editId); |
| 118 | $validationErrors = collect($validationResult) |
| 119 | ->keyBy(function (ValidationResultItem $resultItem) { |
| 120 | return $resultItem->getName(); |
| 121 | })->map(function (ValidationResultItem $resultItem) { |
| 122 | return $resultItem->getMessages(); |
| 123 | }); |
| 124 | $this->variableSetter->set("validationErrors", $validationErrors); |
| 125 | $this->variableSetter->set("addFormTags", true); |
| 126 | $this->renderer->render(self::FORM_VIEW); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |