File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Updates/UpdateController.php
<?php
namespace App\Komma\Updates;
use App\Http\Controllers\Controller;
use App\Komma\Components\ComponentService;
use App\Komma\Updates\Models\Update;
use Carbon\Carbon;
final class UpdateController extends Controller
{
/** @var UpdateService */
private $updateService;
const AMOUNT_EACH_REQUEST = 9;
public function __construct()
{
parent::__construct();
$this->updateService = \App::make(UpdateService::class);
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
// Get the page through the set links
$page = $this->links->updates->node;
$pageNumber = (int) \Input::get('page', 0);
$updates = $this->updateService->getUpdates(null, \Input::get('page', 0) * 9);
$hasMoreUpdates = $this->updateService->getUpdates(null, ($pageNumber + 1) * 9, 1)->isNotEmpty();
// Make language menu for index page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->updates, $this->links->home);
// Return view
return \View::make('site.templates.updates_index', [
'page' => $page,
'links' => $this->links,
'updates' => $updates,
'languageMenu' => $languageMenu,
'hasMoreUpdates' => $hasMoreUpdates,
'nextPageLink' => '/'.$this->links->updates->route.'?page='.($pageNumber + 1),
]);
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function category($categoryId)
{
// Get the page through the set links
$page = $this->links->updates->node;
$pageNumber = (int) \Input::get('page', 0);
$updates = $this->updateService->getUpdates($categoryId, $pageNumber * 9);
$hasMoreUpdates = $this->updateService->getUpdates($categoryId, ($pageNumber + 1) * 9, 1)->isNotEmpty();
// Make language menu for index page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->updates, $this->links->home);
// Return view
return \View::make('site.templates.updates_index', [
'page' => $page,
'links' => $this->links,
'updates' => $updates,
'languageMenu' => $languageMenu,
'activeUpdateCategory' => $categoryId,
'hasMoreUpdates' => $hasMoreUpdates,
'nextPageLink' => '/'.$this->links->updates->route.'/'.$categoryId.'/'.str_slug(__('site/updates.categories.'.$categoryId)).'?page='.($pageNumber + 1),
]);
}
/**
* @param Update $update
* @return \Illuminate\Contracts\View\View
*/
public function show(Update $update)
{
// Load the needed relations
$update->load('translation', 'translations', 'site');
if (app()->getLocale() != 'en' && $update->translation->populate_from_english) {
$fallbackTranslation = $update->translations->where('language_id', 40)->first();
$update->translation = $fallbackTranslation;
$update->used_fallback_translation = true;
}
$this->checkIfModelShouldThrowAbort($update);
// Get the page through the set links
$page = $this->links->updates->node;
$componentService = \App::make(ComponentService::class);
$components = $componentService->getViewComponents($update->translation);
$otherUpdates = $this->updateService->getNextUpdates($update);
// Make language menu for found index page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->updates);
$this->pageService->extendLanguageMenuWithResource($languageMenu, $update, $this->links->home);
// Return view
return \View::make('site.templates.updates_show', [
'page' => $page,
'update' => $update,
'otherModels' => $otherUpdates,
'components' => $components,
'links' => $this->links,
'languageMenu' => $languageMenu,
]);
}
/**
* @param $currentLength
* @return mixed
* @see: updateGrid.blade.php
*/
public function getTimelineUpdates($currentLength)
{
// Set language by the given get lang
$languageIso = \Input::get('lang', null);
if (isset($languageIso)) {
\App::changeLanguageByIso2($languageIso);
}
// Get category from get param
$categoryId = \Input::get('category', null);
// Get the amount to take plus one
$updates = $this->updateService->getUpdates($categoryId, $currentLength, (self::AMOUNT_EACH_REQUEST + 1));
// Get only the amount we need
$jsonUpdates = $updates->take(self::AMOUNT_EACH_REQUEST);
// Convert those to json updates
$jsonUpdates = $jsonUpdates->map(function ($update, $key) {
$image = null;
if (isset($update->images) && $update->images->isNotEmpty() && ! empty($update->languageImages()->isNotEmpty())) {
$image = $update->languageImages()->first()->medium_image_url;
}
return [
'name' => $update->translation->name,
'slug' => $update->translation->slug,
'url' => '/'.$this->links->updates->route.'/'.$update->translation->slug,
'description' => $update->translation->description,
'image' => $image,
'date' => [
'day' => $update->translation->date->day,
'monthName' => __('calendar.monthNames.'.($update->translation->date->month - 1)),
'month' => $update->translation->date->month,
'year' => $update->translation->date->year,
],
];
});
$response = [
'updates' => $jsonUpdates,
'updatesLeft' => $updates->count() > self::AMOUNT_EACH_REQUEST,
'readMoreTranslation' => trans('site/global.readMore'),
];
if (isset($languageIso)) {
$response['language'] = $languageIso;
}
// Return the json if we have more the amount needed, we know we can make an other request
return \Response::json($response);
}
/*
* Note: do not change this method since it is used by Rentman
*/
public function getUpdates()
{
$start = \Input::get('start', 0);
$limit = \Input::get('limit', 10);
// Get the page through the set links
$page = $this->links->updates->node;
$updates = $this->updateService->getUpdatesForApi($limit, $start);
$usedLanguages = $this->site->languages->keyBy('id');
$jsonUpdates = [];
foreach ($updates as $update) {
$model = [
'id' => $update->id,
'date' => $update->date,
];
$hasTranslation = false;
foreach ($update->translations as $translation) {
if (! $translation->active || ! $translation->show_on_feed) {
continue;
}
$hasTranslation = true;
$language = $usedLanguages->find($translation->language_id);
$pageRoute = $page->translations->where('language_id', $translation->language_id)->first()->route->alias;
$model[$language->iso_2] = [
'name' => $translation->name,
'description' => $translation->meta_description,
'date' => $translation->date->format(Carbon::DEFAULT_TO_STRING_FORMAT),
'url' => url($pageRoute.'/'.$translation->slug),
];
}
if ($hasTranslation) {
$jsonUpdates[] = $model;
}
}
return \Response::json($jsonUpdates);
}
}