22
33namespace OWC \PrefillGravityForms \Controllers ;
44
5+ use DateTime ;
6+ use GF_Field ;
57use Exception ;
6- use function OWC \PrefillGravityForms \Foundation \Helpers \decrypt ;
7- use function OWC \PrefillGravityForms \Foundation \Helpers \view ;
8- use function Yard \DigiD \Foundation \Helpers \resolve ;
98use OWC \PrefillGravityForms \Foundation \TeamsLogger ;
109use OWC \PrefillGravityForms \GravityForms \GravityFormsSettings ;
1110
11+ use function Yard \DigiD \Foundation \Helpers \resolve ;
12+ use function OWC \PrefillGravityForms \Foundation \Helpers \view ;
13+ use function OWC \PrefillGravityForms \Foundation \Helpers \decrypt ;
14+
1215abstract class BaseController
1316{
1417 protected GravityFormsSettings $ settings ;
1518 protected TeamsLogger $ teams ;
1619 protected string $ supplier ;
17-
20+
1821 public function __construct ()
1922 {
2023 $ this ->settings = GravityFormsSettings::make ();
@@ -25,11 +28,11 @@ public function resolveTeams(): TeamsLogger
2528 {
2629 try {
2730 if (! function_exists ('Yard\DigiD\Foundation\Helpers\resolve ' )) {
28- throw new \ Exception ;
31+ throw new Exception () ;
2932 }
3033
3134 return TeamsLogger::make (resolve ('teams ' ));
32- } catch (\ Exception $ e ) {
35+ } catch (Exception $ e ) {
3336 return TeamsLogger::make (new \Psr \Log \NullLogger ());
3437 }
3538 }
@@ -73,21 +76,23 @@ protected function preFillFields(array $form, array $response): array
7376 }
7477
7578 $ foundValue = $ this ->findLinkedValue ($ linkedValue , $ response );
76-
79+
7780 if (empty ($ foundValue )) {
78- $ field ->cssClass = 'owc_prefilled ' ; // When field is mapped but there is no value found, set to read-only.
81+ $ field ->cssClass = 'owc_prefilled ' ; // When field has mapping but there is no value found, set to read-only.
82+
7983 continue ;
8084 }
8185
8286 if ($ field ->type === 'text ' ) {
83- $ field ->defaultValue = ucfirst ($ foundValue );
84- $ field ->cssClass = 'owc_prefilled ' ;
87+ $ this ->handleFieldText ($ field , $ foundValue );
88+
89+ continue ;
8590 }
8691
8792 if ($ field ->type === 'date ' ) {
88- $ field -> defaultValue = ( new \ DateTime ( $ foundValue))-> format ( ' d-m-Y ' );
89- $ field -> displayOnly = true ;
90- $ field -> cssClass = ' owc_prefilled ' ;
93+ $ this -> handleFieldDate ( $ field , $ foundValue );
94+
95+ continue ;
9196 }
9297 }
9398
@@ -110,12 +115,13 @@ public function findLinkedValue(string $linkedValue = '', array $response = []):
110115 public function explodeDotNotationValue (string $ dotNotationString , array $ response ): string
111116 {
112117 $ exploded = explode ('. ' , $ dotNotationString );
113- $ holder = [];
118+ $ holder = [];
114119
115120 foreach ($ exploded as $ key => $ item ) {
116121 if ($ key === 0 ) {
117122 // Place the wanted part of the response in $holder.
118123 $ holder = $ response [$ item ] ?? '' ;
124+
119125 continue ;
120126 }
121127
@@ -124,9 +130,9 @@ public function explodeDotNotationValue(string $dotNotationString, array $respon
124130 break ;
125131 }
126132
127- // If holder is a multidimensional array, unflatten .
128- if (!empty ($ holder [0 ]) && is_array ($ holder [0 ])) {
129- $ holder = $ this ->unflattenHolderArray ($ holder );
133+ // If holder is a multidimensional array, flatten .
134+ if (! empty ($ holder [0 ]) && is_array ($ holder [0 ])) {
135+ $ holder = $ this ->flattenMultidimensionalArray ($ holder );
130136 }
131137
132138 // Place the nested part of the response in $holder.
@@ -136,15 +142,57 @@ public function explodeDotNotationValue(string $dotNotationString, array $respon
136142 return is_string ($ holder ) || is_numeric ($ holder ) ? $ holder : '' ;
137143 }
138144
139- protected function unflattenHolderArray (array $ holder ): array
145+ protected function flattenMultidimensionalArray (array $ array ): array
140146 {
141- $ backupHolder = [];
147+ $ holder = [];
142148
143- foreach ($ holder as $ part ) {
144- $ backupHolder = array_merge ($ backupHolder , $ part );
149+ foreach ($ array as $ part ) {
150+ $ holder = array_merge ($ holder , $ part );
151+ }
152+
153+ return $ holder ;
154+ }
155+
156+ protected function handleFieldText (GF_Field $ field , string $ foundValue ): void
157+ {
158+ if ($ this ->isPossibleDate ($ foundValue )) {
159+ $ field ->defaultValue = (new \DateTime ($ foundValue ))->format ('d-m-Y ' );
160+ } else {
161+ $ field ->defaultValue = ucfirst ($ foundValue );
145162 }
146163
147- return $ backupHolder ;
164+ $ field ->cssClass = 'owc_prefilled ' ;
165+ }
166+
167+ public function isPossibleDate (string $ value ): bool
168+ {
169+ return (date ('Y-m-d ' , strtotime ($ value )) == $ value );
170+ }
171+
172+ protected function handleFieldDate (GF_Field $ field , string $ foundValue ): void
173+ {
174+ try {
175+ $ date = new DateTime ($ foundValue );
176+ } catch (Exception $ e ) {
177+ return ;
178+ }
179+
180+ // Field consists of 1 part.
181+ if (empty ($ field ->inputs ) || $ field ->dateType === 'datepicker ' ) {
182+ $ field ->defaultValue = $ date ->format ('d-m-Y ' );
183+ $ field ->displayOnly = true ;
184+ $ field ->cssClass = 'owc_prefilled ' ;
185+
186+ return ;
187+ }
188+
189+ // Field consists of 3 parts which are represented by the input attribute.
190+ if (! empty ($ field ->inputs ) && ($ field ->dateType === 'datefield ' || $ field ->dateType === 'datedropdown ' )) {
191+ $ field ->inputs [0 ]['defaultValue ' ] = $ date ->format ('m ' );
192+ $ field ->inputs [1 ]['defaultValue ' ] = $ date ->format ('d ' );
193+ $ field ->inputs [2 ]['defaultValue ' ] = $ date ->format ('Y ' );
194+ $ field ->cssClass = 'owc_prefilled ' ;
195+ }
148196 }
149197
150198 public function getRequestURL (string $ identifier = '' , string $ expand = '' ): string
@@ -198,22 +246,22 @@ protected function handleCurl(array $args): array
198246 if (! empty ($ this ->settings ->getPassphrase ())) {
199247 curl_setopt ($ curl , CURLOPT_SSLKEYPASSWD , $ this ->settings ->getPassphrase ());
200248 }
201-
249+
202250 curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , false );
203251 curl_setopt ($ curl , CURLOPT_SSL_VERIFYHOST , false );
204-
252+
205253 $ output = curl_exec ($ curl );
206-
254+
207255 if (curl_error ($ curl )) {
208256 throw new \Exception (curl_error ($ curl ));
209257 }
210-
258+
211259 $ decoded = json_decode ($ output , true );
212-
260+
213261 if (! $ decoded || json_last_error () !== JSON_ERROR_NONE ) {
214262 throw new \Exception ('Something went wrong with decoding of the JSON output. ' );
215263 }
216-
264+
217265 return $ decoded ;
218266 } catch (\Exception $ e ) {
219267 return [
0 commit comments