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/Posts/PostService.php
<?php

namespace App\Komma\Posts;

use App\Komma\Base\Service;
use App\Komma\Posts\Models\Post;
use Carbon\Carbon;
use Illuminate\Support\Collection;

class PostService 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 getAllPosts($pagination = false, $itemsPerPage = 9, $category = '')
    {
        $posts = $this->site
            ->posts()
            ->with('translation', 'images', 'fallback_translation')
            ->where('date', '<=', $this->today)
            ->orderBy('date', 'desc')
            ->orderBy('created_at', 'desc')

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

        if(! empty($category)){
            $posts->where('post_category', '=', $category);
        }

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

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

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

        return $posts;
    }

    public function getNextPosts(Post $post, $amountOfPosts = 3)
    {
        $posts = $this->site
            ->posts()
            ->with('translation', 'images', 'fallback_translation')
            ->where('posts.id', '!=', $post->id)
            ->where('date', '<=', $post->date->format(Carbon::DEFAULT_TO_STRING_FORMAT))
            ->orderBy('date', 'desc')
            ->orderBy('created_at', 'desc')

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

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

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

        return $posts;
    }

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