File: D:/HostingSpaces/SBogers10/ehbo.today/app/KommaApp/ArchivedCourses/Kms/ArchivedCourseSection.php
<?php
namespace App\KommaApp\ArchivedCourses\Kms;
//The new object oriented attributes
use App\KommaApp\Competences\Kms\CompetenceService;
use App\KommaApp\ArchivedCourses\Models\ArchivedCourse;
use App\KommaApp\Kms\Core\Attributes\AutocompleteInput;
use App\KommaApp\Kms\Core\Attributes\DatePicker;
use App\KommaApp\Kms\Core\Attributes\Documents;
use App\KommaApp\Kms\Core\Attributes\Link;
use App\KommaApp\Kms\Core\Attributes\OnOff;
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\Sections\AllUsedLanguagesTabsDirector;
use App\KommaApp\Kms\Core\Sections\CurrentSiteLanguagesTabsDirector;
use App\KommaApp\Kms\Core\Sections\NoLanguageTabsDirector;
use App\KommaApp\Routes\RouteService;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Title;
use App\KommaApp\Kms\Core\Sections\Section;
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 App\KommaApp\Users\Kms\UserService;
use App\KommaApp\Sites\SiteServiceInterface;
use App\KommaApp\Users\Roles;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Illuminate\Database\Eloquent\Collection;
class ArchivedCourseSection extends Section
{
protected $title = "ArchivedCourses";
protected $subTitle = "ArchivedCourses for everyone";
protected $slug = "archivedcourses";
public $showSave = 'none';
public $showDelete = [];
public $showCreate = [];
/** @var UserService $userService */
private $userService;
/** @var CompetenceService $competenceService */
private $competenceService;
/**
* ArchivedCourseSection constructor.
* @param ArchivedCourseService $sectionService
* @param RouteService $routeService
* @param SiteServiceInterface $siteService
*/
function __construct(ArchivedCourseService $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
{
$sectionTabDirector = new NoLanguageTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
$this->title = __('kms/ArchivedCourses.title');
$this->subTitle = __('kms/ArchivedCourses.sub_title');
$this->userService = \App::make(UserService::class);
$this->competenceService = \App::make(CompetenceService::class);
parent::__construct($sectionService, $routeService, $siteService, $sectionTabDirector);
}
/**
* 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 ArchivedCourseRepository::saveModel()
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(): Collection
{
/** @var Model $model */
$model = $this->loadModel($this->getModel());
//*****************************************************************************************\\
//*** Define attribute validation sets (Laravel validation rules and message sets) ***\\
//*****************************************************************************************\\
$nameValidationSet = (new ValidationSet())
// ->setRules('required|unique:ArchivedCourses,ArchivedCoursename')
->setRules([
'required',
Rule::unique('ArchivedCourses', 'name')->ignore($model->id)
])
->setMessages([
'required' => __('validation.required'),
'unique' => __('kms/ArchivedCourses.already_exists')
]);
$numericValidationSet = (new ValidationSet())
->setRules(['present', 'integer'])
->setMessages([
'required' => __('validation.required'),
'unique' => __('validation.integer')
]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
$attributes[] = (new Seperator());
//Build the general attributes and put them in the attributes array
$attributes[] = (new Title(__('kms/global.general')));
// $attributes[] = (new Documents())
// ->setLabelText(__('kms/global.images'))
// ->setSubFolder('ArchivedCourses')
// ->setMaxDocuments(1) //TODO. Seems not implemented yet
// ->setAccept('image/*')
// ->mapValueFrom(Attribute::ValueFromDocuments, 'documents');
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.active'))
->mapValueFrom(Attribute::ValueFromModel, 'active');
$attributes[] = (new TextField(__('kms/archivedCourses.max_subscriptions')))
->setValidationSet($numericValidationSet)
->setExplanation(__('kms/archivedCourses.max_subscriptions_explanation'))
->mapValueFrom(Attribute::ValueFromModel, 'max_subscriptions');
$attributes[] = (new DatePicker(__('kms/archivedCourses.date')))
->setTimeEnabled(true)
->mapValueFrom(Attribute::ValueFromModel, 'date');
$competenceOptionModels = $this->competenceService->getOptionsForSelect();
$attributes[] = (new AutocompleteInput())
->setItems($competenceOptionModels->toArray())
->setLabelText(__('kms/competences.competences'))
->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'competences|id');
$usersOptions = $this->userService->getOptionsForSelectIncludingRoles();
$attributes[] = (new AutocompleteInput())
->setItems($usersOptions)
->setLabelText(__('kms/archivedCourses.user_status.'.ArchivedCourse::UserStatusSubscribed))
->setExplanation(__('kms/archivedCourses.status_explanation'))
->mapValueFrom(Attribute::ValueFromItself, 'subscribed_users');
$attributes[] = (new AutocompleteInput())
->setItems($usersOptions)
->setLabelText(__('kms/archivedCourses.user_status.'.ArchivedCourse::UserStatusPresent))
->setExplanation(__('kms/archivedCourses.status_explanation'))
->mapValueFrom(Attribute::ValueFromItself, 'present_users');
$attributes[] = (new Link(Link::TYPE_ROUTE, 'presence.form', __('kms/archivedCourses.go_to_check-in'), ['archivedCourse' => $model]));
//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.description'))
->setPlaceholderText(__('kms/global.description'))
->mapValueFrom(Attribute::ValueFromTranslationModel, 'description'),
// (new TextArea())
// ->setLabelText(__('kms/global.metaDescription'))
// ->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_description'),
]);
//****************************************************************************************************************************************\\
//*** 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($languageIndexedAttributes as $attribute) $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::General));
foreach($attributes as $attribute) $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::General));
return $tabItems;
}
}