File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Trainings/TrainingController.php
<?php
namespace App\Komma\Trainings;
use App\Http\Controllers\Controller;
use App\Komma\Components\ComponentService;
use App\Komma\Pages\Models\Page;
use App\Komma\Trainings\Models\Training;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class TrainingController extends Controller
{
private $trainingService;
private $trainingPaginationKey = 'trainingPagination';
public function __construct()
{
parent::__construct();
$this->trainingService = \App::make(TrainingService::class);
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
if (! isset($this->links->trainings)) {
if (! $previewPage = $this->links->_trainings) {
abort(404);
}
$page = Page::where('code_name', 'trainings')->with('translation')->with('translations')->has('translation')->first();
if (! $page) {
abort(404);
}
if (! $page->translation->preview) {
abort(404);
} else {
$this->links->trainings = (object) [
'name' => $page->translation->name,
'route' => $page->translation->route->alias,
'node' => $page,
];
}
} else {
$page = $this->links->trainings->node;
}
$trainings = $this->trainingService->getAllTrainings(true);
$trainings->withPath('/'.$this->links->trainings->route);
// $this->keepTrackOfPagination($this->trainingPaginationKey);
// Make language menu for index page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->trainings, $this->links->home);
//count trainings by type
$trainingCount = count($trainings->where('training_type', '=', '0')->where('active', 1));
$serviceCount = count($trainings->where('training_type', '=', '1')->where('active', 1));
// Return view
return \View::make('site.templates.trainings_index', [
'page' => $page,
'links' => $this->links,
'trainings' => $trainings,
'trainingCount' => $trainingCount,
'serviceCount' => $serviceCount,
'languageMenu' => $languageMenu,
]);
}
/**
* @param Training $training
* @return \Illuminate\Contracts\View\View
*/
public function show(Training $training)
{
// Load the needed relations
$training->load('translation', 'translations', 'sites', 'images');
if (app()->getLocale() != 'en' && $training->translation->populate_from_english) {
$fallbackTranslation = $training->translations->where('language_id', 40)->first();
$training->translation = $fallbackTranslation;
$training->used_fallback_translation = true;
}
$this->checkIfModelShouldThrowAbort($training, BelongsToMany::class);
// Get the page through the set links
if (! isset($this->links->trainings)) {
if (! $previewPage = $this->links->_trainings) {
abort(404);
}
$page = Page::where('code_name', 'trainings')->with('translation')->with('translations')->has('translation')->first();
if (! $page) {
abort(404);
}
if (! $page->translation->preview) {
abort(404);
} else {
$this->links->trainings = (object) [
'name' => $page->translation->name,
'route' => $page->translation->route->alias,
'node' => $page,
];
}
} else {
$page = $this->links->trainings->node;
}
$componentService = \App::make(ComponentService::class);
$components = $componentService->getViewComponents($training->translation);
// Create previous route for better navigation UX
$previousRoute = $this->createPreviousRoute($this->trainingPaginationKey, $this->links->trainings->route);
$otherTrainings = $this->trainingService->getNextTrainings($training);
// Make language menu for found index page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->trainings);
$this->pageService->extendLanguageMenuWithResource($languageMenu, $training, $this->links->home);
// Return view
return \View::make('site.templates.trainings_show', [
'page' => $page,
'training' => $training,
'otherModels' => $otherTrainings,
'components' => $components,
'links' => $this->links,
'previousRoute' => $previousRoute,
'languageMenu' => $languageMenu,
]);
}
}