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/SBogers26/gripp.nu/app/Komma/Posts/PostService.php
<?php

/**
 * Short description for the file.
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Posts; 

use Carbon\Carbon;
use Komma\Posts\Models\Post;

class PostService
{

    private $today;

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

    public function getAllPosts($pagination = true, $itemsPerPage = 4)
    {

        $posts = Post::where('lft', '!=', 1)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->where('active', '=', 1)
            ->where('date', '<=', $this->today);

        $posts->orderBy('date', 'desc');


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

        return $posts;

    }

    public function getLatestPosts($items = 5)
    {
        $posts = Post::where('lft', '!=', 1)
            ->with('translation')
            ->with('translation.route')
            ->where('active', '=', 1)
            ->where('date', '<=', $this->today);

        $posts->orderBy('date', 'desc');
        $posts->orderBy('lft', 'asc');
        $posts->limit($items);
        $posts = $posts->get();
        return $posts;
    }

    public function getHomePost()
    {
        $posts = Post::where('lft', '!=', 1)
            ->with('translation')
            ->with('translation.route')
            ->where('active', '=', 1)
            ->where('show_on_home', '=', 1)
            ->where('date', '<=', $this->today);

        $posts->orderBy('date', 'desc');
        $posts->orderBy('lft', 'asc');
        $posts = $posts->get();
        return $posts;
    }


    public function getPost($id, $nextAndPrev = true)
    {
        $time_pre = microtime(true);
        if (!$post = Post::where('id', '=', $id)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->where('active', '=', 1)
            ->where('date', '<=', $this->today)
            ->first()
        ) return \App::abort(404, 'post not found');

        if ($nextAndPrev) $nextAndPrev = $this->getNextAndPrevious($post);
        return $post;
    }


    public function getNextAndPrevious(&$post)
    {

        $next = Post::where('date', '>=', $post->date)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->where('active', '=', 1)
            ->where('id', '!=', $post->id)
            ->where('date', '<=', $this->today)
            ->orderBy('lft', 'desc')
            ->first();

        $previous = Post::where('date', '<=', $post->date)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->where('active', '=', 1)
            ->where('id', '!=', $post->id)
            ->where('date', '<=', $this->today)
            ->orderBy('lft', 'asc')
            ->first();

        $post->next = $next;
        $post->previous = $previous;
    }

    public function makeCarbonDate(&$posts){
        foreach ($posts as $post){
            $date = $post->date;
            $post->date = Carbon::createFromFormat('Y-m-d H:i:s',$date);
        }
    }

}