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;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
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),
|
2018-09-29 21:33:52 -07:00
|
|
|
'image' => ($location->image) ? Storage::disk('public')->url('locations/'.e($location->image)) : null,
|
2018-02-16 13:22:55 -08:00
|
|
|
'address' => ($location->address) ? e($location->address) : null,
|
|
|
|
'address2' => ($location->address2) ? e($location->address2) : null,
|
|
|
|
'city' => ($location->city) ? e($location->city) : null,
|
|
|
|
'state' => ($location->state) ? e($location->state) : null,
|
|
|
|
'country' => ($location->country) ? e($location->country) : null,
|
|
|
|
'zip' => ($location->zip) ? e($location->zip) : null,
|
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,
|
2018-02-16 13:22:55 -08:00
|
|
|
'currency' => ($location->currency) ? e($location->currency) : null,
|
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,
|
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,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|