2017-01-12 23:40:35 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-12 23:40:35 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-01-12 23:40:35 -08:00
|
|
|
use App\Models\Location;
|
2023-03-01 13:51:35 -08:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
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
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-01-26 20:07:46 -08:00
|
|
|
foreach ($locations as $location) {
|
|
|
|
$array[] = self::transformLocation($location);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 20:07:46 -08:00
|
|
|
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-05-16 12:30:11 -07:00
|
|
|
$children_arr = [];
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! is_null($location->children)) {
|
|
|
|
foreach ($location->children as $child) {
|
2019-12-06 11:42:36 -08:00
|
|
|
$children_arr[] = [
|
|
|
|
'id' => (int) $child->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => $child->name,
|
2019-12-06 11:42:36 -08:00
|
|
|
];
|
|
|
|
}
|
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,
|
2023-07-10 11:44:21 -07:00
|
|
|
'phone' => ($location->phone!='') ? e($location->phone): null,
|
2023-07-11 03:14:19 -07:00
|
|
|
'fax' => ($location->fax!='') ? e($location->fax): null,
|
2017-11-03 19:40:40 -07:00
|
|
|
'assigned_assets_count' => (int) $location->assigned_assets_count,
|
2022-10-26 09:39:03 -07:00
|
|
|
'assets_count' => (int) $location->assets_count,
|
2022-10-26 00:16:06 -07:00
|
|
|
'rtd_assets_count' => (int) $location->rtd_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,
|
2020-11-17 22:17:07 -08:00
|
|
|
'ldap_ou' => ($location->ldap_ou) ? e($location->ldap_ou) : 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,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($location->parent->name),
|
2017-08-22 15:02:31 -07:00
|
|
|
] : 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,
|
2020-05-23 10:36:02 -07:00
|
|
|
'delete' => $location->isDeletable(),
|
2024-02-16 10:57:50 -08:00
|
|
|
'bulk_selectable' => [
|
2024-02-20 08:40:51 -08:00
|
|
|
'delete' => $location->isDeletable()
|
|
|
|
],
|
2023-03-01 13:51:35 -08:00
|
|
|
'clone' => (Gate::allows('create', Location::class) && ($location->deleted_at == '')),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|