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/SBogers95/rentman.io/app/Komma/Trainings/TrainingService.php
<?php

namespace App\Komma\Trainings;

use App\Komma\Base\Service;
use App\Komma\Trainings\Models\Training;
use Carbon\Carbon;
use Illuminate\Support\Collection;

class TrainingService extends Service
{
    private $today;

    public function __construct()
    {
        $this->today = Carbon::now()->endOfDay();
        $this->today = $this->today->format('Y-m-d H:i:s');
        parent::__construct();
    }

    public function getAllTrainings($pagination = false, $itemsPerPage = 9)
    {
        $trainings = $this->site
            ->trainings()
            ->with('translation', 'images', 'fallback_translation')
            ->orderBy('created_at', 'desc')

            // We need to use a join to select the active trainings because that is defined on the translations
            ->join('training_translations', 'trainings.id', '=', 'training_translations.training_id')
            ->select('trainings.*', 'training_translations.active', 'training_translations.populate_from_english', 'training_translations.language_id')
            ->where(function ($query) {
                $query->where('active', '=', 1)
                    ->orWhere('populate_from_english', '=', 1);
            })
            ->where('language_id', \App::getLanguage()->id);

        if ($pagination) {
            $trainings = $trainings->paginate($itemsPerPage);

            // Skip the population if language is already english
            if (app()->getLocale() != 'en') {
                $this->populatedFallbackTranslationOnCollection($trainings->getCollection());
            }
        } else {
            $trainings = $trainings->get();

            // Skip the population if language is already english
            if (app()->getLocale() != 'en') {
                $this->populatedFallbackTranslationOnCollection($trainings);
            }
        }

        return $trainings;
    }

    public function getNextTrainings(Training $training, $amountOfTrainings = 3)
    {
        $trainings = $this->site
            ->trainings()
            ->with('translation', 'images', 'fallback_translation')
            ->where('trainings.id', '!=', $training->id)
            ->orderBy('created_at', 'desc')

            // We need to use a join to select the active trainings because that is defined on the translations
            ->join('training_translations', 'trainings.id', '=', 'training_translations.training_id')
            ->select('trainings.*', 'training_translations.active', 'training_translations.populate_from_english', 'training_translations.language_id')
            ->where(function ($query) {
                $query->where('active', '=', 1)
                    ->orWhere('populate_from_english', '=', 1);
            })
            ->where('language_id', \App::getLanguage()->id)

            ->take($amountOfTrainings)
            ->get();

        // Skip the population if language is already english
        if (app()->getLocale() != 'en') {
            $this->populatedFallbackTranslationOnCollection($trainings);
        }

        return $trainings;
    }

    private function populatedFallbackTranslationOnCollection(Collection $trainings)
    {
        foreach ($trainings as $training) {
            // Skip if we don't need to populated
            if (! $training->translation->populate_from_english) {
                continue;
            }
            $training->translation = $training->fallback_translation;
            $training->used_fallback_translation = true;
        }
    }
}