HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/stafa/werkenbijstafa.nl/app/Komma/Users/Kms/SiteUserSection.php
<?php

namespace App\Komma\Users\Kms;

//The new object oriented attributes
use App\Komma\Globalization\CultureInfo;
use App\Komma\Globalization\CultureTypes;
use App\Komma\Kms\Core\Sections\NoLanguageTabsDirector;
use App\Komma\Routes\RouteService;
use App\Komma\Kms\Core\Attributes\Attribute;
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\Seperator;
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\SectionTabGroups;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\Core\Sections\SectionTabsBuilder;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Genders;
use App\Komma\Users\Models\KmsUser;
use App\Komma\Users\Models\SiteUserRole;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Illuminate\Database\Eloquent\Collection;

final class SiteUserSection extends Section
{
    protected $slug = "site_users";

    /**
     * UserSection constructor.
     * @param SiteUserService $sectionService
     * @param RouteService $routeService
     * @param SiteServiceInterface $siteService
     */
    function __construct(SiteUserService $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\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()
     * @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)      ***\\
        //*****************************************************************************************\\

        $emailValidationSet = (new ValidationSet())
            ->setRules([
                'required',
                'email',
                Rule::unique('site_users', 'email')->ignore($model->id)
            ])
            ->setMessages([
                'required' => __('validation.required'),
                'email' => __('kms/kms_users.enterValidEmailAddress'),
                'unique' => __('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_users.enterPassword'),
                'min' => __('kms/kms_users.passwordMinLength'),
                'regex' => __('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(CultureInfo::getCultures(CultureTypes::SPECIFIC_CULTURES))->each(function(CultureInfo $cultureInfo) use (&$cultureOptions) {
            $cultureOptions[] = (new SelectOption())->setContent($cultureInfo->getNativeName())->setHtmlContent($cultureInfo->getNativeName())->setValue($cultureInfo->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/global.information')));

        $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 Seperator());

        $attributes[] = (new TextField(__('kms/global.email')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'email')
            ->setValidationSet($emailValidationSet);

        $attributes[] = (new Password())
            ->setLabelText(__('kms/global.password'))
            ->setRepeatLabelText(__('kms/global.password_repeat'))
            ->setPlaceholderText(__('kms/kms_users.enterPassword'))
            ->setValidationSet($passwordValidationSet)
            ->mapValueFrom(Attribute::ValueFromModel, KmsUser::PASSWORD_COLUMN_NAME);


        $attributes[] = (new Select())
            ->setLabelText(__('kms/kms_users.role'))
            ->setItems($roleOptions)
            ->mapValueFrom(Attribute::ValueFromModel, 'role');

        $attributes[] = (new Select())
            ->setLabelText(__('kms/kms_users.culture'))
            ->setItems($cultureOptions)
            ->mapValueFrom(Attribute::ValueFromModel, 'culture');

        $attributes[] = (new Select())
            ->setLabelText(__('auth.gender'))
            ->setItems($genderOptions)
            ->mapValueFrom(Attribute::ValueFromModel, 'gender');


        //****************************************************************************************************************************************\\
        //*** 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-".KmsUser::PASSWORD_COLUMN_NAME;
            if(\Input::get($passwordInputName) == "") $input = \Input::except($passwordInputName);
        }

        return parent::validateInputAndReturnValidator($input);
    }


}