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/ste.komma.pro/app/Trainings/Kms/TrainingController.php
<?php

namespace App\Trainings\Kms;

/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

use App\Trainings\Models\Training;
use App\Trainings\Models\TrainingTranslation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\SectionController;
use Komma\KMS\Helpers\KommaHelpers;

final class TrainingController extends SectionController
{
    protected $slug = "trainings";
    protected $classModelName = Training::class;
    protected $forTranslationModelName = TrainingTranslation::class;

    function __construct()
    {
        $trainingSection = new TrainingSection($this->slug);
        parent::__construct($trainingSection);
    }

    /**
     * This method will validate and save the model
     * It is called on create form submit,
     * and it is called on the form edit.
     *
     * @return mixed
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Throwable
     */
    public function store()
    {
        $this->authorize('store', $this->classModelName);
        $this->modelService->createRootTreeModelIfNeeded();

        //Prepare the new model, and attributes
        $model = $this->modelService->newModel();
        if($this->forTranslationModelName) $model = $this->translationService->makeAndInjectEmptyTranslationsIntoTranslatableIfNeeded($model);
        $this->section->generateAttributesAndAddThemToTabs($model);
        $attributes = $this->section->getAttributes();

        //Validate the form data and fill the attributes from input
        $validator = $this->dataService->validateInputAndReturnValidator($attributes);
        if ($validator->fails()) return redirect()->back()->withInput()->withErrors($validator);
        $attributes = $this->dataService->fillAttributesFromInput($attributes);

        $shouldRedirect = $this->runC4Uniquness($model, $attributes);
        if($shouldRedirect) return $shouldRedirect;

        $byValueFrom = $attributes->mapToGroups(function($attribute, $index) {
            /** @var Attribute $attribute */
            return [$attribute->getsValueFrom() => $attribute];
        });

        $model = $this->save($model, $byValueFrom);

        //Set the siteSlug variable for routes that need the siteSlug variable in their route
        $routeParameters = [];
        $site = $this->siteService->getCurrentSite();
        if($site->exists)
            $routeParameters['siteSlug'] = $site->slug;

        $routeParameters[strtolower(KommaHelpers::getShortNameFromClass($model, true))] = $model;

        $sessionData = [
            'tabslug' => Request::get('tabslug'),
            'success' => __('KMS::kms/global.saved')
        ];

        //TODO AFTER MERGING DEVELOPMENT INTO KMS 2.0: Check if validation did pass. if not, get the uploadedDocuments, and merge them in the request with:             return redirect()->back()->withInput(array_merge(Request::all(), $uploadedDocuments))->withErrors($validator);

        $this->recentModel = $model;
        return Redirect::action('\\'.get_class($this) . '@show', $routeParameters)->with($sessionData);
    }

    /**
     * This method handles the update functionality.
     * And is called by the edit form.
     *
     * @param $model Model
     * @return mixed
     * @throws \Exception
     * @throws \Throwable
     */
    public function update($model)
    {
        $this->authorize('update', $model);
        $this->modelService->createRootTreeModelIfNeeded();

        //Prepare the model and the attributes
        if($this->forTranslationModelName) $model = $this->translationService->makeAndInjectEmptyTranslationsIntoTranslatableIfNeeded($model);
        $this->section->generateAttributesAndAddThemToTabs($model);
        $attributes = $this->section->getAttributes();

        //Validate the form data and fill the attributes from input
        $validator = $this->dataService->validateInputAndReturnValidator($attributes);

        if ($validator->fails()) return redirect()->back()->withInput()->withErrors($validator);
        $attributes = $this->dataService->fillAttributesFromInput($attributes);

        $shouldRedirect = $this->runC4Uniquness($model, $attributes);
        if($shouldRedirect) return $shouldRedirect;

        $byValueFrom = $attributes->mapToGroups(function($attribute, $index) {
            /** @var Attribute $attribute */
            return [$attribute->getsValueFrom() => $attribute];
        });

        $model = $this->save($model, $byValueFrom);

        //Set the siteSlug variable for routes that need the siteSlug variable in their route
        $routeParameters = [];
        $site = $this->siteService->getCurrentSite();
        if($site->exists)
            $routeParameters['siteSlug'] = $site->slug;
        $routeParameters[strtolower(KommaHelpers::getShortNameFromClass($model, true))] = $model;

        $sessionData = [
            'tabslug' => Request::get('tabslug'),
            'success' => __('KMS::kms/global.saved')
        ];

        //Redirect the users to the "show" page.
//        return \Redirect::action('\\'.get_class($this) . '@show', $routeParameters)->with($sessionData);
        return Redirect::action('\\'.get_class($this) . '@show', $routeParameters)->with($sessionData);
    }

    private function runC4Uniquness($model, $attributes)
    {
        foreach ($attributes as $attribute ) {
            $valueFrom = $attribute->getsValueFrom();
            $valueReference = $attribute->getsValueFromReference();
            $value = $attribute->getValue();

            if($valueReference != 'c4_id') return;

            $checkQuery = Training::where('c4_id', $value);
            if($model->exists) $checkQuery = $checkQuery->where('id', '!=', $model->id);

            if($checkQuery->count() != 0) {
                return redirect()->back()->withInput()->withErrors(['validation', 'Ingevoerde C4 id is al in gebruik.']);
            }
        }
    }

}