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/Updates/UpdateService.php
<?php

namespace App\Komma\Updates;

use App\Komma\Base\Service;
use App\Komma\Updates\Models\Update;
use Carbon\Carbon;
use Illuminate\Support\Collection;

final class UpdateService 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 getUpdates($updateCategory = null, $skip = 0, $amount = 9)
    {
        $updatesQuery = $this->site
            ->updates()
            ->with('translation', 'images')
            ->where('date', '<=', $this->today)
            ->orderBy('date', 'desc')
            ->orderBy('created_at', 'desc')

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

        if ($updateCategory !== null) {
            $updatesQuery = $updatesQuery->where('update_category_id', 'LIKE', '%'.$updateCategory.'%');
        }

        $updates = $updatesQuery->skip($skip)
        ->take($amount)
        ->get();

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

        return $updates;
    }

    public function getNextUpdates(Update $update, $amountOfUpdates = 3)
    {
        $updates = $this->site
            ->updates()
            ->with('translation', 'images')
            ->where('updates.id', '!=', $update->id)
            ->where('date', '<=', $update->translation->date->format(Carbon::DEFAULT_TO_STRING_FORMAT))
            ->orderBy('date', 'desc')
            ->orderBy('created_at', 'desc')

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

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

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

        return $updates;
    }

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

    public function getUpdatesForApi($limit, $start)
    {
        return $this->site
            ->updates()
            ->with(['translations', 'images'])
            ->has('translations')
            ->where('date', '<=', $this->today)
            ->orderBy('date', 'desc')
            ->orderBy('created_at', 'desc')

            // Join the english translations as root
            ->join('update_translations', 'updates.id', '=', 'update_translations.update_id')
            ->select('updates.*', 'update_translations.date', 'update_translations.language_id')
            ->where('language_id', 40)

            ->skip($start)
            ->take($limit)
            ->get();
    }

    public function getAmountOfUpdates()
    {
        return $this->site
            ->updates()
            ->with(['translations', 'images'])
            ->has('translations')
            ->count();
    }
}