File: D:/HostingSpaces/SBogers10/kooken.komma.pro/app/Users/Kms/SiteUserSection.php
<?php
namespace App\Users\Kms;
//The new object oriented attributes
use Illuminate\Support\Facades\Request;
use Komma\KMS\Core\Sections\Tabs\Collections\NoLanguageTabs;
use Komma\KMS\Core\Attributes\SendSetPasswordMailButton;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\SelectOption;
use Komma\KMS\Core\Attributes\Password;
use Komma\KMS\Core\Attributes\Select;
use Komma\KMS\Core\Attributes\Seperator;
use Komma\KMS\Core\Attributes\TextField;
use Illuminate\Support\Collection;
use Komma\KMS\Core\Attributes\Title;
use Komma\KMS\Core\Sections\Section;
use Komma\KMS\Core\ValidationSet;
use App\Users\Genders;
use Komma\KMS\Globalization\RegionInfo;
use Komma\KMS\Users\Models\KmsUser;
use App\Users\Models\SiteUser;
use App\Users\Models\SiteUserRole;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
final class SiteUserSection extends Section
{
protected $slug = "site_users";
/**
* SiteUserSection 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 \Illuminate\Support\Collection A collection of SectionTabItems
*/
protected function generateAttributes(Model $currentModel = null): Collection
{
/** @var SiteUser $currentModel */
//*****************************************************************************************\\
//*** Define attribute validation sets (Laravel validation rules and message sets) ***\\
//*****************************************************************************************\\
$emailValidationSet = (new ValidationSet())
->setRules([
'required',
'email',
Rule::unique('site_users', 'email')->ignore($currentModel->id)
])
->setMessages([
'required' => __('validation.required'),
'email' => __('KMS::kms/kms_users.enterValidEmailAddress'),
'unique' => __('KMS::kms/kms_users.enterUniqueValue')
]);
$passwordValidationSet = (new ValidationSet())
->setRules('sometimes|required|min:6|regex:/[a-zA-Z0-9]+/') //The sometimes rule only validates the password only if it is present in the input
->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 = SiteUserRole::getAsArray();
foreach($roles as $role) {
if (\Auth::user()->isAtLeast($role)) {
$roleOptions[] = (new SelectOption())->setContent(__('site/roles.'.$role))->setHtmlContent(__('site/roles.'.$role))->setValue($role);
}
}
//*****************************************************************************************\\
//*** Determine and define culture dropdown data ***\\
//*****************************************************************************************\\
$cultureOptions = [];
collect(RegionInfo::getSpecificCultures())->each(function(RegionInfo $regionInfo) use (&$cultureOptions) {
$cultureOptions[] = (new SelectOption())->setContent($regionInfo->getNativeName())->setHtmlContent($regionInfo->getNativeName())->setValue($regionInfo->getName());
});
//*****************************************************************************************\\
//*** Determine and define gender dropdown data ***\\
//*****************************************************************************************\\
$genderOptions = [];
collect(Genders::getAsArray())->each(function($gender) use (&$genderOptions) {
$genderOptions[] = (new SelectOption())->setContent(__('auth.genders.'.$gender))->setHtmlContent(__('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 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 Seperator());
$attributes[] = (new TextField(__('KMS::kms/global.email')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'email')
->setValidationSet($emailValidationSet);
$attributes[] = (new Password())
->setLabelText(__('KMS::kms/global.password'))
->setRepeatLabelText(__('KMS::kms/global.password_repeat'))
->setPlaceholderText(__('KMS::kms/kms_users.enterPassword'))
->setUserId($currentModel->id)
->setValidationSet($passwordValidationSet)
->mapValueFrom(Attribute::ValueFromModel, SiteUser::PASSWORD_COLUMN_NAME);
if($currentModel->exists) $attributes[] = (new SendSetPasswordMailButton($currentModel))
->setButtonText(__('users.send_password_set_mail'));
$attributes[] = (new Select())
->setLabelText(__('KMS::kms/kms_users.role'))
->setItems($roleOptions)
->mapValueFrom(Attribute::ValueFromModel, 'role');
$attributes[] = (new Select())
->setLabelText(__('KMS::kms/kms_users.culture'))
->setItems($cultureOptions)
->mapValueFrom(Attribute::ValueFromModel, 'culture');
$attributes[] = (new Select())
->setLabelText(__('auth.gender'))
->setItems($genderOptions)
->mapValueFrom(Attribute::ValueFromModel, 'gender');
//Return all attributes as a collection
return collect($attributes);
}
/**
* Strips out an empty password so that the validator validates
*
* @see KmsSection::validateInputAndReturnValidator();
* @param array $input
* @return \Validator
*/
public function validateInputAndReturnValidator(array $input = [])
{
if(empty($input))
{
//Generate a temporary password attribute the same like in the generateAttributes method and retrieve its
//key so that we can exclude it from the input when it is empty so that the validator ignores it.
$passwordInputName = "Password-".KmsUser::PASSWORD_COLUMN_NAME;
if(Request::get($passwordInputName) == "") $input = Request::except($passwordInputName);
}
return parent::validateInputAndReturnValidator($input);
}
}