File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Products/Kms/ProductSection.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Products\Kms;
//The new object oriented attributes
use App\KommaApp\Kms\Core\Attributes\Dynamic;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicFullImageBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicFullTextBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicIntroBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicQuoteBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicVideoBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Currency;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOption;
use App\KommaApp\Kms\Core\Attributes\Select;
use App\KommaApp\Kms\Core\Sections\AbstractSectionTabItem;
use App\KommaApp\Kms\Core\Sections\Section;
use App\KommaApp\Routes\RouteService;
use App\KommaApp\Sites\SiteServiceInterface;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\OnOff;
use App\KommaApp\Kms\Core\Attributes\TextArea;
use App\KommaApp\Kms\Core\Attributes\TextField;
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\ValidationSet;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class ProductSection extends Section
{
protected $slug = "products";
public $showSave = 'all';
public $showDelete = 'all';
public $showCreate = 'all';
/**
* ProductSection constructor.
* @param ProductService $sectionService
* @param RouteService $routeService
* @param SiteServiceInterface $siteService
*/
// function __construct(Kms $kms, ProductRepository $repository)
function __construct(ProductService $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
{
$sectionTabDirector = new ProductTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
parent::__construct($sectionService, $routeService, $siteService, $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 ProductRepository::saveModel()
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(): Collection
{
/** @var Model $model */
$model = $this->loadModel($this->getModel());
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
$nameValidationSet = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
$weFactNameValidationSet = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
$weFactDescriptionValidationSet = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
$priceValidationSet = (new ValidationSet())
->setRules('numeric|min:1')
->setMessages(['numeric' => __('validation.numeric'), 'min' => __('kms/products.section.min')]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
$attributes[] = (new TextField(__('kms/sites.type')))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromItself, 'site_name')
->setStyleClass('hidden');
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.active'))
->switchOn()
->mapValueFrom(Attribute::ValueFromModel, 'active');
$attributes[] = (new TextField(__('kms/products.priceDisplay')))
->setPlaceholderText(__('kms/products.enterPriceDisplay'))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromModel, 'price');
$attributes[] = (new Currency(__('kms/products.price')))
->setValidationSet($priceValidationSet)
->setMin(0)
->mapValueFrom(Attribute::ValueFromModel, 'price_amount');
$vatOptions = [
(new SelectOption())
->setValue(21)
->setContent('21%')
->setHtmlContent('21%'),
(new SelectOption())
->setValue(9)
->setContent('9%')
->setHtmlContent('9%'),
(new SelectOption())
->setValue(0)
->setContent('0%')
->setHtmlContent('0%'),
];
$attributes[] = (new Select())
->setItems($vatOptions)
->setLabelText(__('kms/products.vat_amount'))
->mapValueFrom(Attribute::ValueFromModel, 'vat_amount');
$weFactAttributes = [];
// Only show the weFact product code if filled
if(isset($model) && !empty($model->wefact_code)) {
$weFactAttributes[] =
(new TextField(__('kms/products.weFactCode')))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromItself, 'wefact_code');
}
$weFactAttributes[] = (new TextField(__('kms/products.weFactName')))
->setPlaceholderText(__('kms/products.enterWeFactName'))
->setValidationSet($weFactNameValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'wefact_name');
$weFactAttributes[] = (new TextArea())
->setLabelText(__('kms/products.weFactDescription'))
->setPlaceholderText(__('kms/products.enterWeFactDescription'))
->setValidationSet($weFactDescriptionValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'wefact_description');
//Build an array with attributes for each current site language
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new TextField(__('kms/products.name')))
->setPlaceholderText(__('kms/products.enterName'))
->setValidationSet($nameValidationSet)
->mapValueFrom(Attribute::ValueFromTranslationModel, 'name'),
(new TextField(__('kms/products.slug')))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromItself, 'slug'),
(new TextArea())
->setLabelText(__('kms/products.boxed_description'))
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromTranslationModel, 'boxed_description'),
(new Dynamic())
->setLabelText(__('kms/dynamic.blocks'))
->setSubFolder('dynamic')
->setBlockSettings([
(new DynamicIntroBlockSettings()),
(new DynamicFullTextBlockSettings()),
(new DynamicQuoteBlockSettings()),
(new DynamicFullImageBlockSettings())->setSubFolder("dynamic"),
(new DynamicVideoBlockSettings())
])
->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 ($weFactAttributes as $attribute) {
$tabItems->push(new SectionTabItem($attribute, SectionTabGroups::WeFact));
}
foreach ($languageIndexedAttributes as $attribute) {
$tabItems->push(new SectionTabItem($attribute,SectionTabGroups::Content));
}
return $tabItems;
}
/**
* Adds attributes to their appropriate tabs.
*
* @return void
*/
protected function addAttributesToTabs()
{
$tabsCollection = $this->getSectionTabDirector()->getTabsCollection();
$tabs = $tabsCollection->getTabs();
foreach($tabs as $tab) {
if($tab->getGroup() == SectionTabGroups::Content) {
$this->sectionTabItems->each(function($sectionTabItem, $key) use($tab) {
/** @var AbstractSectionTabItem $sectionTabItem */
if($sectionTabItem->getGroup() == SectionTabGroups::Content) $tab->addItem($sectionTabItem->getAttribute());
});
} elseif($tab->getGroup() == SectionTabGroups::WeFact) {
$this->sectionTabItems->each(function($sectionTabItem, $key) use($tab) {
/** @var AbstractSectionTabItem $sectionTabItem */
if($sectionTabItem->getGroup() == SectionTabGroups::WeFact) $tab->addItem($sectionTabItem->getAttribute());
});
}
}
parent::addAttributesToTabs();
}
/**
* This method will stop the load entities of the kmsSiteSection
*
* @return array
*
*/
public function loadEntities()
{
return [];
}
}