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

64 lines
2.2 KiB
PHP
Raw Normal View History

2017-05-22 21:32:33 -07:00
<?php
namespace App\Http\Transformers;
use App\Models\Department;
use Illuminate\Database\Eloquent\Collection;
use Gate;
use App\Helpers\Helper;
class DepartmentsTransformer
{
2017-05-23 02:37:30 -07:00
public function transformDepartments (Collection $departments, $total)
2017-05-22 21:32:33 -07:00
{
$array = array();
2017-05-23 02:37:30 -07:00
foreach ($departments as $department) {
$array[] = self::transformDepartment($department);
2017-05-22 21:32:33 -07:00
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
2017-05-23 02:37:30 -07:00
public function transformDepartment (Department $department = null)
2017-05-22 21:32:33 -07:00
{
2017-05-23 02:37:30 -07:00
if ($department) {
2017-05-22 21:32:33 -07:00
$array = [
'id' => (int) $department->id,
2017-05-23 02:37:30 -07:00
'name' => e($department->name),
'image' => ($department->image) ? app('departments_upload_url').e($department->image) : null,
2017-05-23 02:37:30 -07:00
'company' => ($department->company) ? [
'id' => (int) $department->company->id,
'name'=> e($department->company->name)
] : null,
'manager' => ($department->manager) ? [
'id' => (int) $department->manager->id,
'name' => e($department->manager->getFullNameAttribute()),
'first_name'=> e($department->manager->first_name),
'last_name'=> e($department->manager->last_name)
] : null,
'location' => ($department->location) ? [
'id' => (int) $department->location->id,
'name' => e($department->location->name)
] : null,
'users_count' => e($department->users_count),
'created_at' => Helper::getFormattedDateObject($department->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($department->updated_at, 'datetime'),
2017-05-22 21:32:33 -07:00
];
$permissions_array['available_actions'] = [
'update' => Gate::allows('update', Department::class) ? true : false,
'delete' => (Gate::allows('delete', Department::class) && ($department->users_count==0) && ($department->deleted_at=='')) ? true : false,
2017-05-22 21:32:33 -07:00
];
$array += $permissions_array;
return $array;
}
}
}