File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Users/Kms/KmsUserSection.php
<?php
namespace App\Komma\Users\Kms;
//The new object oriented attributes
use App\Komma\Globalization\RegionInfo;
use App\Komma\Kms\Core\Attributes\Documents;
use App\Komma\Kms\Core\Attributes\SendSetPasswordMailButton;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\ImageProperty;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\Password;
use App\Komma\Kms\Core\Attributes\Select;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\Tabs\Collections\NoLanguageTabs;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Users\Genders;
use App\Komma\Users\Models\KmsUserRole;
use App\Komma\Users\Models\KmsUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;
final class KmsUserSection extends Section
{
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\Komma\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_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_users.enterValidEmailAddress'),
'unique' => __('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_users.enterPassword'),
'min' => __('kms/kms_users.passwordMinLength'),
'regex' => __('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/roles.'.$role))->setHtmlContent(__('kms/roles.'.$role))->setValue($role);
}
}
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
$attributes[] = (new Title(__('kms/global.information')));
$attributes[] = (new Documents())
->setLabelText(__('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_users.first_name')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'first_name');
$attributes[] = (new TextField(__('kms/kms_users.last_name')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'last_name');
$attributes[] = (new TextField(__('kms/global.email')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'email')
->setValidationSet($emailValidationSet);
if($currentModel && $currentModel->exists) $attributes[] = (new SendSetPasswordMailButton($currentModel))
->setButtonText(__('users.send_password_set_mail'));
$attributes[] = (new Password())
->setValidationSet($passwordValidationSet)
->mapValueFrom(Attribute::ValueFromModel, KmsUser::PASSWORD_COLUMN_NAME);
$attributes[] = (new Select())
->setLabelText(__('kms/kms_users.role'))
->setItems($roleOptions)
->setPlaceholderText(__('kms/kms_users.exampleRole'))
->mapValueFrom(Attribute::ValueFromModel, 'role');
//Return all attributes as a collection
return collect($attributes);
}
}