File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Events/EventController.php
<?php
namespace App\KommaApp\Events;
use App\Http\Controllers\Controller;
use App\Http\Requests\OrderChoosePaymentMethodRequest;
use App\Http\Requests\EventSignUpFreeRequest;
use App\Http\Requests\EventSignUpRequest;
use App\KommaApp\Events\Models\Event;
use App\KommaApp\EventSignUps\EventSignUpService;
use App\KommaApp\EventSignUps\Models\EventSignUp;
use App\KommaApp\Orders\Models\Order;
use App\KommaApp\Orders\OrderService;
use App\KommaApp\Payments\PaymentService;
use App\KommaApp\Regions\Models\Region;
use App\KommaApp\UserGroups\Models\UserGroup;
use App\KommaApp\Users\UserService;
use App\Mail\Events\SignedUpFreeMail;
use App\Mail\Events\SignedUpFreePlusOneMail;
use App\Mail\Events\SignedUpMail;
use App\Mail\Events\SignedUpPlusOneMail;
use App\Mail\EventSignUpMail;
class EventController extends Controller
{
private $modelPrefix = 'pages.events.';
private $eventService;
private $userService;
// kms user should not add or remove these events
const BREAKFAST_EVENT_TYPE_ID = 9;
const DINNER_EVENT_TYPE_ID = 1;
public function __construct(EventService $eventService)
{
parent::__construct();
$this->eventService = $eventService;
$this->userService = app()->make(UserService::class);
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
$page = $this->pageService->getPageById(request()->attributes->get('page_id'));
$eventType = null;
if ($page->code_name === 'networkdinner') $eventType = self::DINNER_EVENT_TYPE_ID;
if ($page->code_name === 'networkbreakfast') $eventType = self::BREAKFAST_EVENT_TYPE_ID;
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);
// Load the highlighted events
$highlightedEvents = isset($eventType) ? $this->eventService->getHighlightedEventsByType($eventType) : $this->eventService->getHighlightedEvents();
// Get the id's of the highlighted events, because we need to exclude them from the paginated events
$eventIds = $highlightedEvents->pluck('id')->all();
$events = isset($eventType) ? $this->eventService->getEventsByType($eventType, true, true, $eventIds) : $this->eventService->getEvents(true, true, $eventIds, null);
$pageCodeName = $page->code_name;
$events->withPath('/' . $this->links->$pageCodeName->route);
$page->translation = $this->decodeDynamicContent( $page->translation );
return \View::make($this->baseViewPath.$this->modelPrefix.'index',[
'page' => $page,
'otherLanguages' => $otherLanguageRoutes,
'highlightedEvents' => $highlightedEvents,
'events' => $events,
]);
}
/**
* @return mixed
*/
public function userGroups() {
$page = $this->pageService->getPageById(request()->attributes->get('page_id'));
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);
$page->translation = $this->decodeDynamicContent( $page->translation );
$userGroups = UserGroup::with('translation', 'images')->get();
// Return view
return \View::make($this->baseViewPath.$this->modelPrefix.'user_groups',[
'page' => $page,
'otherLanguages' => $otherLanguageRoutes,
'userGroups' => $userGroups
]);
}
/**
*
* @param Region $region
* @return \Illuminate\Contracts\View\View
*/
public function region(Region $region){
$page = $this->pageService->getPageById(request()->attributes->get('page_id'));
$eventType = null;
if ($page->code_name === 'networkdinner') $eventType = self::DINNER_EVENT_TYPE_ID;
if ($page->code_name === 'networkbreakfast') $eventType = self::BREAKFAST_EVENT_TYPE_ID;
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);
$events = isset($eventType) ? $this->eventService->getEventsByRegionAndType($region->id, $eventType) : $this->eventService->getEventsByRegion($region->id, true, null, null);
$events->withPath('/' . $this->links->events->route.'/regio/'.$region->slug);
$region = $this->decodeDynamicContent($region, 'dinner');
// Return view
return \View::make($this->baseViewPath.$this->modelPrefix.'region',[
'page' => $page,
'otherLanguages' => $otherLanguageRoutes,
'events' => $events,
'eventRegion' => $region,
]);
}
/**
*
* @param UserGroup $userGroup
* @return \Illuminate\Contracts\View\View
*/
public function usergroup(UserGroup $userGroup){
$userGroup->load(['translation', 'images']);
$page = $this->pageService->getPageById(request()->attributes->get('page_id'));
$events = $this->eventService->getBreakfastEventsByUserGroupId($userGroup->id);
$highlightedEvents = $this->eventService->getHighlightedBreakfastEventsByUserGroupId($userGroup->id);
$userService = new UserService();
$userGroup = collect([
'userGroup' => $userGroup,
'userGroupUsers' => $userService->getUserByUserGroupAndEventType($userGroup->id)
]);
return \View::make($this->baseViewPath.$this->modelPrefix.'group',[
'page' => $page,
'events' => $events,
'userGroup' => $userGroup,
'highlightedEvents' => $highlightedEvents
]);
}
/**
* @param Event $event
* @return \Illuminate\Contracts\View\View
*/
public function show(Event $event)
{
// Load translation by eager loading
$event->load( 'translation', 'images', 'signUps');
$premiumUsers = false;
if($event->event_type_id === 9) {
$usersService = app(UserService::class);
$premiumUsers = $usersService->getUserByUserGroupAndEventType($event->user_group);
}
$eventSignUpService = app(EventSignUpService::class);
$eventSignUpService->bindCompaniesToSignees($event->signUps);
//$page = $this->pageService->getPageByCodeName('events');
$page = $this->pageService->getPageById(request()->attributes->get('page_id'));
// Get the route in other languages for menu and meta title
// $otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page, $event);
if($event->event_type_id === self::BREAKFAST_EVENT_TYPE_ID) {
$otherEvents = $this->eventService->getBreakfastEventsByUserGroupId($event->user_group)->slice(1, 3);
// $otherEvents = $this->eventService->getBreakfastEventsByUserGroupId($event->user_group);
}
elseif($event->event_type_id === self::DINNER_EVENT_TYPE_ID) {
$otherEvents = $this->eventService->getEventsByRegion($event->region_id, false, [$event->id], 3, $event->event_type_id);
}
else
{
$otherEvents = $this->eventService->getEventsByRegion($event->region_id, false, [$event->id], 3);
}
// Get the other event in this region
// Clear the signed up persons from session
session(['signedUpPersons' => null, '_old_input' => null]);
// Return view
return \View::make($this->baseViewPath.$this->modelPrefix. 'show',[
'page' => $page,
'event' => $event,
'premiumUsers' => $premiumUsers,
'otherEvents' => $otherEvents,
]);
}
/**
* Sync the given event with the billing system
*
* @param Event $event
*/
public function sync(Event $event)
{
$this->eventService->syncEvent($event);
}
//================================================================================================================//
//==== =====//
//==== Functions for the sign up pages =====//
//==== =====//
//================================================================================================================//
/**
* Sign up page of an event
*
* @param Event $event
* @return mixed
*/
public function signUp(Event $event)
{
// Load translation by eager loading
$event->load( 'translation', 'images');
$page = $this->pageService->getPageByCodeName('events');
// Clear the signed up persons from session
session(['signedUpPersons' => null]);
$this->populateFormWithUser();
// Return view
return \View::make($this->baseViewPath.$this->modelPrefix. 'signUp.show',[
'page' => $page,
'event' => $event,
]);
}
public function previewMail(){
//$orderService = \App::make(OrderService::class);
//$order = Order::where('id', '=', 2500)->first();
/**
* After signing up for a paid event...
*/
// Sent to the person signing up for an event
//return new SignedUpMail($order);
// Sent to the plus one
//return new SignedUpPlusOneMail($order, 'Kabouter Plop');
/**
* After signing up for a free event
*/
// return new SignedUpFreeMail($order); //Event $event, EventSignUp $eventSignUp
// return new SignedUpFreePlusOneMail($order); //Event $event, EventSignUp $eventSignUp, string $signedUpBy
/**
* After successful payment (Mollie webhook)
*/
//return new PaymentReceivedMail($order); //Order $order
/**
* After any type of signup, sent to Erik
*/
//return new EventSignUpMail(); //EventSignUp $eventSignUp, $signedUpBy, Event $event, $invoiceIdPrefixed
}
/**
* Process the event sign up
*
* @param EventSignUpRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function process(EventSignUpRequest $request)
{
/** @var OrderService $orderService */
$orderService = \App::make(OrderService::class);
/** @var PaymentService $paymentService */
$paymentService = \App::make(PaymentService::class);
// Convert the request into signees and the producted event
[$signees, $eventProduct, $event] = $this->eventService->convertRequest($request->except('_token', 'accept_legal'));
$redirectRoute = null;
$hasPaymentError = false;
//Create array for signee names to put into the session
$sessionSignees = [];
// Loop through the signees
foreach ($signees as $signeeType => $signee){
// Create order for the signee
$order = $orderService->createOrderForSignee($signee, $eventProduct);
// Create Sign Up Models for the signee
$this->eventService->addSigneeToEventSignUps($signee, $signees['main'], $event, $order->invoice_id, $order->invoice_id_prefixed);
$sessionSignees[] = $signee->first_name;
// If it is the main signee
if($signeeType == 'main') {
// The signup confirmation is sent to the $order->mail
\Mail::send(new SignedUpMail($order));
// If the main signee chose iDeal as payment method
if($signee->paymentMethod == PaymentService::PAYMENT_METHOD_IDEAL) {
$paymentDescription = 'Event inschrijving: '. $event->translation->name . ' - '. $signee->first_name . ' '. $signee->last_name;
$redirectRoute = $paymentService->createPayment($order, $paymentDescription, url(route('validate.eventOrder', ['id' => $order->id])), $signee->paymentMethod);
if(empty($redirectRoute)) $hasPaymentError = true;
}
// The signee chose manual bank transfer --> WeFact
else {
// Check if the signee chose to use a different email for the invoice
$orderService->sendInvoiceForOrderByWeFact($order, $event->wefact_code);
}
}
// The plus one will receive a WeFact invoice
else{
$orderService->sendInvoiceForOrderByWeFact($order, $event->wefact_code);
\Mail::send(new SignedUpPlusOneMail($order, $signees['main']->first_name.' '.$signees['main']->last_name));
}
}
// If no redirect route is defined (when mollie is down) or when manual transaction
if(empty($redirectRoute))
{
// If mollie is down
if($hasPaymentError) {
// redirect to page that indicates that there will be contacted about the payment
return redirect($this->links->events->route . '/' . $event->translation->slug. '/'. __('site/routes.events.signUp') . '/' . __('site/routes.events.paymentError'));
}
// Else redirect to page that indicates that the invoice will be send by WeFact
return redirect($this->links->events->route . '/' . $event->translation->slug. '/'. __('site/routes.events.signUp') . '/' . __('site/routes.events.paymentByPartner'));
}
session(['signedUpPersons' => $sessionSignees]);
// Redirect to payment route
return redirect($redirectRoute, 303);
}
public function processFree(EventSignUpFreeRequest $request) {
$event = Event::find($request->input('event'))
->load('translation')
->load('type')
->load('type.translation');
/** @var EventSignUpService $eventSignUpService */
$eventSignUpService = \App::make(EventSignUpService::class);
$inputs = $request->except('_token', 'accept_legal');
$signee = $eventSignUpService->createSignee($inputs);
// Create Sign Up
$signUp = new EventSignUp();
$signUp->fill((array)$signee);
// Save the Sign Up through the event relation
$event->signUps()->save($signUp);
switch ($event->type->translation->slug) {
case 'online-meeting':
\Mail::send(new SignedUpFreeMail($event, $signUp));
break;
default:
throw new \UnexpectedValueException(self::class.': Mailing for event type has not been defined.');
}
// Send mail to erik of the signee
\Mail::send(new EventSignUpMail( $signUp, null, $event, null));
//Create array for signee names to put into the session
$sessionSignees = [];
$sessionSignees[] = $signee->first_name;
if ($request->input('plus_one') == 'on') {
$plusOne = $eventSignUpService->createSignee($inputs, 'plus_one_');
// Create Sign Up
$plusOneSignUp = new EventSignUp();
$plusOneSignUp->fill((array)$plusOne);
// Save the Sign Up through the event relation
$event->signUps()->save($plusOneSignUp);
switch ($event->type->translation->slug) {
case 'online-meeting':
\Mail::send(new SignedUpFreePlusOneMail($event, $plusOneSignUp, $signUp->first_name.' '.$signUp->last_name));
break;
default:
throw new \UnexpectedValueException(self::class.': Mailing for event type has not been defined.');
}
\Mail::send(new EventSignUpMail( $plusOneSignUp, $signUp->first_name.' '.$signUp->last_name, $event, null));
$sessionSignees[] = $plusOneSignUp->first_name;
}
session(['signedUpPersons' => $sessionSignees]);
return redirect(route('events.signUp.success', ['event' => $event->id]), 302);
}
/**
* Payment error page after sign up for an event
*
* @param Event $event
* @return mixed
*/
public function paymentError(Event $event)
{
// Load translation by eager loading
$event->load( 'translation', 'images');
$page = $this->pageService->getPageByCodeName('events');
return \View::make($this->baseViewPath.$this->modelPrefix. 'signUp.message',[
'page' => $page,
'messageCodeName' => 'paymentError',
'event' => $event,
]);
}
/**
* Validate the order and redirect it to the right order status
* This is the controller that Mollie will call after finishing the sign up checkout
*
* @param $orderId
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function validateEventOrder($orderId)
{
// Get the (event) order and the event
[$order, $event] = $this->eventService->getEventOrderStatus($orderId);
$mainEventRoute = $this->links->events->route . '/' . $event->translation->slug. '/' . __('site/routes.events.signUp');
switch ($order->status)
{
case Order::OPEN:
return redirect($mainEventRoute . '/' . __('site/routes.events.choosePaymentMethod') . '/'.$order->id);
case Order::AWAITING_PAYMENT:
return redirect($mainEventRoute . '/' . __('site/routes.events.awaitingPayment'));
case Order::PAID:
return redirect($mainEventRoute . '/' . __('site/routes.events.success'));
case Order::CANCELED:
return redirect($mainEventRoute . '/' . __('site/routes.events.canceled'));
}
}
/**
* Awaiting page after sign up for an event
*
* @param Event $event
* @return mixed
*/
public function awaitingPayment(Event $event)
{
// Load translation by eager loading
$event->load( 'translation', 'images');
$page = $this->pageService->getPageByCodeName('events');
return \View::make($this->baseViewPath.$this->modelPrefix. 'signUp.message',[
'page' => $page,
'messageCodeName' => 'awaitingPayment',
'event' => $event,
]);
}
/**
* Choose a payment method for completing the sign up
* Plus one will receive a link to this page
* rr if you haven't completed the payment process you also receive a link to this page
*
* @param Event $event
* @param $orderId
* @return mixed
*/
public function choosePaymentMethod(Event $event, $orderId)
{
// Load translation by eager loading
$event->load( 'translation', 'images');
$page = $this->pageService->getPageByCodeName('events');
// Clear the signed up persons from session
session(['signedUpPersons' => null]);
// Return view
return \View::make($this->baseViewPath.$this->modelPrefix. 'signUp.choosePaymentMethod',[
'page' => $page,
'event' => $event,
'orderId' => $orderId
]);
}
/**
* Process the choose payment method form
*
* @param OrderChoosePaymentMethodRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function processPaymentMethod(OrderChoosePaymentMethodRequest $request)
{
$orderService = \App::make(OrderService::class);
$paymentService = \App::make(PaymentService::class);
$order = $orderService->getOrder($request->get('order'));
$eventProduct = $order->products()->first();
$redirectRoute = null;
$paymentDescription = 'Event inschrijving: '. $eventProduct->name . ' - '. $order->invoice_first_name . ' '. $order->invoice_last_name;
$redirectRoute = $paymentService->createPayment($order, $paymentDescription, url(route('validate.eventOrder', ['id' => $order->id])), $request->get('paymentMethod'));
// If no redirect route is defined (when mollie is down)
if( empty($redirectRoute) )
{
$event = $this->eventService->getEvent($eventProduct->event_id);
// redirect to page that indicates that there will be contacted about the payment
return redirect($this->links->events->route . '/' . $event->translation->slug. '/'. __('site/routes.events.signUp') . '/' . __('site/routes.events.paymentError'));
}
session(['signedUpPersons' => [$order->invoice_first_name]]);
// Redirect to payment route
return redirect($redirectRoute, 303);
}
/**
* Success page after sign up for an event
*
* @param Event $event
* @return mixed
* @throws \Spatie\CalendarLinks\Exceptions\InvalidLink
*/
public function success(Event $event)
{
// Load translation by eager loading
$event->load( 'translation', 'images');
// There should be signed up persons in the session, else redirect to the event show page
$signedUpPersons = session('signedUpPersons', null);
$page = $this->pageService->getPageByCodeName('events');
$nextEvent = $this->eventService->getNextEvent($event);
return \View::make($this->baseViewPath.$this->modelPrefix. 'signUp.success',[
'page' => $page,
'event' => $event,
'calenderEvent' => $this->eventService->createCalendarEvent($event),
'nextEvent' => $nextEvent,
'signedUpPersons' => $signedUpPersons,
]);
}
/**
* Payment by partner page after sign up for an event
*
* @param Event $event
* @return mixed
* @throws \Spatie\CalendarLinks\Exceptions\InvalidLink
*/
public function paymentByPartner(Event $event)
{
// Load translation by eager loading
$event->load( 'translation', 'images');
// There should be signed up persons in the session, else redirect to the event show page
$signedUpPersons = session('signedUpPersons', null);
$page = $this->pageService->getPageByCodeName('events');
$nextEvent = $this->eventService->getNextEvent($event);
return \View::make($this->baseViewPath.$this->modelPrefix. 'signUp.paymentByPartner',[
'page' => $page,
'event' => $event,
'calenderEvent' => $this->eventService->createCalendarEvent($event),
'nextEvent' => $nextEvent,
'signedUpPersons' => $signedUpPersons,
]);
}
/**
* Canceled sign up for an event
*
* @param Event $event
* @return mixed
*/
public function canceled(Event $event)
{
// Load translation by eager loading
$event->load( 'translation', 'images');
$page = $this->pageService->getPageByCodeName('events');
return \View::make($this->baseViewPath.$this->modelPrefix. 'signUp.message',[
'page' => $page,
'messageCodeName' => 'canceled',
'event' => $event,
]);
}
}