HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/firetech.komma.pro/app/KommaApp/Subscription/SubscriptionController.php
<?php

namespace App\KommaApp\Subscriptions;

use App\Http\Controllers\Controller;
use App\KommaApp\Courses\Models\Course;
use App\KommaApp\Pages\PageController;
use App\KommaApp\Students\Models\Student;
use App\KommaApp\Subscriptions\Models\Subscription;
use App\Rules\CourseLegal;
use App\Rules\StudentsArray;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class SubscriptionController extends Controller
{
    private $subscriptionPrefix = 'subscriptions.';

    /**
     * @param Course $course
     * @param Request $request
     * @return \Illuminate\Contracts\View\View
     */
    public function subscribe(Request $request)
    {
        $request->validate([
            'step' => 'required',
            'date' => 'required',
            'students' => ['required', 'array', new StudentsArray],
            'company' => 'required',
            'street' => 'required',
            'housenumber' => 'required',
            'postal_code' => 'required',
            'city' => 'required',
            'phone' => 'required',
            'email' => 'required|email',
            'course_legal' => ['required', 'boolean', new CourseLegal()],
        ]);
        $studentIds = [];

        foreach (Input::get('students') as $student) {
            if(!empty($student['name'])) {
                $foundStudent = Student::firstOrCreate(
                    ['name' => $student['name']],
                    ['company' => Input::get('company')]
                );


                $foundStudent->email = $student['email'];
                $foundStudent->birthday = $student['birthday'];
                $foundStudent->birthmonth = $student['birthmonth'];
                $foundStudent->birthyear = $student['birthyear'];
                $foundStudent->birthdate = $student['birthyear'].'-'.$student['birthmonth'].'-'.$student['birthday'];
                $foundStudent->gender = $student['gender'];
                $foundStudent->save();
                $studentIds[] = $foundStudent->id;
            }
        }

        $subscription = new Subscription();
        $subscription->course_id = Input::get('courseId');
        $subscription->date = trim(Input::get('date'));
        $subscription->company = Input::get('company');
        $subscription->email = Input::get('email');
        $subscription->street = Input::get('street');
        $subscription->house_number = Input::get('housenumber');
        $subscription->city = Input::get('city');
        $subscription->postal_code = Input::get('postal_code');
        $subscription->code95 = Input::get('code95');
        $subscription->phone = Input::get('phone');
        $subscription->save();

        $subscription->students()->sync($studentIds);

        \App\KommaApp\Subscriptions\Kms\SubscriptionService::notifyAdminsAndClientForNewSubscription($subscription);

        $pageController = new PageController();
        return $pageController->show($this->pageService->getPageByCodeName('subscribe'))->with('send', true);
    }
}