File: D:/HostingSpaces/SBogers10/sportivo.komma.pro/app/Komma/Blog/BlogController.php
<?php
namespace Komma\Blog;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\App;
use Komma\Blog\BlogService;
use Komma\Page\PageService;
class BlogController extends Controller
{
public $data;
protected $blogService;
protected $pageService;
public function __construct(BlogService $blogService, PageService $pageService)
{
$this->blogService = $blogService;
$this->pageService = $pageService;
//get amount of blogitems in db
$items =$this->blogService->getAmountOfBlogItems();
$this->data = [
'amountOfItems' => $items,
//calculated how many paginations there should be
'amountOfPagination' => (ceil(($items - 4) / 5) + 1)
];
}
/**
* Show all articles list
*
* @param int $pagination
* @return mixed
*/
public function show($pagination = 1)
{
//if pagination shouldn't exist, through 404 page
if(($pagination > $this->data['amountOfPagination'])||($pagination <= 0)){
App::abort(404);
}
$this->data['pagination'] = $pagination;
$this->data = array_merge($this->data, $this->pageService->getContent('blog'));
if($pagination == 1)
{
//First page only loads 4 blogitems
$this->data['blogArticles'] = $this->blogService->getBlogItems(4);
$this->data['firstPage'] = true;
}
else{
//calculation for how much articles must be skipped
$pagination-=2;
$paginationSkip = 4 + $pagination*5;
//load next 5 blog items
$this->data['blogArticles'] = $this->blogService->getBlogItems(5, $paginationSkip);
}
return \View::make('layouts.pages.blog')->with('data', (object)$this->data);
}
/**
* Load an specific article
*
* @param $slug
* @return mixed
*/
public function showArticle($slug){
$article = $this->blogService->getBlogItem($slug);
if($article==false){
App::abort(404);
}
$this->data = array_merge($this->data, $this->pageService->getContent('blog'));
$this->data = array_merge($this->data, $article);
//get Previous blogitems
$previousBlog = $this->blogService->getPreviousBlogItems($slug, 2);
//fill to 2 blog items from newest when there aren't 2 previous blogitems
if(count($previousBlog) == 0 ){
$previousBlog = $this->blogService->getLatestBlogItems(2);
}
if(count($previousBlog) == 1 ){
$previousBlog = array_merge($previousBlog, $this->blogService->getLatestBlogItems(1));
}
$this->data = array_merge($this->data, ['articleBlock' => ['header' => 'Lees ook andere blogitems', 'articles'=> $previousBlog, 'footer'=>'Bekijk alle blog artikelen', 'url'=>'/blog']]);
return \View::make('layouts.pages.blogArticle')->with('data', (object)$this->data);
}
}