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);
}
}
}