wi-events/components/EventForm.php

79 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2023-01-07 16:10:18 +01:00
<?php
namespace Zoomyboy\Event\Components;
use Cms\Classes\ComponentBase;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Lang;
use Input;
use Winter\Storm\Support\Facades\Validator;
2023-03-27 23:10:51 +02:00
use Zoomyboy\Event\Jobs\ProcessSubmitJob;
2023-02-12 01:25:16 +01:00
use Zoomyboy\Event\Models\Event;
2023-01-07 18:13:03 +01:00
use Zoomyboy\Event\Models\Participant;
2023-01-07 16:10:18 +01:00
class EventForm extends ComponentBase
{
2023-02-12 01:25:16 +01:00
public Event $event;
2023-01-07 16:10:18 +01:00
public function componentDetails()
{
return [
'name' => 'EventForm Component',
'description' => 'No description provided yet...',
];
}
public function defineProperties()
{
2023-02-12 01:25:16 +01:00
return [
'event_id' => [
'label' => 'ID',
],
];
}
private function customInit(int $eventId)
{
$this->event = Event::findOrFail($eventId);
}
public function onRender()
{
$this->customInit($this->property('event_id'));
2023-02-12 21:30:52 +01:00
return $this->renderPartial('@'.$this->event->slug.'.htm');
2023-01-07 16:10:18 +01:00
}
public function onSubmit(): JsonResponse
{
2023-02-12 01:25:16 +01:00
$this->customInit(Input::get('event_id'));
2023-02-12 21:30:52 +01:00
$rules = $this->event->loadConfig('validator');
2023-01-07 17:51:26 +01:00
if ('Orga' === Input::get('activity')) {
$rules['vorteam'] = 'in:Ja,Nein';
}
$validator = Validator::make(Input::all(), $rules, [
'vorteam.in' => 'Bitte gebe an, ob du am Vorteam teilnehmen willst.',
2023-02-12 01:25:16 +01:00
'parent.accepted' => 'Bitte gebe an, dass du volljährig bzw. ein Elternteil bist.',
2023-01-07 17:51:26 +01:00
], Lang::get('zoomyboy.event::validation.attributes'));
2023-01-07 16:10:18 +01:00
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
2023-02-12 21:30:52 +01:00
$participant = Participant::create([
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'email' => Input::get('email'),
2023-02-12 01:25:16 +01:00
'event_id' => $this->event->id,
2023-03-23 00:45:26 +01:00
'payload' => $this->event->transformInputs(Input::all()),
2023-02-12 21:30:52 +01:00
]);
2023-03-27 23:10:51 +02:00
ProcessSubmitJob::dispatch($participant->id);
2023-01-07 18:13:03 +01:00
return response()->json([], 201);
2023-01-07 16:10:18 +01:00
}
}