File: D:/HostingSpaces/SBogers10/ridderstee.komma.pro/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;
use Komma\Posts\Models\PostTranslation;
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, $spliceFirst = false)
{
// $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($spliceFirst){
$firstPost = $posts->first();
if($firstPost) $posts = $posts->where('posts.id', '!=', $firstPost->id);
}
if($pagination)
{
$posts = $posts->paginate($itemsPerPage);
}
else
{
$posts = $posts->get();
}
if($spliceFirst){
return (object)[
'firstPost' => $firstPost,
'overview' => $posts
];
}
return $posts;
}
public function getLatestPosts($items = 5)
{
$posts = PostTranslation::where('language_id', '=', Language::where('iso_2', \App::getLocale())->first()->id)
->join('posts', 'posts.id', '=', 'post_translations.post_id')
->select('posts.date', 'posts.active', 'post_translations.*')
->with(['post' => function ($query){
$query->where('active', '=', 1)
->where('date', '<=', $this->today)
->orderBy('date');
}, 'route', 'post.images'])
->where('active', 1)
->orderBy('date', 'DESC')
->where('date', '<=', $this->today)
->limit($items)
->get();
// $returnPosts = [];
// foreach ($posts as $key => $post){
//
// if(!isset($post->translation)){
// $posts->forget($key);
// continue;
// }
//
// $returnPosts[] = $post;
// }
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) $post->relativePosts = $this->getRelativePosts($post);
return $post;
}
public function fillPost(Post &$post){
$post->routeInOtherLanguages = $this->postInOtherLanguageRoutes($post->id)->allTranslations;
// $post->translation->meta_description = str_limit(strip_tags($post->translation->description, 155));
//Comment the decode when there aren't dynamic blocks
$post->translation->description = json_decode($post->translation->description);
}
/**
* Get relative posts based upon the current post
*
* @param Post $post
* @return array
*/
public function getRelativePosts($post)
{
$newer = \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', 'images.*')
->where('posts.active', '=', 1)
->where('posts.date', '<=', $this->today)
->where('posts.date', '>', $post->date)
->where('posts.id', '!=', $post->id)
->orderBy('posts.date','asc')
->take(3)
->orderBy('posts.date','desc')
->get();
$older = \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', 'images.*')
->where('posts.active', '=', 1)
->where('posts.date', '<=', $this->today)
->where('posts.date', '<', $post->date)
->where('posts.id', '!=', $post->id)
->orderBy('posts.date','desc')
->take(3)
->get();
$relativePosts = [];
// Check if there are even newer posts
if(sizeof($newer) == 0){
$relativePosts = $older;
}
// Or if there are older posts
elseif(sizeof($older) == 0){
$relativePosts = array_reverse($newer);
}
else
{
// Reverse order because we first sort them on closest first
// But in the view it needs to be oldest first
$newer = array_reverse($newer);
// Get the newer posts
for ($i = 0; $i <= sizeof($newer); $i++)
{
if($i >= 2 || ! isset($newer[$i])) break;
$relativePosts[] = $newer[$i];
}
// Get older post till there are 3 in $relativePosts
for ($j = 0; $j <= (3 - sizeof($relativePosts)); $j++)
{
if( ! isset($older[$j])) break;
$relativePosts[] = $older[$j];
}
}
$this->makeCarbonDate($relativePosts);
return $relativePosts;
}
public function makeCarbonDate(&$posts)
{
foreach ($posts as $post)
{
$date = $post->date;
$post->date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
}
}
public function makeCarbonDateOnTranslations(&$posts)
{
foreach ($posts as $post)
{
if(isset($post->post)){
$date = $post->post->date;
$post->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();
}
}