| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * 2017 Romain CANON <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is part of the TYPO3 FormZ project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * It is free software; you can redistribute it and/or modify it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * under the terms of the GNU General Public License, either | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * version 3 of the License, or any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, see: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * http://www.gnu.org/licenses/gpl-3.0.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Romm\Formz\Service\ViewHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Romm\Formz\Behaviours\BehavioursManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Romm\Formz\Core\Core; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Romm\Formz\Error\FormResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Romm\Formz\Exceptions\DuplicateEntryException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Romm\Formz\Form\FormObject\FormObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Romm\Formz\Validation\Validator\Form\DefaultFormValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use TYPO3\CMS\Core\SingletonInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * This class contains methods that help view helpers to manipulate data and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * know more things concerning the current form state. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * It is mainly configured inside the `FormViewHelper`, and used in other | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * view helpers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class FormViewHelperService implements SingletonInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $formContext = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var FormObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected $formObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Reset every state that can be used by this service. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function resetState() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->formContext = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->formObject = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Will activate the form context, changing the result returned by the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * function `formContextExists()`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return FormViewHelperService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @throws DuplicateEntryException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function activateFormContext() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if (true === $this->formContext) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             throw DuplicateEntryException::duplicatedFormContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->formContext = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Returns `true` if the `FormViewHelper` context exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function formContextExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $this->formContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Will loop on the submitted form fields and apply behaviours if their | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * configuration contains. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param ControllerContext $controllerContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function applyBehavioursOnSubmittedForm(ControllerContext $controllerContext) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if ($this->formObject->formWasSubmitted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $request = $controllerContext->getRequest()->getOriginalRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $formName = $this->formObject->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             if ($request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 && $request->hasArgument($formName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 /** @var BehavioursManager $behavioursManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $behavioursManager = GeneralUtility::makeInstance(BehavioursManager::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 /** @var array $originalForm */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $originalForm = $request->getArgument($formName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $formProperties = $behavioursManager->applyBehaviourOnPropertiesArray( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     $originalForm, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     $this->formObject->getDefinition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $request->setArgument($formName, $formProperties); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Fetches all data attributes that are bound to the form: fields values, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * validation result and others. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param DataAttributesAssetHandler $dataAttributesAssetHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     public function getDataAttributes(DataAttributesAssetHandler $dataAttributesAssetHandler) | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         $dataAttributes = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         if ($this->formObject->hasForm()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             /* | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |              * Getting the data attributes for the form values. It is needed to | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |              * have a validation result because a field can be deactivated (in | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |              * that case, the data attribute for this field is removed). | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |              */ | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |             if (false === $this->formObject->formWasValidated()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |                 $formResult = $this->getFormValidationResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |                 $formResult = $this->formObject->getFormResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             $dataAttributes += $dataAttributesAssetHandler->getFieldsValuesDataAttributes($formResult); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         if (true === $this->formObject->formWasSubmitted()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |             $dataAttributes += $dataAttributesAssetHandler->getFieldSubmissionDoneDataAttribute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         if (true === $this->formObject->formWasValidated()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             $dataAttributes += $dataAttributesAssetHandler->getFieldsValidDataAttributes(); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |             $dataAttributes += $dataAttributesAssetHandler->getFieldsMessagesDataAttributes(); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         return $dataAttributes; | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @param string $formName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @return DefaultFormValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     protected function getFormValidator($formName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         /** @var DefaultFormValidator $validation */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $validation = Core::instantiate(DefaultFormValidator::class, ['name' => $formName]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $validation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @return FormObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     public function getFormObject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         return $this->formObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param FormObject $formObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     public function setFormObject(FormObject $formObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $this->formObject = $formObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * @return FormResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     protected function getFormValidationResult() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $formValidator = $this->getFormValidator($this->formObject->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return $formValidator->validateGhost($this->formObject->getForm()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 189 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |  |