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/PDeckers/opelkapitan.nl/workbench/komma/kms/src/Komma/Kms/Users/UserRepository.php
<?php
/**
 * Short description for the file.
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Kms\Users;

use Illuminate\Support\Collection;
use Komma\Kms\Core\KmsRepository;

class UserRepository extends KmsRepository
{
    public function newEntity()
    {
        $user = new User();
        $entity = new UserEntity($user->toArray());
        return $entity;
    }

    public function getEntity($id = null)
    {
        if($id == null) return $this->newEntity();
        $user = User::find($id);
        $entity = new UserEntity($user->toArray(),$id);
        return $entity;
    }

    public function getEntities()
    {
        $records = User::all();
        $entities = [];
        foreach($records as $record){
            $entities[] = new UserEntity($record->toArray());
        }
        return $entities;
    }

    public function saveEntity($entity)
    {
        $record = User::firstOrNew(['id' => $entity->id]);
        if($entity->password) $record->password = $entity->password;
        $record->fill([
            'username' => $entity->username,
            'email' => $entity->email,
            'first_name' => $entity->first_name,
            'last_name' => $entity->last_name,
            'is_admin' => $entity->is_admin
        ]);
        $record->save();

        $entity = new UserEntity($record->toArray());

        return $entity;
    }

    public function destroyEntity($id)
    {
        User::destroy($id);
    }

    public function getUsersForBestFriendSelectBox()
    {
        $records = User::all();
        $entities = [];
        foreach($records as $record){
            $entity = [];
            $entity['value'] = $record->id;
            $entity['content'] = $record->first_name . ' ' . $record->last_name;
            $entity['htmlContent'] = $record->first_name . ' ' . $record->last_name . ' <i>('.$record->email.')</i>';
            $entities[] = $entity;
        }
        return $entities;
    }

}