Add Dynamic Form Processing for Custom Forms
This commit is contained in:
@@ -2,8 +2,13 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Application;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Response;
|
||||
use App\Vacancy;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class ApplicationController extends Controller
|
||||
{
|
||||
@@ -59,4 +64,66 @@ class ApplicationController extends Controller
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function saveApplicationAnswers(Request $request, $vacancySlug)
|
||||
{
|
||||
$vacancy = Vacancy::with('forms')->where('vacancySlug', $vacancySlug)->get();
|
||||
|
||||
Log::info('Processing new application!');
|
||||
|
||||
$formStructure = json_decode($vacancy->first()->forms->formStructure, true);
|
||||
$responseStructure = [];
|
||||
|
||||
$excludedNames = [
|
||||
'_token',
|
||||
];
|
||||
|
||||
$validator = [];
|
||||
|
||||
foreach($request->all() as $fieldName => $value)
|
||||
{
|
||||
if(!in_array($fieldName, $excludedNames))
|
||||
{
|
||||
$validator[$fieldName] = 'required|string';
|
||||
|
||||
$responseStructure['responses'][$fieldName]['type'] = $formStructure['fields'][$fieldName]['type'] ?? 'Unavailable';
|
||||
$responseStructure['responses'][$fieldName]['title'] = $formStructure['fields'][$fieldName]['title'];
|
||||
$responseStructure['responses'][$fieldName]['response'] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
Log::info('Built response & validator structure!');
|
||||
|
||||
$validation = Validator::make($request->all(), $validator);
|
||||
|
||||
if (!$validation->fails())
|
||||
{
|
||||
$response = Response::create([
|
||||
'responseFormID' => $vacancy->first()->forms->id,
|
||||
'associatedVacancyID' => $vacancy->first()->id, // Since a form can be used by multiple vacancies, we can only know which specific vacancy this response ties to by using a vacancy ID
|
||||
'responseData' => json_encode($responseStructure)
|
||||
]);
|
||||
|
||||
Log::info('Registered form response for user ' . Auth::user()->name . ' for vacancy ' . $vacancy->first()->vacancyName);
|
||||
|
||||
Application::create([
|
||||
'applicantUserID' => Auth::user()->id,
|
||||
'applicantFormResponseID' => $response->id,
|
||||
'applicationStatus' => 'STAGE_SUBMITTED',
|
||||
]);
|
||||
|
||||
Log::info('Submitted application for user ' . Auth::user()->name . ' with response ID' . $response->id);
|
||||
|
||||
$request->session()->flash('success', 'Thank you for your application! It will be reviewed as soon as possible.');
|
||||
return redirect()->to(route('userPendingApps'));
|
||||
}
|
||||
else
|
||||
{
|
||||
Log::warning('Application form for ' . Auth::user()->name . ' contained errors, resetting!');
|
||||
$request->session()->flash('error', 'There are one or more errors in your application. Please make sure none of your fields are empty, since they are all required.');
|
||||
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
@@ -14,6 +14,9 @@ class HomeController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// TODO: Relationships for Applications, Users and Responses
|
||||
// Also prevent apps if user already has one in the space of 30d
|
||||
// Display apps in the relevant menus
|
||||
return view('home')
|
||||
->with('positions', Vacancy::where('vacancyStatus', 'OPEN')->get());
|
||||
}
|
||||
|
Reference in New Issue
Block a user