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/farmfun.komma.pro/app/Komma/Components/ComponentType/Types/Programs.php
<?php

namespace App\Komma\Components\ComponentType\Types;

use App\Komma\Components\Component\ViewComponent;
use App\Komma\Components\ComponentType\ComponentTypes;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\OnOff;
use App\Komma\Kms\Core\Attributes\Select;
use App\Komma\Kms\Core\Attributes\Seperator;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Products\Models\Product;

class Programs extends AbstractComponentType
{
    const NTH_ITEMS = 5;

    public function __construct()
    {
        $this->id = ComponentTypes::PROGRAMS;
        $this->name = 'programs';

        parent::__construct();

        $this->attributes->push(
            (new TextField('Titel'))
                ->mapValueFrom(Attribute::ValueFromComponent, 'block_title')
        );

        $scoreOptions = [];
        for ($i = 0; $i <= 5; $i++) {
            $scoreOptions[] = (new SelectOption())
                ->setValue($i)
                ->setHtmlContent(trans_choice('kms/products.stars', $i));
        }

        $productOptions = [];
        $products = Product::with('translation')->get();
        foreach ($products as $product) {
            $productOptions[] = (new SelectOption())
                ->setValue($product->id)
                ->setHtmlContent($product->translation->name);
        }

        for ($i = 0; $i < self::NTH_ITEMS; $i++) {
            $index = $i + 1;

            $this->attributes->push(
                (new OnOff())
                    ->setLabelText('Actief')
                    ->mapValueFrom(Attribute::ValueFromComponent, 'active_'.$index)
                    ->setValue(1)
                    ->setDataAttribute('tab', $index),
            );

            $this->attributes->push(
                (new Select())
                    ->setLabelText('Product')
                    ->setItems($productOptions)
                    ->mapValueFrom(Attribute::ValueFromComponent, 'product_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('Top label'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'top_label_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push((new Seperator())->setDataAttribute('tab', $index));

            $this->attributes->push(
                (new Select())
                    ->setLabelText('Teamwork')
                    ->setItems($scoreOptions)
                    ->mapValueFrom(Attribute::ValueFromComponent, 'teamwork_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new Select())
                    ->setLabelText('Uithouding')
                    ->setItems($scoreOptions)
                    ->mapValueFrom(Attribute::ValueFromComponent, 'stamina_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new Select())
                    ->setLabelText('Strategisch')
                    ->setItems($scoreOptions)
                    ->mapValueFrom(Attribute::ValueFromComponent, 'strategic_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push((new Seperator())->setDataAttribute('tab', $index));

            $this->attributes->push(
                (new TextField('USP 1'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'usp_1_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('USP 2'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'usp_2_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('USP 3'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'usp_3_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('USP 4'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'usp_4_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('USP 5'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'usp_5_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push((new Seperator())->setDataAttribute('tab', $index));

            $this->attributes->push(
                (new TextField('Uren'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'time_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('Personen'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'people_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push((new Seperator())->setDataAttribute('tab', $index));

            $this->attributes->push(
                (new TextField('Offert teaser'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'offer_teaser_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('Offert label'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'offer_link_'.$index)
                    ->setDataAttribute('tab', $index)
            );

            $this->attributes->push(
                (new TextField('Doorlink knop'))
                    ->mapValueFrom(Attribute::ValueFromComponent, 'button_'.$index)
                    ->setDataAttribute('tab', $index)
            );
        }
    }

    public function prepare(ViewComponent $viewComponent): ViewComponent
    {
        $keys = [
            'active',
            'product',
            'top_label',
            'teamwork',
            'stamina',
            'strategic',
            'usp_1',
            'usp_2',
            'usp_3',
            'usp_4',
            'usp_5',
            'time',
            'people',
            'offer_teaser',
            'offer_link',
            'button',
        ];

        $productIds = [];
        for ($i = 0; $i < self::NTH_ITEMS; $i++) {
            $productIds[] = $viewComponent->{'product_'.($i + 1)};
        }

        $products = Product::whereIn('id', $productIds)->where('active', 1)->get();

        $items = [];
        for ($i = 0; $i < self::NTH_ITEMS; $i++) {
            $item = [];
            foreach ($keys as $key) {
                $attributeKey = $key.'_'.($i + 1);
                $item[$key] = $viewComponent->{$attributeKey};
            }

            $item['product'] = $products->where('id', $item['product'])->first();
            $item['price'] = euro_pricing_format($item['product']->price_each_unit);
            $item['price_ex'] = euro_pricing_format($item['product']->price_each_unit_excluding_vat);

            $items[] = $item;
        }

        $viewComponent->items = $items;

        return $viewComponent;
    }
}