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/SBogers68/ouddorp-duin.nl/app/Komma/Posts/PostService.php
<?php

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

namespace Komma\Posts;

use Carbon\Carbon;
use Komma\Blocks\BlockService;
use Komma\Images\ImageService;
use Komma\Kms\Languages\Language;
use Komma\Pages\PageService;
use Komma\Posts\Models\Post;

class PostService
{

    private $today;

    public function __construct()
    {
        $this->today = Carbon::now()->addHour();
        //Detect daylight saving time
        if( Carbon::now()->format('I') == 0 ) $this->today->addHour();
        $this->today = $this->today->format('Y-m-d H:i:s');
    }

    public function getAllPosts($pagination = false, $itemsPerPage = 5)
    {

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

        // Get pages with translations and routes


        $posts = \DB::table('posts')
            ->leftJoin('post_translations', function ($join){
                $join->on( 'post_translations.post_id', '=', 'posts.id')
                    ->where('language_id', '=', Language::where('languages.iso_2', '=', \App::getLocale())->first()->id);

            })
            ->where('languages.iso_2', '=', \App::getLocale())
            ->join('languages', 'languages.id', '=', 'post_translations.language_id')
            ->leftJoin('images', function($join)
            {
                $join->on('images.imageble_id', '=', 'posts.id')
                    ->where('images.imageble_type', '=', 'Komma\Kms\Posts\Models\Post')
                    ->where('sort_order', '=', 1);
            })
            ->leftJoin('routes', function($join){
                $join->on('post_translations.id', '=', 'routes.routable_id')
                    ->where('routes.routable_type', '=', 'Komma\Kms\Posts\Models\PostTranslation');
            })
            ->select('posts.*', 'post_translations.language_id', 'post_translations.name', 'post_translations.sub_title', 'post_translations.description', 'post_translations.meta_description', 'routes.route', 'images.medium_image_url')
            ->where('posts.active', '=', 1)
            ->where('posts.date', '<=', $this->today)
            ->orderBy('posts.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('images')
            ->with('translation.route')
            ->where('active', '=', 1)
            ->where('date', '<=', $this->today);

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

        return $posts;

    }


    public function getPost($id, $nextAndPrev = 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');

        $this->fillPost($post);

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

        return $post;
    }

    public function fillPost(Post &$post){
        $post->routeInOtherLanguages = $this->postInOtherLanguageRoutes($post->id)->allTranslations;
        $post->meta_description = strip_tags($post->translation->meta_description);

        //Comment the decode when there aren't dynamic blocks
//        $post->translation->description = json_decode($post->translation->description);

        $post->translation->meta_title = $post->translation->name;
    }


    public function getNextAndPrevious(&$post)
    {
        $previous = \DB::table('posts')
            ->leftJoin('post_translations', function ($join){
                $join->on( 'post_translations.post_id', '=', 'posts.id')
                    ->where('language_id', '=', Language::where('languages.iso_2', '=', \App::getLocale())->first()->id);

            })
            ->where('languages.iso_2', '=', \App::getLocale())
            ->join('languages', 'languages.id', '=', 'post_translations.language_id')
            ->leftJoin('routes', function($join){
                $join->on('post_translations.id', '=', 'routes.routable_id')
                    ->where('routes.routable_type', '=', 'Komma\Kms\Posts\Models\PostTranslation');
            })
            ->leftJoin('images', function($join)
            {
                $join->on('images.imageble_id', '=', 'posts.id')
                    ->where('images.imageble_type', '=', 'Komma\Kms\Posts\Models\Post')
                    ->where('sort_order', '=', 1);
            })
            ->select('posts.*', 'post_translations.language_id', 'post_translations.name', 'post_translations.sub_title', 'post_translations.meta_description', 'routes.route')
            ->where('posts.active', '=', 1)
            ->where('posts.date', '<=', $this->today)
            ->where('posts.date', '>', $post->date)
            ->where('posts.id', '!=', $post->id)
            ->orderBy('posts.date','asc')
            ->first();
        $next = \DB::table('posts')
            ->leftJoin('post_translations', function ($join){
                $join->on( 'post_translations.post_id', '=', 'posts.id')
                    ->where('language_id', '=', Language::where('languages.iso_2', '=', \App::getLocale())->first()->id);

            })
            ->where('languages.iso_2', '=', \App::getLocale())
            ->join('languages', 'languages.id', '=', 'post_translations.language_id')
            ->leftJoin('routes', function($join){
                $join->on('post_translations.id', '=', 'routes.routable_id')
                    ->where('routes.routable_type', '=', 'Komma\Kms\Posts\Models\PostTranslation');
            })
            ->leftJoin('images', function($join)
            {
                $join->on('images.imageble_id', '=', 'posts.id')
                    ->where('images.imageble_type', '=', 'Komma\Kms\Posts\Models\Post')
                    ->where('sort_order', '=', 1);
            })
            ->select('posts.*', 'post_translations.language_id', 'post_translations.name', 'post_translations.sub_title', 'post_translations.meta_description', 'routes.route')
            ->where('posts.active', '=', 1)
            ->where('posts.date', '<=', $this->today)
            ->where('posts.date', '<', $post->date)
            ->where('posts.id', '!=', $post->id)
            ->orderBy('posts.date','desc')
            ->first();

//        $previous = Post::where('date', '>', $post->date)->orderBy('date', 'asc')
//            ->with('translation')
//            ->with('translation.route')
//            ->with('images')
//            ->where('active', '=', 1)
//            ->where('date', '<=', $this->today)
//            ->where('id', '!=', $post->id)
//            ->first();
//        $next = Post::where('date', '<', $post->date)->orderBy('date', 'desc')
//            ->with('translation')
//            ->with('translation.route')
//            ->with('images')
//            ->where('active', '=', 1)
//            ->where('date', '<=', $this->today)
//            ->where('id', '!=', $post->id)
//            ->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);
        }
    }

    /**
     * Get all translations of an page
     * based upon this page id
     *
     * @param $page_id
     * @return mixed
     */
    public function postInOtherLanguageRoutes($id)
    {
        return Post::where('id', '=', $id)
            ->with('allTranslations')
            ->with('allTranslations.route')
            ->first();
    }

}