Browse code

Updates

Benjamin Roth authored on09/06/2023 14:31:40
Showing3 changed files
... ...
@@ -1,6 +1,17 @@
1 1
 {% extends '@Contao/block_unsearchable' %}
2 2
 
3 3
 {% block content %}
4
-    {{ form|raw }}
4
+
5
+    {% if applicationSubmitted is defined %}
6
+        <div class="message success">
7
+            Ihre Bewerbung wurde erfolgreich übermittelt
8
+        </div>
9
+    {% elseif hasError is defined %}
10
+        <div class="message error">
11
+            {{ errorMsg is defined ? errorMsg : 'Es ist ein Fehler aufgetreten' }}
12
+        </div>
13
+    {% else %}
14
+        {{ form|raw }}
15
+    {% endif %}
5 16
 
6 17
 {% endblock %}
... ...
@@ -13,6 +13,8 @@ declare(strict_types=1);
13 13
 namespace vossmedien\DacoreBundle\API;
14 14
 
15 15
 use Contao\System;
16
+use Symfony\Component\Mime\Part\DataPart;
17
+use Symfony\Component\Mime\Part\Multipart\FormDataPart;
16 18
 use Symfony\Component\Serializer\SerializerInterface;
17 19
 use Symfony\Contracts\HttpClient\HttpClientInterface;
18 20
 use vossmedien\DacoreBundle\Model\KatalogModel;
... ...
@@ -232,114 +234,119 @@ class Softgarden
232 234
         return false;
233 235
     }
234 236
 
235
-    public function getUserAccessToken($username, $password)
237
+    public function addApplicationInfo(array $data, string $applicationId, $token_type, $access_token)
236 238
     {
237
-        // bewerber@dacore.api / Aebekai2ail4coum
238 239
         $options = [
239
-            'auth_basic' => $this->getBasicAuthorization(),
240
-            'body' => [
241
-                'grant_type' => 'password',
242
-                'username' => $username,
243
-                'password' => $password
244
-            ]
240
+            'headers' => [
241
+                'Authorization: ' . $token_type . ' ' . $access_token,
242
+                'Content-Type: application/json'
243
+            ],
244
+            'body' => json_encode($data)
245 245
         ];
246 246
 
247
-        $response = $this->sendRequest('https://api.softgarden.io/api/rest/oauth/frontend/token',$options,'POST',true);
247
+        $response = $this->sendRequest('applications/' . $applicationId,$options,'POST');
248 248
 
249
-        if ($response->getStatusCode() == 200)
249
+        if ($response->getStatusCode() == 204)
250 250
         {
251
-            $content = $response->getContent();
252
-
253
-            return json_decode($content);
251
+            return true;
254 252
         }
255 253
 
256
-        return null;
254
+        return false;
257 255
     }
258 256
 
259
-    public function getStellenFiltered(array $params=[]): ?StellenModel
257
+    public function addApplicationFileCV(string $path, string $filename, string $mimetype, string $applicationId, $token_type, $access_token)
260 258
     {
259
+        $fields = [
260
+            'file' => DataPart::fromPath($path,$filename,$mimetype)
261
+        ];
262
+        $formData = new FormDataPart($fields);
263
+
261 264
         $options = [
262
-            'headers' => $this->getAuthorizeRequestHeader()
265
+            'headers' => array_merge([
266
+                    'Authorization: ' . $token_type . ' ' . $access_token
267
+                ],
268
+                $formData->getPreparedHeaders()->toArray()
269
+            ),
270
+            'body' => $formData->bodyToString()
263 271
         ];
264 272
 
265
-        if (count($params))
273
+        $response = $this->sendRequest('attachments/cv?applicationId=' . $applicationId,$options,'POST');
274
+
275
+        if ($response->getStatusCode() == 200)
266 276
         {
267
-            $options['query'] = $params;
277
+            return true;
268 278
         }
269 279
 
270
-        $response = $this->sendRequest('Stelle/GetStellenFiltered',$options);
280
+        return false;
281
+    }
282
+
283
+    public function addApplicationFile(string $path, string $filename, string $mimetype, string $applicationId, $token_type, $access_token)
284
+    {
285
+        $fields = [
286
+            'file' => DataPart::fromPath($path,$filename,$mimetype)
287
+        ];
288
+        $formData = new FormDataPart($fields);
271 289
 
290
+        $options = [
291
+            'headers' => array_merge([
292
+                'Authorization: ' . $token_type . ' ' . $access_token
293
+            ],
294
+                $formData->getPreparedHeaders()->toArray()
295
+            ),
296
+            'body' => $formData->bodyToString()
297
+        ];
272 298
 
299
+        $response = $this->sendRequest('attachments/?applicationId=' . $applicationId,$options,'POST');
273 300
 
274 301
         if ($response->getStatusCode() == 200)
275 302
         {
276
-            $content = $response->getContent();
277
-
278
-            /** @var StellenModel $collection */
279
-            $collection = $this->serializer->deserialize($content,StellenModel::class,'json');
280
-
281
-            return $collection;
303
+            return true;
282 304
         }
283 305
 
284
-        return null;
306
+        return false;
285 307
     }
