File: D:/HostingSpaces/Neopoints/momsecurity.be/app/Komma/Sites/Kms/SiteSection.php
<?php
namespace App\Komma\Sites\Kms;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\AutocompleteInput;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Sections\NoLanguageTabsDirector;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\SectionTabGroups;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\Core\Sections\SectionTabsBuilder;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Languages\Kms\LanguageService;
use App\Komma\Routes\RouteService;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Models\Role;
use Illuminate\Database\Eloquent\Collection;
//class SiteSection extends KmsSection
class SiteSection extends Section
{
protected $slug = "sites";
public $showDelete = [Role::SuperAdmin];
public $showCreate = [Role::SuperAdmin];
private $languageService;
/**
* SiteSection constructor.
* @param SiteServiceInterface $siteService
* @param RouteService $routeService
* @param LanguageService $languageService
*/
function __construct(SiteServiceInterface $siteService, RouteService $routeService, LanguageService $languageService)
{
$sectionTabDirector = new NoLanguageTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
$this->languageService = $languageService;
parent::__construct($siteService, $routeService, $siteService, $sectionTabDirector);
}
/**
* Generates the attributes for this section. They all must extend the App\Komma\Kms\Core\Attributes\Attribute class
*
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(): Collection
{
$model = $this->getModel();
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
$nameValidationSet = (new ValidationSet())
->setRules('required|min:3')
->setMessages([
'required' => __('validation.required'),
'min' => __('validation.min.string'),
]);
$slugValidationSet = (new ValidationSet())
->setRules([
'required',
'min:3',
'regex:/^[a-z0-9_-]*$/',
($model) ? 'unique:sites,slug,'.$model->id : 'unique:sites,slug'
])
->setMessages([
'required' => __('validation.required'),
'min' => __('validation.min.string'),
'unique' => __('validation.unique'),
'regex' => __('validation.regex'),
]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
$attributes[] = (new Title(__('kms/global.information')));
$attributes[] = (new TextField(__('kms/global.name')))
->setPlaceholderText(__('kms/sites.enter_name'))
->setReadOnly(false)
->setValidationSet($nameValidationSet)
->mapValueFrom(Attribute::ValueFromModel, 'name');
$attributes[] = (new TextField(__('kms/sites.slug')))
->setPlaceholderText(__('kms/sites.enter_slug'))
->setReadOnly(false)
->setValidationSet($slugValidationSet)
->setExplanation(__('kms/sites.slug_explanation'))
->mapValueFrom(Attribute::ValueFromModel, 'slug');
$languageOptionsModels = $this->languageService->getAvailableLanguagesForSitesAsSelectOptions();
$attributes[] = (new AutocompleteInput())
->setItems($languageOptionsModels)
->setLabelText(__('kms/sites.languages'))
->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'languages|id');
//****************************************************************************************************************************************\\
//*** 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));
return $tabItems;
}
}