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-05-22 17:27:00 -07: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-05-22 17:27:00 -07: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
|
|
|
|
2017-05-16 12:30:11 -07:00
|
|
|
$children_arr = [];
|
2017-08-25 05:32:12 -07:00
|
|
|
foreach($location->childLocations as $child) {
|
|
|
|
$children_arr[] = [
|
|
|
|
'id' => (int) $child->id,
|
|
|
|
'name' => $child->name
|
|
|
|
];
|
2017-05-16 12:30:11 -07:00
|
|
|
}
|
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-03-10 22:09:21 -08:00
|
|
|
'created_at' => Helper::getFormattedDateObject($location->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($location->updated_at, 'datetime'),
|
2017-08-22 15:02:31 -07:00
|
|
|
'parent' => ($location->parent) ? [
|
|
|
|
'id' => (int) $location->parent->id,
|
|
|
|
'name'=> e($location->parent->name)
|
|
|
|
] : null,
|
2017-08-25 06:04:22 -07:00
|
|
|
'manager' => ($location->manager) ? (new UsersTransformer)->transformUser($location->manager) : 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|