File: D:/HostingSpaces/SBogers10/csb.komma.pro/app/Pages/Kms/PageSection.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Pages\Kms;
//The new object oriented attributes
use App\Attributes\Attribute;
use App\Attributes\ComponentArea;
use App\Base\SectionWithSiteLogic;
use App\Components\ComponentType\ComponentTypes;
use Komma\KMS\Core\Attributes\Documents;
use Komma\KMS\Core\ModelServiceInterface;
use Komma\KMS\Core\Sections\Tabs\Collections\CurrentSiteLanguagesTabs;
use App\Pages\Models\Page;
use Komma\KMS\Sites\SiteServiceInterface;;
use Komma\KMS\Core\Attributes\Models\ImageProperty;
use Komma\KMS\Core\Attributes\OnOff;
use Komma\KMS\Core\Attributes\Select;
use Komma\KMS\Core\Attributes\Seperator;
use Komma\KMS\Core\Attributes\TextArea;
use Komma\KMS\Core\Attributes\TextField;
use Komma\KMS\Core\Attributes\Title;
use Komma\KMS\Core\ValidationSet;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Komma\KMS\Users\Models\KmsUserRole;
final class PageSection extends SectionWithSiteLogic
{
protected $hasRoutes = true;
public $showSave = 'all';
public $showDelete = [1];
public $showCreate = [1];
/**
* PageSection constructor.
* @param $slug
*/
function __construct(string $slug)
{
$tabs = new CurrentSiteLanguagesTabs();
$this->siteService = app(SiteServiceInterface::class);
parent::__construct($tabs, $slug);
}
/**
* Generates the attributes for this section. They all must extend the App\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
{
$fixedAttributeTabItems = $this->generateFixedAttributes($currentModel);
$tabItems = $fixedAttributeTabItems;
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new ComponentArea())
->setComponentTypes([
ComponentTypes::TEXT_IMAGE,
ComponentTypes::DOUBLE_TEXT,
ComponentTypes::VIDEO,
ComponentTypes::TEXT,
ComponentTypes::IMAGE,
ComponentTypes::DOUBLE_IMAGE,
ComponentTypes::TEXT_SERVICEPOINT,
])
->setSubFolder('dynamic_group_sections')
->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)
->mapValueFrom(Attribute::ValueFromItself, 'site_name');
$attributes[] = (new Seperator());
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.uses_wildcard'))
->setExplanation(__('kms/pages.wildcard_explanation'))
->setMinimumUserRole(KmsUserRole::SuperAdmin)
->setValidationSet($hasWildcardValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'has_wildcard');
$attributes[] = (new Seperator())->setMinimumUserRole(KmsUserRole::SuperAdmin);
$attributes[] = (new Title(__('kms/pages.page')));
// $attributes[] = (new Video('Youtube'));
$attributes[] = (new TextField(__('kms/global.codeName')))
->setPlaceholderText(__('kms/global.enterCodeName'))
->setMinimumUserRole(KmsUserRole::SuperAdmin)
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'code_name')
->setValidationSet($codeNameValidationSet);
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.active'))
->switchOn()
->mapValueFrom(Attribute::ValueFromModel, 'active');
$pageOptionModels = $pageModelService->getOptionsForSelectAsTree();
$attributes[] = (new Select())
->setItems($pageOptionModels->toArray())
->setLabelText(__('kms/pages.parent_page'))
->setExcludeCurrentModelItem(true)
->mapValueFrom(Attribute::ValueFromItself, 'parent_id');
// Set large hero images
if ($currentModel && $currentModel->code_name === 'home') {
$attributes[] = (new Documents())
->setLabelText(__('kms/global.heroImages'))
->setMaxDocuments(6)
->setSubFolder('pages')
->setImageProperties([
(new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Fit)->setWidth(280)->setHeight(144),
(new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Fit)->setWidth(821)->setHeight(417),
(new ImageProperty())->setName('large')->setCropMethod(ImageProperty::Fit)->setWidth(1152)->setHeight(592),
])
->mapValueFrom(Attribute::ValueFromDocuments, 'hero');
} else {
$attributes[] = (new Documents())
->setLabelText(__('kms/global.heroImages'))
->setMaxDocuments(2)
->setSubFolder('pages')
->setImageProperties([
(new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Fit)->setWidth(352)->setHeight(240),
(new ImageProperty())->setName('large')->setCropMethod(ImageProperty::Fit)->setWidth(528)->setHeight(320),
])
->mapValueFrom(Attribute::ValueFromDocuments, 'hero');
}
$attributes[] = (new Seperator());
//Build an array with attributes for each current site language
$languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
(new Title(__('kms/global.information'))),
(new TextField(__('kms/global.title')))
->setPlaceholderText(__('kms/global.enterTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'name'),
(new TextArea())
->setLabelText(__('kms/global.subtitle'))
->setPlaceholderText(__('kms/global.enterSubtitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'subtitle'),
(new Seperator()),
(new TextField(__('kms/global.metaTitle')))
->setPlaceholderText(__('kms/global.enterTitle'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_title'),
(new TextArea())
->setLabelText(__('kms/global.metaDescription'))
->setPlaceholderText(__('kms/global.enterMetaDescription'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_description'),
(new Seperator()),
(new TextArea())
->setLabelText(__('kms/global.intro'))
->setPlaceholderText(__('kms/global.enterIntro'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'intro'),
]);
//Return all attributes as a collection
return collect(array_merge($attributes, $languageIndexedAttributes));
}
/**
* This method will stop the load entities of the kmsSiteSection
*
* @return array
*
*/
public function loadEntities(){
return [];
}
}