File: D:/HostingSpaces/SBogers10/rentman.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 Komma\Kms\Languages\Language;
use Komma\LanguageService;
use Komma\Posts\Models\Post;
class PostService
{
private $today;
public function __construct()
{
$this->today = Carbon::now()->addHour()->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('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.language_id','post_translations.active', 'post_translations.title', 'post_translations.sub_title', 'post_translations.description', 'post_translations.meta_title', 'post_translations.meta_description', 'routes.route', 'images.medium_image_url')
->where('post_translations.active', '=', 1)
->where('posts.date', '<=', $this->today)
->where('post_translations.title', '!=', '')
->where('post_translations.description', '!=', '[]')
->orderBy('posts.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')
->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);
})
->select('posts.*', 'post_translations.active')
->where('post_translations.active', '=', 1)
->where('posts.date', '<=', $this->today);
$posts->orderBy('posts.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')
->with('socialMediaImages')
->where('date', '<=', $this->today)
->first()
) return \App::abort(404, 'post not found');
if($nextAndPrev) $nextAndPrev = $this->getNextAndPrevious($post);
// Check if there are social media images are defined
if($post->socialMediaImages->count() != 0){
$languageArray = [];
$languageService = new LanguageService();
foreach ($post->socialMediaImages as $socialMediaImage){
$explodedAttributeKey = explode('_', $socialMediaImage->attribute_key);
$langIso = end($explodedAttributeKey);
$language = $languageService->getIsoById($langIso);
$languageArray[$language->iso_2] = $socialMediaImage;
}
$post->socialMediaImageByLanguage = (object)$languageArray;
}
return $post;
}
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.language_id','post_translations.active', 'post_translations.title', 'post_translations.sub_title', 'post_translations.meta_description', 'routes.route')
->where('post_translations.active', '=', 1)
->where('posts.date', '<=', $this->today)
->where('posts.date', '>', $post->date)
->where('posts.id', '!=', $post->id)
->where('post_translations.title', '!=', '')
->where('post_translations.description', '!=', '[]')
->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.language_id','post_translations.active', 'post_translations.title', 'post_translations.sub_title', 'post_translations.meta_description', 'routes.route')
->where('post_translations.active', '=', 1)
->where('posts.date', '<=', $this->today)
->where('posts.date', '<', $post->date)
->where('posts.id', '!=', $post->id)
->where('post_translations.title', '!=', '')
->where('post_translations.description', '!=', '[]')
->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;
}
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 getOtherLanguageRoutes($id)
{
return Post::where('id', '=', $id)
->with('allTranslations')
->with('allTranslations.route')
->first();
}
}