File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Users/Kms/UserSection.php
<?php
namespace App\KommaApp\Users\Kms;
//The new object oriented attributes
use App\KommaApp\Companies\Kms\CompanyService;
use App\KommaApp\Kms\Core\Attributes\Documents;
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\Images;
use App\KommaApp\Kms\Core\Attributes\Models\ImageProperty;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOption;
use App\KommaApp\Kms\Core\Attributes\Password;
use App\KommaApp\Kms\Core\Attributes\Select;
use App\KommaApp\Kms\Core\Attributes\Seperator;
use App\KommaApp\Kms\Core\Attributes\TextField;
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\Sites\SiteServiceInterface;
use App\KommaApp\UserGroups\Models\UserGroup;
use App\KommaApp\Users\Models\Role;
use App\KommaApp\Users\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Illuminate\Database\Eloquent\Collection;
class UserSection extends Section
{
protected $slug = "users";
public $showSave = 'all';
public $showDelete = [Role::SuperAdmin,Role::Admin];
public $showCreate = [Role::SuperAdmin,Role::Admin];
/**
* UserSection constructor.
* @param UserService $sectionService
* @param RouteService $routeService
* @param SiteServiceInterface $siteService
*/
function __construct(UserService $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
{
$sectionTabDirector = new NoLanguageTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
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 UserRepository::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) ***\\
//*****************************************************************************************\\
// $usernameValidationSet = (new ValidationSet())
//// ->setRules('required|unique:users,username')
// ->setRules([
// 'required',
// Rule::unique('users', 'username')->ignore($model->id)
// ])
// ->setMessages([
// 'required' => __('validation.required'),
// 'unique' => __('kms/users.username_already_registered')
// ]);
$emailValidationSet = (new ValidationSet())
->setRules([
'required',
'email',
Rule::unique('users', 'email')->ignore($model->id)
])
->setMessages([
'required' => __('validation.required'),
'email' => __('kms/users.enterValidEmailAddress'),
'unique' => __('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/users.enterPassword'),
'min' => __('kms/users.passwordMinLength'),
'regex' => __('kms/users.lowerCapitalNumber')
]);
//*****************************************************************************************\\
//*** Determine and define role dropdown data ***\\
//*****************************************************************************************\\
$roleOptions = [];
$roles = Role::all();
$roles->each(function($role) use (&$roleOptions) {
/** @var Role $role */
/** @var Role $userRole */
$userRole = \Auth::user()->role;
if($userRole->isAtLeast($role)) {
$roleOptions[] = (new SelectOption())->setContent($role->name)->setHtmlContent($role->name)->setValue($role->id);
}
});
//*****************************************************************************************\\
//*** Fill subscription select ***\\
//*****************************************************************************************\\
$subscriptionOptions = [];
$subscriptionOptions[] = (new SelectOption())->setContent('Regulier lid')->setHtmlContent('Regulier lid')->setValue('regular');
$subscriptionOptions[] = (new SelectOption())->setContent('Premium lid')->setHtmlContent('Premium lid')->setValue('premium');
//*****************************************************************************************\\
//*** Fill board function select ***\\
//*****************************************************************************************\\
$boardOptions = [];
$boardOptions[] = (new SelectOption())->setContent('N.v.t.')->setHtmlContent('N.v.t.')->setValue('');
$boardOptions[] = (new SelectOption())->setContent('Voorzitter')->setHtmlContent('Voorzitter')->setValue('voorzitter');
$boardOptions[] = (new SelectOption())->setContent('Secretaris')->setHtmlContent('Secretaris')->setValue('secretaris');
$boardOptions[] = (new SelectOption())->setContent('Ledencoördinator')->setHtmlContent('Ledencoördinator')->setValue('Ledencoördinator');
//*****************************************************************************************\\
//*** Selectoptions for user groups ***\\
//*****************************************************************************************\\
$userGroups = [];
$groups = UserGroup::with('translations')->get();
$userGroups[] = (new SelectOption())->setContent('Geen')->setHtmlContent('Geen')->setValue(0);
$groups->each(function($group) use (&$userGroups) {
$userGroups[] = (new SelectOption())->setContent($group->translations[0]->name)->setHtmlContent($group->translations[0]->name)->setValue($group->id);
});
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
$attributes[] = (new Title(__('kms/global.information')));
// $attributes[] = (new Images())
// ->setImageSizes([
// (new ImageProperty())->setName('thumb')->setCropMethod(ImageProperty::Resize)->setWidth(300),
// ])
// ->setLabelText(__('kms/global.images'))
// ->setMaxImages(1)
// ->setSubFolder('users')
// ->mapValueFrom(Attribute::ValueFromImages, 'images');
$attributes[] = (new Documents())
->setLabelText(__('kms/global.image'))
->onlyAllowImages()
->setSmallDragAndDropArea()
->setMaxDocuments(1)
->setSubFolder('users')
->setImageProperties([
(new ImageProperty())->setName('original')->setCropMethod(ImageProperty::Resize)->setWidth(2000),
(new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Resize)->setWidth(300),
(new ImageProperty())->setName('thumb')->setCropMethod(ImageProperty::Fit)->setWidth(128)
])
->mapValueFrom(Attribute::ValueFromDocuments, 'user');
$attributes[] = (new TextField(__('kms/users.first_name')))
->setPlaceholderText(__('kms/users.enter_first_name'))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'first_name');
$attributes[] = (new TextField(__('kms/users.last_name')))
->setPlaceholderText(__('kms/users.enter_last_name'))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'last_name');
$attributes[] = (new TextField(__('kms/users.function')))
->setPlaceholderText(__('kms/users.enterFunction'))
->mapValueFrom(Attribute::ValueFromModel, 'function');
$attributes[] = (new Select())
->setLabelText('Lidmaatschap')
->setItems($subscriptionOptions)
->mapValueFrom(Attribute::ValueFromModel, 'membership');
$attributes[] = (new Select())
->setLabelText('Gebruikersgroep')
->setItems($userGroups)
->mapValueFrom(Attribute::ValueFromModel, 'breakfast_group');
$attributes[] = (new Select())
->setLabelText(__('kms/users.board_function'))
->setItems($boardOptions)
->mapValueFrom(Attribute::ValueFromModel, 'board_function');
$attributes[] = (new TextField(__('kms/users.phone')))
->setPlaceholderText(__('kms/users.enterPhone'))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'phone');
$attributes[] = (new Seperator());
$attributes[] = (new TextField(__('kms/global.email')))
->setPlaceholderText(__('kms/users.emailPlaceholder'))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromModel, 'email')
->setValidationSet($emailValidationSet);
$attributes[] = (new Password())
->setLabelText(__('kms/global.password'))
->setRepeatLabelText(__('kms/global.password_repeat'))
->setPlaceholderText(__('kms/users.enterPassword'))
->setValidationSet($passwordValidationSet)
->mapValueFrom(Attribute::ValueFromModel, User::PASSWORD_COLUMN_NAME);
$attributes[] = (new Select())
->setLabelText(__('kms/users.role'))
->setItems($roleOptions)
->setPlaceholderText(__('kms/users.exampleRole'))
->mapValueFrom(Attribute::ValueFromModel, 'role_id');
$companyService = \App::make(CompanyService::class);
$companyModels= $companyService->getOptionsForSelect(true);
$attributes[] = (new Select())
->setItems($companyModels->toArray())
->setLabelText(__('kms/users.company'))
->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'companies|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;
}
/**
* 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-".User::PASSWORD_COLUMN_NAME;
if(\Input::get($passwordInputName) == "") $input = \Input::except($passwordInputName);
}
return parent::validateInputAndReturnValidator($input);
}
}