File: D:/HostingSpaces/debout/debout.nl/vendor/komma/kms/src/Users/Kms/KmsUserSection.php
<?php
namespace Komma\KMS\Users\Kms;
//The new object oriented attributes
use Komma\KMS\Globalization\RegionInfo;
use Komma\KMS\Core\Attributes\Documents;
use Komma\KMS\Core\Attributes\SendSetPasswordMailButton;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\ImageProperty;
use Komma\KMS\Core\Attributes\Models\SelectOption;
use Komma\KMS\Core\Attributes\Password;
use Komma\KMS\Core\Attributes\Select;
use Komma\KMS\Core\Attributes\TextField;
use Komma\KMS\Core\Attributes\Title;
use Komma\KMS\Core\Sections\Section;
use Komma\KMS\Core\Sections\Tabs\Collections\NoLanguageTabs;
use Komma\KMS\Core\ValidationSet;
use Komma\KMS\Users\Genders;
use Komma\KMS\Users\Models\KmsUserRole;
use Komma\KMS\Users\Models\KmsUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;
final class KmsUserSection extends Section
{
protected $packagePrefix = 'KMS::';
protected $slug = "kms_users";
/**
* UserSection constructor.
* @param string $slug
*/
function __construct(string $slug)
{
$sectionTabDirector = new NoLanguageTabs();
parent::__construct($sectionTabDirector, $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 UserRepository::saveModel()
* @param Model $currentModel
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(Model $currentModel = null): Collection
{
//*****************************************************************************************\\
//*** Define attribute validation sets (Laravel validation rules and message sets) ***\\
//*****************************************************************************************\\
$usernameValidationSet = (new ValidationSet())
// ->setRules('required|unique:users,username')
->setRules([
'required',
Rule::unique('kms_users', 'username')->ignore(($currentModel) ? $currentModel->id : null)
])
->setMessages([
'required' => __('validation.required'),
'unique' => __('KMS::kms/kms_users.username_already_registered')
]);
$emailValidationSet = (new ValidationSet())
->setRules([
'required',
'email',
Rule::unique('kms_users', 'email')->ignore(($currentModel) ? $currentModel->id : null)
])
->setMessages([
'required' => __('validation.required'),
'email' => __('KMS::kms/kms_users.enterValidEmailAddress'),
'unique' => __('KMS::kms/kms_users.enterUniqueValue')
]);
$passwordValidationSet = (new ValidationSet())
->sometimes(function($input) {
return $input['Password-password'] != ''; //Only do the validation when password is not empty
})
->setRules('required|min:6|regex:/[a-zA-Z0-9]+/')
->setMessages([
'sometimes' => '',
'required' => __('KMS::kms/kms_users.enterPassword'),
'min' => __('KMS::kms/kms_users.passwordMinLength'),
'regex' => __('KMS::kms/kms_users.lowerCapitalNumber')
]);
//*****************************************************************************************\\
//*** Determine and define role dropdown data ***\\
//*****************************************************************************************\\
$roleOptions = [];
$roles = KmsUserRole::getAsArray();
foreach($roles as $role) {
if (\Auth::user()->isAtLeast($role)) {
$roleOptions[] = (new SelectOption())->setContent(__('KMS::kms/roles.'.$role))->setHtmlContent(__('KMS::kms/roles.'.$role))->setValue($role);
}
}
//*****************************************************************************************\\
//*** Determine and define culture dropdown data ***\\
//*****************************************************************************************\\
/** @var $regular Collection */
/** @var $hotlist Collection */
list($regular, $hotlist) = RegionInfo::getSpecificCultures()->sortBy(function(RegionInfo $regionInfo) {
return $regionInfo->getNativeName();
})->partition(function(RegionInfo $regionInfo) {
return (!in_array($regionInfo->getThreeLetterISORegionName(), config('languages.country_hotlist', []))); //Region infos that are in the hotlist will be put in the hotlist collection, the rest in the regular one.
});
$regionInfoOptions = $hotlist->merge($regular)->map(function(RegionInfo $regionInfo) {
return (new SelectOption())->setContent($regionInfo->getNativeName())->setHtmlContent($regionInfo->getNativeName())->setValue($regionInfo->getName());
})->toArray();
//*****************************************************************************************\\
//*** Determine and define gender dropdown data ***\\
//*****************************************************************************************\\
$genderOptions = [];
collect(Genders::getAsArray())->each(function($gender) use (&$genderOptions) {
$genderOptions[] = (new SelectOption())->setContent(__('KMS::auth.genders.'.$gender))->setHtmlContent(__('KMS::auth.genders.'.$gender))->setValue($gender);
});
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
$attributes[] = (new Title(__('KMS::kms/global.information')));
$attributes[] = (new Documents())
->setLabelText(__('KMS::kms/global.image'))
->onlyAllowImages()
->setMaxDocuments(1)
->setSmallDragAndDropArea()
->setSubFolder('users')
->setImageProperties([
(new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Resize)->setWidth(300),
])
->mapValueFrom(Attribute::ValueFromDocuments, 'user');
$attributes[] = (new TextField(__('KMS::kms/kms_users.first_name')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'first_name');
$attributes[] = (new TextField(__('KMS::kms/kms_users.last_name')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'last_name');
$attributes[] = (new TextField(__('KMS::kms/global.email')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'email')
->setValidationSet($emailValidationSet);
if($currentModel && $currentModel->exists) $attributes[] = (new SendSetPasswordMailButton($currentModel))
->setButtonText(__('KMS::users.send_password_set_mail'));
$attributes[] = (new Password())
->setValidationSet($passwordValidationSet)
->mapValueFrom(Attribute::ValueFromModel, KmsUser::PASSWORD_COLUMN_NAME);
$attributes[] = (new Select())
->setLabelText(__('KMS::kms/kms_users.role'))
->setItems($roleOptions)
->setPlaceholderText(__('KMS::kms/kms_users.exampleRole'))
->mapValueFrom(Attribute::ValueFromModel, 'role');
$attributes[] = $regionSelect = (new Select())
->setLabelText(__('KMS::kms/kms_users.culture'))
->setItems($regionInfoOptions)
->mapValueFrom(Attribute::ValueFromModel, 'culture');
if(!$currentModel) $regionSelect->setValue('nl-NL');
$attributes[] = (new Select())
->setLabelText(__('KMS::auth.gender'))
->setItems($genderOptions)
->mapValueFrom(Attribute::ValueFromModel, 'gender');
//Return all attributes as a collection
return collect($attributes);
}
}