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/farmfun/reserveren.farmfun.be/app/Komma/Products/Kms/ProductSection.php
<?php
/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

namespace App\Komma\Products\Kms;

//The new object oriented attributes
use App\Komma\Components\ComponentType\ComponentTypes;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\ComponentArea;
use App\Komma\Kms\Core\Attributes\Documents;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\MultiSelect;
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\TextArea;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Attributes\Video;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\Tabs\Collections\SectionTabs;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Products\Models\Product;
use App\Komma\Users\Kms\KmsUserService;
use App\Komma\Users\Models\KmsUserRole;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

final class ProductSection extends Section
{
    /** @var KmsUserService */
    protected $kmsUserService;

    /**
     * PageSection constructor.
     * @param $slug
     */
//    function __construct(Kms $kms, PageRepository $repository)
    public function __construct($slug)
    {
        $this->kmsUserService = app(KmsUserService::class);
        $tabs = new SectionTabs();
        parent::__construct($tabs, $slug);
    }

    /**
     * Generates the attributes for this section. They all must extend the App\Komma\Kms\Core\Attributes\Attribute class
     * This is the place where you need to setup your sections appearance. Just make sure you build an array of attributes
     * and put each attribute in a AbstractSectionTabItem with a SectionTabGroups constant to link them to a tab.
     *
     * @see PageRepository::saveModel()
     * @param Model $currentModel
     * @return Collection A collection of SectionTabItems
     */
    protected function generateAttributes(Model $currentModel = null): Collection
    {
//        \Log::info("PageSection::Generating attributes");

        //*****************************************************************************************\\
        //*** Define attribute validation sets                                                  ***\\
        //*****************************************************************************************\\

        $requiredValidator = (new ValidationSet())
            ->setRules('required')
            ->setMessages(['required' => __('validation.required')]);

        $numberRequiredValidator = (new ValidationSet())
            ->setRules('required')
            ->setMessages(['required' => __('validation.required')]);

        //*****************************************************************************************\\
        //*** Generate the attributes                                                           ***\\
        //*****************************************************************************************\\
        $attributes = [];

        //Build the general attributes and put them in the attributes array
        //Site selection
//        $attributes[] = (new Title(__('kms/sites.type')));

        $siteOptionModels = $this->siteService->getOptionsForSelect();
//        $attributes[] = (new MultiSelect())
//            ->setItems($siteOptionModels->toArray())
//            ->setLabelText(__('kms/sites.type'))
//            ->setDefaultValue(0)
        ////            ->setStyleClass('hidden')
//            ->mapValueFrom(Attribute::ValueFromItself, 'site_id');

        $attributes[] = (new Select())
            ->setItems($siteOptionModels->toArray())
            ->setLabelText(__('kms/products.type'))
            ->setStyleClass('hidden')
            ->mapValueFrom(Attribute::ValueFromItself, 'site_id');

        $attributes[] = (new OnOff())
            ->setLabelText(__('kms/global.active'))
            ->switchOn()
            ->mapValueFrom(Attribute::ValueFromModel, 'active');

        $attributes[] = (new OnOff())
            ->setLabelText('Verberg op website')
            ->mapValueFrom(Attribute::ValueFromModel, 'hide_on_site');

        $attributes[] = (new OnOff())
            ->setLabelText('Meest gekozen')
            ->mapValueFrom(Attribute::ValueFromModel, 'mostly_chosen');

        $attributes[] = (new OnOff())
            ->setLabelText('Catering mogelijk')
            ->mapValueFrom(Attribute::ValueFromModel, 'cafeteria_possible');

        $attributes[] = (new TextField(__('kms/products.landingPage')))
            ->setPlaceholderText(__('kms/products.enterLandingPage'))
            ->mapValueFrom(Attribute::ValueFromModel, 'landingpage');

        $attributes[] = (new TextField(__('kms/global.codeName')))
            ->setPlaceholderText(__('kms/global.enterCodeName'))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->mapValueFrom(Attribute::ValueFromModel, 'code_name');

        $productTypeOptions = [];
        foreach (Product::$types as $productTypeId => $productType) {
            $productTypeOptions[] = (new SelectOption())
                ->setValue($productTypeId)
                ->setHtmlContent(__('site/availability.productTypes.' . $productType));
        }

        $attributes[] = (new Select())
            ->setItems($productTypeOptions)
            ->setLabelText(__('kms/products.type'))
            ->mapValueFrom(Attribute::ValueFromModel, 'product_type');

        $vatPercentages = [0, 6, 9, 12, 21]; // https://financien.belgium.be/nl/ondernemingen/btw/btw-plicht/tarieven-en-berekening/btw-tarieven#q1
        $productVatPercentages = [];
        foreach ($vatPercentages as $vatPercentage) {
            $productVatPercentages[] = (new SelectOption())
                ->setValue($vatPercentage)
                ->setHtmlContent($vatPercentage . '%');
        }

        $attributes[] = (new Select())
            ->setItems($productVatPercentages)
            ->setLabelText(__('kms/products.vat_percentage'))
            ->mapValueFrom(Attribute::ValueFromModel, 'vat_percentage');

        $attributes[] = (new Seperator());

        $attributes[] = (new Title(__('kms/products.pricing')));

        $attributes[] = (new TextField(__('kms/products.price_each_unit')))
            ->setPlaceholderText(__('kms/products.enter_price_each_unit'))
            ->mapValueFrom(Attribute::ValueFromModel, 'price_each_unit');

        $attributes[] = (new TextField(__('kms/products.price_start_up')))
            ->setPlaceholderText(__('kms/products.enter_price_start_up'))
            ->mapValueFrom(Attribute::ValueFromModel, 'price_start_up');

        $attributes[] = (new OnOff())
            ->setLabelText(__('kms/products.fixed_price'))
            ->mapValueFrom(Attribute::ValueFromModel, 'has_fixed_price');

        $attributes[] = (new Seperator());

        $attributes[] = (new Title(__('kms/products.times')));

        $attributes[] = (new TextField(__('kms/products.duration')))
            ->setPlaceholderText(__('kms/products.enter_duration'))
            ->mapValueFrom(Attribute::ValueFromModel, 'duration');

        $attributes[] = (new TextField(__('kms/products.system_duration')))
            ->setPlaceholderText(__('kms/products.enter_system_duration'))
            ->setValidationSet($requiredValidator)
            ->mapValueFrom(Attribute::ValueFromModel, 'system_duration');

        $attributes[] = (new Seperator());

        $attributes[] = (new Title(__('kms/products.age_persons')));

        $attributes[] = (new TextField(__('kms/products.required_age')))
            ->setPlaceholderText(__('kms/products.enter_required_age'))
            ->mapValueFrom(Attribute::ValueFromModel, 'required_age');

        $attributes[] = (new TextField(__('kms/products.minimum_amount_of_persons')))
            ->setPlaceholderText(__('kms/products.enter_minimum_amount_of_persons'))
            ->mapValueFrom(Attribute::ValueFromModel, 'minimum_amount_of_persons');

        $attributes[] = (new TextField(__('kms/products.maximum_amount_of_persons')))
            ->setPlaceholderText(__('kms/products.enter_maximum_amount_of_persons'))
            ->setStyleClass('hidden')
            ->mapValueFrom(Attribute::ValueFromModel, 'maximum_amount_of_persons');

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

        //Loop for generating the score attributes
        foreach (['score_fun', 'score_variation', 'score_teamwork', 'score_endurance', 'score_strategy'] as $scoreAttributes) {
            $attributes[] = (new Select())
                ->setTab('score')
                ->setItems($scoreOptions)
                ->setLabelText(__('kms/products.' . $scoreAttributes))
                ->mapValueFrom(Attribute::ValueFromModel, $scoreAttributes);
        }

//        $attributes[] = (new OnOff())
//            ->setTab('score')
//            ->setLabelText(__('kms/products.with_trophy'))
//            ->mapValueFrom(Attribute::ValueFromModel, 'with_trophy');

        $attributes[] = (new Documents())
            ->setLabelText(__('kms/products.overview_image'))
            ->onlyAllowImages()
            ->setTab('media')
            ->setSmallDragAndDropArea()
            ->setMaxDocuments(1)
            ->setSubFolder('products')
            ->mapValueFrom(Attribute::ValueFromDocuments, 'products_index_image');



        $attributes[] = (new Documents())
            ->setLabelText(__('kms/products.product_images'))
            ->onlyAllowImages()
            ->setTab('media')
            ->setSmallDragAndDropArea()
            ->setMaxDocuments(10)
            ->setSubFolder('products')
            ->mapValueFrom(Attribute::ValueFromDocuments, 'product_images');



//        $attributes[] = (new Seperator())
//            ->setTab('media');
//
//        $attributes[] = (new Video(__('kms/products.video')))
//            ->setTab('media')
//            ->mapValueFrom(Attribute::ValueFromModel, 'youtube_id');

//        $attributes[] = (new Seperator())
//            ->setTab('media');

        $attributes[] = (new Documents())
            ->setLabelText(__('kms/products.images'))
            ->onlyAllowImages()
            ->setTab('media')
            ->setStyleClass('hidden')
            ->setMaxDocuments(8)
            ->setSubFolder('products')
            ->mapValueFrom(Attribute::ValueFromDocuments, 'product_detail_images');


        //Build an array with attributes for each current site language
        $languageIndexedAttributes = $this->createAttributesFromExistingAttributeForAllUsedLanguagesBySites([

            (new TextField(__('kms/global.title')))
                ->setTab('page')
                ->setPlaceholderText(__('kms/global.enterTitle'))
                ->setValidationSet($requiredValidator)
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'name'),

            (new TextField(__('kms/global.enterVideo')))
                ->setTab('media')
                ->setPlaceholderText(__('kms/global.videoCode'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'youtube_url'),

            (new TextArea())
                ->setLabelText(__('kms/global.description'))
                ->setTab('page')
                ->setPlaceholderText(__('kms/global.enterDescription'))
                ->enableTinymceEditor()
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'description'),

            (new TextField('Inspiratiegids URL'))
                ->setTab('page')
                ->setPlaceholderText('https://')
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'inspo_url'),

            (new TextArea())
                ->setLabelText('pluspunten (in bullet list)')
                ->setTab('score')
                ->enableTinymceEditor()
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'positives'),

