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),
|
2017-11-07 11:11:47 -08:00
|
|
|
'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),
|
2017-09-25 20:45:05 -07:00
|
|
|
'country' => e($location->country),
|
|
|
|
'zip' => e($location->zip),
|
2017-11-03 19:40:40 -07:00
|
|
|
'assigned_assets_count' => (int) $location->assigned_assets_count,
|
|
|
|
'assets_count' => (int) $location->assets_count,
|
2017-10-30 19:21:35 -07:00
|
|
|
'users_count' => (int) $location->users_count,
|
2017-09-25 20:45:05 -07:00
|
|
|
|
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,
|
2017-12-12 07:03:31 -08:00
|
|
|
'delete' => (Gate::allows('delete', Department::class) && ($location->assigned_assets_count==0) && ($location->assets_count==0) && ($location->users_count==0) && ($location->deleted_at=='')) ? 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|