snipe-it/app/Presenters/UserPresenter.php

494 lines
16 KiB
PHP
Raw Normal View History

<?php
namespace App\Presenters;
use App\Helpers\Helper;
use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
2020-08-26 12:17:18 -07:00
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
/**
* Class UserPresenter
*/
class UserPresenter extends Presenter
{
/**
* Json Column Layout for bootstrap table
* @return string
*/
public static function dataTableLayout()
{
$layout = [
[
'field' => 'checkbox',
'checkbox' => true,
],
[
'field' => 'id',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.id'),
'visible' => false,
],
2017-10-24 09:51:07 -07:00
[
'field' => 'avatar',
'searchable' => false,
'sortable' => false,
'switchable' => true,
'title' => trans('general.importer.avatar'),
'visible' => false,
'formatter' => 'imageFormatter',
2017-10-24 09:51:07 -07:00
],
[
'field' => 'company',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/companies/table.title'),
'visible' => false,
'formatter' => 'companiesLinkObjFormatter',
],
[
'field' => 'name',
'searchable' => true,
'sortable' => true,
'title' => trans('admin/users/table.name'),
'visible' => true,
'formatter' => 'usersLinkFormatter',
],
[
'field' => 'first_name',
'searchable' => true,
'sortable' => true,
'title' => trans('general.first_name'),
'visible' => false,
'formatter' => 'usersLinkFormatter',
],
[
'field' => 'last_name',
'searchable' => true,
'sortable' => true,
'title' => trans('general.last_name'),
'visible' => false,
'formatter' => 'usersLinkFormatter',
],
[
'field' => 'jobtitle',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/table.title'),
'visible' => true,
'formatter' => 'usersLinkFormatter',
],
2023-01-24 12:29:27 -08:00
[
2023-01-25 12:58:44 -08:00
'field' => 'vip',
2023-01-24 12:29:27 -08:00
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/general.vip_label'),
'visible' => false,
'formatter' => 'trueFalseFormatter',
],
[
'field' => 'remote',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/general.remote'),
'visible' => false,
'formatter' => 'trueFalseFormatter',
],
[
'field' => 'email',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/table.email'),
'visible' => true,
'formatter' => 'emailFormatter',
],
[
'field' => 'phone',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/table.phone'),
'visible' => true,
'formatter' => 'phoneFormatter',
],
[
'field' => 'website',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.website'),
'visible' => false,
'formatter' => 'externalLinkFormatter',
],
2017-10-30 18:57:00 -07:00
[
'field' => 'address',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.address'),
'visible' => false,
2017-10-30 18:57:00 -07:00
],
[
'field' => 'city',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.city'),
'visible' => false,
2017-10-30 18:57:00 -07:00
],
[
'field' => 'state',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.state'),
'visible' => false,
2017-10-30 18:57:00 -07:00
],
[
'field' => 'country',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.country'),
'visible' => false,
2017-10-30 18:57:00 -07:00
],
[
'field' => 'zip',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.zip'),
'visible' => false,
2017-10-30 18:57:00 -07:00
],
[
'field' => 'username',
'searchable' => true,
'sortable' => true,
'switchable' => false,
'title' => trans('admin/users/table.username'),
'visible' => true,
'formatter' => 'usersLinkFormatter',
],
[
'field' => 'employee_num',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.employee_number'),
'visible' => false,
],
[
'field' => 'department',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.department'),
'visible' => true,
'formatter' => 'departmentsLinkObjFormatter',
],
[
'field' => 'location',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/table.location'),
'visible' => true,
'formatter' => 'locationsLinkObjFormatter',
],
[
'field' => 'manager',
'searchable' => true,
'sortable' => true,
'title' => trans('admin/users/table.manager'),
'visible' => true,
'formatter' => 'usersLinkObjFormatter',
],
[
'field' => 'assets_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'escape' => true,
'class' => 'css-barcode',
'title' => trans('general.assets'),
'visible' => true,
'formatter' => 'linkNumberToUserAssetsFormatter',
],
[
'field' => 'licenses_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'class' => 'css-license',
'title' => trans('general.licenses'),
'visible' => true,
'formatter' => 'linkNumberToUserLicensesFormatter',
],
[
'field' => 'consumables_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'class' => 'css-consumable',
'title' => trans('general.consumables'),
'visible' => true,
'formatter' => 'linkNumberToUserConsumablesFormatter',
],
[
'field' => 'accessories_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'class' => 'css-accessory',
'title' => trans('general.accessories'),
'visible' => true,
'formatter' => 'linkNumberToUserAccessoriesFormatter',
],
[
'field' => 'manages_users_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'class' => 'css-users',
'title' => trans('admin/users/table.managed_users'),
'visible' => true,
'formatter' => 'linkNumberToUserManagedUsersFormatter',
],
[
'field' => 'manages_locations_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'class' => 'css-location',
'title' => trans('admin/users/table.managed_locations'),
'visible' => true,
'formatter' => 'linkNumberToUserManagedLocationsFormatter',
],
[
'field' => 'notes',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.notes'),
'visible' => true,
],
[
'field' => 'groups',
'searchable' => false,
'sortable' => false,
'switchable' => true,
'title' => trans('general.groups'),
'visible' => true,
'formatter' => 'groupsFormatter',
],
[
'field' => 'ldap_import',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/settings/general.ldap_enabled'),
'visible' => false,
'formatter' => 'trueFalseFormatter',
2020-12-02 11:01:05 -08:00
],
[
'field' => 'two_factor_enrolled',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/general.two_factor_enrolled'),
'visible' => false,
'formatter' => 'trueFalseFormatter',
],
[
'field' => 'two_factor_optin',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/users/general.two_factor_active'),
'visible' => false,
'formatter' => 'trueFalseFormatter',
],
[
'field' => 'activated',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.login_enabled'),
'visible' => true,
'formatter' => 'trueFalseFormatter',
],
[
'field' => 'autoassign_licenses',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.autoassign_licenses'),
'visible' => false,
'formatter' => 'trueFalseFormatter',
],
[
'field' => 'created_by',
'searchable' => false,
'sortable' => true,
'title' => trans('general.created_by'),
'visible' => false,
'formatter' => 'usersLinkObjFormatter',
],
[
'field' => 'created_at',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.created_at'),
'visible' => false,
'formatter' => 'dateDisplayFormatter',
2017-03-03 18:28:13 -08:00
],
[
'field' => 'start_date',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.start_date'),
'visible' => false,
'formatter' => 'dateDisplayFormatter',
],
[
'field' => 'end_date',
'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.end_date'),
'visible' => false,
'formatter' => 'dateDisplayFormatter',
],
2017-03-03 18:28:13 -08:00
[
'field' => 'last_login',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.last_login'),
'visible' => false,
'formatter' => 'dateDisplayFormatter',
],
[
'field' => 'actions',
'searchable' => false,
'sortable' => false,
'switchable' => false,
'title' => trans('table.actions'),
'visible' => true,
'formatter' => 'usersActionsFormatter',
],
];
return json_encode($layout);
}
public function emailLink()
{
if ($this->email) {
return '<a href="mailto:'.$this->email.'">'.$this->email.'</a><a href="mailto:'.$this->email.'" class="hidden-xs hidden-sm"><i class="far fa-envelope"></i></a>';
}
return '';
}
/**
* Returns the user full name, it simply concatenates
* the user first and last name.
*
* @return string
*/
public function fullName()
{
return html_entity_decode($this->first_name.' '.$this->last_name, ENT_QUOTES | ENT_XML1, 'UTF-8');
}
2016-12-27 16:24:41 -08:00
/**
* Standard accessor.
* @TODO Remove presenter::fullName() entirely?
* @return string
*/
public function name()
{
return $this->fullName();
}
/**
* Returns the user Gravatar image url.
*
* @return string
*/
public function gravatar()
{
// User's specific avatar
if ($this->avatar) {
// Check if it's a google avatar or some external avatar
if (Str::startsWith($this->avatar, ['http://', 'https://'])) {
return $this->avatar;
}
// Otherwise assume it's an uploaded image
2020-08-26 12:17:18 -07:00
return Storage::disk('public')->url('avatars/'.e($this->avatar));
}
// If there is a default avatar
if (Setting::getSettings()->default_avatar!= '') {
return Storage::disk('public')->url('avatars/'.e(Setting::getSettings()->default_avatar));
}
// Fall back to Gravatar if the settings allow loading remote scripts
if (Setting::getSettings()->load_remote == '1') {
if ($this->model->gravatar != '') {
$gravatar = md5(strtolower(trim($this->model->gravatar)));
return '//gravatar.com/avatar/'.$gravatar;
} elseif ($this->email != '') {
$gravatar = md5(strtolower(trim($this->email)));
return '//gravatar.com/avatar/'.$gravatar;
}
}
return false;
}
/**
* Formatted url for use in tables.
* @return string
*/
public function nameUrl()
{
return (string) link_to_route('users.show', $this->getFullNameAttribute(), $this->id);
}
/**
* Url to view this item.
* @return string
*/
public function viewUrl()
{
return route('users.show', $this->id);
}
public function glyph()
{
return '<i class="fas fa-user" aria-hidden="true"></i>';
}
}