diff --git a/app/Http/Controllers/admin/package/Host.php b/app/Http/Controllers/admin/package/Host.php index aeb53f3..50ecb3f 100644 --- a/app/Http/Controllers/admin/package/Host.php +++ b/app/Http/Controllers/admin/package/Host.php @@ -14,21 +14,74 @@ use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; +use Yajra\DataTables\Facades\DataTables; class Host extends Controller { public function __construct( public HostRepositoryInterface $hostRepository, public DatacenterRepositoryInterface $datacenterRepository - ) {} + ) { + } public function index(Request $request) { - $hosts = $this->hostRepository->filter([ - 'page' => $request->get('page') ?? 1 - ], ['datacenter.vCenter']); $title = 'List all Host'; - return view("admin.package.host.index", compact('hosts', 'title')); + return view("admin.package.host.index", compact('title')); + } + + public function datatable(Request $request) + { + $query = $this->hostRepository->filter( + $request->all(), + ['datacenter.vCenter'], + [], + ['*'], + true + ); + + return DataTables::eloquent($query) + ->editColumn('updated_at', function ($d) { + return $d->updated_at ? $d->updated_at->format('d/m/Y') : ''; + }) + ->addColumn('action', function ($d) { + $editUrl = route('package.host.edit', ['id' => $d->id]); + $deleteBtn = ''; + + if (auth()->guard('admins')->user()->can("delete module vcenter")) { + $deleteUrl = route('package.host.destroy', ['id' => $d->id]); + $csrf = csrf_field(); + $method = method_field('delete'); + $deleteBtn = << + $method + $csrf + + +HTML; + } + + return << + + + $deleteBtn +HTML; + }) + ->filterColumn('datacenter.name', function ($query, $keyword) { + $query->whereHas('datacenter', function ($q) use ($keyword) { + $q->where('name', 'like', "%{$keyword}%"); + }); + }) + ->filterColumn('datacenter.v_center.host', function ($query, $keyword) { + $query->whereHas('datacenter.vCenter', function ($q) use ($keyword) { + $q->where('host', 'like', "%{$keyword}%"); + }); + }) + ->rawColumns(['action']) + ->make(true); } public function sync(SyncHost $syncHost) { @@ -45,7 +98,7 @@ class Host extends Controller { $request->validated(); $datacenter = $this->datacenterRepository->find($request->get('module_datacenter_id')); - if (! $datacenter) { + if (!$datacenter) { return back()->withErrors('Invalid Request'); } $isCreated = $this->hostRepository->findBy('host_id', $request->get('host_id')); @@ -65,19 +118,19 @@ class Host extends Controller public function edit(int $id = 0) { $host = $this->hostRepository->findWithRelationships($id, ['datacenter']); - if (! $host) { + if (!$host) { return back()->withErrors('Invalid Request'); } $title = "View Host Information"; - return view('admin.package.host.edit', compact('host', 'title')); + return view('admin.package.host.edit', compact('host', 'title')); } public function update(int $id = 0, HostRequest $request) { $request->validated(); $host = $this->hostRepository->find($id); - if (! $host) { + if (!$host) { return redirect()->route('package.host.index')->withErrors('Invalid Request'); } if ($request->get('host_id') !== $host->host_id) { @@ -100,7 +153,7 @@ class Host extends Controller { $host = $this->hostRepository->find($id); - if (! $host) { + if (!$host) { return back()->withErrors('Invalid request.'); } diff --git a/app/Repositories/Module/Host/HostCachedRepository.php b/app/Repositories/Module/Host/HostCachedRepository.php index 9f6af17..ca5dd64 100644 --- a/app/Repositories/Module/Host/HostCachedRepository.php +++ b/app/Repositories/Module/Host/HostCachedRepository.php @@ -16,18 +16,23 @@ class HostCachedRepository extends BaseCachedRepository implements HostRepositor { return $this->hostRepository->getModel(); } - public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) - { + public function filter( + array $data = [], + array $relationships = [], + array $withCount = [], + array $select = ['*'], + bool $returnQuery = false + ) { + if ($returnQuery || isset($data['page']) || isset($data['paginate'])) { + return $this->hostRepository->filter($data, $relationships, $withCount, $select, $returnQuery); + } + $cacheKey = $this->getCacheKey(md5(json_encode([ - 'filterItem' => [ - 'data' => $data, - 'relationships' => $relationships, - 'select' => $select, - 'withCount' => $withCount - ] + 'filter' => compact('data', 'relationships', 'withCount', 'select') ]))); - return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) { - return $this->hostRepository->filter($data, $relationships, $withCount, $select); + + return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount, $returnQuery) { + return $this->hostRepository->filter($data, $relationships, $withCount, $select, $returnQuery); }); } public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []) diff --git a/app/Repositories/Module/Host/HostRepository.php b/app/Repositories/Module/Host/HostRepository.php index bff34fb..f778ab9 100644 --- a/app/Repositories/Module/Host/HostRepository.php +++ b/app/Repositories/Module/Host/HostRepository.php @@ -12,23 +12,32 @@ class HostRepository extends BaseRepository implements HostRepositoryInterface return ModuleHost::class; } - public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) - { - $model = $this->model->with($relationships)->select($select); + public function filter( + array $data = [], + array $relationships = [], + array $withCount = [], + array $select = ['*'], + bool $returnQuery = false + ) { + $query = $this->model->with($relationships)->select($select); if (isset($data['module_datacenter_id']) && $data['module_datacenter_id']) { - $model = $model->where("module_datacenter_id", $data['module_datacenter_id']); + $query = $query->where("module_datacenter_id", $data['module_datacenter_id']); } if (count($withCount) > 0) { - $model = $model->withCount($withCount); + $query = $query->withCount($withCount); } - if (isset($data['page'])) { - return $model->paginate(15); + if ($returnQuery) { + return $query; } - return $model->get(); + if (isset($data['page']) || isset($data['paginate'])) { + return $query->paginate($data['per_page'] ?? 15); + } + + return $query->get(); } public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = []) diff --git a/app/Repositories/Module/Host/HostRepositoryInterface.php b/app/Repositories/Module/Host/HostRepositoryInterface.php index f0e9382..9d0842f 100644 --- a/app/Repositories/Module/Host/HostRepositoryInterface.php +++ b/app/Repositories/Module/Host/HostRepositoryInterface.php @@ -4,7 +4,13 @@ namespace App\Repositories\Module\Host; use App\Repositories\BaseRepositoryInterface; interface HostRepositoryInterface extends BaseRepositoryInterface{ - public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']); + public function filter( + array $data = [], + array $relationships = [], + array $withCount = [], + array $select = ['*'], + bool $returnQuery = false + ); public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []); public function findBy(string $key = '',string|int|null $value = '' ); diff --git a/database/migrations/2024_02_01_090134_module_datacenter.php b/database/migrations/2024_02_01_090134_module_datacenter.php index da4b5dc..efec5c6 100644 --- a/database/migrations/2024_02_01_090134_module_datacenter.php +++ b/database/migrations/2024_02_01_090134_module_datacenter.php @@ -16,7 +16,7 @@ return new class extends Migration { $table->id()->autoIncrement(); $table->string('name', 255)->nullable(); $table->string('datacenter_id', 255)->nullable(); - $table->bigInteger('vcenter_id')->default(0)->index(); + $table->foreignId('vcenter_id')->constrained('module_vcenter')->cascadeOnDelete()->nullable()->index(); $table->integer('status')->default(0); $table->timestamps(); }); diff --git a/database/migrations/2024_02_01_090537_module_host.php b/database/migrations/2024_02_01_090537_module_host.php index 5c40e23..2c60e6a 100644 --- a/database/migrations/2024_02_01_090537_module_host.php +++ b/database/migrations/2024_02_01_090537_module_host.php @@ -18,7 +18,7 @@ return new class extends Migration $table->string('name',100)->nullable(); $table->string('host_id',255)->nullable(); $table->string('template',255)->nullable(); - $table->bigInteger('module_datacenter_id')->default(0)->index(); + $table->foreignId('module_datacenter_id')->constrained('module_datacenter')->cascadeOnDelete()->nullable()->index(); $table->string('console',255)->nullable(); $table->string('iso_path',255)->nullable(); $table->timestamps(); diff --git a/database/migrations/2026_01_29_120327_create_product_v_m_templates_table.php b/database/migrations/2026_01_29_120327_create_product_v_m_templates_table.php index 2e0fabf..3bfa2ab 100644 --- a/database/migrations/2026_01_29_120327_create_product_v_m_templates_table.php +++ b/database/migrations/2026_01_29_120327_create_product_v_m_templates_table.php @@ -11,10 +11,10 @@ return new class extends Migration */ public function up(): void { - Schema::create('product_vm_templates', function (Blueprint $table) { - $table->foreignId('product_id')->constrained('products')->cascadeOnDelete(); - $table->foreignId('vm_template_id')->constrained('vm_templates')->cascadeOnDelete(); - }); + // Schema::create('product_vm_templates', function (Blueprint $table) { + // $table->foreignId('product_id')->constrained('products')->cascadeOnDelete(); + // $table->foreignId('vm_template_id')->constrained('vm_templates')->cascadeOnDelete(); + // }); } /** diff --git a/database/migrations/2026_01_30_170136_update_module_datacenter_id_in_module_host_table.php b/database/migrations/2026_01_30_170136_update_module_datacenter_id_in_module_host_table.php new file mode 100644 index 0000000..4d83c15 --- /dev/null +++ b/database/migrations/2026_01_30_170136_update_module_datacenter_id_in_module_host_table.php @@ -0,0 +1,37 @@ +unsignedBigInteger('module_datacenter_id')->nullable()->change(); + + $table->foreign('module_datacenter_id') + ->references('id') + ->on('module_datacenter') + ->cascadeOnDelete(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('module_host', function (Blueprint $table) { + // Drop the foreign key first + $table->dropForeign(['module_datacenter_id']); + + // Revert column type (assuming it was bigInteger with default 0 based on previous context) + $table->bigInteger('module_datacenter_id')->default(0)->change(); + }); + } +}; diff --git a/database/migrations/2026_01_30_170804_update_module_vcenter_id_in_module_datacenter.php b/database/migrations/2026_01_30_170804_update_module_vcenter_id_in_module_datacenter.php new file mode 100644 index 0000000..02618c0 --- /dev/null +++ b/database/migrations/2026_01_30_170804_update_module_vcenter_id_in_module_datacenter.php @@ -0,0 +1,33 @@ +unsignedBigInteger('vcenter_id')->nullable()->change(); + + $table->foreign('vcenter_id') + ->references('id') + ->on('module_vcenter') + ->cascadeOnDelete(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('module_datacenter', function (Blueprint $table) { + // + }); + } +}; diff --git a/resources/views/admin/package/host/add.blade.php b/resources/views/admin/package/host/add.blade.php index be510bc..e51c68f 100644 --- a/resources/views/admin/package/host/add.blade.php +++ b/resources/views/admin/package/host/add.blade.php @@ -1,51 +1,76 @@ -@extends("admin.main") -@section("content") -@if(auth()->guard('admins')->user()->can("add module vcenter")) -
-
- @csrf -
- - - {{--Name--}} -
- -
- -
-
- {{--Host ID--}} -
- -
- -
-
- {{--Template ID--}} -
- -
- -
-
- {{--Console ID--}} -
- -
- -
-
-
- -
-
-@else -
-
Access Denied - You are not assigned to any Module vCenter - management department
-
-@endif +@extends('administrator.layout.master') + +@section('breadcrumb') + @php + $breadcrumbItems = [ + ['label' => 'Modules'], + ['label' => 'Package Host'], + ['label' => 'Host', 'active' => true], + ]; + @endphp + @endsection +@push('plugin-styles') + +@endpush +@section("content") + @if(auth()->guard('admins')->user()->can("add module vcenter")) +
+
+ @csrf +
+ + + {{--Name--}} +
+ +
+ +
+
+ {{--Host ID--}} +
+ +
+ +
+
+ {{--Template ID--}} +
+ +
+ +
+
+ {{--Console ID--}} +
+ +
+ +
+
+
+ +
+
+ @else +
+
Access Denied - You are not assigned to any Module vCenter + management department
+
+ @endif +@endsection +@push('plugin-scripts') + + + + +@endpush + +@push('plugin-styles') + +@endpush \ No newline at end of file diff --git a/resources/views/admin/package/host/edit.blade.php b/resources/views/admin/package/host/edit.blade.php index 113c9f9..7b808b0 100644 --- a/resources/views/admin/package/host/edit.blade.php +++ b/resources/views/admin/package/host/edit.blade.php @@ -1,53 +1,82 @@ -@extends("admin.main") -@section("content") -@if(auth()->guard('admins')->user()->can("add module vcenter")) -
-
- @csrf - @method('put') -
- - - - {{--Name--}} -
- -
- -
-
- {{--Host ID--}} -
- -
- -
-
- {{--Template ID--}} -
- -
- -
-
- {{--Console ID--}} -
- -
- -
-
-
- -
-
-@else -
-
Access Denied - You are not assigned to any Module vCenter - management department
-
-@endif +@extends('administrator.layout.master') + +@section('breadcrumb') + @php + $breadcrumbItems = [ + ['label' => 'Modules'], + ['label' => 'Package Host'], + ['label' => 'Host', 'active' => true], + ]; + @endphp + @endsection +@push('plugin-styles') + +@endpush +@section("content") + @if(auth()->guard('admins')->user()->can("add module vcenter")) +
+
+ @csrf + @method('put') +
+ + + + {{--Name--}} +
+ +
+ +
+
+ {{--Host ID--}} +
+ +
+ +
+
+ {{--Template ID--}} +
+ +
+ +
+
+ {{--Console ID--}} +
+ +
+ +
+
+
+ +
+
+ @else +
+
Access Denied - You are not assigned to any Module vCenter + management department
+
+ @endif +@endsection + +@push('plugin-scripts') + + + + +@endpush + +@push('plugin-styles') + +@endpush \ No newline at end of file diff --git a/resources/views/admin/package/host/index.blade.php b/resources/views/admin/package/host/index.blade.php index 3e6cf50..b30f4b1 100644 --- a/resources/views/admin/package/host/index.blade.php +++ b/resources/views/admin/package/host/index.blade.php @@ -1,79 +1,30 @@ -@php use App\Http\Helpers\Helper; @endphp -@extends('admin.main') +@extends('administrator.layout.master') + +@section('breadcrumb') + @php + $breadcrumbItems = [ + ['label' => 'Modules'], + ['label' => 'Package Host'], + ['label' => 'Host', 'active' => true], + ]; + @endphp + +@endsection + @section('content') @if(auth()->guard('admins')->user()->can("manager package module"))
@include("admin.block.Sync.Host")
-
- - - - - - - - - - - - - - @if(count($hosts) > 0) - @foreach($hosts->all() as $item) - - - - - - - - - - @endforeach - @endif - -
- - Host NameDatacenter NamevCenter HostLast Updated
- - - {{ $item->name }} - - @isset($item->datacenter->name) - {{$item->datacenter->name}} - @endisset - - @isset($item->datacenter->vCenter->host) - {{$item->datacenter->vCenter->host}} - @endisset - - @time($item->updated_at) - - - - - - @if(auth()->guard('admins')->user()->can("delete module vcenter")) -
- @method('delete') - @csrf - -
- @endif -
-
- @else @@ -83,3 +34,14 @@
@endif @endsection + +@push('plugin-scripts') + + + + +@endpush + +@push('plugin-styles') + +@endpush \ No newline at end of file diff --git a/resources/views/components/data-table.blade.php b/resources/views/components/data-table.blade.php index 6a80dcb..844f982 100644 --- a/resources/views/components/data-table.blade.php +++ b/resources/views/components/data-table.blade.php @@ -74,6 +74,9 @@ $dtColumns = collect($columns)->map(function ($c) { minimumResultsForSearch: Infinity, width: '60px' }); + if (window.lucide) { + lucide.createIcons(); + } } }); diff --git a/routes/admin.php b/routes/admin.php index 2996908..c1fd5da 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -248,6 +248,7 @@ Route::prefix('administrator')->group(function () { Route::get('edit/{id}', 'edit')->name('edit'); Route::put('edit/{id}', 'update')->name('update'); Route::delete('remove/{id}', 'destroy')->name('destroy'); + Route::get('datatable', 'datatable')->name('datatable'); }); // Network Route::group([ diff --git a/routes/web.php b/routes/web.php index b644f57..ac32ebe 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,7 +19,3 @@ use Yajra\DataTables\DataTables; include __DIR__ . '/admin.php'; include __DIR__ . '/customer.php'; - -Route::get('/users/datatable-test', function () { - return DataTables::of(Customer::query())->make(true); -})->name('admin.users.datatable');