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/SBogers10/finsteps.komma.pro/app/Users/Kms/UserService.php
<?php
/**
 * Created by PhpStorm.
 * User: julesgraus
 * Date: 31/01/2018
 * Time: 13:13
 */
namespace App\Users\Kms;

use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Attributes\Password;
use Komma\KMS\Core\Sections\SectionService;
use Komma\KMS\Core\Sections\SectionTabItem;
use Komma\KMS\Core\Sections\SideBarListItem;
use Komma\KMS\Users\Models\KmsUser;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

abstract class UserService extends SectionService
{
    protected $sortable = false;

    function __construct()
    {
        if(!$this->forModelName) throw new \RuntimeException('Please set the for model name of '.get_class(static::class));

        parent::__construct();
    }

    /**
     * This method will get all the models.
     * And add these to the sidebarList.
     *
     * @return array $sidebarList
     */
    public function getModelsForSideBar():array
    {
        // Get users based upon your user role
        $users = $this->forModelName::where('role' , '>=', \Auth::user()->role)->get();

        $sidebarList = [];
        $users = $users->load('documents');
        foreach ($users as $user) {
            //New SidebarListItem
            $sidebarListItem = new SidebarListItem();
            $user->title = $user->email; //used in KmsRepository::setThumbnail
            /** @var HasThumbnailInterface $user */
            $user->generateThumbnail();

            //Set the values for the sidebar
            $sidebarListItem->setId($user->id);
            $sidebarListItem->setStatus(!$user->is_admin);
            $sidebarListItem->setName($user->getDisplayName());
            $sidebarListItem->setThumbnail($user->getThumbnail());
            $sidebarListItem->alsoSearchInAttributesOfModel($user);

            $sidebarList[] = $sidebarListItem;
        }

        return $sidebarList;
    }

    /**
     * This method will save an model
     *
     * @param Model $model or null
     * @param Collection $sectionTabItems These must be filled with data. This is something you need to do yourself.
     *
     * @return mixed
     */
    public function saveModel(Model $model = null, Collection $sectionTabItems): Model
    {
        /** @var KmsUser $model */

        $sectionTabItems->each(
            function($sectionTabItem, $key) use($model, &$editedModel) {
                /** @var KmsUser $model */
                /** @var SectionTabItem $sectionTabItem */
                $attribute = $sectionTabItem->getAttribute();
                $value = $attribute->getValue();

//              if(is_a($attribute, Select::class)) dd($attribute); //debugging helper

                $reference = $attribute->getsValueFromReference();
                switch ($attribute->getsValueFrom()) {
                    case Attribute::ValueFromModel:
                        if ($reference == KmsUser::PASSWORD_COLUMN_NAME) {
                            /** @var Password $attribute */
                            if($attribute->isEmpty()) {
                                //Prevents the parent from saving an empty password hash since "valueFromItself" isn't save to the database at all
                                $attribute->mapValueFrom(Attribute::ValueFromItself, '');
                            }
                        }
                        break;
                }
            }
        );

        $exists = $model->exists;
        $model->role = 1;
        $model->save();

        parent::saveModel($model, $sectionTabItems);

        return $model;
    }
}