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

74 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\Location;
use Illuminate\Database\Eloquent\Collection;
use Gate;
2017-03-10 22:09:21 -08:00
use App\Helpers\Helper;
2017-01-12 23:40:35 -08:00
class LocationsTransformer
{
public function transformLocations(Collection $locations, $total)
2017-01-12 23:40:35 -08:00
{
$array = array();
foreach ($locations as $location) {
$array[] = self::transformLocation($location);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
2017-01-12 23:40:35 -08:00
}
public function transformLocation(Location $location = null)
2017-01-12 23:40:35 -08:00
{
if ($location) {
$children_arr = [];
foreach($location->childLocations as $child) {
$children_arr[] = [
'id' => (int) $child->id,
'name' => $child->name
];
}
$array = [
'id' => (int) $location->id,
2017-01-13 00:13:57 -08:00
'name' => e($location->name),
'image' => ($location->image) ? app('locations_upload_url').e($location->image) : null,
2017-01-13 00:13:57 -08:00
'address' => e($location->address),
'city' => e($location->city),
'state' => e($location->state),
'country' => e($location->country),
'zip' => e($location->zip),
'assigned_assets_count' => (int) $location->assigned_assets_count,
'assets_count' => (int) $location->assets_count,
'users_count' => (int) $location->users_count,
2017-03-10 22:09:21 -08:00
'created_at' => Helper::getFormattedDateObject($location->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($location->updated_at, 'datetime'),
'parent' => ($location->parent) ? [
'id' => (int) $location->parent->id,
'name'=> e($location->parent->name)
] : null,
'manager' => ($location->manager) ? (new UsersTransformer)->transformUser($location->manager) : null,
'children' => $children_arr,
2017-01-12 23:40:35 -08:00
];
$permissions_array['available_actions'] = [
'update' => Gate::allows('update', Location::class) ? true : false,
2018-01-20 04:39:31 -08:00
'delete' => (Gate::allows('delete', Location::class) && ($location->assigned_assets_count==0) && ($location->assets_count==0) && ($location->users_count==0) && ($location->deleted_at=='')) ? true : false,
];
$array += $permissions_array;
return $array;
2017-01-12 23:40:35 -08:00
}
}
}