            (new TextArea())
                ->setLabelText('minpunten (in bullet list)')
                ->setTab('score')
                ->enableTinymceEditor()
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'negatives'),

            (new TextField(__('kms/global.usp')))
                ->setTab('page')
                ->setPlaceholderText(__('kms/global.usp'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'usp'),

            (new TextField(__('kms/global.usp')))
                ->setTab('page')
                ->setPlaceholderText(__('kms/global.usp'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'usp_1'),

            (new TextField(__('kms/global.usp')))
                ->setTab('page')
                ->setPlaceholderText(__('kms/global.usp'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'usp_2'),

            (new TextField(__('kms/global.usp')))
                ->setTab('page')
                ->setPlaceholderText(__('kms/global.usp'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'usp_3'),

            (new TextArea())
                ->setLabelText(__('kms/products.notification'))
                ->setTab('page')
                ->setPlaceholderText(__('kms/products.enter_notification'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'notification'),

            (new TextField(__('kms/global.metaTitle')))
                ->setTab('seo')
                ->setPlaceholderText(__('kms/global.enterMetaTitle'))
                ->setExplanation("Hier wordt automatisch de locatie en product meta-title aan toegevoegd.<br/>Bv: '{meta titel} | Bocholt | Activiteiten | FarmFun'")
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_title'),

            (new TextArea())
                ->setTab('seo')
                ->setLabelText(__('kms/global.metaDescription'))
                ->setPlaceholderText(__('kms/global.enterMetaDescription'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_description'),

            (new TextArea())
                ->setLabelText(__('kms/products.quote_description'))
                ->setTab('quote')
                ->setPlaceholderText(__('kms/products.enter_quote_description'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'quote_description'),

            (new TextField(__('kms/products.quote_author')))
                ->setTab('quote')
                ->setPlaceholderText(__('kms/products.enter_quote_author'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'quote_author'),

            (new TextField(__('kms/products.quote_function')))
                ->setTab('quote')
                ->setPlaceholderText(__('kms/products.enter_quote_function'))
                ->mapValueFrom(Attribute::ValueFromTranslationModel, 'quote_function'),

            //            (new ComponentArea())
            //                ->setSubFolder('product_component_data')
            //                ->setComponentTypes([
            //                    ComponentTypes::TEXT,
            //                    ComponentTypes::IMAGE
            //                ])
            //                ->mapValueFrom(Attribute::ValueFromComponentArea, 'product_components')
        ]);

        $attributes = array_merge($attributes, $languageIndexedAttributes);

        $attributes[] = (new Documents())
            ->setLabelText(__('kms/products.quote_image'))
            ->onlyAllowImages()
            ->setTab('quote')
            ->setSmallDragAndDropArea()
            ->setMaxDocuments(1)
            ->setSubFolder('products')
            ->mapValueFrom(Attribute::ValueFromDocuments, 'products_quote_image');

        //Return all attributes as a collection
        return collect($attributes);
    }

    /**
     * Adds attributes to their appropriate tabs.
     *
     * @param Collection $attributeCollection
     * @return void
     */
    protected function addAttributesToTabs(Collection $attributeCollection)
    {
        $sectionTranslationTabKey = 'kms/' . $this->slug . '.tabs';
        if (__($sectionTranslationTabKey) == $sectionTranslationTabKey) {
            throw new \UnexpectedValueException(static::class . ': Translations tabs not found for section: "' . $this->slug . '"');
        }
        $tabOrder = array_keys(__($sectionTranslationTabKey));

        $attributeCollection->each(function (Attribute $attribute) use ($tabOrder) {
            // Get the tab key or set to default
            $tabKey = $attribute->getTab();
            if (! isset($tabKey)) {
                $tabKey = 'default';
            }

            // Get the tab name and make sure that the translation isset
            $tabTranslationKey = 'kms/' . $this->slug . '.tabs.' . $tabKey;
            $tabTranslation = __($tabTranslationKey);
            if ($tabTranslation == $tabTranslationKey) {
                throw new \UnexpectedValueException('ProductSection: Translation for tab not found: "' . $tabTranslationKey . '"');
            }

            // Append the tab
            $tab = $this->tabs->getTab($tabTranslation);
            $tab->tab_code_name = $tabKey;
            $tab->tab_order = array_search($tabKey, $tabOrder);
            $tab->addItem($attribute);
        });

        // Order the tab by the given tab_order
        $this->tabs = $this->tabs->sortBy('tab_order');
    }
}