File: D:/HostingSpaces/groendesignkoencox/groendesign-koencox.be/app/Posts/PostController.php
<?php
namespace App\Posts;
use App\Base\Controller;
use App\Buttons\Models\Button;
use App\Components\ComponentService;
use App\Pages\Models\Page;
use App\Posts\Models\Post;
final class PostController extends Controller
{
private $postService;
private $postPaginationKey = 'postPagination';
public function __construct(PostService $postService)
{
parent::__construct();
$this->postService = $postService;
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
// If you don't know the code_name get page through given page id through request
// $page = $this->pageService->getPage(request()->get('page_id'));
$page = $this->links->posts->node;
$posts = $this->postService->getPostsPaginated();
$posts->withPath($this->links->posts->route);
// Make language menu for given page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->{$page->code_name}, $this->links->home);
$this->keepTrackOfPagination($this->postPaginationKey);
$this->pageService->setSharableVariables($page->servicepoint_id, $page->servicepoint_button_id, $page->servicepoint_heading);
//Get the "discover more" page code names.
$discover_more_page_codenames = $page->discoverPages()->get()->map(function(Page $page) {
return $page->code_name;
})->toArray();
// Return view
return view('site.templates.posts_index',[
'page' => $page,
'links' => $this->links,
'languageMenu' => $languageMenu,
'posts' => $posts,
'discover_page_codenames' => $discover_more_page_codenames,
]);
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function filters()
{
$filters = request('filters');
dd('customize filter function in PostController');
// if (sizeof($filters) != 1) {
// throw abort(404);
// }
// $categoryName = $filters[0];
//
// // Check if category exist else trow 404
// if ( ! $category = PostCategoryTranslation::where('slug', $categoryName)
// ->with('translatable')
// ->first()) {
// throw abort(404);
// }
//
// $page = $this->pageService->getPageByCodeName('blog');
// $otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);
//
// //Get posts through the category
// $posts = $category->translatable->posts()->paginate(8);
// $posts->withPath('/' . $this->links->blog->route . '/' . $category->slug);
//
//
// // Return view
// return view($this->baseViewPath.$this->pagePrefix.'index',[
// 'page' => $page,
// 'links' => $this->links,
// 'otherLanguages' => $otherLanguageRoutes,
// 'posts' => $posts,
// ]);
}
/**
* @param Post $post
* @return \Illuminate\Contracts\View\View
*/
public function show(Post $post)
{
$post->load('translation','translations');
$latestPosts = $this->postService->getLatestPost(10)->get();
$page = $this->links->posts->node;
// Make language menu for given page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->{$page->code_name}, $this->links->home);
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($post->translation);
// Create previous route for better navigation UX
$previousRoute = $this->createPreviousRoute($this->postPaginationKey, $this->links->posts->route);
// split the Hero images from the "normal" post images
list($documents, $heroDocuments) = $post->documents->partition(function ($document) {
return $document->key == 'Documents-posts';
});
$post->documents = $documents;
// fill the page hero object
$heroButton = Button::find($post->translation->hero_button_id);
$post->hero = (object)[
'documents' => $heroDocuments,
'active' => $post->translation->hero_active,
'title' => $post->translation->hero_title,
'description' => $post->translation->hero_description,
'buttons' => !empty($heroButton) ? $heroButton : null,
];
$this->pageService->setSharableVariables($page->servicepoint_id, $page->servicepoint_button_id, $page->servicepoint_heading);
// Return view
return view('site.templates.posts_show',[
'page' => $page,
'post' => $post,
'latestPosts' => $latestPosts,
'components' => $components,
'links' => $this->links,
'previousRoute' => $previousRoute,
'languageMenu' => $languageMenu,
]);
}
}