HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/blijegasten/blijegasten.be/app/Komma/Shop/Categories/CategoryController.php
<?php

namespace App\Komma\Shop\Categories;

use App\Komma\Base\Controller;
use App\Komma\Components\ComponentService;
use App\Komma\Shop\Categories\Models\Category;
use App\Komma\Shop\Vat\VatServiceInterface;
use Illuminate\Database\Eloquent\Collection;

class CategoryController extends Controller
{
    private $categoryService;

    public function __construct(CategoryService $categoryService)
    {
        parent::__construct();
        $this->categoryService = $categoryService;
    }

    /**
     * @return \Illuminate\Contracts\View\View
     */
    public function index()
    {
        $page = $this->links->categories->node;
//        $otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);

        // Get tree and directly the children, no need for the tree model
        $categories = $this->categoryService->getCategoryTree()->children;

        // Load the images for these categories (so the main ones)
        $categories->load('images');

        // Return view
        return view('site.templates.categories_index',[
            'page' => $page,
            'links' => $this->links,
            'categories' => $categories
//            'otherLanguages' => $otherLanguageRoutes,
        ]);
    }

    /**
     * @param Category $category
     * @return \Illuminate\Contracts\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function show(Category $category)
    {
        // We call this because we already have loaded all the categories and their translations
        // So no need to do this double
        $category = $this->categoryService->getCategory($category->id);
        $subCategories = $this->categoryService->getCategoriesBelow($category);

        $page = $this->links->categories->node;

        // Append all the sub categories and main categories into one collection
        $categories = Collection::make($subCategories);
        $categories->push($category);

        // Then we can eloquent load the products
        $categories->load('products', 'products.images', 'products.translation');
        $products = Collection::make($categories->pluck('products')->flatten());
        $products = $products->load('categories', 'categories.translation')->sortByDesc('updated_at');

        //Calculate vat for products
        $rateService =  app()->make(VatServiceInterface::class);
        foreach ($products as $product) $rateService->calculateVatForProductable($product);

        $componentService = app(ComponentService::class);
        $components = $componentService->getViewComponents($category->translation);

        // Return view
        return view('site.templates.categories_show',[
            'page' => $page,
            'category' => $category,
            'headerImageFolder' => 'categories/' . $category->code_name,
            'products' => $products,
            'components' => $components,
            'links' => $this->links,
        ]);
    }


//    public function apiGetProductsByFilters()
//    {
//        $filters = request('filters');
//
//        $selectedSubCategories = [];
//        if($filters) {
//            foreach($filters as $filter) {
//                // filter syntax should be "filterName: Filter1, Filter2, etc"
//                if(strpos($filter, ':') !== false) {
//                    $filterName = explode(':', $filter)[0];
//                    $filterString = explode(':', $filter)[1];
//                    $filterArray = explode(',', $filterString);
//
//                    switch($filterName) {
//                        case 'categories':
//                            foreach ($subCategories as $subCategory) {
//                                if (in_array($subCategory->translation->slug, $filterArray)) {
//                                    $selectedSubCategories[] = $subCategory->id;
//                                }
//                            }
//                            if(count($selectedSubCategories) > 0) {
//                                $products = $products->filter(function ($product) use ($selectedSubCategories) {
//                                    return $product->categories->contains(function ($category) use ($selectedSubCategories) {
//                                        return in_array($category->id, $selectedSubCategories);
//                                    });
//                                });
//                            }
//
//                            break;
//                        case 'properties':
//                            // example of future extensions
//                            break;
//                    }
//                }
//            }
//        }
//    }

}