2017-01-12 23:40:35 -08:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2017-01-13 04:50:20 -08:00
|
|
|
use phpDocumentor\Reflection\Types\Integer;
|
2017-02-08 18:24:55 -08:00
|
|
|
use Gate;
|
2017-03-03 17:53:42 -08:00
|
|
|
use App\Helpers\Helper;
|
2017-01-12 23:40:35 -08:00
|
|
|
|
|
|
|
class UsersTransformer
|
|
|
|
{
|
|
|
|
|
2017-01-13 04:50:20 -08:00
|
|
|
public function transformUsers (Collection $users, $total)
|
2017-01-12 23:40:35 -08:00
|
|
|
{
|
2017-01-13 04:50:20 -08:00
|
|
|
$array = array();
|
2017-01-12 23:40:35 -08:00
|
|
|
foreach ($users as $user) {
|
2017-01-13 04:50:20 -08:00
|
|
|
$array[] = self::transformUser($user);
|
2017-01-12 23:40:35 -08:00
|
|
|
}
|
2017-01-13 04:50:20 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
2017-01-12 23:40:35 -08:00
|
|
|
}
|
|
|
|
|
2017-01-13 04:50:20 -08:00
|
|
|
public function transformUser (User $user)
|
2017-01-12 23:40:35 -08:00
|
|
|
{
|
2017-01-13 04:50:20 -08:00
|
|
|
$array = [
|
2017-07-25 23:40:30 -07:00
|
|
|
'id' => (int) $user->id,
|
2017-10-24 09:51:07 -07:00
|
|
|
'avatar' => e($user->present()->gravatar),
|
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),
|
2017-01-13 04:50:20 -08:00
|
|
|
'employee_num' => e($user->employee_num),
|
2017-07-26 08:47:54 -07:00
|
|
|
'manager' => ($user->manager) ? [
|
|
|
|
'id' => (int) $user->manager->id,
|
2017-07-26 08:51:16 -07:00
|
|
|
'name'=> e($user->manager->username)
|
2017-07-26 08:47:54 -07:00
|
|
|
] : null,
|
|
|
|
'jobtitle' => ($user->jobtitle) ? e($user->jobtitle) : null,
|
2017-10-23 14:21:51 -07:00
|
|
|
'phone' => ($user->phone) ? e($user->phone) : null,
|
2017-01-13 00:13:57 -08:00
|
|
|
'email' => e($user->email),
|
2017-05-23 02:48:03 -07:00
|
|
|
'department' => ($user->department) ? [
|
|
|
|
'id' => (int) $user->department->id,
|
|
|
|
'name'=> e($user->department->name)
|
|
|
|
] : null,
|
2017-07-26 08:47:54 -07:00
|
|
|
'location' => ($user->userloc) ? [
|
|
|
|
'id' => (int) $user->userloc->id,
|
|
|
|
'name'=> e($user->userloc->name)
|
|
|
|
] : null,
|
2017-10-07 03:36:50 -07:00
|
|
|
'notes'=> e($user->notes),
|
2017-01-12 23:40:35 -08:00
|
|
|
'permissions' => $user->decodePermissions(),
|
2017-01-13 04:50:20 -08:00
|
|
|
'activated' => ($user->activated =='1') ? true : false,
|
2017-07-29 16:25:42 -07:00
|
|
|
'two_factor_activated' => ($user->two_factor_active()) ? true : false,
|
2017-07-25 23:40:30 -07:00
|
|
|
'assets_count' => (int) $user->assets_count,
|
|
|
|
'licenses_count' => (int) $user->licenses_count,
|
|
|
|
'accessories_count' => (int) $user->accessories_count,
|
|
|
|
'consumables_count' => (int) $user->consumables_count,
|
|
|
|
'company' => ($user->company) ? ['id' => (int) $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
|
|
|
];
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$permissions_array['available_actions'] = [
|
2017-09-06 17:11:43 -07:00
|
|
|
'update' => (Gate::allows('update', User::class) && ($user->deleted_at=='')) ? true : false,
|
2017-10-19 17:15:21 -07:00
|
|
|
'delete' => (Gate::allows('delete', User::class) && ($user->deleted_at=='') && ($user->assets_count == 0) && ($user->licenses_count == 0) && ($user->accessories_count == 0) && ($user->consumables_count == 0)) ? true : false,
|
2017-09-06 17:11:43 -07:00
|
|
|
'clone' => (Gate::allows('create', User::class) && ($user->deleted_at=='')) ,
|
|
|
|
'restore' => (Gate::allows('create', User::class) && ($user->deleted_at!='')) ? true : false,
|
2017-02-08 18:24:55 -08:00
|
|
|
];
|
2017-03-03 17:53:42 -08:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$array += $permissions_array;
|
|
|
|
|
2017-08-10 13:37:54 -07:00
|
|
|
|
|
|
|
$numGroups = count($user->groups);
|
|
|
|
if($numGroups > 0)
|
|
|
|
{
|
|
|
|
$groups["total"] = $numGroups;
|
|
|
|
foreach($user->groups as $group)
|
|
|
|
{
|
|
|
|
$groups["rows"][] = [
|
|
|
|
'id' => (int) $group->id,
|
|
|
|
'name' => e($group->name)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$array["groups"] = $groups;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$array["groups"] = null;
|
|
|
|
}
|
|
|
|
|
2017-01-13 04:50:20 -08:00
|
|
|
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-18 20:41:40 -08:00
|
|
|
|
|
|
|
|
2017-01-12 23:40:35 -08:00
|
|
|
}
|