| 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\AssetHandler\JavaScript; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Romm\Formz\AssetHandler\AbstractAssetHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Romm\Formz\Error\FormzMessageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Romm\Formz\Service\ArrayService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Will handle the "one time run" data needed by JavaScript: the submitted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * values, and others. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class FormRequestDataJavaScriptAssetHandler extends AbstractAssetHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * See class description. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function getFormRequestDataJavaScriptCode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $submittedFormValues = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $fieldsExistingMessages = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $deactivatedFields = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $formWasSubmitted = $this->getFormObject()->hasLastValidationResult() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ? 'true' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             : 'false'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if ($formWasSubmitted) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $submittedFormValues = ArrayService::get()->arrayToJavaScriptJson($this->getSubmittedFormValues()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $fieldsExistingMessages = ArrayService::get()->arrayToJavaScriptJson($this->getFieldsExistingMessages()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $deactivatedFields = ArrayService::get()->arrayToJavaScriptJson($this->getDeactivatedFields()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $javaScriptCode = <<<JS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | (function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     Formz.Form.beforeInitialization($formName, function(form) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         form.injectRequestData($submittedFormValues, $fieldsExistingMessages, $formWasSubmitted, $deactivatedFields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | })(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | JS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $javaScriptCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Will fetch the values of the fields of the submitted form, if a form has | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * been submitted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     protected function getSubmittedFormValues() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $formName = $this->getFormObject()->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $originalRequest = $this->getControllerContext() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ->getRequest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ->getOriginalRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (null !== $originalRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             && $originalRequest->hasArgument($formName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $result = $originalRequest->getArgument($formName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * This function checks every message which may exist on every property of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * the form (used to tell to JavaScript which messages already exist). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     protected function getFieldsExistingMessages() | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $fieldsMessages = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         if ($this->getFormObject()->hasLastValidationResult()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             $lastValidationResult = $this->getFormObject()->getLastValidationResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             foreach ($this->getFormObject()->getProperties() as $fieldName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 $result = $lastValidationResult->forProperty($fieldName); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 $messages = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                 if ($result->hasErrors()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                     $messages['errors'] = $this->formatMessages($result->getErrors()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 if ($result->hasWarnings()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                     $messages['warnings'] = $this->formatMessages($result->getWarnings()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |                 if ($result->hasNotices()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                     $messages['notices'] = $this->formatMessages($result->getNotices()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 $fieldsMessages[$fieldName] = $messages; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         return $fieldsMessages; | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param FormzMessageInterface[] $messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     protected function formatMessages(array $messages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $sortedMessages = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         foreach ($messages as $message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             $validationName = $message->getValidationName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $messageKey = $message->getMessageKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             if (false === isset($sortedMessages[$validationName])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 $sortedMessages[$validationName] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $sortedMessages[$validationName][$messageKey] = $message->render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $sortedMessages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     protected function getDeactivatedFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return ($this->getFormObject()->hasLastValidationResult()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             ? array_keys($this->getFormObject()->getLastValidationResult()->getDeactivatedFields()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  | 
            
                        
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: