File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Pages/Kms/PageSection.php
<?php
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Pages\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\Link;
use App\Komma\Kms\Core\Attributes\OnOff;
use App\Komma\Kms\Core\Attributes\Select;
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\ModelServiceInterface;
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\Models\Page;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Models\KmsUserRole;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
final class PageSection extends Section
{
protected $hasRoutes = true;
public $showSave = 'all';
public $showDelete = [1];
public $showCreate = [1];
/**
* PageSection constructor.
* @param $slug
*/
public function __construct(string $slug)
{
$tabs = new SectionTabs();
$this->siteService = app(SiteServiceInterface::class);
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
{
$tabItems = $this->generateFixedAttributes($currentModel);
// Return for pages without dynamic components
// if (isset($currentModel) && in_array($currentModel->code_name, ['posts', 'vacancies'])) return $tabItems;
$componentTypes = [];
if (isset($currentModel->code_name) && ComponentTypes::hasSet($currentModel->code_name)) {
$componentTypes = ComponentTypes::getSet($currentModel->code_name);
} else {
$componentTypes = ComponentTypes::all();
}
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new ComponentArea())
->setComponentTypes($componentTypes)
->setSubFolder('dynamic_group_sections')
->setTab('page')
->mapValueFrom(Attribute::ValueFromComponentArea, 'dynamic_group_sections'),
]);
return $tabItems->merge(collect($languageIndexedAttributes));
}
/**
* Generate fixed attributes
*
* @param Model|null $currentModel
* @return Collection
*/
private function generateFixedAttributes(Model $currentModel = null):Collection
{
/** @var ModelServiceInterface $pageModelService */
$pageModelService = app(ModelServiceInterface::class);
$pageModelService->setModelClassName(Page::class);
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
// $codeNameValidationSet = (new ValidationSet())
// ->setRules('required')
// ->setMessages(['required' => __('validation.required')]);
$hasWildcardValidationSet = (new ValidationSet())
->setRules(['sometimes', 'required', 'integer'])
->setMessages([
'sometimes' => __('validation.required'),
'required' => __('validation.required'),
'min' => __('validation.min.numeric'),
]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
// $attributes[] = (new Title(__('kms/sites.type')));
$attributes[] = (new TextField(__('kms/sites.type')))
->setValue($this->siteService->getCurrentSite()->name)
->setReadOnly(true)
->setStyleClass('hidden')
->setTab('page')
->mapValueFrom(Attribute::ValueFromItself, 'site_name');
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.uses_wildcard'))
->setExplanation(__('kms/pages.wildcard_explanation'))
->setMinimumUserRole(KmsUserRole::SuperAdmin)
->setValidationSet($hasWildcardValidationSet)
->setStyleClass('hidden')
->setTab('page')
->mapValueFrom(Attribute::ValueFromModel, 'has_wildcard');
// $attributes[] = (new Video('Youtube'));
$attributes[] = (new TextField(__('kms/global.codeName')))
->setPlaceholderText(__('kms/global.enterCodeName'))
->setMinimumUserRole(KmsUserRole::SuperAdmin)
->setReadOnly(false)
->setTab('page')
->mapValueFrom(Attribute::ValueFromModel, 'code_name');
// ->setValidationSet($codeNameValidationSet);
$attributes[] = (new OnOff())
->setMinimumUserRole(KmsUserRole::Admin)
->setLabelText(__('kms/global.active'))
->switchOn()
->setTab('page')
->mapValueFrom(Attribute::ValueFromModel, 'active');
$attributes[] = (new OnOff())
->setMinimumUserRole(KmsUserRole::Admin)
->setLabelText('SEO pagina')
->setTab('page')
->mapValueFrom(Attribute::ValueFromModel, 'seo_page');
if (isset($currentModel) && $currentModel->code_name == 'products') {
$attributes[] = (new Documents())
->setLabelText('Hero Afbeeldingen')
->onlyAllowImages()
->setMaxDocuments(5)
->setSubFolder('activities')
->setSmallDragAndDropArea()
->mapValueFrom(Attribute::ValueFromDocuments, 'hero_images');
}
$pageOptionModels = $pageModelService->getOptionsForSelectAsTree();
$attributes[] = (new Select())
->setMinimumUserRole(KmsUserRole::SuperAdmin)
->setItems($pageOptionModels->toArray())
->setLabelText(__('kms/pages.parent_page'))
->setExcludeCurrentModelItem(true)
->setTab('page')
->mapValueFrom(Attribute::ValueFromItself, 'parent_id');
$languageAttributes = [
(new TextField('Naam voor URL'))
->setMinimumUserRole(KmsUserRole::Admin)
->setPlaceholderText(__('kms/global.enterTitle'))
->setTab('page')
->mapValueFrom(Attribute::ValueFromTranslationModel, 'name'),
];
if (isset($currentModel)) {
$currentModel->load('translation', 'translation.route');
if (isset($currentModel->translation) && isset($currentModel->translation->route)) {
$url = url($currentModel->translation->route->alias);
$languageAttributes[] = (new Link('URL:'))
->setLink($url)
->setLinkText($url)
->openInNewWindowOrTab()
->setTab('page');
}
}
// Hide visual name and description
if (isset($currentModel) && ! in_array($currentModel->code_name, ['eventbanners'])) {
$languageAttributes[] = (new TextField(__('kms/global.title')))
->setPlaceholderText(__('kms/global.enterTitle'))
->setTab('page')
->mapValueFrom(Attribute::ValueFromTranslationModel, 'visual_name');
$languageAttributes[] = (new TextArea())
->setLabelText(__('kms/global.description'))
->setPlaceholderText(__('kms/global.enterDescription'))
->setTab('page')
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromTranslationModel, 'description');
}
$languageAttributes[] = (new TextField(__('kms/global.metaTitle')))
->setPlaceholderText(__('kms/global.enterTitle'))
->setTab('seo')
->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_title');
$languageAttributes[] = (new TextArea())
->setLabelText(__('kms/global.metaDescription'))
->setPlaceholderText(__('kms/global.enterMetaDescription'))
->setTab('seo')
->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_description');
//Build an array with attributes for each current site language
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages($languageAttributes);
$extraAttributes = [];
if (isset($currentModel) && $currentModel->code_name == 'products') {
$extraAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new TextField('YOUTUBE WATCH CODE (WATCH?V=XXXXXX)'))
->setPlaceholderText('xxxxxx')
->setTab('page')
->mapValueFrom(Attribute::ValueFromTranslationModel, 'youtube_url'),
]);
}
//Return all attributes as a collection
return collect(array_merge($attributes, $languageIndexedAttributes, $extraAttributes));
}
/**
* This method will stop the load entities of the kmsSiteSection
*
* @return array
*/
public function loadEntities()
{
return [];
}
/**
* 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('ProductSection: 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');
}
}