staffmanager/app/Http/Controllers/FormController.php

132 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2020-05-06 04:42:55 +00:00
use App\Form;
2020-10-21 00:01:41 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use ContextAwareValidator;
class FormController extends Controller
{
public function index()
{
$forms = Form::all();
$this->authorize('viewAny', Form::class);
2020-05-06 23:36:33 +00:00
return view('dashboard.administration.forms')
->with('forms', $forms);
}
public function showFormBuilder()
{
$this->authorize('viewFormbuilder', Form::class);
return view('dashboard.administration.formbuilder');
}
public function saveForm(Request $request)
{
$this->authorize('create', Form::class);
$fields = $request->all();
$contextValidation = ContextAwareValidator::getValidator($fields, true, true);
2020-05-06 04:42:55 +00:00
if (!$contextValidation->get('validator')->fails())
{
$storableFormStructure = $contextValidation->get('structure');
2020-05-06 04:42:55 +00:00
Form::create(
[
'formName' => $fields['formName'],
2020-05-06 04:42:55 +00:00
'formStructure' => $storableFormStructure,
'formStatus' => 'ACTIVE'
2020-05-06 04:42:55 +00:00
]
);
$request->session()->flash('success', 'Form created! You can now link this form to a vacancy.');
2020-05-06 23:36:33 +00:00
return redirect()->to(route('showForms'));
2020-05-06 04:42:55 +00:00
}
$request->session()->flash('errors', $contextValidation->get('validator')->errors()->getMessages());
2020-05-06 04:42:55 +00:00
return redirect()->back();
}
public function destroy(Request $request, Form $form)
2020-05-06 23:36:33 +00:00
{
$this->authorize('delete', $form);
$deletable = true;
2020-05-06 23:36:33 +00:00
if (!is_null($form) && !is_null($form->vacancies) && $form->vacancies->count() !== 0 || !is_null($form->responses))
{
$deletable = false;
}
2020-07-12 18:36:12 +00:00
if ($deletable)
{
$form->delete();
2020-05-06 23:36:33 +00:00
$request->session()->flash('success', 'Form deleted successfully.');
}
else
{
$request->session()->flash('error', 'You cannot delete this form because it\'s tied to one or more applications and ranks, or because it doesn\'t exist.');
2020-05-06 23:36:33 +00:00
}
return redirect()->back();
2020-05-06 23:36:33 +00:00
}
2020-07-12 18:36:12 +00:00
public function preview(Request $request, Form $form)
{
$this->authorize('viewAny', Form::class);
2020-07-12 18:36:12 +00:00
return view('dashboard.administration.formpreview')
->with('form', json_decode($form->formStructure, true))
->with('title', $form->formName)
->with('formID', $form->id);
}
public function edit(Request $request, Form $form)
{
$this->authorize('update', $form);
return view('dashboard.administration.editform')
->with('formStructure', json_decode($form->formStructure, true))
->with('title', $form->formName)
->with('formID', $form->id);
}
public function update(Request $request, Form $form)
{
$this->authorize('update', $form);
$contextValidation = ContextAwareValidator::getValidator($request->all(), true);
$this->authorize('update', $form);
if (!$contextValidation->get('validator')->fails())
{
// Add the new structure into the form. New, subsquent fields will be identified by the "new" prefix
// This prefix doesn't actually change the app's behavior when it receives applications.
// Additionally, old applications won't of course display new and updated fields, because we can't travel into the past and get data for them
$form->formStructure = $contextValidation->get('structure');
$form->save();
$request->session()->flash('success', 'Hooray! Your form was updated. New applications for it\'s vacancy will use it.');
}
else
{
$request->session()->flash('errors', $contextValidation->get('validator')->errors()->getMessages());
}
return redirect()->to(route('previewForm', ['form' => $form->id]));
2020-07-12 18:36:12 +00:00
}
}