snipe-it/app/Http/Transformers/UsersTransformer.php

68 lines
2.5 KiB
PHP
Raw Normal View History

2017-01-12 23:40:35 -08:00
<?php
namespace App\Http\Transformers;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use phpDocumentor\Reflection\Types\Integer;
use Gate;
2017-03-03 17:53:42 -08:00
use App\Helpers\Helper;
2017-01-12 23:40:35 -08:00
class UsersTransformer
{
public function transformUsers (Collection $users, $total)
2017-01-12 23:40:35 -08:00
{
$array = array();
2017-01-12 23:40:35 -08:00
foreach ($users as $user) {
$array[] = self::transformUser($user);
2017-01-12 23:40:35 -08:00
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
2017-01-12 23:40:35 -08:00
}
public function transformUser (User $user)
2017-01-12 23:40:35 -08:00
{
$array = [
2017-01-12 23:40:35 -08:00
'id' => e($user->id),
2017-01-13 00:13:57 -08:00
'name' => e($user->first_name).' '.($user->last_name),
'firstname' => e($user->first_name),
'lastname' => e($user->last_name),
'username' => e($user->username),
'employee_num' => e($user->employee_num),
'manager' => ($user->manager) ? (new UsersTransformer)->transformUser($user->manager) : '',
'groups' => $user->groups,
2017-01-13 00:13:57 -08:00
'jobtitle' => e($user->jobtitle),
'email' => e($user->email),
2017-01-12 23:40:35 -08:00
'location' => (new LocationsTransformer)->transformLocation($user->userloc),
'permissions' => $user->decodePermissions(),
'activated' => ($user->activated =='1') ? true : false,
'two_factor_activated' => ($user->activated =='1') ? true : false,
'assets_count' => $user->assets_count,
'licenses_count' => $user->licenses_count,
'accessories_count' => $user->accessories_count,
'consumables_count' => $user->consumables_count,
'company' => ($user->company) ? ['id' => $user->company->id,'name'=> e($user->company->name)] : null,
2017-03-03 17:53:42 -08:00
'created_at' => Helper::getFormattedDateObject($user->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($user->updated_at, 'datetime'),
2017-03-03 18:28:13 -08:00
'last_login' => Helper::getFormattedDateObject($user->last_login, 'datetime'),
2017-01-12 23:40:35 -08:00
];
$permissions_array['available_actions'] = [
2017-03-03 17:53:42 -08:00
'update' => Gate::allows('update', User::class) ? true : false,
'delete' => Gate::allows('delete', User::class) ? true : false,
];
2017-03-03 17:53:42 -08:00
$array += $permissions_array;
return $array;
2017-01-12 23:40:35 -08:00
}
2017-01-13 00:13:57 -08:00
public function transformUsersDatatable($users) {
return (new DatatablesTransformer)->transformDatatables($users);
}
2017-01-12 23:40:35 -08:00
2017-01-12 23:40:35 -08:00
}