. */ namespace App\Http\Controllers; use App\Exceptions\FormHasConstraintsException; use App\Form; use App\Services\FormManagementService; use ContextAwareValidator; use Illuminate\Http\Request; class FormController extends Controller { private $formService; public function __construct(FormManagementService $formService) { $this->formService = $formService; } public function index() { $forms = Form::all(); $this->authorize('viewAny', Form::class); 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) { $form = $this->formService->addForm($request->all()); // Form is boolean or array if ($form) { return redirect() ->back() ->with('success', __('Form created!')); } return redirect() ->back() ->with('errors', $form); } public function destroy(Request $request, Form $form) { $this->authorize('delete', $form); try { $this->formService->deleteForm($form); return redirect() ->back() ->with('success', __('Form deleted successfuly')); } catch (FormHasConstraintsException $ex) { return redirect() ->back() ->with('error', $ex->getMessage()); } } public function preview(Request $request, Form $form) { $this->authorize('viewAny', Form::class); 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); $updatedForm = $this->formService->updateForm($form, $request->all()); if ($updatedForm instanceof Form) { return redirect()->to(route('previewForm', ['form' => $updatedForm->id])); } // array of errors return redirect() ->back() ->with('errors', $updatedForm); } }