File: D:/HostingSpaces/SBogers10/zuiderbos.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 Illuminate\Support\Facades\Paginator;
use Komma\Kms\Languages\Language;
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::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)
{
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);
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);
}
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();
}
/**
* Generate pagination for combined newsletters en posts
*
* @param $array
* @return mixed
*/
public function customPaginator($array)
{
$perPage = 9;
$page = \Input::get('page', 1);
if ($page > count($array) or $page < 1) {
$page = 1;
}
$offset = ($page * $perPage) - $perPage;
$perPageUnits = array_slice($array, $offset, $perPage);
$pagination = Paginator::make($perPageUnits, count($array), $perPage);
return $pagination;
}
/**
* Get relevant newsitems
*
* @param $post (Post of Newsletter)
* @param $news
*/
public function getMoreNews($post, $news)
{
$news = array_values($news);
foreach ($news as $key => $item) {
if ($post->code_name != $item->code_name) {
continue;
}
// var_dump($key);
if ($key >= 2) {
if (isset($news[$key + 1]) && isset($news[$key + 2])) {
return [$news[$key + 1], $news[$key + 2]];
} elseif (isset($news[$key + 1]) && ! isset($news[$key + 2])) {
return [$news[$key - 1], $news[$key + 1]];
} else {
return [$news[$key - 2], $news[$key - 1]];
}
} elseif ($key == 1) {
return array_slice($news, 2, 2);
} else {
return array_slice($news, 1, 2);
}
}
}
}