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/SBogers10/zelfverkopen.komma.pro/app/KommaApp/Products/ProductComposer.php
<?php


namespace App\KommaApp\Products;


use App\KommaApp\Images\ImageService;
use App\KommaApp\Products\Models\Product;
use Illuminate\View\View;

class ProductComposer
{
    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {

        $products = Product::with('translation')
            ->where('active', 1)
            ->where('lft', '!=', 1)
            ->whereNotIn('code_name', ['default', 'funda_top_position_week', 'funda_top_position_month'])
            ->orderBy('lft')
            ->get();

        foreach ($products as $product) $product->translation = $this->decodeDynamicContent($product->translation);

        $view->with('products', $products);
    }

    /*
     * This function should only be temporary until dynamic blocks is rewrite to it's own blocks
     * instead of using json
     */
    private function decodeDynamicContent($translation)
    {
        $decodedJson = json_decode($translation->description);

        $dynamicBlocks = [];
        $imageController = new ImageService();

        foreach ($decodedJson as $dynamicElement){

            if(isset($dynamicElement->fileIds)) {
                $images = $imageController->getDynamicBlockImages( $dynamicElement->fileIds );
                $dynamicElement->images = $images;
            }

            // Order dynamic blocks
            if($dynamicElement->code_name != '' ){ // If dynamic block has code name then add it to the translation
                $translation->{$dynamicElement->code_name} = $dynamicElement;
            } else{ // Else just add them to the default dynamic content
                $dynamicBlocks[] = $dynamicElement;
            }
        }
        $translation->description = $dynamicBlocks;

        return $translation;
    }
}