File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Webinars/WebinarService.php
<?php
namespace App\Komma\Webinars;
use App\Komma\Base\Service;
use Illuminate\Support\Collection;
class WebinarService extends Service
{
public function getWebinars($pagination = false, $itemsPerPage = 9)
{
$webinars = $this->site
->webinars()
->with('translation', 'images', 'fallback_translation')
->orderBy('webinars.lft')
// We need to use a join to select the active webinars because that is defined on the translations
->join('webinar_translations', 'webinars.id', '=', 'webinar_translations.webinar_id')
->select('webinars.*', 'webinar_translations.active', 'webinar_translations.populate_from_english', 'webinar_translations.language_id')
->where(function ($query) {
$query->where('active', '=', 1)
->orWhere('populate_from_english', '=', 1);
})
->where('language_id', \App::getLanguage()->id);
if ($pagination) {
$webinars = $webinars->paginate($itemsPerPage);
// Skip the population if language is already english
if (app()->getLocale() != 'en') {
$this->populatedFallbackTranslationOnCollection($webinars->getCollection());
}
} else {
$webinars = $webinars->paginate(999);
// Skip the population if language is already english
if (app()->getLocale() != 'en') {
$this->populatedFallbackTranslationOnCollection($webinars->getCollection());
}
}
return $webinars;
}
private function populatedFallbackTranslationOnCollection(Collection $webinars)
{
foreach ($webinars as $webinar) {
// Skip if we don't need to populated
if (! $webinar->translation->populate_from_english) {
continue;
}
$webinar->translation = $webinar->fallback_translation;
$webinar->used_fallback_translation = true;
}
}
}