File: D:/HostingSpaces/SBogers110/franciscaansebeweging.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\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 = 6, $ids = null, $onlyHighlighted = 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('images.attribute_key', '=' ,'images')
->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.*', 'routes.route', 'images.medium_image_url')
->where('posts.active', '=', 1)
->where('posts.date', '<=', $this->today)
->orderBy('posts.date', 'desc');
if($ids !== null)
{
$posts = $posts->whereIn('posts.id', $ids);
}
if($onlyHighlighted === true){
$posts = $posts->where('posts.highlighted', 1);
}
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')
->with('author_image')
->where('active', '=', 1)
->where('date', '<=', $this->today)
->first()
) return \App::abort(404, 'post not found');
$this->fillPost($post);
if($nextAndPrev) $nextAndPrev = $this->getNextAndPrevious($post);
return $post;
}
public function fillPost(Post &$post)
{
// $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 getNextAndPrevious(&$post)
{
$previous = \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.*', 'routes.route')
->where('posts.active', '=', 1)
->where('posts.date', '<=', $this->today)
->where('posts.date', '>', $post->date)
->where('posts.id', '!=', $post->id)
->orderBy('posts.date', 'asc')
->first();
$next = \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.*', 'routes.route')
->where('posts.active', '=', 1)
->where('posts.date', '<=', $this->today)
->where('posts.date', '<', $post->date)
->where('posts.id', '!=', $post->id)
->orderBy('posts.date', 'desc')
->first();
// $previous = Post::where('date', '>', $post->date)->orderBy('date', 'asc')
// ->with('translation')
// ->with('translation.route')
// ->with('images')
// ->where('active', '=', 1)
// ->where('date', '<=', $this->today)
// ->where('id', '!=', $post->id)
// ->first();
// $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();
$post->next = $next;
$post->previous = $previous;
}
/**
* Convert post date to carbon
*
* @param $posts
*/
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();
}
/**
* Convert for sorting
*
* @param $activities
*/
public function convertForSorting($activities)
{
$activitiesArray = [];
foreach ($activities as $activity) {
$createDate = new \DateTime( $activity->date );
$strippedDate = $createDate->format('d-m-Y');
$newDate = Carbon::createFromFormat('d-m-Y', $strippedDate);
$cloneActivity = clone $activity;
$cloneActivity->date = $newDate;
$activitiesArray[$newDate . $cloneActivity->name] = $cloneActivity;
}
ksort($activitiesArray);
return $activitiesArray;
}
}