File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Magazines/MagazineService.php
<?php
namespace App\KommaApp\Magazines;
use App\KommaApp\Base\Service;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Magazines\Models\Magazine;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Carbon;
class MagazineService extends Service
{
const MAGAZINES_PER_PAGE = 6;
const MAGAZINE_ARTICLES_PER_PAGE = 8;
private $today;
public function __construct()
{
$this->today = Carbon::now()->addHour();
$this->today = $this->today->format('Y-m-d H:i:s');
parent::__construct();
}
/**
* Get the slug of the latest magazine
* (that has at least one article)
*
* @return string
*/
public function getLatestMagazineAlias(){
if($magazine = $this->site
->magazines()
->has('magazineArticles')
->with('translation')
->where('active', 1)
->orderBy('date', 'desc')
->first()
) return $magazine->translation->slug;
}
/**
* Get the latest magazine
* (that has at least one article)
*
* @return string
*/
public function getLatestMagazine(){
$magazine = $this->site
->magazines()
->has('magazineArticles')
->with('translation', 'images')
->where('active', 1)
->orderBy('date', 'desc')
->first();
return $magazine;
}
/**
* Get the magazine route for all languages
*
* @param Magazine $magazine
* @return array
*/
public function getLanguageRoutes(Magazine $magazine)
{
if(!isset($magazine->translations)) $magazine->load('translations');
$languageRoutes = [];
foreach ($magazine->translations as $translation){
if(!$translation->slug || $translation->slug == '') continue;
$languageRoutes[$translation->language_id] = (object)[
'iso' => Language::where('id', $translation->language_id)->first()->iso_2,
'route' => 'magazine/' . $translation->slug
];
}
return $languageRoutes;
}
/**
* Get the latest active magazines
* with optional parameter to paginate and get a given amount
*
* @param bool $pagination
* @param int|null $amount
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getMagazines(bool $pagination = false, int $amount = 4)
{
$magazines = $this->site
->magazines()
->has('magazineArticles')
->with('translation', 'images')
->where('active', 1)
->where('date', '<=', $this->today)
->orderBy('date','desc')
->orderBy('created_at', 'asc')
->with('translation');
// Get the magazines paginated or by get with optional a given amount
if($pagination){
$magazines = $magazines->paginate(self::MAGAZINES_PER_PAGE);
}
else {
if($amount) $magazines = $magazines->take($amount);
$magazines = $magazines->get();
}
return $magazines;
}
/**
* Get the magazines newer then or of current year
*
* @param int $year
* @param bool $onlyCurrentYear
* @return Collection|\Illuminate\Database\Eloquent\Relations\HasMany
*/
public function getMagazinesFromStartOfYear(int $year, bool $onlyCurrentYear = false)
{
$carbonYearDate = Carbon::createMidnightDate($year,1,1);
$magazines = $this->site
->magazines()
->has('magazineArticles')
->where('active', 1)
->with('translation', 'images')
->where('date', '>=', $carbonYearDate->format('Y-m-d H:i:s'));
if($onlyCurrentYear){
$magazines = $magazines->where('date', '<=', $carbonYearDate->addYear()->format('Y-m-d H:i:s') );
}
$magazines = $magazines->orderBy('date','asc')
->orderBy('created_at', 'asc')
->get();
return $magazines;
}
/**
* Create array with magazine each year
*
* @param Collection $magazines
* @return array
*/
public function divideMagazinesIntoYears(Collection $magazines)
{
// Divide the magazines in years
$magazineEachYear = [];
foreach ($magazines as $number => $magazine)
{
$magazineYear = $magazine->date->year;
if(!isset($magazineEachYear[$magazineYear])) $magazineEachYear[$magazineYear] = [];
$magazineEachYear[$magazineYear][] = $magazine;
}
return $magazineEachYear;
}
/**
* Get the highlighted article of this Magazine
*
* @param Magazine $magazine
* @return \Illuminate\Database\Eloquent\Model|null|object
*/
public function getCoverMagazineArticle(Magazine $magazine)
{
$highlightedArticle = $magazine->magazineArticles()
->where('active', 1)
->with('translation', 'images')
->first();
return $highlightedArticle;
}
/**
* Get the magazine articles of the given magazine
* this can be either with or without exclude id's or pagination
*
* @param Magazine $magazine
* @param array|null $excludeIds
* @param bool $paginate
* @param int $amount
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|Collection|\Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
*/
public function getMagazineArticles(Magazine $magazine, array $excludeIds = null, $paginate = true, $amount = 3)
{
$articles = $magazine->magazineArticles()
->where('active', 1)
->with('translation', 'images');
if($excludeIds) $articles = $articles->whereNotIn('magazine_articles.id', $excludeIds);
if($paginate) $articles = $articles->paginate(self::MAGAZINE_ARTICLES_PER_PAGE);
else $articles = $articles->take($amount)->get();
return $articles;
}
public function getMagazineForHome()
{
$magazine = $this->getLatestMagazine();
if(empty($magazine)) return;
$coverArticle = $this->getCoverMagazineArticle($magazine);
// If there isn't a article found return the magazine
if(!$coverArticle) return $magazine;
$magazine->cover = $coverArticle;
// Get the magazine articles marked for home
$homeArticles = $magazine->magazineArticles()
->where('show_on_home', 1)
->where('magazine_articles.id', '!=', $coverArticle->id)
->with('images', 'translation')
->get();
// If there aren't any home marked articles then load the first three
if($homeArticles->count() == 0) $homeArticles = $this->getMagazineArticles($magazine,[$coverArticle->id], false);
$homeArticles->load('tags', 'tags.translation');
$magazine->homeArticles = $homeArticles;
return $magazine;
}
}