2017-01-12 23:40:35 -08:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Models\Location;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2017-02-08 18:24:55 -08:00
|
|
|
use Gate;
|
2017-03-10 22:09:21 -08:00
|
|
|
use App\Helpers\Helper;
|
2017-01-12 23:40:35 -08:00
|
|
|
|
|
|
|
class LocationsTransformer
|
|
|
|
{
|
|
|
|
|
2017-01-26 20:07:46 -08:00
|
|
|
public function transformLocations (Collection $locations, $total)
|
2017-01-12 23:40:35 -08:00
|
|
|
{
|
2017-01-26 20:07:46 -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
|
|
|
}
|
|
|
|
|
2017-01-13 01:45:14 -08:00
|
|
|
public function transformLocation (Location $location = null)
|
2017-01-12 23:40:35 -08:00
|
|
|
{
|
|
|
|
if ($location) {
|
2017-01-26 21:02:59 -08:00
|
|
|
|
|
|
|
$assets_arr = [];
|
|
|
|
foreach($location->assets() as $asset) {
|
|
|
|
$assets_arr = ['id' => $asset->id];
|
|
|
|
}
|
2017-05-16 12:30:11 -07:00
|
|
|
|
|
|
|
$children_arr = [];
|
|
|
|
foreach($location->childLocations() as $child) {
|
|
|
|
$children_arr = ['id' => $child->id];
|
|
|
|
}
|
2017-01-26 21:02:59 -08:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$array = [
|
2017-07-25 23:40:30 -07:00
|
|
|
'id' => (int) $location->id,
|
2017-01-13 00:13:57 -08:00
|
|
|
'name' => e($location->name),
|
|
|
|
'address' => e($location->address),
|
|
|
|
'city' => e($location->city),
|
|
|
|
'state' => e($location->state),
|
2017-01-26 20:07:46 -08:00
|
|
|
'assets_checkedout' => $location->assets()->count(),
|
|
|
|
'assets_default' => $location->assignedassets()->count(),
|
2017-01-13 00:13:57 -08:00
|
|
|
'country' => e($location->country),
|
2017-01-26 21:02:59 -08:00
|
|
|
'assets' => $assets_arr,
|
2017-03-10 22:09:21 -08:00
|
|
|
'created_at' => Helper::getFormattedDateObject($location->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($location->updated_at, 'datetime'),
|
2017-07-25 23:40:30 -07:00
|
|
|
'parent_id' => ($location->parent_id) ? (int) $location->parent_id : null,
|
2017-05-16 12:30:11 -07:00
|
|
|
'children' => $children_arr,
|
2017-01-12 23:40:35 -08:00
|
|
|
];
|
2017-01-26 21:02:59 -08:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$permissions_array['available_actions'] = [
|
2017-03-03 17:56:05 -08:00
|
|
|
'update' => Gate::allows('update', Location::class) ? true : false,
|
|
|
|
'delete' => Gate::allows('delete', Location::class) ? true : false,
|
2017-02-08 18:24:55 -08:00
|
|
|
];
|
2017-01-26 21:02:59 -08:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$array += $permissions_array;
|
2017-01-26 21:02:59 -08:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
return $array;
|
2017-01-12 23:40:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|