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/vebon.komma.pro/app/KommaApp/Users/Kms/UserRepository.php
<?php
/**
 *
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma Mediadesign
 */

namespace KommaApp\Users\Kms;

use Illuminate\Support\Collection;
use KommaApp\Kms\Core\KmsRepository;
use KommaApp\Kms\SidebarListItem;
use KommaApp\Users\Models\User;

class UserRepository extends KmsRepository
{

    /**
     * This method will get a model based on the id.
     * When the id is NULL, we will generate a new.
     * This is Called in KmsSection@loadEntity.
     *
     * @param $model
     * @return User
     */
    public function getModel($model)
    {
        if ($model == null) return $this->newModel();

        //Set the extra form fields, eg. title, thumbnail
        $this->setExtraModelFields($model);


        return $model;
    }

    /**
     * This method will set extra modelFields.
     * eg title and thumbnail for the form.
     *
     * @param $model
     */
    public function setExtraModelFields(&$model, $new = false)
    {
        //Set title
        $model->title = $model->username;

        //Set the thumbnail
        $this->setThumbnail($model);

    }

    /**
     * Set the thumbnail for the model.
     *
     * @param $model
     */
    public function setThumbnail(&$model)
    {
        //Set the thumbnail
        $model->thumbnail = ['image_url' => '/images/kms/structure/users.png'];
    }

    /**
     * This method will genereate a new User.
     * It is only used in the creation of the Form.
     *
     * @param null $siteId
     * @return User
     */
    public function newModel($siteId = null)
    {
        $user = new User();
        //Set the thumbnail
        $this->setThumbnail($user);

        return $user;
    }


    /**
     * This method will get all the models.
     * And add these to the sidebarList.
     *
     * @return array $sidebarList
     */
    public function getModels()
    {

        if (!\Auth::user()->get()) return;
        if (\Auth::user()->get()->role == 1) $users = User::all();
        if (\Auth::user()->get()->role == 3) $users = User::where('role', '!=', 1)->get();
        else $users = User::where('id', '=', \Auth::user()->get()->id)->get();

        $sidebarList = [];
        foreach ($users as $user) {
            //New SidebarListItem
            $sidebarListItem = new SidebarListItem();
            //Set the values for the sidebar
            $sidebarListItem->setId($user->id);
            $sidebarListItem->setStatus(!$user->is_admin);
            $sidebarListItem->setName($user->username);
            $sidebarListItem->setThumbnail($user->id);
            $sidebarList[] = $sidebarListItem;
        }
        return $sidebarList;
    }

    public function saveModel($input, $user = null)
    {
        //Create an empty fill
        $fill = [];

        if (!$user) {
            $user = new User;
        }

        $role = 0;
        if ($user->role) $role = $user->role;
        if (isset($input['role'])) $role = $input['role'];

        $fill['role'] = $role;

        //Set the form data in the Fill array

        $fill['username'] = $input['username'];
        $fill['email'] = $input['email'];
        if (isset($input['password'])) {
            $fill['password'] = $input['password'];
        }
        $fill['first_name'] = $input['first_name'];
        $fill['last_name'] = $input['last_name'];

        //Fill the User
        $user->fill($fill);

        //Save the User
        $user->save();

        return $user->id;
    }

    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;
    }

}