File: D:/HostingSpaces/sdo/sdoschoonmaak.nl/app/KommaApp/Shop/Products/Product/ProductSection.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Shop\Products\Product;
//The new object oriented attributes
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\AutocompleteInput;
use App\KommaApp\Kms\Core\Attributes\Currency;
use App\KommaApp\Kms\Core\Attributes\Documents;
use App\KommaApp\Kms\Core\Attributes\Images;
use App\KommaApp\Kms\Core\Attributes\Models\ImageProperty;
use App\KommaApp\Kms\Core\Attributes\OnOff;
use App\KommaApp\Kms\Core\Attributes\PropertyValueSelect;
use App\KommaApp\Kms\Core\Attributes\Seperator;
use App\KommaApp\Kms\Core\Attributes\Staggering;
use App\KommaApp\Kms\Core\Attributes\TextArea;
use App\KommaApp\Kms\Core\Attributes\TextField;
use App\KommaApp\Kms\Core\Attributes\Title;
use App\KommaApp\Kms\Core\Sections\AllUsedLanguagesTabsDirector;
use App\KommaApp\Kms\Core\Sections\SectionTabGroups;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Kms\Core\Sections\SectionTabsBuilder;
use App\KommaApp\Kms\Core\Sections\SiteSection;
use App\KommaApp\Kms\Core\ValidationSet;
use App\KommaApp\Routes\RouteService;
use App\KommaApp\Shop\Categories\Kms\CategoryServiceInterface;
use App\KommaApp\Shop\Properties\Kms\PropertyService;
use Illuminate\Database\Eloquent\Collection;
class ProductSection extends SiteSection
{
protected $title = "Products";
protected $subTitle = "All products";
protected $slug = "products";
/** @var CategoryServiceInterface $categoryService */
protected $categoryService;
/** @var PropertyService $propertyService */
protected $propertyService;
public $showSave = 'all';
public $showDelete = [1];
public $showCreate = [1];
/**
* ProductSection constructor.
*
* @param ProductServiceInterface $sectionService
* @param RouteService $routeService
*/
// function __construct(Kms $kms, PageRepository $repository)
function __construct(ProductServiceInterface $sectionService, RouteService $routeService)
{
$this->title = ucfirst(__("shop/products.products"));
$this->subTitle = __("shop/products.all_products");
$this->categoryService = \App::make(CategoryServiceInterface::class);
$this->propertyService = \App::make(PropertyService::class);
$sectionTabDirector = new AllUsedLanguagesTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
parent::__construct($sectionService, $routeService, $sectionTabDirector);
}
/**
* Generates the attributes for this section. They all must extend the App\KommaApp\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()
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(): 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')
]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Category selection
$attributes[] = (new Title(__('shop/categories.category')));
$categoryOptionModels = $this->categoryService->getOptionsForSelect(null, null);
$attributes[] = (new AutocompleteInput())
->setItems($categoryOptionModels)
->setLabelText(__('shop/categories.category'))
->mapValueFrom(Attribute::ValueFromItself, 'category_id');
$attributes[] = (new Seperator());
//Build the general attributes and put them in the attributes array
$attributes[] = (new Title(__('shop/products.product')));
$attributes[] = (new TextField(__('kms/global.title')))
->setPlaceholderText(__('kms/global.enterTitle'))
->setValidationSet($nameValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'title');
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.active'))
->switchOn()
->mapValueFrom(Attribute::ValueFromModel, 'active');
$attributes[] = (new Currency(__('shop/products.price')))
->setMin(0)
->setStep(0.01)
->setValidationSet($priceValidationSet)
->setExplanation(__('shop/products.currency_example'))
->mapValueFrom(Attribute::ValueFromModel, 'price');
$attributes[] = (new TextField(__('shop/products.sku')))
->setPlaceholderText(__('shop/products.sku_placeholder'))
->setPattern('[A-Za-z0-9]{4,11}')
->setExplanation(__('shop/products.sku_example'))
->mapValueFrom(Attribute::ValueFromModel, 'stock_keeping_unit');
$attributes[] = (new Images())
->setImageSizes([
(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),
])
->setLabelText(__('kms/global.images'))
->setMaxImages(5)
->setSubFolder('products')
->mapValueFrom(Attribute::ValueFromImages, 'images');
$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 Currency(__('shop/discounts.quantity').' '.__('shop/discounts.discount')))
->setMin(0)
->setStep(0.01)
->setExplanation(__('shop/products.currency_example'))
->mapValueFrom(Attribute::ValueFromItself, 'quantity_price');
$attributes[] = (new TextField(' '.__('shop/discounts.quantity')))
->setPlaceholderText(__('shop/products.sku_placeholder'))
->setPattern('[1-9][0-9]*')
->setExplanation(__('shop/discounts.quantity_explanation'))
->mapValueFrom(Attribute::ValueFromItself, 'quantity');
//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 TextField(__('kms/global.subtitle')))
->setPlaceholderText(__('kms/global.enterTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'subtitle'),
(new TextField(__('kms/global.metaTitle')))
->setPlaceholderText(__('kms/global.enterTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_title'),
(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')
]);
//****************************************************************************************************************************************\\
//*** Put the all attributes in a SectionTabItem so we can track for which tab they are. And then put SectionTabItems in a Collection ***\\
//****************************************************************************************************************************************\\
$tabItems = new Collection();
foreach($attributes as $attribute) $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::General));
foreach($languageIndexedAttributes as $attribute) $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::Languages));
return $tabItems;
}
/**
* This method will stop the load entities of the kmsSiteSection
*
* @return array
*
*/
public function loadEntities(){
return [];
}
}