diff --git a/app/Enums/Status.php b/app/Enums/Status.php new file mode 100644 index 0000000..723e527 --- /dev/null +++ b/app/Enums/Status.php @@ -0,0 +1,33 @@ + 'Không hoạt động', + self::ACTIVE => 'Hoạt động', + }; + } + + public function badge(): string + { + return match ($this) { + self::DEACTIVE => 'Không hoạt động', + self::ACTIVE => 'Hoạt động', + }; + } + + public static function getOptions(): array + { + return [ + self::DEACTIVE->value => self::DEACTIVE->label(), + self::ACTIVE->value => self::ACTIVE->label(), + ]; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/admin/package/Group.php b/app/Http/Controllers/admin/package/Group.php index f5496ea..c3c5c97 100644 --- a/app/Http/Controllers/admin/package/Group.php +++ b/app/Http/Controllers/admin/package/Group.php @@ -16,6 +16,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; use Maatwebsite\Excel\Facades\Excel; +use Yajra\DataTables\Facades\DataTables; class Group extends Controller { @@ -33,7 +34,7 @@ class Group extends Controller ); $title = 'List all group Os for all datastore'; - return view('admin.package.group.index', compact('group', 'title')); + return view('administrator.pages.package.group.index', compact('group', 'title')); } public function import(Request $request) { @@ -73,7 +74,7 @@ class Group extends Controller { $title = 'Add new Group OS'; - return view('admin.package.group.add', compact('title')); + return view('administrator.pages.package.group.add', compact('title')); } public function store(OsGroupRequest $request) { @@ -98,7 +99,7 @@ class Group extends Controller } $title = 'Edit Group OS'; - return view('admin.package.group.edit', compact('title', 'group')); + return view('administrator.pages.package.group.edit', compact('title', 'group')); } public function update(int $id = 0, OsGroupRequest $request) { @@ -134,4 +135,50 @@ class Group extends Controller return redirect()->route('package.group.index')->with('success', 'Delete Group success'); } + + + public function datatable(Request $request) + { + $query = $this->osGroupRepository->filter( + $request->all(), + ['host'], + [], + ['*'], + true + ); + + return DataTables::eloquent($query) + ->editColumn('thumbnail', function ($d) { + if (!$d->thumbnail) { + return '-'; + } + + return '' . e($d->name) . ''; + }) + ->editColumn('module_host_id', function ($d) { + if ($d->host) { + return $d->host->name . ' (Host ID: ' . $d->host->host_id . ')'; + } + return '-'; + }) + ->editColumn('status', function ($d) { + return $d->getStatus(); + }) + ->addColumn('action', function ($d) { + return ' + Edit + '; + }) + ->filterColumn('host', function ($query, $keyword) { + $query->whereHas('host', function ($q) use ($keyword) { + $q->where('host', 'like', "%{$keyword}%"); + }); + }) + ->rawColumns(['thumbnail', 'action', 'status']) + ->make(true); + } } diff --git a/app/Http/Controllers/admin/package/OsVersion.php b/app/Http/Controllers/admin/package/OsVersion.php index 8cb5341..8013224 100644 --- a/app/Http/Controllers/admin/package/OsVersion.php +++ b/app/Http/Controllers/admin/package/OsVersion.php @@ -28,13 +28,13 @@ class OsVersion extends Controller ], ['osGroup.host']); $title = 'List all Version OS'; - return view('admin.package.version.index', compact('versions', 'title')); + return view('administrator.pages.package.version.index', compact('versions', 'title')); } public function create() { $title = 'Add new Version Os'; - return view('admin.package.version.add', compact('title')); + return view('administrator.pages.package.version.add', compact('title')); } public function store(OsVersionRequest $request) { @@ -65,7 +65,7 @@ class OsVersion extends Controller $title = "Edit version OS"; - return view('admin.package.version.edit', compact('title', 'version')); + return view('administrator.pages.package.version.edit', compact('title', 'version')); } public function update(int $id = 0, OsVersionRequest $request) { diff --git a/app/Repositories/OsGroup/OsGroupCachedRepository.php b/app/Repositories/OsGroup/OsGroupCachedRepository.php index ed8fbca..69e5212 100644 --- a/app/Repositories/OsGroup/OsGroupCachedRepository.php +++ b/app/Repositories/OsGroup/OsGroupCachedRepository.php @@ -15,21 +15,34 @@ class OsGroupCachedRepository extends BaseCachedRepository implements OsGroupRep { return $this->osGroupRepository->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 || + !empty($data['paginate']) + ) { + return $this->osGroupRepository + ->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->osGroupRepository->filter($data, $relationships, $withCount, $select); - }); + + return Cache::remember( + $cacheKey, + $this->cacheTime, + fn() => + $this->osGroupRepository + ->filter($data, $relationships, $withCount, $select, $returnQuery) + ); } - public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []) + public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = []) { $cacheKey = $this->getCacheKey(md5(json_encode([ 'findWithRelationships' => [ @@ -37,8 +50,8 @@ class OsGroupCachedRepository extends BaseCachedRepository implements OsGroupRep 'relationships' => $relationships ] ]))); - return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) { - return $this->osGroupRepository->findWithRelationships($id, $relationships,$withCount,$withSum); + return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships, $withCount, $withSum) { + return $this->osGroupRepository->findWithRelationships($id, $relationships, $withCount, $withSum); }); } } diff --git a/app/Repositories/OsGroup/OsGroupRepository.php b/app/Repositories/OsGroup/OsGroupRepository.php index a8baec0..211b8c8 100644 --- a/app/Repositories/OsGroup/OsGroupRepository.php +++ b/app/Repositories/OsGroup/OsGroupRepository.php @@ -11,20 +11,36 @@ class OsGroupRepository extends BaseRepository implements OsGroupRepositoryInter { return OsGroup::class; } - public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) - { - $model = $this->model->with($relationships)->select($select); - - if (isset($data['status'])) { - $model = $model->where("status", $data['status']); + public function filter( + array $data = [], + array $relationships = [], + array $withCount = [], + array $select = ['*'], + bool $returnQuery = false + ) { + $query = $this->model + ->select($select) + ->with($relationships); + + if (!empty($data['module_host_id'])) { + $query->where('module_host_id', $data['module_host_id']); } - if (count($withCount) > 0) { - $model = $model->withCount($withCount); + + if (!empty($withCount)) { + $query->withCount($withCount); } - if(isset($data['page'])) { - return $model->paginate(15); + + if ($returnQuery) { + return $query; } - return $model->get(); + + if (!empty($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/OsGroup/OsGroupRepositoryInterface.php b/app/Repositories/OsGroup/OsGroupRepositoryInterface.php index 6415bc0..347aaf7 100644 --- a/app/Repositories/OsGroup/OsGroupRepositoryInterface.php +++ b/app/Repositories/OsGroup/OsGroupRepositoryInterface.php @@ -4,8 +4,15 @@ namespace App\Repositories\OsGroup; use App\Repositories\BaseRepositoryInterface; -interface OsGroupRepositoryInterface extends BaseRepositoryInterface{ - public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']); - public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []); +interface OsGroupRepositoryInterface extends BaseRepositoryInterface +{ + 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 = []); } \ No newline at end of file diff --git a/database/migrations/2024_02_02_194120_os__group.php b/database/migrations/2024_02_02_194120_os__group.php index 9425c59..5694184 100644 --- a/database/migrations/2024_02_02_194120_os__group.php +++ b/database/migrations/2024_02_02_194120_os__group.php @@ -17,7 +17,7 @@ return new class extends Migration $table->id()->autoIncrement(); $table->string('name')->nullable(); $table->string('thumbnail')->nullable(); - $table->bigInteger('module_host_id')->default(0)->index(); + $table->foreignId('module_host_id')->constrained('module_host')->index()->nullable(); $table->tinyInteger('status')->default(1); $table->timestamps(); }); diff --git a/resources/views/administrator/pages/package/group/index.blade.php b/resources/views/administrator/pages/package/group/index.blade.php index e63a7c9..294fa96 100644 --- a/resources/views/administrator/pages/package/group/index.blade.php +++ b/resources/views/administrator/pages/package/group/index.blade.php @@ -1,79 +1,32 @@ -@php use App\Http\Helpers\Helper; @endphp -@extends('admin.main') +@extends('administrator.layout.master') +@push('plugin-styles') + +@endpush + +@section('breadcrumb') + @php + $breadcrumbItems = [ + ['label' => 'Modules'], + ['label' => 'Package vCenter'], + ['label' => 'Datacenter', 'active' => true], + ]; + @endphp + +@endsection @section('content') - @if(auth()->guard('admins')->user()->can("manager package module")) + @if (auth()->guard('admins')->user()->can('manager package module'))
- @include("admin.block.Group") + @include('administrator.pages.block.Group')
- - - - - - - - - - - - - - @if($group->count()) - @foreach($group->all() as $item) - - - - - - - - - - @endforeach - @endif - -
- - Group NameThumbnailHost NameStatus
- - - {{ $item->name }} - - {{ $item->name }} - - @isset( $item->host->name) - {{$item->host->name}} - @endisset - - {!! $item->getStatus() !!} - - - - - -
- @method('delete') - @csrf - -
-
-
-
@else @@ -83,3 +36,9 @@ @endif @endsection +@push('plugin-scripts') + + + + +@endpush diff --git a/routes/admin.php b/routes/admin.php index 838b623..36549c1 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -292,6 +292,7 @@ Route::prefix('administrator')->group(function () { Route::get('edit/{id}', 'edit')->name('edit'); Route::put('edit-group/{id}', 'update')->name('update'); Route::delete('remove-group/{id}', 'destroy')->name('destroy'); + Route::get('datatable', 'datatable')->name('datatable'); }); Route::group([ 'prefix' => 'version',