File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Courses/Kms/CourseSection.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Courses\Kms;
use App\KommaApp\CourseTypes\Kms\CourseTypeService;
use App\KommaApp\Kms\Core\Attributes\Dynamic;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicContentBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicFullImageBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicFullTextBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicMultipleImagesBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicMultipleImageTabSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicTwoColumnBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\DynamicVideoBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOption;
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\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\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\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;
use Illuminate\Validation\Validator;
class CourseSection extends Section
{
protected $slug = "courses";
public $showSave = 'all';
public $showDelete = 'all';
public $showCreate = 'all';
protected $sectionView = "kms/section.events";
/**
* CourseSection constructor.
* @param CourseService $sectionService
* @param RouteService $routeService
* @param SiteServiceInterface $siteService
*/
// function __construct(Kms $kms, CourseRepository $repository)
function __construct(CourseService $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
{
$sectionTabDirector = new CourseTabsDirector(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['OnOff-with_invoicing'] == 0) {
$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 CourseRepository::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')]);
$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/courses.name')))
->setPlaceholderText(__('kms/courses.enterName'))
->setValidationSet($nameValidationSet)
->mapValueFrom(Attribute::ValueFromTranslationModel, 'name')
]);
$attributes = array_merge($attributes, $nameAttributes);
$courseCategoryService = \App::make(CourseTypeService::class);
$courseCategoryModels = $courseCategoryService->getOptionsForSelect();
$attributes[] = (new Select())
->setItems($courseCategoryModels->toArray())
->setLabelText(__('kms/courses.course_type'))
->mapValueFrom(Attribute::ValueFromModel, 'course_type_id');
$attributes[] = (new TextField(__('kms/courses.priceDisplay')))
->setPlaceholderText(__('kms/courses.enterPriceDisplay'))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromModel, 'price');
$attributes[] = (new Currency(__('kms/courses.price')))
// ->setValidationSet($priceValidationSet)
->setMin(0)
->mapValueFrom(Attribute::ValueFromModel, 'price_amount');
$attributes[] = (new DatePicker(__('kms/courses.date')))
->setTimeEnabled(false)
->mapValueFrom(Attribute::ValueFromModel, 'date');
$attributes[] = (new TextField(__('kms/courses.time')))
->setPlaceholderText(__('kms/courses.enterTime'))
->mapValueFrom(Attribute::ValueFromModel, 'time');
$attributes[] = (new Select(__('kms/courses.startCalendar')))
->setLabelText(__('kms/courses.startCalendar'))
->setItems($calendarTimeSelectOptions)
->mapValueFrom(Attribute::ValueFromModel, 'calendar_start_time');
$attributes[] = (new Select(__('kms/courses.endCalendar')))
->setLabelText(__('kms/courses.endCalendar'))
->setItems($calendarTimeSelectOptions)
->mapValueFrom(Attribute::ValueFromModel, 'calendar_end_time');
$attributes[] = (new OnOff())
->setLabelText('Digitaal')
->switchOn()
->mapValueFrom(Attribute::ValueFromModel, 'digital');
$attributes[] = (new TextField(__('kms/courses.online_url')))
->setPlaceholderText(__('kms/courses.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/courses.locationName')))
->setPlaceholderText(__('kms/courses.enterLocationName'))
->mapValueFrom(Attribute::ValueFromModel, 'location_name'),
(new TextField(__('kms/courses.locationAddress')))
->setPlaceholderText(__('kms/courses.enterLocationAddress'))
->mapValueFrom(Attribute::ValueFromModel, 'location_address'),
(new TextField(__('kms/courses.locationPostal')))
->setPlaceholderText(__('kms/courses.enterLocationPostal'))
->mapValueFrom(Attribute::ValueFromModel, 'location_postal'),
(new TextField(__('kms/courses.locationCity')))
->setPlaceholderText(__('kms/courses.enterLocationCity'))
->mapValueFrom(Attribute::ValueFromModel, 'location_city'),
];
$organisationAttributes = [];
$organisationAttributes[] = (new TextField('Naam'))
->setPlaceholderText('Vul het organisatie naam in')
->mapValueFrom(Attribute::ValueFromModel, 'organisation_name');
$organisationAttributes[] = (new TextField('Website'))
->setPlaceholderText('Vul het organisatie website in')
->mapValueFrom(Attribute::ValueFromModel, 'organisation_site');
$organisationAttributes[] = (new TextField('Email'))
->setPlaceholderText('Vul het organisatie email in')
->mapValueFrom(Attribute::ValueFromModel, 'organisation_email');
$organisationAttributes[] = (new TextField('Telefoon'))
->setPlaceholderText('Vul het organisatie telefoon in')
->mapValueFrom(Attribute::ValueFromModel, 'organisation_phone');
// $weFactAttributes = [];
//
// $weFactAttributes[] = (new OnOff())
// ->setLabelText('Heeft factuur?')
// ->mapValueFrom(Attribute::ValueFromModel, 'with_invoicing');
//
// // Only show the weFact product code if filled
// if(isset($model) && !empty($model->wefact_code) && $model->with_invoicing) {
// $weFactAttributes[] =
// (new TextField(__('kms/courses.weFactCode')))
// ->setReadOnly(true)
// ->mapValueFrom(Attribute::ValueFromItself, 'wefact_code');
// }
//
// $weFactAttributes[] = (new TextField(__('kms/courses.weFactName')))
// ->setPlaceholderText(__('kms/courses.enterWeFactName'))
//// ->setValidationSet($weFactNameValidationSet)
// ->mapValueFrom(Attribute::ValueFromModel, 'wefact_name');
// $weFactAttributes[] = (new TextArea())
// ->setLabelText(__('kms/courses.weFactDescription'))
// ->setPlaceholderText(__('kms/courses.enterWeFactDescription'))
//// ->setValidationSet($weFactDescriptionValidationSet)
// ->mapValueFrom(Attribute::ValueFromModel, 'wefact_description');
$attributes[] = (new Documents())
->setLabelText(__('kms/global.images'))
->onlyAllowImages()
->setMaxDocuments(5)
->setSubFolder('courses')
->setSmallDragAndDropArea()
->mapValueFrom(Attribute::ValueFromDocuments, 'images');
//Build an array with attributes for each current site language
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new TextField(__('kms/courses.subTitle')))
->setPlaceholderText(__('kms/courses.enterSubTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'sub_title'),
(new TextArea())
->setLabelText(__('kms/courses.intro'))
->enableTinymceEditor()
->setDefaultHeight(150)
->mapValueFrom(Attribute::ValueFromTranslationModel, 'intro'),
(new Seperator()),
(new Title(__('kms/courses.overrulesDescription'))),
(new TextArea())
->setLabelText(__('kms/global.description'))
->setPlaceholderText(__('kms/courses.enterDescription'))
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromTranslationModel, 'description'),
(new Seperator()),
(new Documents())
->setLabelText('Trainer(s)')
->onlyAllowImages()
->setMaxDocuments(5)
->setSubFolder('courses')
->setSmallDragAndDropArea()
->mapValueFrom(Attribute::ValueFromDocuments, 'trainee'),
(new TextArea())
->setLabelText('Trainer(s) omschrijving')
->enableTinymceEditor()
->setDefaultHeight(150)
->mapValueFrom(Attribute::ValueFromTranslationModel, 'trainee_text'),
(new Dynamic())
->setLabelText('Blokken')
->setSubFolder('course_dynamic')
->setBlockSettings([
(new DynamicFullTextBlockSettings()),
(new DynamicTwoColumnBlockSettings())->setSubFolder("dynamic"),
(new DynamicFullImageBlockSettings())->setSubFolder("dynamic"),
(new DynamicVideoBlockSettings()),
(new DynamicContentBlockSettings())->setSubFolder("dynamic"),
])
->mapValueFrom(Attribute::ValueFromTranslationModel, 'content'),
]);
//****************************************************************************************************************************************\\
//*** 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 ($organisationAttributes as $attribute) {
$tabItems->push(new SectionTabItem($attribute, SectionTabGroups::Organisation));
}
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::Organisation) {
$this->sectionTabItems->each(function($sectionTabItem, $key) use($tab) {
/** @var AbstractSectionTabItem $sectionTabItem */
if($sectionTabItem->getGroup() == SectionTabGroups::Organisation) $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 [];
}
}