File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Locations/LocationController.php
<?php
namespace App\Komma\Locations;
use App\Komma\Base\Controller;
use App\Komma\Components\ComponentService;
use App\Komma\Kiyoh\KiyohService;
use App\Komma\Locations\Models\Location;
use App\Komma\Products\ProductService;
use Illuminate\Support\Arr;
final class LocationController extends Controller
{
/**
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
if (! $pageNode = $this->links->locations) {
abort(404);
}
$page = $pageNode->node;
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($page->translation);
// Make language menu for given page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->{$page->code_name}, $this->links->home);
$provinces = Arr::where((array) $this->links, function ($value, $key) {
return substr($key, 0, 10) === 'locations.';
});
// Return view
return view('site.templates.locations', [
'components' => $components,
'links' => $this->links,
'languageMenu' => $languageMenu,
'page' => $page,
'provinces' => $provinces,
]);
}
/**
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function province()
{
// Extracts the page out of the links
if (! $pageNode = collect($this->links)
->where('page_id', request()->get('page_id'))
->first()) {
abort(404);
}
$page = $pageNode->node;
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($page->translation);
// Make language menu for given page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->{$page->code_name}, $this->links->home);
$locations = Location::where('province_page_id', $page->id)
->with('translation')
->get();
return view('site.templates.locations_province', [
'components' => $components,
'links' => $this->links,
'languageMenu' => $languageMenu,
'page' => $page,
'pageNode' => $pageNode,
'locations' => $locations,
]);
}
/**
* @param Location $location
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function show(Location $location)
{
// Make sure location has province page set
if (! isset($location->province_page_id)) {
abort(404);
}
$location->load('translation', 'images', 'boundProducts');
// Extracts the page out of the links
if (! $provinceNode = collect($this->links)
->where('page_id', $location->province_page_id)
->first()) {
abort(404);
}
// Make sure the correct route isset, so the desired province page with location
if (url($provinceNode->route.'/'.$location->translation->slug) !== request()->get('original_path')) {
abort(404);
}
/** @var KiyohService $kiyohService */
$kiyohService = app()->make(KiyohService::class);
$location->kiyoh = $kiyohService->getScoreForLocation($location->id);
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($location->translation);
// session()->put('searchLocation', $location->id);
/** @var ProductService $productService */
$productService = app()->make(ProductService::class);
$locationProducts = $productService->getActivityProductsWhereIn(
$location->boundProducts->pluck('id')->toArray()
);
$otherLocationIds = $this->locationService->getOtherLocationIds($location);
return view('site.templates.locations_show', [
'links' => $this->links,
'location' => $location,
'components' => $components,
'provinceNode' => $provinceNode,
'otherLocationIds' => $otherLocationIds,
'locationProducts' => $locationProducts,
'showPricesInc' => '',
]);
}
}