File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Events/Kms/EventSection.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Events\Kms;
//The new object oriented attributes
use App\KommaApp\EventTypes\Kms\EventTypeService;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOption;
use App\KommaApp\Kms\Core\Attributes\MultiSelect;
use App\KommaApp\Kms\Core\Attributes\Currency;
use App\KommaApp\Kms\Core\Attributes\DatePicker;
use App\KommaApp\Kms\Core\Attributes\Documents;
use App\KommaApp\Kms\Core\Sections\AbstractSectionTabItem;
use App\KommaApp\Kms\Core\Sections\Section;
use App\KommaApp\Regions\Kms\RegionService;
use App\KommaApp\Routes\RouteService;
use App\KommaApp\Sites\SiteServiceInterface;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Images;
use App\KommaApp\Kms\Core\Attributes\OnOff;
use App\KommaApp\Kms\Core\Attributes\Select;
use App\KommaApp\Kms\Core\Attributes\Seperator;
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\CurrentSiteLanguagesTabsDirector;
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 App\KommaApp\Tags\Kms\TagService;
use App\KommaApp\UserGroups\Kms\UserGroupService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Validator;
class EventSection extends Section
{
protected $slug = "events";
public $showSave = 'all';
public $showDelete = 'all';
public $showCreate = 'all';
protected $sectionView = "kms/section.events";
/**
* EventSection constructor.
* @param EventService $sectionService
* @param RouteService $routeService
* @param SiteServiceInterface $siteService
*/
// function __construct(Kms $kms, EventRepository $repository)
function __construct(EventService $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
{
$sectionTabDirector = new EventTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
parent::__construct($sectionService, $routeService, $siteService, $sectionTabDirector);
}
public function customValidationRules(Validator $validator)
{
$input = $validator->getData();
if($input['Select-region_id'] == 5) {
$validator->addRules([
'TextField-online_url' => 'required',
]);
$validator->validate();
return $validator;
}
$validator->addRules([
'Currency-price_amount' => 'numeric|min:1',
'TextField-wefact_name' => 'required',
'TextArea-wefact_description' => 'required',
]);
$validator->validate();
return $validator;
}
/**
* 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 EventRepository::saveModel()
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(): Collection
{
/** @var Model $model */
$model = $this->loadModel($this->getModel());
// \Log::info("EventSection::Generating attributes");
/** @var EventService $eventService */
$eventService = $this->getSectionService();
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
$nameValidationSet = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
// This logica has been moved to the custom validation rules above
// $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')]);
$calendarTimeSelectOptions = [];
foreach (config('times.times') as $time) {
$calendarTimeSelectOptions[] = (new SelectOption())
->setHtmlContent($time)
->setContent($time)
->setValue($time);
}
//*****************************************************************************************\\
//*** 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');
$nameAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new TextField(__('kms/events.name')))
->setPlaceholderText(__('kms/events.enterName'))
->setValidationSet($nameValidationSet)
->mapValueFrom(Attribute::ValueFromTranslationModel, 'name')
]);
$attributes = array_merge($attributes, $nameAttributes);
$eventCategoryService = \App::make(EventTypeService::class);
$eventCategoryModels = $eventCategoryService->getOptionsForSelect();
$attributes[] = (new Select())
->setItems($eventCategoryModels->toArray())
->setLabelText(__('kms/events.event_type'))
->mapValueFrom(Attribute::ValueFromModel, 'event_type_id');
$regionService = \App::make(RegionService::class);
$regionModels = $regionService->getOptionsForSelect(true);
$userGroupService = \App::make(UserGroupService::class);
$userGroups = $userGroupService->getOptionsForSelect(true);
$attributes[] = (new Select())
->setItems($regionModels->toArray())
->setLabelText(__('kms/events.region'))
->mapValueFrom(Attribute::ValueFromModel, 'region_id');
$attributes[] = (new Select())
->setItems($userGroups->toArray())
->setLabelText(__('kms/events.user_group'))
->mapValueFrom(Attribute::ValueFromModel, 'user_group');
$attributes[] = (new TextField(__('kms/events.priceDisplay')))
->setPlaceholderText(__('kms/events.enterPriceDisplay'))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromModel, 'price');
$attributes[] = (new Currency(__('kms/events.price')))
// ->setValidationSet($priceValidationSet)
->setMin(0)
->mapValueFrom(Attribute::ValueFromModel, 'price_amount');
$attributes[] = (new DatePicker(__('kms/events.date')))
->setTimeEnabled(false)
->mapValueFrom(Attribute::ValueFromModel, 'date');
$attributes[] = (new TextField(__('kms/events.time')))
->setPlaceholderText(__('kms/events.enterTime'))
->mapValueFrom(Attribute::ValueFromModel, 'time');
$attributes[] = (new Select(__('kms/events.startCalendar')))
->setLabelText(__('kms/events.startCalendar'))
->setItems($calendarTimeSelectOptions)
->mapValueFrom(Attribute::ValueFromModel, 'calendar_start_time');
$attributes[] = (new Select(__('kms/events.endCalendar')))
->setLabelText(__('kms/events.endCalendar'))
->setItems($calendarTimeSelectOptions)
->mapValueFrom(Attribute::ValueFromModel, 'calendar_end_time');
$attributes[] = (new TextField(__('kms/events.online_url')))
->setPlaceholderText(__('kms/events.enter_online_url'))
->mapValueFrom(Attribute::ValueFromModel, 'online_url');
$tagService = \App::make(TagService::class);
$tagSelectModels = $tagService->getOptionsForSelect();
$attributes[] = (new MultiSelect())
->setLabelText(__('kms/global.tags'))
->setItems($tagSelectModels->toArray())
->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'tags|id');
$locationAttributes = [
(new TextField(__('kms/events.locationName')))
->setPlaceholderText(__('kms/events.enterLocationName'))
->mapValueFrom(Attribute::ValueFromModel, 'location_name'),
(new TextField(__('kms/events.locationAddress')))
->setPlaceholderText(__('kms/events.enterLocationAddress'))
->mapValueFrom(Attribute::ValueFromModel, 'location_address'),
(new TextField(__('kms/events.locationPostal')))
->setPlaceholderText(__('kms/events.enterLocationPostal'))
->mapValueFrom(Attribute::ValueFromModel, 'location_postal'),
(new TextField(__('kms/events.locationCity')))
->setPlaceholderText(__('kms/events.enterLocationCity'))
->mapValueFrom(Attribute::ValueFromModel, 'location_city'),
];
$weFactAttributes = [];
// Only show the weFact product code if filled
if(isset($model) && !empty($model->wefact_code)) {
$weFactAttributes[] =
(new TextField(__('kms/events.weFactCode')))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromItself, 'wefact_code');
}
$weFactAttributes[] = (new TextField(__('kms/events.weFactName')))
->setPlaceholderText(__('kms/events.enterWeFactName'))
// ->setValidationSet($weFactNameValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'wefact_name');
$weFactAttributes[] = (new TextArea())
->setLabelText(__('kms/events.weFactDescription'))
->setPlaceholderText(__('kms/events.enterWeFactDescription'))
// ->setValidationSet($weFactDescriptionValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'wefact_description');
$attributes[] = (new Documents())
->setLabelText(__('kms/global.images'))
->onlyAllowImages()
->setMaxDocuments(5)
->setSubFolder('events')
->setSmallDragAndDropArea()
->mapValueFrom(Attribute::ValueFromDocuments, 'images');
//Build an array with attributes for each current site language
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new TextField(__('kms/events.subTitle')))
->setPlaceholderText(__('kms/events.enterSubTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'sub_title'),
(new TextArea())
->setLabelText(__('kms/events.intro'))
->enableTinymceEditor()
->setDefaultHeight(150)
->mapValueFrom(Attribute::ValueFromTranslationModel, 'intro'),
(new Seperator()),
(new Title(__('kms/events.overrulesDescription'))),
(new TextArea())
->setLabelText(__('kms/global.description'))
->setPlaceholderText(__('kms/events.enterDescription'))
->enableTinymceEditor()
->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 ($locationAttributes as $attribute) {
$tabItems->push(new SectionTabItem($attribute, SectionTabGroups::Location));
}
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::Location) {
$this->sectionTabItems->each(function($sectionTabItem, $key) use($tab) {
/** @var AbstractSectionTabItem $sectionTabItem */
if($sectionTabItem->getGroup() == SectionTabGroups::Location) $tab->addItem($sectionTabItem->getAttribute());
});
} elseif($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 [];
}
}