feat: Refactor addon management to use AddonType and AddonInputType enums, update the database schema, and implement a grouped datatable display for addons.

This commit is contained in:
pbao9
2026-02-13 11:31:54 +07:00
parent b127fa7c66
commit 245f5ade37
11 changed files with 684 additions and 216 deletions
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Enums;
enum AddonInputType: string
{
case NUMBER = 'number';
case SLIDER = 'slider';
case SELECT = 'select';
public function label(): string
{
return match ($this) {
self::NUMBER => 'Số',
self::SLIDER => 'Thanh trượt',
self::SELECT => 'Chọn',
};
}
public static function getOptions(): array
{
return [
self::NUMBER->value => self::NUMBER->label(),
self::SLIDER->value => self::SLIDER->label(),
self::SELECT->value => self::SELECT->label(),
];
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Enums;
enum AddonType: string
{
case VPS = 'vps';
case HOSTING = 'hosting';
case DOMAIN = 'domain';
public function label(): string
{
return match ($this) {
self::VPS => 'VPS',
self::HOSTING => 'Hosting',
self::DOMAIN => 'Domain',
};
}
public static function getOptions(): array
{
return [
self::VPS->value => self::VPS->label(),
self::HOSTING->value => self::HOSTING->label(),
self::DOMAIN->value => self::DOMAIN->label(),
];
}
public static function values(): array
{
return array_column(self::cases(), 'value');
}
}
+51 -14
View File
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\admin;
use App\Enums\AddonType;
use App\Http\Controllers\Controller;
use App\Http\Requests\AddOnRequest;
use App\Models\Addon;
@@ -102,22 +103,58 @@ class AddonController extends Controller
public function datatable(Request $request)
{
$query = $this->addonRepository->filter(
$request->all(),
[],
[],
['*'],
true
);
$addons = Addon::orderBy('sort_order')->get();
return DataTables::eloquent($query)
->addColumn(
'action',
function ($d) {
return view('administrator.pages.addon.datatable.action', ['row' => $d])->render();
$grouped = $addons->groupBy(function ($item) {
return $item->type instanceof AddonType
? $item->type->value
: $item->type;
});
$types = AddonType::values();
$data = [];
foreach ($types as $type) {
$typeEnum = AddonType::tryFrom($type);
$typeLabel = $typeEnum ? $typeEnum->label() : $type;
$typeAddons = $grouped->get($type, collect());
$childrenHtml = view('administrator.pages.addon.datatable.children', [
'children' => $typeAddons
])->render();
$data[] = [
'id' => $type,
'type' => $type,
'name' => $typeLabel,
'code' => $type,
'children' => $typeAddons->toArray(),
'children_html' => $childrenHtml,
'status' => true,
];
}
return DataTables::of(collect($data))
->addColumn('details_control', function ($d) {
$hasChildren = !empty($d['children']) && count($d['children']) > 0;
if ($hasChildren) {
return '<i data-lucide="plus-circle" class="cursor-pointer text-success mx-auto" style="width:15px; height:15px"></i>';
}
)
->rawColumns(['status', 'action'])
return '';
})
->addColumn('name', function ($d) {
return $d['name'];
})
->addColumn('slug', function ($d) {
return $d['code'];
})
->addColumn('is_active', function ($d) {
return '<span class="badge bg-success">Active</span>';
})
->addColumn('action', function ($d) {
return '';
})
->rawColumns(['details_control', 'is_active', 'children_html'])
->make(true);
}
}
+8 -1
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Enums\AddonType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -12,7 +13,7 @@ class Addon extends Model
protected $table = 'product_addon';
protected $fillable = [
'type_id',
'type',
'code',
'name',
'price',
@@ -20,7 +21,13 @@ class Addon extends Model
'max',
'step',
'prefix',
'input_type',
'status',
'sort_order'
];
protected $casts = [
'type' => AddonType::class,
'status' => 'boolean',
];
}