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

namespace App\Komma\Shop\Products\Product;

//The new object oriented attributes
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Currency;
use App\Komma\Kms\Core\Attributes\Documents;
use App\Komma\Kms\Core\Attributes\Models\ImageProperty;
use App\Komma\Kms\Core\Attributes\MultiSelect;
use App\Komma\Kms\Core\Attributes\OnOff;
use App\Komma\Kms\Core\Attributes\QuantityDiscount;
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\ModelServiceInterface;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\Tabs\Collections\AllUsedLanguagesTabs;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Shop\Categories\Models\Category;
use App\Komma\Shop\Properties\Kms\PropertyService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class ProductSection extends Section
{
    protected $title = 'Products';

    protected $subtitle = 'All products';

    /** @var PropertyService */
    protected $propertyService;

    public $showSave = 'all';

    public $showDelete = [1];

    public $showCreate = [1];

    /** @var ModelServiceInterface */
    private $categoryModelService;

    /**
     * ProductSection constructor.
     * @param string $slug
     */
//    function __construct(Kms $kms, PageRepository $repository)
    public function __construct(string $slug)
    {
        $this->title = ucfirst(__('shop/products.products'));
        $this->subtitle = __('shop/products.all_products');

        $this->propertyService = \App::make(PropertyService::class);
        $this->categoryModelService = app(ModelServiceInterface::class);
        $this->categoryModelService->setModelClassName(Category::class);

        $sectionTabDirector = new AllUsedLanguagesTabs();
        parent::__construct($sectionTabDirector, $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
    {
        //*****************************************************************************************\\
        //*** Define attribute validation sets                                                  ***\\
        //*****************************************************************************************\\
        $nameValidationSet = (new ValidationSet())
            ->setRules('required')
            ->setMessages(['required' => __('validation.required')]);

        $priceValidationSet = (new ValidationSet())
            ->setRules('required|min:0')
            ->setMessages([
                'required' => __('validation.required'),
                'min' => __('validation.min.numeric'),
            ]);

        $percentageValidationSet = (new ValidationSet())
            ->setRules([
                'integer',
            ])
            ->setMessages([
                'integer' => __('shop/vatrates.validation.percentage'),
            ]);

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

        //Category selection
        $attributes[] = (new Title(__('shop/categories.category')));

        $categoryOptionModels = $this->categoryModelService->getOptionsForSelect();
        $attributes[] = (new MultiSelect())
            ->setItems($categoryOptionModels->toArray())
            ->setLabelText(__('shop/categories.category'))
            ->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'categories|id');
        $attributes[] = (new Seperator());

        //Site selection
        $attributes[] = (new Title(__('kms/sites.type')));

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

        //Build the general attributes and put them in the attributes array
        $attributes[] = (new Title(__('shop/products.product')));

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

        $attributes[] = (new TextField(__('shop/products.stock')))
            ->setPlaceholderText(__('shop/products.stock_placeholder'))
            ->setPattern('^\d+$')
            ->mapValueFrom(Attribute::ValueFromModel, 'stock');

        $attributes[] = (new Currency(__('shop/products.price')))
            ->setMin(0)
            ->setStep(0.01)
            ->setValidationSet($priceValidationSet)
            ->setExplanation(__('shop/products.currency_example'))
            ->mapValueFrom(Attribute::ValueFromModel, 'price');

        //NOTE: We don't implement this right now since you need to take the combination rules into account for dutch webshops. See: https://www.belastingdienst.nl/wps/wcm/connect/bldcontentnl/belastingdienst/zakelijk/btw/btw_berekenen_aan_uw_klanten/waarover_btw_berekenen/combinatie_goederen_en_diensten/combinatie_van_goederen_en_diensten
//        $attributes[] = (new TextField(__('shop/vatrates.rate')))
//            ->setReadOnly(false)
//            ->setExplanation(__('shop/products.custom_vat_explanation'))
//            ->setPlaceholderText(__('shop/vatrates.percentage_placeholder'))
//            ->mapValueFrom(Attribute::ValueFromModel, 'custom_vatrate')
//            ->setValidationSet($percentageValidationSet);

        $attributes[] = (new TextField(__('shop/products.sku')))
            ->setPlaceholderText(__('shop/products.sku_placeholder'))
            ->setPattern('[A-Za-z0-9]{4,15}')
            ->setExplanation(__('shop/products.sku_example'))
            ->mapValueFrom(Attribute::ValueFromModel, 'stock_keeping_unit');

        $attributes[] = (new Documents())
            ->setImageProperties([
                (new ImageProperty())->setName('thumb')->setCropMethod(ImageProperty::Fit)->setWidth(100),
                (new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Fit)->setWidth(240),
                (new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Fit)->setWidth(460),
                (new ImageProperty())->setName('large')->setCropMethod(ImageProperty::Fit)->setWidth(500),
            ])
            ->setMaxDocuments(5)
            ->onlyAllowImages()
            ->setLabelText(__('kms/global.images'))
            ->setSubFolder('products')
            ->mapValueFrom(Attribute::ValueFromDocuments, 'documents');

        $attributes[] = (new Documents())
            ->setLabelText(__('kms/global.documents'))
            ->setSubFolder('products')
            ->setMaxDocuments(3)
            ->setEnablePreviewsIfPossible(true)
            ->mapValueFrom(Attribute::ValueFromDocuments, 'product_documents');

        //Quantity discount
        $attributes[] = (new Seperator());

        $attributes[] = (new Title(__('shop/discounts.quantity').' '.__('shop/discounts.discount')));
        $attributes[] = (new QuantityDiscount(__('shop/discounts.newPriceText'), __('shop/discounts.whenAmountIsReached')))
            ->mapValueFrom(Attribute::ValueFromItself, 'quantity_discount');

        //Build an array with attributes for each current site language
        $languageIndexedAttributes = $this->createAttributesFromExistingAttributeForAllUsedLanguagesBySites([
            (new Title(__('kms/global.information'))),

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

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

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

            (new Video())->mapValueFrom(Attribute::ValueFromTranslationModel, 'video'),
        ]);

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

    /**
     * This method will stop the load entities of the kmsSiteSection
     *
     * @return array
     */
    public function loadEntities()
    {
        return [];
    }
}