File: D:/HostingSpaces/ASmits/kemi.nl/app/KommaApp/Users/Kms/UserRepository.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Users\Kms;
use App\KommaApp\Kms\Core\Attributes\Password;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use Illuminate\Database\Eloquent\Model;
use App\KommaApp\Images\ImageService;
use App\KommaApp\Kms\Core\Kms;
use App\KommaApp\Kms\Core\KmsRepository;
use App\KommaApp\Kms\Core\Sections\KmsSection;
use App\KommaApp\Kms\SidebarListItem;
use App\KommaApp\Users\Models\User;
use Illuminate\Support\Collection;
class UserRepository extends KmsRepository
{
/**
* @var ImageService $imageService
*/
private $imageService;
protected $forModelName = User::class;
function __construct(Kms $kms, ImageService $imageService)
{
parent::__construct($kms);
$this->imageService = $imageService;
//set the imageable type
$this->imageService->setImageableType($this->forModelName);
}
/**
* This method will get a model based on the id.
* When the id is NULL or the model does not exist, we will generate a new.
*
* @see KmsSection::loadModel()
* @param int $modelId
* @return User
*/
public function getModel($modelId = null): Model
{
if ($modelId == null || !$model = User::find($modelId)) {
return $this->newModel();
}
$this->setThumbnail($model);
return $model;
}
/**
* This method will generate a new User.
* It is only used in the creation of the Form.
*
* @param null $siteId
* @return User
*/
public function newModel($siteId = null): Model
{
$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()
{
// Get users based upon your user role
if (\Auth::user()->role->id == 1) {
$users = User::all();
}
elseif (\Auth::user()->role->id >= 2) {
$users = User::where('role_id', '>', 2)->orWhere('id', \Auth::user()->id)->get();
}
else {
$users = User::where('id', '=', \Auth::user()->id)->get();
}
$sidebarList = [];
foreach ($users as $user) {
//New SidebarListItem
$sidebarListItem = new SidebarListItem();
$user->title = $user->username; //used in KmsRepository::setThumbnail
$this->setThumbnail($user);
$this->generateThumbnail($user);
//Set the values for the sidebar
$sidebarListItem->setId($user->id);
$sidebarListItem->setStatus(!$user->is_admin);
$sidebarListItem->setName($user->username);
$sidebarListItem->setThumbnail($user->thumbnail);
$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
{
//Remove the password if it is an empty string (indicating that it did not change)
$sectionTabItems->each(function($sectionTabItem, $key) use ($sectionTabItems) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
if(is_a($attribute, Password::class)) {
if(\Hash::check("", $attribute->getValue()))
{
$sectionTabItems->forget($key);
}
};
});
$model->save(); //Save the user
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
//Return the page
return $model;
}
public function destroyModel($idOrModel)
{
if(is_numeric($idOrModel)) {
User::destroy($idOrModel);
} elseif(is_a($idOrModel, Model::class)) {
$idOrModel->delete();
} else {
throw new \InvalidArgumentException("Trying to delete the user but could not because the id wasn't an eloquent model or numeric id but: ".gettype($idOrModel));
}
}
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;
}
}