File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Shop/Products/ProductController.php
<?php
namespace App\KommaApp\Shop\Products;
use App\Http\Controllers\Controller;
use App\KommaApp\Shop\Catalog\Kms\CatalogService;
use App\KommaApp\Shop\Categories\CategoryService;
use App\KommaApp\Shop\Categories\Kms\CategoryServiceInterface;
use App\KommaApp\Shop\Discounts\DiscountServiceInterface;
use App\KommaApp\Shop\Products\Product\Product;
class ProductController extends Controller
{
private $pagePrefix = 'shop.pages.products.';
/** @var CatalogService $catalogService */
private $catalogService;
/** @var DiscountServiceInterface $discountService */
private $discountService;
public function __construct()
{
parent::__construct();
// $this->catalogService = \App::make(CatalogServiceInterface::class);
$this->discountService = \App::make(DiscountServiceInterface::class);
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
// show the categories
return \App::call('App\KommaApp\Shop\Categories\CategoryController@index');
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function filters()
{
$filters = request('filters');
var_dump('Filter posts with these filters:');
$page = $this->pageService->getPageByCodeName('products');
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);
//$posts = $this->productService->getAllPosts(true);
$products = null; //$this->catalogService->modelsWithoutGroups(); //All products that are not in groups or composites with their translation for the current language
// Return view
return \View::make($this->pagePrefix.'index',[
'page' => $page,
'links' => $this->links,
'otherLanguages' => $otherLanguageRoutes,
'products' => $products,
]);
}
/**
* @param Product $page
* @return \Illuminate\Contracts\View\View
*/
public function show(Product $product)
{
$product->load('translation', 'documents');
$discount = $this->discountService->getQuantityDiscountForASpecificDiscountable($product);
$page = $this->pageService->getPageByCodeName('products');
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page, $product);
$productCategories = $product->categories();
if($productCategories->count() > 1) {
// elaborate way to check whether the user navigated to this product from a different category than the first
$myDomain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
$requestsSource = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
if (parse_url($myDomain, PHP_URL_HOST) === parse_url($requestsSource, PHP_URL_HOST)) {
$refererSegments = explode('/', $requestsSource);
$productCategories = $productCategories->get();
$filteredCategories = $productCategories->filter(function ($value) use ($refererSegments) {
return $value->code_name === end($refererSegments);
});
if($filteredCategories->count() > 0) {
$productCategory = $filteredCategories->first();
} else {
$productCategory = $productCategories->first();
}
} else {
$productCategory = $productCategories->first();
}
} else {
$productCategory = $productCategories->first();
}
$productCategoryParent = $productCategory->getParentId() != 0 ? $productCategory->getParent() : null;
$otherProducts = [];
if(isset($productCategory->translation)) {
$fullProducts = $this->categoryService->getRelatedCategorizablesForCategoryName($product, $productCategory->translation->name, 4);
// $fullProducts = $this->catalogService->getRelatedCategorizablesForCategoryName($product, $productCategory->translation->name)->take(4);
foreach ($fullProducts as $fullProduct) {
$otherProducts[] = $fullProduct;
}
}
// Return view
return \View::make($this->pagePrefix.'show',[
// 'page' => $page,
'product' => $product,
'productCategory' => $productCategory,
'category' => $productCategory,
'productCategoryParent' => $productCategoryParent,
'discount' => $discount,
'links' => $this->links,
'otherProducts' => $otherProducts,
'otherLanguages' => $otherLanguageRoutes
]);
}
}