mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-14 01:24:06 -08:00
934afa036f
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions. You may customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config file to your project root. Feel free to use [Shift's Laravel ruleset][2] to help you get started. [1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer [2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
60 lines
2.2 KiB
PHP
60 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Models\Department;
|
|
use Gate;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class DepartmentsTransformer
|
|
{
|
|
public function transformDepartments(Collection $departments, $total)
|
|
{
|
|
$array = [];
|
|
foreach ($departments as $department) {
|
|
$array[] = self::transformDepartment($department);
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
}
|
|
|
|
public function transformDepartment(Department $department = null)
|
|
{
|
|
if ($department) {
|
|
$array = [
|
|
'id' => (int) $department->id,
|
|
'name' => e($department->name),
|
|
'image' => ($department->image) ? Storage::disk('public')->url(app('departments_upload_url').e($department->image)) : null,
|
|
'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'),
|
|
];
|
|
|
|
$permissions_array['available_actions'] = [
|
|
'update' => Gate::allows('update', Department::class),
|
|
'delete' => (Gate::allows('delete', Department::class) && ($department->users_count == 0)),
|
|
];
|
|
|
|
$array += $permissions_array;
|
|
|
|
return $array;
|
|
}
|
|
}
|
|
}
|