286 308
 
287
-    public function getStelleById(string $uuid, array $params=[]): ?StelleModel
309
+    public function getUserAccessToken($username, $password)
288 310
     {
311
+        // bewerber@dacore.api / Aebekai2ail4coum
289 312
         $options = [
290
-            'headers' => $this->getAuthorizeRequestHeader(),
291
-            'query' => [
292
-                'stelleUuid' => $uuid
313
+            'auth_basic' => $this->getBasicAuthorization(),
314
+            'body' => [
315
+                'grant_type' => 'password',
316
+                'username' => $username,
317
+                'password' => $password
293 318
             ]
294 319
         ];
295 320
 
296
-        if (count($params))
297
-        {
298
-            $options['query'] = array_merge($options['query'],$params);
299
-        }
300
-
301
-        $response = $this->sendRequest('Stelle/GetStelleById',$options);
302
-
303
-
321
+        $response = $this->sendRequest('https://api.softgarden.io/api/rest/oauth/frontend/token',$options,'POST',true);
304 322
 
305 323
         if ($response->getStatusCode() == 200)
306 324
         {
307 325
             $content = $response->getContent();
308 326
 
309
-            /** @var StelleModel $model */
310
-            $model = $this->serializer->deserialize($content,StelleModel::class,'json');
311
-
312
-            return $model;
327
+            return json_decode($content);
313 328
         }
314 329
 
315 330
         return null;
316 331
     }
317 332
 
318
-    public function getKatalogByRelationName(string $entityName, string $relationName): ?KatalogModel
333
+    public function finalizeApplication(string $applicationId, $token_type, $access_token)
319 334
     {
320 335
         $options = [
321
-            'headers' => $this->getAuthorizeRequestHeader(),
322
-            'query' => [
323
-                'entityName' => $entityName,
324
-                'relationName' => $relationName
336
+            'headers' => [
337
+                'Authorization: ' . $token_type . ' ' . $access_token,
338
+                'Content-Type: application/json'
325 339
             ]
326 340
         ];
327 341
 
328
-        $response = $this->sendRequest('Katalog/GetByRelationName',$options);
329
-
330
-
342
+        $response = $this->sendRequest('applications/' . $applicationId . '/submit',$options,'POST');
331 343
 
332
-        if ($response->getStatusCode() == 200)
344
+        if ($response->getStatusCode() == 204)
333 345
         {
334
-            $content = $response->getContent();
335
-
336
-            /** @var KatalogModel $collection */
337
-            $collection = $this->serializer->deserialize($content,KatalogModel::class,'json');
338
-
339
-            return $collection;
346
+            return true;
340 347
         }
341 348
 
342
-        return null;
349
+        return false;
343 350
     }
344 351
 
345 352
 }
... ...
@@ -15,6 +15,7 @@ namespace vossmedien\DacoreBundle\Controller\Frontend\Module;
15 15
 use Contao\Controller;
16 16
 use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
17 17
 use Contao\CoreBundle\ServiceAnnotation\FrontendModule;
18
+use Contao\FormModel;
18 19
 use Contao\ModuleModel;
19 20
 use Contao\PageModel;
20 21
 use Contao\Template;
... ...
@@ -22,6 +23,8 @@ use Haste\Form\Form;
22 23
 use Symfony\Component\HttpFoundation\Request;
23 24
 use Symfony\Component\HttpFoundation\RequestStack;
24 25
 use Symfony\Component\HttpFoundation\Response;
26
+use Symfony\Component\Mime\Part\DataPart;
27
+use Symfony\Component\Mime\Part\Multipart\FormDataPart;
25 28
 use vossmedien\DacoreBundle\API\Softgarden;
26 29
 use vossmedien\DacoreBundle\Generator\CodeGenerator;
27 30
 use vossmedien\DacoreBundle\Model\VrApiUserCounterModel;
... ...
@@ -43,6 +46,11 @@ class JobApplicationFormController extends AbstractFrontendModuleController
43 46
      */
44 47
     protected $requestStack;
45 48
 
49
+    /**
50
+     * @var Form
51
+     */
52
+    protected $Form;
53
+
46 54
     public function __construct(Softgarden $api, RequestStack $requestStack)
47 55
     {
48 56
         $this->api = $api;
... ...
@@ -52,12 +60,30 @@ class JobApplicationFormController extends AbstractFrontendModuleController
52 60
 
53 61
     protected function getResponse(Template $template, ModuleModel $model, Request $request): ?Response
54 62
     {
63
+        global $objPage;
55 64
         $session = $this->requestStack->getSession();
56 65
         $arrApiUser = [];
66
+        $jobId = intval($request->get('jobid'));
67
+
68
+        if (!$jobId)
69
+        {
70
+            $pageType = $GLOBALS['TL_PTY']['error_404'];
71
+            $objHandler = new $pageType();
72
+            return $objHandler->getResponse($objPage);
73
+        }
57 74
 
58 75
 
59 76
         if (!$session->has('vr_api_user'))
60 77
         {
78
+            // DEBUG
79
+            /*$arrApiUser = [
80
+                'username' => 'bewerber3@dacore.api',
81
+                'password' => 'Ui:v%qCm<3B]'
82
+            ];
83
+            $session->set('vr_api_user',$arrApiUser);*/
84
+
85
+
86
+
61 87
             $time = time();
62 88
 
63 89
             $NewApiUser = new VrApiUserCounterModel();
... ...
@@ -79,39 +105,121 @@ class JobApplicationFormController extends AbstractFrontendModuleController
79 105
 
80 106
         if (($userToken = $this->api->getUserAccessToken($arrApiUser['username'],$arrApiUser['password'])) === null)
81 107
         {
82
-            return new Response();
108
+            $template->hasError = true;
109
+            $template->errorMsg = 'Aufgrund eines technischen Fehlers ist eine Bewerbung auf diese Stelle derzeit nicht möglich';
110
+            return $template->getResponse();
83 111
         }
84 112
 
85
-        if (!$this->api->hasApplied(32630195,$userToken->token_type,$userToken->access_token))
113
+        if ($this->api->hasApplied($jobId,$userToken->token_type,$userToken->access_token))
86 114
         {
87
-            $applicationId = $this->api->startApplication(32630195,$userToken->token_type,$userToken->access_token);
115
+            $applicationId = $this->api->getApplicationId($jobId,$userToken->token_type,$userToken->access_token);
116
+            if ($this->api->deleteApplication($applicationId,$userToken->token_type,$userToken->access_token))
117
+            {
118
+                $applicationId = $this->api->startApplication($jobId,$userToken->token_type,$userToken->access_token);
119
+            }
88 120
         } else {
89
-            $applicationId = $this->api->getApplicationId(32630195,$userToken->token_type,$userToken->access_token);
121
+            $applicationId = $this->api->startApplication($jobId,$userToken->token_type,$userToken->access_token);
90 122
         }
91 123
 
124
+
92 125
         $application = $this->api->getApplication($applicationId,$userToken->token_type,$userToken->access_token);
93
-dump($application);
126
+
94 127
         if ($application !== null && isset($application->applicationEditable) && !$application->applicationEditable)
95 128
         {
96
-            return new Response('Already applied');
129
+            $template->hasError = true;
130
+            $template->errorMsg = 'Sie haben sich bereits auf diese Stelle beworben';
131
+            return $template->getResponse();
97 132
         }
98 133
         elseif ($application === null || !isset($application->applicationId))
99 134
         {
100
-            return new Response('Bewerbung derzeit nicht möglich');
135
+            $template->hasError = true;
136
+            $template->errorMsg = 'Aufgrund eines technischen Fehlers ist eine Bewerbung auf diese Stelle derzeit nicht möglich';
137
+            return $template->getResponse();
101 138
         }
102 139
 
103 140
 
104 141
         $strFormId = 'jobapplication-form';
105 142
 
106
-        $Form = new Form($strFormId,'POST', function($objHaste) {
143
+        $this->Form = new Form($strFormId,'POST', function($objHaste) {
107 144
             return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
108 145
         });
109 146
 
110
-        $Form->addFieldsFromFormGenerator($model->form, function(&$strField, &$arrDca) {
147
+        $this->Form->addFieldsFromFormGenerator($model->form, function(&$strField, &$arrDca) {
111 148
             return true;
112 149
         });
113 150
 
114
-        $template->form = $Form->generate();
151
+        // Form submitted
152
+        if ($this->Form->validate())
153
+        {
154
+            $blnSuccess = false;
155
+            $blnDoNotSubmit = false;
156
+            $applicationProfileDTO = [];
157
+
158
+            $this->addValueIfPresent($applicationProfileDTO,'firstname');
159
+            $this->addValueIfPresent($applicationProfileDTO,'lastname');
160
+            $this->addValueIfPresent($applicationProfileDTO,'sex');
161
+            $this->addValueIfPresent($applicationProfileDTO,'email');
162
+            $this->addValueIfPresent($applicationProfileDTO,'phone');
163
+            $this->addValueIfPresent($applicationProfileDTO,'coverLetterText');
164
+
165
+            if (!$this->api->addApplicationInfo($applicationProfileDTO, $applicationId, $userToken->token_type,$userToken->access_token))
166
+            {
167
+                $blnDoNotSubmit = true;
168
+            }
169
+
170
+            if ($this->Form->hasUploads()) {
171
+                if (isset($_SESSION['FILES']['cv']))
172
+                {
173
+                    if (!$this->api->addApplicationFileCV($_SESSION['FILES']['cv']['tmp_name'], $_SESSION['FILES']['cv']['name'], $_SESSION['FILES']['cv']['type'],$applicationId,$userToken->token_type,$userToken->access_token))
174
+                    {
175
+                        $blnDoNotSubmit = true;
176
+                    }
177
+                }
178
+
179
+                if (isset($_SESSION['FILES']['coverLetterFile']))
180
+                {
181
+                    if (!$this->api->addApplicationFile($_SESSION['FILES']['coverLetterFile']['tmp_name'], $_SESSION['FILES']['coverLetterFile']['name'], $_SESSION['FILES']['coverLetterFile']['type'],$applicationId,$userToken->token_type,$userToken->access_token))
182
+                    {
183
+                        $blnDoNotSubmit = true;
184
+                    }
185
+                }
186
+
187
+                for ($i = 1; $i < 4; $i++)
188
+                {
189
+                    $fieldname = 'attachment_' . $i;
190
+
191
+                    if (isset($_SESSION['FILES'][$fieldname]))
192
+                    {
193
+                        if (!$this->api->addApplicationFile($_SESSION['FILES'][$fieldname]['tmp_name'], $_SESSION['FILES'][$fieldname]['name'], $_SESSION['FILES'][$fieldname]['type'],$applicationId,$userToken->token_type,$userToken->access_token))
194
+                        {
195
+                            $blnDoNotSubmit = true;
196
+                        }
197
+                    }
198
+                }
199
+            }
200
+
201
+            if (!$blnDoNotSubmit)
202
+            {
203
+                // Finalize Application
204
+                $blnSuccess = $this->api->finalizeApplication($applicationId,$userToken->token_type,$userToken->access_token);
205
+                if ($blnSuccess && ($FormModel = FormModel::findByPk($model->form)) !== null && $FormModel->jumpTo && ($successPage = PageModel::findByPk($FormModel->jumpTo)) !== null)
206
+                {
207
+                    Controller::redirect($successPage->getFrontendUrl());
208
+                }
209
+
210
+                if ($blnSuccess) {
211
+                    $template->applicationSubmitted = true;
212
+                }
213
+            }
214
+
215
+            if (!$blnDoNotSubmit || !$blnSuccess)
216
+            {
217
+                $template->hasError = true;
218
+                $template->errorMsg = 'Ihre Bewerbung konnte aufgrund eines Fehlers nicht übermittelt werden. Bitte versuchen Sie es erneut oder setzen Sie sich mit uns in Verbindung.';
219
+            }
220
+        }
221
+
222
+        $template->form = $this->Form->generate();
115 223
 
116 224
         return $template->getResponse();
117 225
     }
... ...
@@ -133,4 +241,20 @@ dump($application);
133 241
         return $password;
134 242
     }
135 243
 
244
+    protected function addValueIfPresent(array &$data, string $fieldname)
245
+    {
246
+        if (!$this->Form->isSubmitted() || !$this->Form->hasFormField($fieldname))
247
+        {
248
+            return false;
249
+        }
250
+
251
+        if (($value = $this->Form->fetch($fieldname)) !== null)
252
+        {
253
+            $data[$fieldname] = $value;
254
+            return true;
255
+        }
256
+
257
+        return false;
258
+    }
259
+
136 260
 }