wi-events/components/EventForm.php

80 lines
2.2 KiB
PHP
Raw 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;
2023-01-07 18:42:56 +01:00
use Illuminate\Support\Facades\Mail;
2023-01-07 16:10:18 +01:00
use Input;
use Winter\Storm\Support\Facades\Validator;
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-02-12 21:30:52 +01:00
'payload' => Input::all(),
]);
Mail::send('confirm_'.$this->event->slug, ['data' => Input::all()], function ($message) use ($participant) {
2023-02-12 01:25:16 +01:00
$message->to($participant->email, $participant->firstname.' '.$participant->lastname);
});
2023-01-07 18:13:03 +01:00
return response()->json([], 201);
2023-01-07 16:10:18 +01:00
}
}