2020-04-29 17:15:54 +00:00
< ? php
namespace App\Http\Controllers ;
2020-05-06 04:42:55 +00:00
use App\Form ;
2020-04-29 17:15:54 +00:00
use Illuminate\Http\Request ;
2020-05-06 04:42:55 +00:00
use Illuminate\Support\Facades\Validator ;
2020-06-27 18:15:33 +00:00
use Illuminate\Support\Facades\Auth ;
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-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-05-06 04:42:55 +00:00
2020-06-27 18:15:33 +00:00
$this -> authorize ( 'create' , Form :: class );
2020-05-06 04:42:55 +00:00
$formFields = $request -> all ();
$formStructure = [];
$excludedNames = [
'_token' ,
'formName' // It's added outside the loop. Not excluding causes unwanted duplication.
];
$validator = [
'formName' => 'required|string|max:100'
];
foreach ( $formFields as $fieldName => $field )
{
if ( ! in_array ( $fieldName , $excludedNames ))
{
$validator [ $fieldName . " .0 " ] = 'required|string' ;
$validator [ $fieldName . " .1 " ] = 'required|string' ;
$formStructure [ 'fields' ][ $fieldName ][ 'title' ] = $field [ 0 ];
$formStructure [ 'fields' ][ $fieldName ][ 'type' ] = $field [ 1 ];
}
}
$validation = Validator :: make ( $formFields , $validator );
if ( ! $validation -> fails ())
{
$storableFormStructure = json_encode ( $formStructure );
Form :: create (
[
'formName' => $formFields [ 'formName' ],
'formStructure' => $storableFormStructure ,
'formStatus' => 'ACTIVE'
]
);
2020-05-06 22:16:34 +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
}
2020-05-06 22:16:34 +00:00
$request -> session () -> flash ( 'errors' , $validation -> errors () -> getMessages ());
2020-05-06 04:42:55 +00:00
return redirect () -> back ();
2020-05-06 02:44:39 +00:00
}
2020-05-06 23:36:33 +00:00
public function destroy ( Request $request , $id )
{
$form = Form :: find ( $id );
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-07-12 05:39:39 +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
2020-07-12 05:39:39 +00:00
if ( $deletable )
2020-05-06 23:36:33 +00:00
{
2020-07-12 05:39:39 +00:00
$form -> delete ();
2020-05-06 23:36:33 +00:00
2020-07-12 05:39:39 +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 )
{
return view ( 'dashboard.administration.formpreview' )
-> with ( 'form' , json_decode ( $form -> formStructure , true ))
-> with ( 'title' , $form -> formName );
}
2020-04-29 17:15:54 +00:00
}