Skip to content

Commit

Permalink
Implement forms
Browse files Browse the repository at this point in the history
  • Loading branch information
mryan43 committed Oct 20, 2018
1 parent 0c53853 commit 236318f
Show file tree
Hide file tree
Showing 26 changed files with 1,285 additions and 34 deletions.
12 changes: 12 additions & 0 deletions app/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@

class Event extends Model
{

protected $dates = [
'created_at',
'updated_at',
'deleted_at',
'time'
];

//
public function fields()
{
return $this->belongsToMany('App\Field', 'event_fields');
}
}
2 changes: 1 addition & 1 deletion app/EventRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class EventRegistration extends Model
{
//
protected $fillable = ['user_id', 'event_id'];
}
5 changes: 5 additions & 0 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function index()
//
}


public function export($id)
{
//
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LoginController extends Controller
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/events';

/**
* Create a new controller instance.
Expand Down
89 changes: 81 additions & 8 deletions app/Http/Controllers/EventRegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use App\Event;
use App\EventRegistration;
use App\RegistrationValue;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class EventRegistrationController extends Controller
{
Expand All @@ -25,15 +28,19 @@ public function index()
*/
public function create($eventId)
{
$registration = EventRegistration::where('event_id', $eventId)->where('user_id', auth()->user()->id);
$registration = EventRegistration::where('event_id', $eventId)->where('user_id', 43/*auth()->user()->id*/);

if ($registration != null){
return redirect()->route('event.registration.show', ['event' => $eventId,'registration' => $registration]);
if ($registration->exists()){
return redirect()->route('events.registration.show', ['event' => $eventId,'registration' => $registration->first()->id]);
}

$event = Event::findOrFail($eventId);

return view("events.registration.form", ['event' => $event]);
return view("registrations.form", [
'event' => $event,
'user' => ['id' => 43]/*auth()->user()*/,
'mode' => 'create'
]);
}

/**
Expand All @@ -42,9 +49,56 @@ public function create($eventId)
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store($eventId, Request $request)
{
//
$request->validate([
'field-1'=>'required'
]);

$event = Event::findOrFail($eventId);

DB::transaction(function() use ($event, $request){
$registration = EventRegistration::where('user_id', 43)->where("event_id", $event->id);
if (!$registration->exists()){
$registration = new EventRegistration([
'user_id' => 43/*auth()->user()->id*/,
'event_id'=> $event->id
]);
$registration->save();
}

RegistrationValue::where('event_registration_id', $registration->first()->id)->delete();

$value = new RegistrationValue([
'event_registration_id' => $registration->first()->id,
'field_id' => 1,
'value' => $request->input("field-1")
]);

$value->save();

foreach ($event->fields as $field){

if ($field->type === "doubletext"){
$content = $request->input("field-".$field->id."-1")."|".$request->input("field-".$field->id."-2");
} else {
$content = $request->input("field-".$field->id);
}

$value = new RegistrationValue([
'event_registration_id' => $registration->first()->id,
'field_id' => $field->id,
'value' => $content
]);
Log::info("Saving value");
$value->save();
}
});

$registration = EventRegistration::where('user_id', 43)->where("event_id", $eventId);
$registrationId = $registration->first()->id;

return redirect("/events/$eventId/registration/$registrationId")->with('success', 'Your registration was saved');
}

/**
Expand All @@ -53,9 +107,28 @@ public function store(Request $request)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
public function show($eventId)
{
//
$event = Event::findOrFail($eventId);

$registration = EventRegistration::where('event_id', $eventId)->where('user_id', 43/*auth()->user()->id*/);

if (!$registration->exists()){
redirect("events.registration.create", ['event' => $eventId]);
}

$values = RegistrationValue::where('event_registration_id', $registration->first()->id);

$values = $values->get()->mapWithKeys(function ($item) {
return [$item['field_id'] => $item['value']];
});

