File: D:/HostingSpaces/SBogers85/dale-int.com/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\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 = 3)
{
$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->limit($items);
$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)->orderBy('date', 'asc')
->with('translation')
->with('translation.route')
->with('images')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->first();
$previous = Post::where('date', '<', $post->date)->orderBy('date', 'desc')
->with('translation')
->with('translation.route')
->with('images')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->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);
}
}
}