File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Posts/PostController.php
<?php
namespace App\Komma\Posts;
use App\Http\Controllers\Controller;
use App\Komma\Components\ComponentService;
use App\Komma\Dynamic\ComponentType\ComponentTypes;
use App\Komma\Posts\Models\Post;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class PostController extends Controller
{
private $postService;
private $postPaginationKey = 'postPagination';
public function __construct()
{
parent::__construct();
$this->postService = \App::make(PostService::class);
}
public function mb_ucfirst($string, $encoding = 'UTF-8') {
$firstChar = mb_substr($string, 0, 1, $encoding);
$then = mb_substr($string, 1, mb_strlen($string, $encoding) - 1, $encoding);
return mb_strtoupper($firstChar, $encoding).$then;
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index(string $category = '')
{
// Get the page through the set links
$page = $this->links->posts->node;
if(empty($category)) $category = __('postCategories.default');
$filterArray = __('postCategories.categories');
$filterKey = array_search(ucfirst($category), $filterArray);
if($filterKey === 5) $filterKey = '';
$posts = $this->postService->getAllPosts(true, 9, $filterKey);
if(! empty($filterKey)) $posts->withPath('/'.$this->links->posts->route.'/'.$category);
else $posts->withPath('/'.$this->links->posts->route.'/'.strtolower(__('postCategories.categories.5')));
$this->keepTrackOfPagination($this->postPaginationKey);
// Make language menu for index page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->posts, $this->links->home);
// Return view
return \View::make('site.templates.posts_index', [
'page' => $page,
'links' => $this->links,
'posts' => $posts,
'languageMenu' => $languageMenu,
'currentCategory' => $category,
]);
}
/**
* @param Post $post
* @return \Illuminate\Contracts\View\View
*/
public function show(Post $post)
{
// Load the needed relations
$post->load('translation', 'translations', 'sites', 'images');
if (app()->getLocale() != 'en' && $post->translation->populate_from_english) {
$fallbackTranslation = $post->translations->where('language_id', 40)->first();
$post->translation = $fallbackTranslation;
$post->used_fallback_translation = true;
}
$this->checkIfModelShouldThrowAbort($post, BelongsToMany::class);
// Get the page through the set links
$page = $this->links->posts->node;
$componentService = \App::make(ComponentService::class);
$components = $componentService->getViewComponents($post->translation);
//Legacy versie aanmaken als fallback
//Knoppen als dynamic component maken
// Insert actual component type id
$showEndMenu = $components->where('type_id', ComponentTypes::ARTICLE_BUTTONS)->isEmpty();
// Create previous route for better navigation UX
$previousRoute = $this->createPreviousRoute($this->postPaginationKey, $this->links->posts->route);
$otherPosts = $this->postService->getNextPosts($post);
// Make language menu for found index page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->posts);
$this->pageService->extendLanguageMenuWithResource($languageMenu, $post, $this->links->home);
// Return view
return \View::make('site.templates.posts_show', [
'page' => $page,
'post' => $post,
'otherModels' => $otherPosts,
'components' => $components,
'showEndMenu' => $showEndMenu,
'links' => $this->links,
'previousRoute' => $previousRoute,
'languageMenu' => $languageMenu,
]);
}
}