File: D:/HostingSpaces/SBogers10/inzigd.komma.pro/app/Komma/Posts/PostController.php
<?php
namespace App\Komma\Posts;
use App\Komma\Base\Controller;
use App\Komma\Components\ComponentService;
use App\Komma\Pages\Models\Page;
use App\Komma\Posts\Models\Post;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
final class PostController extends Controller
{
private $postService;
private $postPaginationKey = 'postPagination';
public function __construct(PostService $postService)
{
parent::__construct();
$this->postService = $postService;
}
/**
*
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
// Get the page through the set links
$page = $this->links->posts->node;
$posts = $this->postService->getPostsPaginated();
$posts->withPath($this->links->posts->route);
$this->keepTrackOfPagination($this->postPaginationKey);
// If we are on the first page return a custom view
if($posts->currentPage() == 1) return $this->indexFirstPage($page, $posts);
// Return view
return view('site.templates.posts_index',[
'page' => $page,
'links' => $this->links,
'posts' => $posts,
]);
}
/**
* The first page of the index
*
* @param Page $page
* @param LengthAwarePaginator $posts
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
private function indexFirstPage(Page $page, LengthAwarePaginator $posts)
{
// Divide posts items into 2 groups
$postItems = $posts->items();
$highlightedPosts = array_splice($postItems,0 ,4);
// Return view
return view('site.templates.posts_index_first',[
'page' => $page,
'links' => $this->links,
'highlightedPost' => array_shift($highlightedPosts),
'subHighlightedPosts' => $highlightedPosts,
'postItems' => $postItems,
'postPaginator' => $posts,
]);
}
/**
* @param Post $post
* @return \Illuminate\Contracts\View\View
*/
public function show(Post $post)
{
// Load the needed relations
$post->load('translation','translations', 'sites', 'images');
// Get the page through the set links
$page = $this->links->posts->node;
$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);
// $otherPosts = $this->postService->getNextPosts($post);
// Return view
return view('site.templates.posts_show',[
'page' => $page,
'post' => $post,
// 'otherModels' => $otherPosts,
'components' => $components,
'links' => $this->links,
'previousRoute' => $previousRoute,
]);
}
}