File: D:/HostingSpaces/blijegasten/blijegasten.be/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\DataTable;
use App\Komma\Kms\Core\Attributes\Models\DataTableColumn;
use App\Komma\Kms\Core\Attributes\MultiSelect;
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\Number;
use App\Komma\Kms\Core\Attributes\OnOff;
use App\Komma\Kms\Core\Attributes\QuantityDiscount;
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\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 $propertyService */
protected $propertyService;
/** @var ModelServiceInterface */
private $categoryModelService;
/**
* ProductSection constructor.
* @param string $slug
*/
// function __construct(Kms $kms, PageRepository $repository)
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')
]);
$defaultQuantityValidation = (new ValidationSet())
->setRules('required|numeric|min:1')
->setMessages([
'required' => __('validation.required'),
'numeric' => __('validation.numeric'),
'min' => __('validation.min.numeric')
]);
$percentageValidationSet = (new ValidationSet())
->setRules([
'integer',
])
->setMessages([
'integer' => __('shop/vatrates.validation.percentage')
]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Category selection
$siteOptionModels = $this->siteService->getOptionsForSelect();
$attributes[] = (new Select())
->setItems($siteOptionModels->toArray())
->setLabelText(__('kms/sites.type'))
->setStyleClass('hidden')
->mapValueFrom(Attribute::ValueFromItself, 'site_id');
//Build the general attributes and put them in the attributes array
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.active'))
->switchOn()
->mapValueFrom(Attribute::ValueFromModel, 'active');
$categoryOptionModels = $this->categoryModelService->getOptionsForSelect();
$attributes[] = (new Select())
->setItems($categoryOptionModels->toArray())
->setLabelText(__('shop/products.category'))
->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'categories|id');
$attributes[] = (new OnOff())
->setLabelText(__('shop/products.newLabel'))
->mapValueFrom(Attribute::ValueFromModel, 'new');
$attributes[] = (new OnOff())
->setLabelText(__('shop/products.favorite'))
->mapValueFrom(Attribute::ValueFromModel, 'favorite');
$attributes[] = (new Seperator());
$attributes[] = (new Title(__('shop/products.priceTitle')));
$attributes[] = (new Currency(__('shop/products.price')))
->setMin(0)
->setStep(0.01)
->setValidationSet($priceValidationSet)
->setExplanation(__('shop/products.currency_example'))
->mapValueForPrice('price')
->mapValueFrom(Attribute::ValueFromModel, 'price_excluding_vat');
$attributes[] = (new Seperator());
$attributes[] = (new Title(__('shop/products.intake')));
$attributes[] = (new Select())
->setItems(ProductModelService::getDepositTypes()->toArray())
->setLabelText(__('shop/products.deposit_type'))
->mapValueFrom(Attribute::ValueFromModel, 'deposit_type');
$attributes[] = (new TextField(__('shop/products.default_quantity')))
->setValidationSet($defaultQuantityValidation)
->setExplanation('Bij vrije invoer wordt deze niet gebruikt, dan 1 invoeren ivm validatie.')
->mapValueFrom(Attribute::ValueFromModel, 'quantity_step');
$attributes[] = (new Number(__('shop/products.shortage_price')))
->setMin(0)
->setStep(0.01)
->setExplanation('Bij vrije invoer wordt deze niet gebruikt.')
->mapValueFrom(Attribute::ValueFromModel, 'shortage_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 Seperator());
$attributes[] = (new Title(__('shop/products.media')));
$attributes[] = (new Documents())
->setImageProperties([
(new ImageProperty())->setName('thumb')->setCropMethod(ImageProperty::Fit)->setWidth(72),
(new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Resize)->setWidth(340),
(new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Resize)->setWidth(600),
(new ImageProperty())->setName('large')->setCropMethod(ImageProperty::Resize)->setWidth(900),
])
->setMaxDocuments(5)
->onlyAllowImages()
->setLabelText(__('kms/global.images'))
->setSubFolder('products')
->mapValueFrom(Attribute::ValueFromDocuments, 'product');
$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 TextField(__('kms/global.title')))
->setPlaceholderText(__('kms/global.enterTitle'))
->setValidationSet($nameValidationSet)
->mapValueFrom(Attribute::ValueFromTranslationModel,'name'),
(new TextArea())
->setLabelText(__('shop/products.description'))
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromTranslationModel,'description'),
(new DataTable())
->setLabelText(__('shop/products.specs'))
->setTableStructure([
new DataTableColumn(DataTableColumn::INPUT_TEXT,'Naam'),
new DataTableColumn(DataTableColumn::TEXTAREA,'Omschrijving', false, 2),
])
->mapValueFrom(Attribute::ValueFromTranslationModel,'specs'),
]);
//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 [];
}
}