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/Eurotools/euro-tools.nl/app/Http/Wildcards/CategoriesWildcard.php
<?php

namespace App\Http\Wildcards;



use App\KommaApp\Shop\Categories\Models\CategoryTranslation;

class CategoriesWildcard implements WildcardInterface
{

    /**
     * @param $request
     * @param $wildcard
     * @return mixed
     */
    public function handle($request, $wildcard)
    {

        /** @var CategoryTranslation $categoryTranslation */
        $foundCategoryTranslations = CategoryTranslation::where('slug', strtolower($wildcard->tail[0]))->where('language_id', \App::getLanguage()->id)->get();
        if(count($foundCategoryTranslations) > 0)
        {
            $categoryTranslation = null;
            if(count($foundCategoryTranslations) == 1){
                $categoryTranslation = $foundCategoryTranslations->first();
            } else {
                foreach($foundCategoryTranslations as $foundCategoryTranslation) {
                    //dd($foundCategoryTranslation->translatable()->first()->getParent());
                    if(!empty($foundCategoryTranslation->translatable()->first()->getParent()) &&
                        $foundCategoryTranslation->translatable()->first()->getParent()->active > 0 &&
                        strtolower($foundCategoryTranslation->translatable()->first()->getParent()->translations()->where('language_id', $foundCategoryTranslation->language_id)->first()->slug) == strtolower($wildcard->tail[1]))
                    {
                        $categoryTranslation = $foundCategoryTranslation;
                    }
                }
                if(empty($categoryTranslation)) {
                    $categoryTranslation = $foundCategoryTranslations->first();
                }
            }

            $category = $categoryTranslation->translatable()->first();
            if($category->getParent()->lft > 1 && !\Auth::check()) {
                return abort(403);
            }

            // set the language if it is not the same as the current language
            $language = $categoryTranslation->language;
            if(isset($language) && \App::getLanguage() != $language) \App::setLanguage($language);

            //Set the request URI and the original path
//            dd('categories/'.$categoryTranslation->category_id);
            $request->server->set('REQUEST_URI', 'categories/'.$categoryTranslation->category_id);
            return $request;
        }

        // Bind filters to the request and set uri to the filter function of the product controller
        $request->merge(['filters' => $wildcard->tail]);

        //Set the request URI and the original path
//        $request->server->set('REQUEST_URI', 'products/filters');

        return $request;
    }
}