File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Locations/Kms/LocationSection.php
<?php
namespace App\Komma\Locations\Kms;
//The new object oriented attributes
use App\Komma\Components\ComponentType\ComponentTypes;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\ComponentArea;
use App\Komma\Kms\Core\Attributes\Documents;
use App\Komma\Kms\Core\Attributes\OnOff;
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\View;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\Tabs\Collections\SectionTabs;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Pages\Kms\PageModelService;
use App\Komma\Users\Models\KmsUserRole;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
final class LocationSection extends Section
{
/**
* PageSection constructor.
* @param $slug
*/
public function __construct($slug)
{
$tabs = new SectionTabs();
parent::__construct($tabs, $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
{
// \Log::info("PageSection::Generating attributes");
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
$requiredValidationSet = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
$requiredEmailValidationSet = (new ValidationSet())
->setRules(['required', 'email'])
->setMessages(['required' => __('validation.required'), 'email' => __('validation.email')]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
$activeOnOff = (new OnOff())
->setLabelText(__('kms/global.active'))
->switchOn()
->mapValueFrom(Attribute::ValueFromModel, 'active');
if (! auth()->user()->isAtLeast(KmsUserRole::Admin)) {
$activeOnOff->setStyleClass('hidden');
}
//Build the general attributes and put them in the attributes array
$attributes[] = $activeOnOff;
/** @var PageModelService $pageService */
$pageService = app()->make(PageModelService::class);
$provinceOptions = $pageService->getProvincePages();
$attributes[] = (new Select())
->setItems($provinceOptions->toArray())
->setLabelText('Province')
->mapValueFrom(Attribute::ValueFromModel, 'province_page_id')
->setTab('page');
//Build an array with attributes for each current site language
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new TextField(__('kms/global.title')))
->setPlaceholderText(__('kms/global.enterTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'name'),
(new TextField(__('kms/global.phone')))
->mapValueFrom(Attribute::ValueFromModel, 'phone'),
(new TextArea())
->setLabelText(__('kms/global.description'))
->setTab('page')
->setPlaceholderText(__('kms/global.enterDescription'))
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromTranslationModel, 'description'),
(new TextArea())
->setLabelText("Beknopte omschrijving voor 'Overzicht locaties' component")
->setTab('page')
->setPlaceholderText(__('kms/global.enterDescription'))
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromTranslationModel, 'short_description'),
(new TextArea())
->setLabelText("Activiteiten voor 'Overzicht locaties' component")
->setTab('page')
->setPlaceholderText(__('kms/global.enterDescription'))
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromTranslationModel, 'available_activities_description'),
(new Seperator())
->setTab('page'),
(new Title('SEO'))
->setTab('page'),
(new TextField(__('kms/global.metaTitle')))
->setTab('page')
->setPlaceholderText(__('kms/global.enterMetaTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_title'),
(new TextArea())
->setTab('page')
->setLabelText(__('kms/global.metaDescription'))
->setPlaceholderText(__('kms/global.enterMetaDescription'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_description'),
(new ComponentArea())
->setTab('page')
->setComponentTypes(ComponentTypes::getSet('locations'))
->setSubFolder('location_components')
->mapValueFrom(Attribute::ValueFromComponentArea, 'location_components'),
]);
$attributes = array_merge($attributes, $languageIndexedAttributes);
$attributes[] = (new TextField(__('kms/global.email')))
->setPlaceholderText(__('kms/global.email'))
->setValidationSet($requiredEmailValidationSet)
->setStyleClass('hidden')
->mapValueFrom(Attribute::ValueFromModel, 'email');
$attributes[] = (new Documents())
->setLabelText(__('kms/global.image'))
->onlyAllowImages()
->setMaxDocuments(1)
->setSubFolder('locations')
->setSmallDragAndDropArea()
->mapValueFrom(Attribute::ValueFromDocuments, 'locations');
$attributes[] = (new Seperator())
->setStyleClass('hidden');
$attributes[] = (new Title(__('kms/locations.maps')))
->setStyleClass('hidden');
$attributes[] = (new TextField(__('kms/locations.google_lat')))
->setPlaceholderText(__('kms/locations.enter_google_lat'))
// ->setValidationSet($requiredValidationSet)
->setStyleClass('hidden')
->mapValueFrom(Attribute::ValueFromModel, 'google_lat');
$attributes[] = (new TextField(__('kms/locations.google_long')))
->setPlaceholderText(__('kms/locations.enter_google_long'))
// ->setValidationSet($requiredValidationSet)
->setStyleClass('hidden')
->mapValueFrom(Attribute::ValueFromModel, 'google_long');
$attributes[] = (new Seperator());
$attributes[] = (new Title(__('kms/locations.address')));
$attributes[] = (new TextField(__('kms/locations.street')))
->setPlaceholderText(__('kms/locations.enter_street'))
->setValidationSet($requiredValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'street');
$attributes[] = (new TextField(__('kms/locations.postal_code')))
->setPlaceholderText(__('kms/locations.enter_postal_code'))
->setValidationSet($requiredValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'postal_code');
$attributes[] = (new TextField(__('kms/locations.city')))
->setPlaceholderText(__('kms/locations.enter_city'))
->setValidationSet($requiredValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'city');
$locationService = new \App\Komma\Locations\LocationService();
$attributes[] = (new Select())
->setItems($locationService->getCountries()->toArray())
->setLabelText('Land')
->mapValueFrom(Attribute::ValueFromModel, 'country');
$attributes[] = (new Seperator());
$attributes[] = (new Title('Google Maps'));
$attributes[] = (new TextField('Latitude'))
->mapValueFrom(Attribute::ValueFromModel, 'lat_marker');
$attributes[] = (new TextField('Longitude'))
->mapValueFrom(Attribute::ValueFromModel, 'lon_marker');
$timeSelectOptions = [];
$timeOption = Carbon::now()->startOfDay();
while ($timeOption < Carbon::tomorrow()->startOfDay()) {
$timeSelectOptions[] = (object) [
'label' => $timeOption->format('H:i'),
'value' => $timeOption->format('H:i:s'),
];
$timeOption->addMinutes(30);
}
// These tabs we only show if the model exists
if (isset($currentModel)) {
$attributes[] = (new View('kms.attributes.locationAvailability'))
->setViewData([
'locationAvailabilityKey' => LocationAvailabilityService::LOCATION_AVAILABILITY_KEY,
'timeOptions' => $timeSelectOptions,
'availability' => isset($currentModel) ? $currentModel->availability : null,
])
->setTab('availability');
if (auth()->user()->isAtLeast(KmsUserRole::Admin)) {
$attributes[] = (new View('kms.attributes.locationProducts'))
->setViewData([
'locationProducts' => isset($currentModel) ? $currentModel->productSettings->load('product', 'product.translation') : null,
'locationId' => isset($currentModel) ? $currentModel->id : null,
])
->setTab('products');
}
}
//Return all attributes as a collection
return collect($attributes);
}
/**
* Adds attributes to their appropriate tabs.
*
* @param Collection $attributeCollection
* @return void
*/
protected function addAttributesToTabs(Collection $attributeCollection)
{
$sectionTranslationTabKey = 'kms/'.$this->slug.'.tabs';
if (__($sectionTranslationTabKey) == $sectionTranslationTabKey) {
throw new \UnexpectedValueException(static::class.': Translations tabs not found for section: "'.$this->slug.'"');
}
$tabOrder = array_keys(__($sectionTranslationTabKey));
$attributeCollection->each(function (Attribute $attribute) use ($tabOrder) {
// Get the tab key or set to default
$tabKey = $attribute->getTab();
if (! isset($tabKey)) {
$tabKey = 'default';
}
// Get the tab name and make sure that the translation isset
$tabTranslationKey = 'kms/'.$this->slug.'.tabs.'.$tabKey;
$tabTranslation = __($tabTranslationKey);
if ($tabTranslation == $tabTranslationKey) {
throw new \UnexpectedValueException(static::class.': Translation for tab not found: "'.$tabTranslationKey.'"');
}
// Append the tab
$tab = $this->tabs->getTab($tabTranslation);
$tab->tab_code_name = $tabKey;
$tab->tab_order = array_search($tabKey, $tabOrder);
$tab->addItem($attribute);
});
// Order the tab by the given tab_order
$this->tabs = $this->tabs->sortBy('tab_order');
}
}