return view("registrations.form", [
'event' => $event,
'user' => ['id' => 43]/*auth()->user()*/,
'values' => $values,
'mode' => 'show'
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/RegistrationValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class RegistrationValue extends Model
{
//
protected $fillable = ['field_id', 'event_registration_id', 'value'];
}
5 changes: 5 additions & 0 deletions config/gmaps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'apikey' => env('GMAPS_API_KEY'),
];
2 changes: 2 additions & 0 deletions database/migrations/2018_10_18_121117_create_events_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function up()
Schema::create('events', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('location');
$table->text('gmap');
$table->dateTimeTz('time');
$table->timestamps();
});
Expand Down
7 changes: 4 additions & 3 deletions database/migrations/2018_10_18_122948_create_fields_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public function up()
Schema::create('fields', function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->string('name');
$table->string('options');
$table->string('description');
$table->string('label');
$table->string('options')->nullable();
$table->string('condition')->nullable();
$table->string('help')->nullable();
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class CreateRegistrationValuesTable extends Migration
public function up()
{
Schema::create('registration_values', function (Blueprint $table) {
$table->increments('id');
$table->integer('event_registration_id')->unsigned();
$table->integer('field_id')->unsigned();
$table->string('value');
$table->string('value')->nullable();
$table->timestamps();
});

Expand Down
2 changes: 2 additions & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ class DatabaseSeeder extends Seeder
public function run()
{
$this->call(EventTableSeeder::class);
$this->call(FieldTableSeeder::class);
$this->call(UserTableSeeder::class);
}
}
4 changes: 3 additions & 1 deletion database/seeds/EventTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class EventTableSeeder extends Seeder
*/
public function run()
{
$datetime = new DateTime("2019-02-15T17:00:00.0000000Z");
$datetime = new DateTime("2019-02-15T16:00:00.0000000Z");
$timezone = new DateTimeZone('CET');
$datetime->setTimezone($timezone);

DB::table('events')->insert([
'name' => "SQ Party 2019",
'location' => "Geneva, Palexpo",
'gmap' => "https://www.google.com/maps/dir/Swissquote+Bank+SA,+Chemin+de+la+Cr%C3%A9taux,+Gland/Palexpo,+Route+Fran%C3%A7ois-Peyrot+30,+1218+Le+Grand-Saconnex/@46.3306213,6.053582,11z/data=!3m1!4b1!4m14!4m13!1m5!1m1!1s0x478c437c978b78ad:0x956a7940b0fb4daf!2m2!1d6.2677941!2d46.4177308!1m5!1m1!1s0x478c64897abb301b:0x809a09de3beff13a!2m2!1d6.1168898!2d46.2363842!3e0",
'time' => $datetime,
]);
}
Expand Down
69 changes: 69 additions & 0 deletions database/seeds/FieldTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Illuminate\Database\Seeder;

class FieldTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('fields')->insert([
'id' => 1,
'type' => 'radio',
'label' => 'Will you be present at the event ?',
'options' => 'Yes;No'
]);

DB::table('fields')->insert([
'id' => 2,
'type' => 'radio',
'label' => 'Will you take your own car to go to the event ?',
'options' => 'Yes;No',
'help' => 'There will be no shuttle transports this year, parking is 12 CHF for the night'
]);

DB::table('fields')->insert([
'id' => 3,
'type' => 'radio',
'label' => 'Do you need a hotel room ?',
'help' => 'Answer "No" if you already have a room (FAME participants) or if you have another accommodation plan for the night.',
'options' => 'Yes;No'
]);

DB::table('fields')->insert([
'id' => 4,
'type' => 'doubletext',
'label' => 'Who do you want to share the room with ?',
'options' => 'Yes;No',
'help' => 'Rooms have 3 occupants in total, input the name and department of the persons you want to share the room with',
'condition' => "3:Yes"
]);


DB::table('fields')->insert([
'id' => 5,
'type' => 'radio',
'label' => 'Do you have any dietary restrictions (for example allergies) ?',
'options' => 'Yes;No'
]);

DB::table('fields')->insert([
'id' => 6,
'type' => 'text',
'label' => 'Which ones ?',
'condition' => "5:Yes"
]);

for ($i = 2; $i < 7; $i++){
DB::table('event_fields')->insert([
'event_id' => 1,
'field_id' => $i
]);
}

}
}
22 changes: 22 additions & 0 deletions database/seeds/UserTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'id' => 43,
'name' => "test",
'department' => "test",
'login' => 'test',
'email' => '[email protected]'
]);
}
}
4 changes: 4 additions & 0 deletions public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/js/app.js": "/js/app.js",
"/css/app.css": "/css/app.css"
}
36 changes: 27 additions & 9 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,34 @@

require('./bootstrap');

window.Vue = require('vue');
function updateFormVisibility(){
var participation = $("input[name=field-1]:checked");
if (participation.val() === "Yes"){
$("#participant-form").show();
} else {
$("#participant-form").hide();
}

/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
$("form input").each(function(){
var input = $(this);
var group = $(this).closest(".form-group");
var condition = group.attr("data-condition");
if (condition !== undefined && condition !== ""){
group.hide();
var inputId = condition.split(":")[0];
var expectedValue = condition.split(":")[1];
if ($("input[name=field-"+inputId+"]:checked").val() === expectedValue){
group.show();
} else {
group.hide();
}
}

});

Vue.component('example-component', require('./components/ExampleComponent.vue'));
}

const app = new Vue({
el: '#app'
$(document).ready(function () {
$("input").on( "change", updateFormVisibility);
updateFormVisibility();
});
9 changes: 9 additions & 0 deletions resources/views/forms/doubletext.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="form-group col-sm-6" id="field-group-{{$field->id}}" data-condition="{{$field->condition}}">
<label for="field-{{$field->id}}">{{$field->label}}</label>
<input type="text" class="form-control" name="field-{{$field->id}}-1" id="field-{{$field->id}}-1" placeholder="Name and department"><br>
<input type="text" class="form-control" name="field-{{$field->id}}-2" id="field-{{$field->id}}-2" placeholder="Name and department">
<small class="form-text text-muted">{{$field->help}}</small>
</div>



Loading

0 comments on commit 236318f

Please sign in to comment.