161 lines
4.7 KiB
PHP
161 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Enums\AddonType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\AddOnRequest;
|
|
use App\Models\Addon;
|
|
use App\Repositories\Addon\AddonRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class AddonController extends Controller
|
|
{
|
|
//
|
|
public function __construct(
|
|
public AddonRepositoryInterface $addonRepository
|
|
) {
|
|
}
|
|
public function index(Request $request)
|
|
{
|
|
$addOns = $this->addonRepository->filter(
|
|
[
|
|
'page' => $request->get('page', 1),
|
|
]
|
|
);
|
|
$title = 'List Add-On';
|
|
|
|
return view('administrator.pages.addon.index', compact('addOns', 'title'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$title = 'Add new addon';
|
|
return view('administrator.pages.addon.add', compact('title'));
|
|
}
|
|
|
|
public function store(AddOnRequest $request)
|
|
{
|
|
$request->validated();
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->addonRepository->create($request->all());
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return redirect()->route('addon.index')->with('success', 'Added successfully');
|
|
}
|
|
public function edit($id = 0)
|
|
{
|
|
|
|
$addon = $this->addonRepository->find($id);
|
|
if (!$addon) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
|
|
$title = 'Edit Addon';
|
|
|
|
return view('administrator.pages.addon.edit', compact('title', 'addon'));
|
|
}
|
|
public function update(int $id = 0, AddOnRequest $request)
|
|
{
|
|
$addon = $this->addonRepository->find($id);
|
|
if (!$addon) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
$request->validated();
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->addonRepository->update($id, $request->all());
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return redirect()->route('addon.index')->with('success', 'Updated successfully');
|
|
}
|
|
public function destroy($id)
|
|
{
|
|
$addon = $this->addonRepository->find($id);
|
|
if (!$addon) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->addonRepository->delete($id);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return redirect()->route('addon.index')->with('success', 'Deleted successfully');
|
|
}
|
|
|
|
|
|
public function datatable(Request $request)
|
|
{
|
|
$addons = Addon::orderBy('sort_order')->get();
|
|
|
|
$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>';
|
|
}
|
|
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);
|
|
}
|
|
}
|