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 ;
2020-05-06 04:42:55 +00:00
use App\Form ;
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
{
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 )
{
2020-06-27 18:15:33 +00:00
$this -> authorize ( 'create' , Form :: class );
2020-07-15 05:48:49 +00:00
$fields = $request -> all ();
2020-06-27 18:15:33 +00:00
2020-10-10 16:30:26 +00:00
if ( count ( $fields ) == 2 ) {
2020-10-08 23:56:11 +00:00
// form is probably empty, since forms with fields will alawys have more than 2 items
$request -> session () -> flash ( 'error' , 'Sorry, but you may not create empty forms.' );
2020-10-10 16:30:26 +00:00
2020-10-08 23:56:11 +00:00
return redirect () -> to ( route ( 'showForms' ));
}
2020-07-15 05:48:49 +00:00
$contextValidation = ContextAwareValidator :: getValidator ( $fields , true , true );
2020-05-06 04:42:55 +00:00
2020-10-10 16:30:26 +00:00
if ( ! $contextValidation -> get ( 'validator' ) -> fails ()) {
2020-07-15 05:48:49 +00:00
$storableFormStructure = $contextValidation -> get ( 'structure' );
2020-05-06 04:42:55 +00:00
Form :: create (
[
2020-07-15 05:48:49 +00:00
'formName' => $fields [ 'formName' ],
2020-05-06 04:42:55 +00:00
'formStructure' => $storableFormStructure ,
2020-10-10 16:30:26 +00:00
'formStatus' => 'ACTIVE' ,
2020-05-06 04:42:55 +00:00
]
);
2020-05-06 22:16:34 +00:00
$request -> session () -> flash ( 'success' , 'Form created! You can now link this form to a vacancy.' );
2020-10-10 16:30:26 +00:00
2020-05-06 23:36:33 +00:00
return redirect () -> to ( route ( 'showForms' ));
2020-05-06 04:42:55 +00:00
}
2020-07-15 05:48:49 +00:00
$request -> session () -> flash ( 'errors' , $contextValidation -> get ( 'validator' ) -> errors () -> getMessages ());
2020-10-10 16:30:26 +00:00
2020-05-06 04:42:55 +00:00
return redirect () -> back ();
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 );
2020-07-12 05:39:39 +00:00
$deletable = true ;
2020-05-06 23:36:33 +00:00
2020-10-10 16:30:26 +00:00
if ( ! is_null ( $form ) && ! is_null ( $form -> vacancies ) && $form -> vacancies -> count () !== 0 || ! is_null ( $form -> responses )) {
$deletable = false ;
2020-07-12 05:39:39 +00:00
}
2020-07-12 18:36:12 +00:00
2020-10-10 16:30:26 +00:00
if ( $deletable ) {
$form -> delete ();
2020-05-06 23:36:33 +00:00
2020-10-10 16:30:26 +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-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 );
2020-07-15 05:48:49 +00:00
2020-10-10 16:30:26 +00:00
$contextValidation = ContextAwareValidator :: getValidator ( $request -> all (), true );
$this -> authorize ( 'update' , $form );
2020-07-15 05:48:49 +00:00
2020-10-10 16:30:26 +00:00
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 ();
2020-07-15 05:48:49 +00:00
2020-10-10 16:30:26 +00:00
$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 ());
}
2020-07-15 05:48:49 +00:00
2020-10-10 16:30:26 +00:00
return redirect () -> to ( route ( 'previewForm' , [ 'form' => $form -> id ]));
2020-07-12 18:36:12 +00:00
}
2020-04-29 17:15:54 +00:00
}