2017-02-16 00:32:33 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-16 00:32:33 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-02-16 00:32:33 -08:00
|
|
|
use App\Models\Category;
|
2023-03-18 11:58:09 -07: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-02-16 00:32:33 -08:00
|
|
|
|
|
|
|
class CategoriesTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformCategories(Collection $categorys, $total)
|
2017-02-16 00:32:33 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-02-16 00:32:33 -08:00
|
|
|
foreach ($categorys as $category) {
|
|
|
|
$array[] = self::transformCategory($category);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-16 00:32:33 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformCategory(Category $category = null)
|
2017-02-16 00:32:33 -08:00
|
|
|
{
|
2023-01-10 13:30:01 -08:00
|
|
|
|
2023-01-10 16:29:51 -08:00
|
|
|
// We only ever use item_count for categories in this transformer, so it makes sense to keep it
|
|
|
|
// simple and do this switch here.
|
2023-01-10 13:30:01 -08:00
|
|
|
switch ($category->category_type) {
|
|
|
|
case 'asset':
|
|
|
|
$category->item_count = $category->assets_count;
|
|
|
|
break;
|
|
|
|
case 'accessory':
|
|
|
|
$category->item_count = $category->accessories_count;
|
|
|
|
break;
|
|
|
|
case 'consumable':
|
|
|
|
$category->item_count = $category->consumables_count;
|
|
|
|
break;
|
|
|
|
case 'component':
|
|
|
|
$category->item_count = $category->components_count;
|
|
|
|
break;
|
2023-04-27 16:26:50 -07:00
|
|
|
case 'license':
|
|
|
|
$category->item_count = $category->licenses_count;
|
|
|
|
break;
|
2023-01-10 13:30:01 -08:00
|
|
|
default:
|
|
|
|
$category->item_count = 0;
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:32:33 -08:00
|
|
|
if ($category) {
|
|
|
|
$array = [
|
2017-08-22 14:53:48 -07:00
|
|
|
'id' => (int) $category->id,
|
2017-02-16 00:32:33 -08:00
|
|
|
'name' => e($category->name),
|
2018-09-29 21:33:52 -07:00
|
|
|
'image' => ($category->image) ? Storage::disk('public')->url('categories/'.e($category->image)) : null,
|
2023-03-15 15:57:45 -07:00
|
|
|
'category_type' => Helper::categoryTypeList($category->category_type),
|
2020-09-11 16:05:42 -07:00
|
|
|
'has_eula' => ($category->getEula() ? true : false),
|
2021-09-10 20:23:49 -07:00
|
|
|
'use_default_eula' => ($category->use_default_eula=='1' ? true : false),
|
2020-05-23 10:36:02 -07:00
|
|
|
'eula' => ($category->getEula()),
|
2021-06-10 13:15:52 -07:00
|
|
|
'checkin_email' => ($category->checkin_email == '1'),
|
2020-05-23 10:36:02 -07:00
|
|
|
'require_acceptance' => ($category->require_acceptance == '1'),
|
2023-01-10 13:30:01 -08:00
|
|
|
'item_count' => (int) $category->item_count,
|
2018-05-18 15:48:48 -07:00
|
|
|
'assets_count' => (int) $category->assets_count,
|
|
|
|
'accessories_count' => (int) $category->accessories_count,
|
|
|
|
'consumables_count' => (int) $category->consumables_count,
|
|
|
|
'components_count' => (int) $category->components_count,
|
|
|
|
'licenses_count' => (int) $category->licenses_count,
|
2024-09-19 09:56:05 -07:00
|
|
|
'created_by' => ($category->adminuser) ? [
|
|
|
|
'id' => (int) $category->adminuser->id,
|
|
|
|
'name'=> e($category->adminuser->present()->fullName()),
|
|
|
|
] : null,
|
2017-08-22 14:26:08 -07:00
|
|
|
'created_at' => Helper::getFormattedDateObject($category->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($category->updated_at, 'datetime'),
|
2017-02-16 00:32:33 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
2020-05-23 10:36:02 -07:00
|
|
|
'update' => Gate::allows('update', Category::class),
|
|
|
|
'delete' => $category->isDeletable(),
|
2017-02-16 00:32:33 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
$array += $permissions_array;
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|