File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Shop/Products/ProductController.php
<?php
namespace App\Komma\Shop\Products;
use App\Http\Middleware\WildcardResolver;
use App\Komma\Base\Controller;
use App\Komma\Globalization\RegionInfoInterface;
use App\Komma\Shop\Catalog\Kms\CatalogService;
use App\Komma\Shop\Categories\Models\Category;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Vat\VatService;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpKernel\Exception\HttpException;
class ProductController extends Controller
{
private $pagePrefix = 'shop.pages.products.';
/** @var CatalogService */
private $catalogService;
public function __construct(CatalogService $catalogService)
{
parent::__construct();
$this->catalogService = $catalogService;
}
/**
* @return HttpException|\Illuminate\Contracts\View\View
*/
public function index()
{
$page = $this->links->products->node;
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);
$productWildcardIndexRoute = WildcardResolver::getWildCardIndexRouteForPageWithCodeName('products');
$perPage = Input::get('per_page') ?? 20;
$page = Input::get('page') ?? 1;
$allItems = $this->catalogService->itemsQueryWithRelations()->paginate($perPage);
$allItems->withPath($productWildcardIndexRoute->alias);
// Return view
return view($this->pagePrefix.'index', [
'page' => $page,
'links' => $this->links,
'otherLanguages' => $otherLanguageRoutes,
'productWildcardIndexRoute' => $productWildcardIndexRoute,
'items' => $allItems,
]);
}
/**
* @param Product $product
* @return \Illuminate\Contracts\View\View
*/
public function show(Product $product)
{
$product->load('translation', 'documents');
//Get regional information to format prices properly
$regionInfo = app(RegionInfoInterface::class);
//Calculate vat for product
$rateService = new VatService();
$rateService->calculateVatForProductable($product);
//Get the product category parent, based on the url. Since that contains information about the parent category.
$productCategoryParent = null;
if ($product->categories()->count() > 0) {
//Get the parent category of the product
$myDomain = request()->server('HTTP_HOST'); //Example: localhost:8000
$requestsSource = request()->server('HTTP_REFERER'); //Example: http://localhost:8000/nl/producten
$explodedRequestSource = explode('/', $requestsSource);
$categorySlug = end($explodedRequestSource);
/** @var Category $productCategory */
$productCategory = $product->categories()->where('code_name', $categorySlug);
$productCategoryParent = $productCategory->getParentId() != 0 ? $productCategory->getParent() : null;
}
$page = $this->links->products->node;
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page, $product);
// Return view
return view($this->pagePrefix.'show', [
'page' => $page,
'product' => $product,
'productCategoryParent' => $productCategoryParent,
'links' => $this->links,
'otherLanguages' => $otherLanguageRoutes,
'regionInfo' => $regionInfo,
]);
}
}