File: D:/HostingSpaces/SBogers104/angeliekly.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\Kms\Categories\Models\Category;
use Komma\Kms\Languages\Language;
use Komma\Pages\PageService;
use Komma\Posts\Models\Post;
class PostService
{
private $today;
public $pageService;
public function __construct(PageService $pageService)
{
$this->pageService = $pageService;
$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 = true, $itemsPerPage = 6)
{
$posts = Post::where('lft', '!=', 1)
->with('translation')
->with('translation.route')
->with('images')
->with('categories')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->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->limit($items);
$posts = $posts->get();
return $posts;
}
public function getPostsByCategory($categoryCodeName, $pagination = true, $itemsPerPage = 6)
{
$posts = Category::where('code_name', $categoryCodeName)
->first()
->posts()
->with('translation')
->with('translation.route')
->with('images')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->orderBy('date', 'desc');
if($pagination)
{
return $posts->paginate($itemsPerPage);
}
else
{
return $posts->get();
}
}
public function getRelativePosts(Post $post)
{
//Get category
$categoryId = $post->categories->first()->id;
//Get posts with same category ordered by date (except this post)
$postsWithCategory = Category::where('id', $categoryId)
->first()
->posts()
->where('post_id', '!=', $post->id)
->orderBy('date', 'DESC')
->with('translation')
->with('translation.route')
->get();
//Filter the post before current post
$postsBefore = $postsWithCategory->filter(function ($item) use ($post)
{
if($item->date >= $post->date)
{
$date = $item->date;
$item->date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
return $item;
}
});
//Filter the post after current post
$postsAfter = $postsWithCategory->filter(function ($item) use ($post)
{
if($item->date <= $post->date)
{
$date = $item->date;
$item->date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
return $item;
}
});
// echo('Total: ' . $postsWithCategory->count() . '<br/>');
// echo('before: ' . $postsBefore->count() . '<br/>');
// echo('after: ' . $postsAfter->count());
//If there are none before, take 3 posts after this post
if($postsBefore->count() == 0)
{
return $postsAfter->take(3);
}
//If there are none after take 3 posts before this post
elseif($postsAfter->count() == 0)
{
return $postsBefore->take(3);
}
//If there is only 1 post after this one, just take 2 from before
elseif($postsAfter->count() == 1)
{
$posts = $postsBefore->take(2);
$prePosts = $postsAfter->take(1);
return $posts->merge($prePosts);
}
else
{
$posts = $postsBefore->take(1);
$prePosts = $postsAfter->take(2);
return $posts->merge($prePosts);
}
}
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);
$filtered = \Session::get('category', false);
if($nextAndPrev)
{
if($filtered) $this->getNextAndPreviousFilteredByCategory($post, $filtered);
else $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);
$post->date = Carbon::createFromFormat('Y-m-d H:i:s', $post->date);
$metaParent = $this->pageService->getMetaData('blog');
$post->translation->meta_title = $post->translation->name . ' | ' . $metaParent->meta_title;
}
public function getNextAndPrevious(&$post)
{
$post->previous = Post::where('date', '>', $post->date)->orderBy('date', 'asc')
->with('translation')
->with('translation.route')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->where('id', '!=', $post->id)
->first();
$post->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();
}
public function getNextAndPreviousFilteredByCategory(&$post, $category)
{
$post->previous = Category::where('code_name', $category)
->first()
->posts()
->with('translation')
->with('translation.route')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->where('date', '>', $post->date)
->orderBy('date', 'asc')
->first();
$post->next = Category::where('code_name', $category)
->first()
->posts()
->with('translation')
->with('translation.route')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->where('date', '<', $post->date)
->orderBy('date', 'desc')
->first();
}
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();
}
}