2020-04-29 17:15:54 +00:00
|
|
|
<?php
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2020 Miguel Nogueira
|
|
|
|
*
|
|
|
|
* This file is part of Raspberry Staff Manager.
|
|
|
|
*
|
|
|
|
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Raspberry Staff Manager is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-04-29 17:15:54 +00:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2021-09-03 23:44:54 +00:00
|
|
|
use App\Exceptions\EmptyFormException;
|
2021-07-25 21:54:15 +00:00
|
|
|
use App\Exceptions\FormHasConstraintsException;
|
2020-05-06 04:42:55 +00:00
|
|
|
use App\Form;
|
2021-07-25 21:54:15 +00:00
|
|
|
use App\Services\FormManagementService;
|
2020-07-15 05:48:49 +00:00
|
|
|
use ContextAwareValidator;
|
2020-10-10 16:30:26 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-07-15 05:48:49 +00:00
|
|
|
|
2020-04-29 17:15:54 +00:00
|
|
|
class FormController extends Controller
|
|
|
|
{
|
2021-07-25 21:54:15 +00:00
|
|
|
private $formService;
|
|
|
|
|
|
|
|
public function __construct(FormManagementService $formService) {
|
|
|
|
$this->formService = $formService;
|
|
|
|
}
|
|
|
|
|
2020-05-05 04:25:56 +00:00
|
|
|
public function index()
|
|
|
|
{
|
2020-06-27 18:15:33 +00:00
|
|
|
$forms = Form::all();
|
|
|
|
$this->authorize('viewAny', Form::class);
|
|
|
|
|
2020-05-06 23:36:33 +00:00
|
|
|
return view('dashboard.administration.forms')
|
2020-06-27 18:15:33 +00:00
|
|
|
->with('forms', $forms);
|
2020-05-06 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function showFormBuilder()
|
|
|
|
{
|
2020-06-27 18:15:33 +00:00
|
|
|
$this->authorize('viewFormbuilder', Form::class);
|
2020-10-10 16:30:26 +00:00
|
|
|
|
2020-05-06 22:16:34 +00:00
|
|
|
return view('dashboard.administration.formbuilder');
|
2020-05-05 04:25:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 02:44:39 +00:00
|
|
|
public function saveForm(Request $request)
|
|
|
|
{
|
2021-09-03 23:44:54 +00:00
|
|
|
try {
|
|
|
|
$form = $this->formService->addForm($request->all());
|
|
|
|
}
|
|
|
|
catch (EmptyFormException $ex)
|
|
|
|
{
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('exception', $ex->getMessage());
|
|
|
|
}
|
2021-07-25 21:54:15 +00:00
|
|
|
|
|
|
|
// Form is boolean or array
|
|
|
|
if ($form)
|
|
|
|
{
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success', __('Form created!'));
|
2020-10-08 23:56:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('errors', $form);
|
2020-05-06 02:44:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 20:21:28 +00:00
|
|
|
public function destroy(Request $request, Form $form)
|
2020-05-06 23:36:33 +00:00
|
|
|
{
|
2020-06-27 18:15:33 +00:00
|
|
|
$this->authorize('delete', $form);
|
2021-07-25 21:54:15 +00:00
|
|
|
try {
|
2021-01-27 02:23:30 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
$this->formService->deleteForm($form);
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success', __('Form deleted successfuly'));
|
2020-05-06 23:36:33 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
} catch (FormHasConstraintsException $ex) {
|
2020-07-12 18:36:12 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('error', $ex->getMessage());
|
2020-05-06 23:36:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 18:36:12 +00:00
|
|
|
public function preview(Request $request, Form $form)
|
|
|
|
{
|
2020-07-16 20:21:28 +00:00
|
|
|
$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))
|
2020-07-15 05:48:49 +00:00
|
|
|
->with('title', $form->formName)
|
|
|
|
->with('formID', $form->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit(Request $request, Form $form)
|
|
|
|
{
|
2020-10-10 16:30:26 +00:00
|
|
|
$this->authorize('update', $form);
|
2020-07-16 20:21:28 +00:00
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
return view('dashboard.administration.editform')
|
2020-07-15 05:48:49 +00:00
|
|
|
->with('formStructure', json_decode($form->formStructure, true))
|
|
|
|
->with('title', $form->formName)
|
|
|
|
->with('formID', $form->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(Request $request, Form $form)
|
|
|
|
{
|
2020-10-10 16:30:26 +00:00
|
|
|
$this->authorize('update', $form);
|
2021-07-25 21:54:15 +00:00
|
|
|
$updatedForm = $this->formService->updateForm($form, $request->all());
|
2020-07-15 05:48:49 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
if ($updatedForm instanceof Form) {
|
|
|
|
return redirect()->to(route('previewForm', ['form' => $updatedForm->id]));
|
2020-10-10 16:30:26 +00:00
|
|
|
}
|
2020-07-15 05:48:49 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
// array of errors
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('errors', $updatedForm);
|
2020-07-12 18:36:12 +00:00
|
|
|
}
|
2020-04-29 17:15:54 +00:00
|
|
|
}
|