Browse code

Add some more api error handling situations

Benjamin Roth authored on19/06/2023 17:18:55
Showing1 changed files
... ...
@@ -16,6 +16,7 @@ use Contao\Controller;
16 16
 use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
17 17
 use Contao\CoreBundle\ServiceAnnotation\FrontendModule;
18 18
 use Contao\FormModel;
19
+use Contao\FrontendTemplate;
19 20
 use Contao\ModuleModel;
20 21
 use Contao\PageModel;
21 22
 use Contao\Template;
... ...
@@ -98,6 +99,13 @@ class JobApplicationFormController extends AbstractFrontendModuleController
98 99
             if ($this->api->createApplicantUser($arrApiUser['username'],$arrApiUser['password']))
99 100
             {
100 101
                 $session->set('vr_api_user',$arrApiUser);
102
+
103
+                if (($userToken = $this->api->getUserAccessToken($arrApiUser['username'],$arrApiUser['password'])) === null)
104
+                {
105
+                    $template = new FrontendTemplate('mod_dacore_jobapplication_userInit');
106
+                    return $template->getResponse();
107
+                }
108
+            } else {
101 109
                 Controller::reload();
102 110
             }
103 111
         } else {
Browse code

Updates

Benjamin Roth authored on15/06/2023 18:26:12
Showing1 changed files
... ...
@@ -98,6 +98,7 @@ class JobApplicationFormController extends AbstractFrontendModuleController
98 98
             if ($this->api->createApplicantUser($arrApiUser['username'],$arrApiUser['password']))
99 99
             {
100 100
                 $session->set('vr_api_user',$arrApiUser);
101
+                Controller::reload();
101 102
             }
102 103
         } else {
103 104
             $arrApiUser = $session->get('vr_api_user');
... ...
@@ -106,7 +107,7 @@ class JobApplicationFormController extends AbstractFrontendModuleController
106 107
         if (($userToken = $this->api->getUserAccessToken($arrApiUser['username'],$arrApiUser['password'])) === null)
107 108
         {
108 109
             $template->hasError = true;
109
-            $template->errorMsg = 'Aufgrund eines technischen Fehlers ist eine Bewerbung auf diese Stelle derzeit nicht möglich';
110
+            $template->errorMsg = 'Aufgrund eines technischen Fehlers ist eine Bewerbung auf diese Stelle derzeit nicht möglich [ERR-1001]';
110 111
             return $template->getResponse();
111 112
         }
112 113
 
... ...
@@ -127,17 +128,16 @@ class JobApplicationFormController extends AbstractFrontendModuleController
127 128
         if ($application !== null && isset($application->applicationEditable) && !$application->applicationEditable)
128 129
         {
129 130
             $template->hasError = true;
130
-            $template->errorMsg = 'Sie haben sich bereits auf diese Stelle beworben';
131
+            $template->errorMsg = 'Sie haben sich bereits auf diese Stelle beworben [ERR-2001]';
131 132
             return $template->getResponse();
132 133
         }
133 134
         elseif ($application === null || !isset($application->applicationId))
134 135
         {
135 136
             $template->hasError = true;
136
-            $template->errorMsg = 'Aufgrund eines technischen Fehlers ist eine Bewerbung auf diese Stelle derzeit nicht möglich';
137
+            $template->errorMsg = 'Aufgrund eines technischen Fehlers ist eine Bewerbung auf diese Stelle derzeit nicht möglich [ERR-1002]';
137 138
             return $template->getResponse();
138 139
         }
139 140
 
140
-
141 141
         $strFormId = 'jobapplication-form';
142 142
 
143 143
         $this->Form = new Form($strFormId,'POST', function($objHaste) {
Browse code

Updates

Benjamin Roth authored on09/06/2023 14:31:40
Showing1 changed files
... ...
@@ -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
 }
Browse code

Updates

Benjamin Roth authored on01/06/2023 23:39:31
Showing1 changed files
... ...
@@ -12,6 +12,7 @@ declare(strict_types=1);
12 12
 
13 13
 namespace vossmedien\DacoreBundle\Controller\Frontend\Module;
14 14
 
15
+use Contao\Controller;
15 16
 use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
16 17
 use Contao\CoreBundle\ServiceAnnotation\FrontendModule;
17 18
 use Contao\ModuleModel;
... ...
@@ -19,8 +20,11 @@ use Contao\PageModel;
19 20
 use Contao\Template;
20 21
 use Haste\Form\Form;
21 22
 use Symfony\Component\HttpFoundation\Request;
23
+use Symfony\Component\HttpFoundation\RequestStack;
22 24
 use Symfony\Component\HttpFoundation\Response;
23 25
 use vossmedien\DacoreBundle\API\Softgarden;
26
+use vossmedien\DacoreBundle\Generator\CodeGenerator;
27
+use vossmedien\DacoreBundle\Model\VrApiUserCounterModel;
24 28
 
25 29
 /**
26 30
  * @FrontendModule(JobApplicationFormController::TYPE, category="miscellaneous")
... ...
@@ -34,15 +38,46 @@ class JobApplicationFormController extends AbstractFrontendModuleController
34 38
      */
35 39
     protected $api;
36 40
 
37
-    public function __construct(Softgarden $api)
41
+    /**
42
+     * @var RequestStack
43
+     */
44
+    protected $requestStack;
45
+
46
+    public function __construct(Softgarden $api, RequestStack $requestStack)
38 47
     {
39 48
         $this->api = $api;
49
+        $this->requestStack = $requestStack;
40 50
     }
41 51
 
42 52
 
43 53
     protected function getResponse(Template $template, ModuleModel $model, Request $request): ?Response
44 54
     {
45
-        if (($userToken = $this->api->getUserAccessToken('bewerber@dacore.api','Aebekai2ail4coum')) === null)
55
+        $session = $this->requestStack->getSession();
56
+        $arrApiUser = [];
57
+
58
+
59
+        if (!$session->has('vr_api_user'))
60
+        {
61
+            $time = time();
62
+
63
+            $NewApiUser = new VrApiUserCounterModel();
64
+            $NewApiUser->setRow(['tstamp'=>$time,'created'=>$time]);
65
+            $NewApiUser->save()->refresh();
66
+
67
+            $arrApiUser = [
68
+                'username' => 'bewerber' . $NewApiUser->id . '@dacore.api',
69
+                'password' => $this->generatePassword()
70
+            ];
71
+
72
+            if ($this->api->createApplicantUser($arrApiUser['username'],$arrApiUser['password']))
73
+            {
74
+                $session->set('vr_api_user',$arrApiUser);
75
+            }
76
+        } else {
77
+            $arrApiUser = $session->get('vr_api_user');
78
+        }
79
+
80
+        if (($userToken = $this->api->getUserAccessToken($arrApiUser['username'],$arrApiUser['password'])) === null)
46 81
         {
47 82
             return new Response();
48 83
         }
... ...
@@ -55,7 +90,7 @@ class JobApplicationFormController extends AbstractFrontendModuleController
55 90
         }
56 91
 
57 92
         $application = $this->api->getApplication($applicationId,$userToken->token_type,$userToken->access_token);
58
-
93
+dump($application);
59 94
         if ($application !== null && isset($application->applicationEditable) && !$application->applicationEditable)
60 95
         {
61 96
             return new Response('Already applied');
... ...
@@ -81,4 +116,21 @@ class JobApplicationFormController extends AbstractFrontendModuleController
81 116
         return $template->getResponse();
82 117
     }
83 118
 
119
+    protected function generatePassword()
120
+    {
121
+        $digits    = array_flip(range('0', '9'));
122
+        $lowercase = array_flip(range('a', 'z'));
123
+        $uppercase = array_flip(range('A', 'Z'));
124
+        $special   = array_flip(str_split('!@#$%^&*()_+=-}{[}]\|;:<>?/'));
125
+        $combined  = array_merge($digits, $lowercase, $uppercase, $special);
126
+
127
+        $password  = str_shuffle(array_rand($digits) .
128
+            array_rand($lowercase) .
129
+            array_rand($uppercase) .
130
+            array_rand($special) .
131
+            implode(array_rand($combined, 8)));
132
+
133
+        return $password;
134
+    }
135
+
84 136
 }
Browse code

Updates

Benjamin Roth authored on01/06/2023 21:51:23
Showing1 changed files
... ...
@@ -42,6 +42,30 @@ class JobApplicationFormController extends AbstractFrontendModuleController
42 42
 
43 43
     protected function getResponse(Template $template, ModuleModel $model, Request $request): ?Response
44 44
     {
45
+        if (($userToken = $this->api->getUserAccessToken('bewerber@dacore.api','Aebekai2ail4coum')) === null)
46
+        {
47
+            return new Response();
48
+        }
49
+
50
+        if (!$this->api->hasApplied(32630195,$userToken->token_type,$userToken->access_token))
51
+        {
52
+            $applicationId = $this->api->startApplication(32630195,$userToken->token_type,$userToken->access_token);
53
+        } else {
54
+            $applicationId = $this->api->getApplicationId(32630195,$userToken->token_type,$userToken->access_token);
55
+        }
56
+
57
+        $application = $this->api->getApplication($applicationId,$userToken->token_type,$userToken->access_token);
58
+
59
+        if ($application !== null && isset($application->applicationEditable) && !$application->applicationEditable)
60
+        {
61
+            return new Response('Already applied');
62
+        }
63
+        elseif ($application === null || !isset($application->applicationId))
64
+        {
65
+            return new Response('Bewerbung derzeit nicht möglich');
66
+        }
67
+
68
+
45 69
         $strFormId = 'jobapplication-form';
46 70
 
47 71
         $Form = new Form($strFormId,'POST', function($objHaste) {
Browse code

Initial commit

Benjamin Roth authored on01/06/2023 13:12:27
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,60 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of dacore bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license commercial
11
+ */
12
+
13
+namespace vossmedien\DacoreBundle\Controller\Frontend\Module;
14
+
15
+use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
16
+use Contao\CoreBundle\ServiceAnnotation\FrontendModule;
17
+use Contao\ModuleModel;
18
+use Contao\PageModel;
19
+use Contao\Template;
20
+use Haste\Form\Form;
21
+use Symfony\Component\HttpFoundation\Request;
22
+use Symfony\Component\HttpFoundation\Response;
23
+use vossmedien\DacoreBundle\API\Softgarden;
24
+
25
+/**
26
+ * @FrontendModule(JobApplicationFormController::TYPE, category="miscellaneous")
27
+ */
28
+class JobApplicationFormController extends AbstractFrontendModuleController
29
+{
30
+    public const TYPE = 'dacore_jobapplication';
31
+
32
+    /**
33
+     * @var Softgarden
34
+     */
35
+    protected $api;
36
+
37
+    public function __construct(Softgarden $api)
38
+    {
39
+        $this->api = $api;
40
+    }
41
+
42
+
43
+    protected function getResponse(Template $template, ModuleModel $model, Request $request): ?Response
44
+    {
45
+        $strFormId = 'jobapplication-form';
46
+
47
+        $Form = new Form($strFormId,'POST', function($objHaste) {
48
+            return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
49
+        });
50
+
51
+        $Form->addFieldsFromFormGenerator($model->form, function(&$strField, &$arrDca) {
52
+            return true;
53
+        });
54
+
55
+        $template->form = $Form->generate();
56
+
57
+        return $template->getResponse();
58
+    }
59
+
60
+}