File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Categories/CategoryController.php
<?php
namespace App\Categories;
use App\Base\Controller;
use App\Catalog\CatalogService;
use App\Categories\Kms\CategoryModelService;
use App\Categories\Kms\CategoryTreeService;
use App\Categories\Models\Categorizable;
use App\Http\Middleware\WildcardResolver;
use App\Products\AbstractProductable;
use App\Products\Product\Product;
use App\Products\ProductGroup\ProductGroup;
use App\Vat\VatService;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use App\Categories\Models\Category;
use Komma\KMS\Core\Tree\NestedSets\Nodes\TreeModelInterface;
use Komma\KMS\Globalization\RegionInfoInterface;
class CategoryController extends Controller
{
/** @var CategoryModelService */
private $categoryService;
/** @var CatalogService */
private $catalogService;
/** @var CategoryTreeService */
private $categoryTreeService;
public function __construct()
{
parent::__construct();
$this->categoryService = new CategoryModelService();
$this->categoryTreeService = new CategoryTreeService();
$this->catalogService = new CatalogService();
}
/**
* @throws \ReflectionException
* @throws \Exception
* @return Factory|\Illuminate\View\View
*/
public function index()
{
//Initialize services
$categoryTreeService = new CategoryTreeService();
// Todo improve link class
$page = $this->links->categories->node;
//Get the wildcard index route. Append a category slug to this url, and you have it's show page.
$wildcardIndexRoute = WildcardResolver::getWildCardIndexRouteForPageWithCodeName('categories');
if(!$wildcardIndexRoute) throw new \RuntimeException('There is no wildcard index route for a page with code name: "categories". Make sure it exists and enable wildcard routing for that page');
//Put a category tree in the view and sort it by the display names of the categories.
$rootCategory = $categoryTreeService->treeWithTranslationsLinksAndActiveStates();
$categories = collect($rootCategory->getChildren())->sortBy(function(Category $category) {
return $category->getDisplayName(); //TODO Optimize. Triggers additional queries
});
// Return view
return view('templates.categories', [
'links' => $this->links,
'categories' => $categories->take(5),
'page' => $page
]);
}
/**
* @throws \Exception
*
* @param Category $category
*
* @return View
*/
public function show(Category $category)
{
//Paginate the categories productables
$category->load('translation');
$perPage = request()->get('perPage') ?? 10;
$catalogItemPaginator = $this->catalogService->getCategorizablesByCategory($category, $perPage, function(Categorizable $categorizable) {
if(is_a($categorizable->productable, ProductGroup::class)) {
/** @var ProductGroup $productGroup */
$productGroup = $categorizable->productable;
return (!$productGroup->group_products); //The product groups products are grouped together. So when you click one of the products, you will be able to switch to them all via them all.
} elseif(is_a($categorizable->productable, Product::class)) {
$product = $categorizable->productable;
if($product->groups->count() === 0) return true; //Not in product groups, so always show the product
//Only show the product when it is not in a group that isn't shown, because its products group themselves together on their prouct pages.
return $product->groups->filter(function(ProductGroup $productGroup) {
return ($productGroup->group_products);
})->count() > 0;
}
return true;
});
//Load all category names
/** @var TreeModelInterface $rootCategory */
$rootCategory = $this->categoryTreeService->treeWithTranslationsLinksAndActiveStates();
//Calculate vat for each productable
$rateService = new VatService();
foreach($catalogItemPaginator->items() as $categorizable) {
/** @var Categorizable $categorizable */
if(is_a($categorizable->productable, AbstractProductable::class))
$rateService->calculateVatForModelWithVatScenarioEnum($categorizable->productable);
}
$productWildcardIndexRoute = WildcardResolver::getWildCardIndexRouteForPageWithCodeName('products');
if(!$productWildcardIndexRoute) throw new \RuntimeException('There is no wildcard index route for a page with code name: "products". Make sure it exists and enable wildcard routing for that page');
//TODO Can we put this at a higher level?
//Get region info. This is needed to display product price information, formatted to the shops region.
$regionInfo = app(RegionInfoInterface::class);
// Todo improve link class
$page = $this->links->categories->node;
// Return view
return view('templates.assortment',[
'page' => $page,
'category' => $category,
'rootCategory' => $rootCategory,
'links' => $this->links,
'catalogItemsPaginator' => $catalogItemPaginator,
'regionInfo' => $regionInfo,
]);
}
}