This commit is contained in:
pbao9
2026-02-04 11:08:30 +07:00
parent 0b29522bd2
commit 8e4e0f33e3
9 changed files with 156 additions and 110 deletions
+2 -2
View File
@@ -18,8 +18,8 @@ enum Status: int
public function badge(): string public function badge(): string
{ {
return match ($this) { return match ($this) {
self::DEACTIVE => '<span class="badge badge-danger">Không hoạt động</span>', self::DEACTIVE => '<span class="badge bg-danger">Không hoạt động</span>',
self::ACTIVE => '<span class="badge badge-success">Hoạt động</span>', self::ACTIVE => '<span class="badge bg-success">Hoạt động</span>',
}; };
} }
@@ -18,12 +18,14 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use Yajra\DataTables\Facades\DataTables;
class ModulevCenterController extends Controller class ModulevCenterController extends Controller
{ {
public function __construct( public function __construct(
public vCenterRepositoryInterface $vCenterRepository public vCenterRepositoryInterface $vCenterRepository
) {} ) {
}
// show all hosting module // show all hosting module
public function index(Request $request) public function index(Request $request)
{ {
@@ -37,7 +39,7 @@ class ModulevCenterController extends Controller
} }
public function import(Request $request) public function import(Request $request)
{ {
if (! $request->file) { if (!$request->file) {
return back()->withErrors("Please choose your file"); return back()->withErrors("Please choose your file");
} }
$data = Excel::toArray([], $request->file); $data = Excel::toArray([], $request->file);
@@ -55,7 +57,7 @@ class ModulevCenterController extends Controller
} }
} }
if (! isset($allData) || (isset($allData) && count($allData) < 1)) { if (!isset($allData) || (isset($allData) && count($allData) < 1)) {
return back()->withErrors('No data imported'); return back()->withErrors('No data imported');
} }
@@ -86,7 +88,7 @@ class ModulevCenterController extends Controller
DB::beginTransaction(); DB::beginTransaction();
try { try {
$data = $request->all(); $data = $request->all();
$data['password'] = SignRSA::EncryptFunc($request->password); $data['password'] = SignRSA::EncryptFunc($request->password);
$this->vCenterRepository->create($data); $this->vCenterRepository->create($data);
DB::commit(); DB::commit();
} catch (\Exception $error) { } catch (\Exception $error) {
@@ -98,14 +100,16 @@ class ModulevCenterController extends Controller
public function edit(int $id = 0) public function edit(int $id = 0)
{ {
$server = $this->vCenterRepository->find($id); $server = $this->vCenterRepository->find($id);
if (! $server) return back()->withErrors('Can\'t find server by id ' . $id); if (!$server)
return back()->withErrors('Can\'t find server by id ' . $id);
$title = 'View vCenter information'; $title = 'View vCenter information';
return view('administrator.pages.module.vcenter.edit', compact('title', 'server')); return view('administrator.pages.module.vcenter.edit', compact('title', 'server'));
} }
public function update(UpdatevCenterRequest $request, int $id = 0) public function update(UpdatevCenterRequest $request, int $id = 0)
{ {
$server = $this->vCenterRepository->find($id); $server = $this->vCenterRepository->find($id);
if (! $server) return back()->withErrors('Invalid Request'); if (!$server)
return back()->withErrors('Invalid Request');
$request->validated(); $request->validated();
if ($request->get('host') != $server->name) { if ($request->get('host') != $server->name) {
$isExitsHost = $this->vCenterRepository->findByName($request->get('host')); $isExitsHost = $this->vCenterRepository->findByName($request->get('host'));
@@ -116,8 +120,8 @@ class ModulevCenterController extends Controller
DB::beginTransaction(); DB::beginTransaction();
try { try {
$data = $request->all(); $data = $request->all();
if (! empty($request->get('password'))) { if (!empty($request->get('password'))) {
$data['password'] = SignRSA::EncryptFunc($request->password); $data['password'] = SignRSA::EncryptFunc($request->password);
} }
$this->vCenterRepository->update($id, $data); $this->vCenterRepository->update($id, $data);
DB::commit(); DB::commit();
@@ -131,7 +135,7 @@ class ModulevCenterController extends Controller
public function destroy(int $id = 0) public function destroy(int $id = 0)
{ {
$server = $this->vCenterRepository->find($id); $server = $this->vCenterRepository->find($id);
if (! $server) { if (!$server) {
return back()->withErrors('Invalid Request'); return back()->withErrors('Invalid Request');
} }
DB::beginTransaction(); DB::beginTransaction();
@@ -145,4 +149,36 @@ class ModulevCenterController extends Controller
} }
return redirect()->route('module.vcenter.index')->with('success', 'Deleted successfully.'); return redirect()->route('module.vcenter.index')->with('success', 'Deleted successfully.');
} }
public function datatable(Request $request)
{
$query = $this->vCenterRepository->filter(
$request->all(),
[],
[],
['*'],
true
);
return DataTables::eloquent($query)
->addColumn(
'action',
function ($d) {
return view('administrator.pages.module.vcenter.datatable.action', ['row' => $d])->render();
}
)
->addColumn(
'status',
function ($d) {
return view('administrator.pages.module.vcenter.datatable.status', ['row' => $d])->render();
}
)
->filterColumn('v_center.host', function ($query, $keyword) {
$query->whereHas('vCenter', function ($q) use ($keyword) {
$q->where('host', 'like', "%{$keyword}%");
});
})
->rawColumns(['action', 'status'])
->make(true);
}
} }
@@ -16,19 +16,32 @@ class vCenterCachedRepository extends BaseCachedRepository implements vCenterRep
{ {
return $this->vCenterRepository->getModel(); return $this->vCenterRepository->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->vCenterRepository
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([ $cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [ 'filter' => compact('data', 'relationships', 'withCount', 'select')
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
]))); ])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->vCenterRepository->filter($data, $relationships, $withCount, $select); return Cache::remember(
}); $cacheKey,
$this->cacheTime,
fn() =>
$this->vCenterRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
} }
public function findByName(string $name = '') public function findByName(string $name = '')
{ {
@@ -37,7 +50,7 @@ class vCenterCachedRepository extends BaseCachedRepository implements vCenterRep
'name' => $name 'name' => $name
] ]
]))); ])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($name){ return Cache::remember($cacheKey, $this->cacheTime, function () use ($name) {
return $this->vCenterRepository->findByName($name); return $this->vCenterRepository->findByName($name);
}); });
} }
+27 -11
View File
@@ -11,26 +11,42 @@ class vCenterRepository extends BaseRepository implements vCenterRepositoryInter
{ {
return vCenter::class; return vCenter::class;
} }
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) public function filter(
{ array $data = [],
$model = $this->model->with($relationships)->select($select); array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
$query = $this->model
->select($select)
->with($relationships);
if (isset($data['status'])) { // if (!empty($data['vcenter_id'])) {
$model = $model->where("status", $data['status']); // $query->where('vcenter_id', $data['vcenter_id']);
// }
if (!empty($withCount)) {
$query->withCount($withCount);
} }
if (count($withCount) > 0) {
$model = $model->withCount($withCount); if ($returnQuery) {
return $query;
} }
if(isset($data['page'])) {
return $model->paginate(15); if (!empty($data['paginate'])) {
return $query->paginate(
$data['per_page'] ?? 15
);
} }
return $model->get();
return $query->get();
} }
public function findByName(string $name = '') public function findByName(string $name = '')
{ {
if (empty($name)) { if (empty($name)) {
return false; return false;
} }
return $this->model->where('host','LIKE',"%$name%")->first(); return $this->model->where('host', 'LIKE', "%$name%")->first();
} }
} }
@@ -3,7 +3,14 @@ namespace App\Repositories\vCenter;
use App\Repositories\BaseRepositoryInterface; use App\Repositories\BaseRepositoryInterface;
interface vCenterRepositoryInterface extends BaseRepositoryInterface { interface vCenterRepositoryInterface 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 findByName(string $name = ''); public function findByName(string $name = '');
} }
@@ -0,0 +1,2 @@
<x-button-crm type="button" variant="primary" size="sm" route="module.vcenter.edit" :params="['id' => $row->id]" icon="pencil">Edit</x-button-crm>
<x-button-crm type="button" variant="danger" size="sm" route="module.vcenter.destroy" :params="['id' => $row->id]" icon="trash">Delete</x-button-crm>
@@ -0,0 +1,9 @@
@php
$status = App\Enums\Status::tryFrom($row->status);
@endphp
@if($status)
{!! $status->badge() !!}
@else
<span class="badge badge-secondary">Unknown</span>
@endif
@@ -1,5 +1,20 @@
@php use App\Http\Helpers\Helper; @endphp
@extends('administrator.layout.master') @extends('administrator.layout.master')
@push('plugin-styles')
@foreach (config('assets.datatable.css') as $datatable)
<link href="{{ asset($datatable) }}" rel="stylesheet" />
@endforeach
@endpush
@section('breadcrumb')
@php
$breadcrumbItems = [
['label' => 'Modules'],
['label' => 'Package vCenter'],
['label' => 'Datacenter', 'active' => true],
];
@endphp
<x-admin-breadcrumb :items="$breadcrumbItems" />
@endsection
@section('content') @section('content')
@if(auth()->guard('admins')->user()->can("manager module vcenter")) @if(auth()->guard('admins')->user()->can("manager module vcenter"))
<div class="card card-primary card-outline"> <div class="card card-primary card-outline">
@@ -7,76 +22,23 @@
@include("administrator.pages.block.ModuleVCenter") @include("administrator.pages.block.ModuleVCenter")
</div> </div>
<div class="card-body"> <div class="card-body">
<table class="table table-invoice table-bordered"> <x-datatable id="vcenterTable" ajax="{{ route('module.vcenter.datatable') }}" :columns="[
<thead> ['key' => 'host', 'label' => 'Host'],
<tr> ['key' => 'user', 'label' => 'User'],
<th> ['key' => 'status', 'label' => 'Status'],
<label> ['key' => 'action', 'label' => 'Action'],
<input type="checkbox" name="check" class="check-all"> ]" />
</label> </div>
</th> </div>
<th>IP Host</th>
<th>Username</th>
<th>Status</th>
<th>Last Updated</th>
<th style="width:40px"></th>
<th style="width: 40px"></th>
</tr>
</thead>
<tbody>
@if(count($modules) > 0)
@foreach($modules as $item)
<tr>
<td class="text-center">
<label for=""></label><input type="checkbox" name="id[]" value="{{ $item->id }}"
id="">
</td>
<td class="text-center">
{{ $item->host }}
</td>
<td class="text-center">
{{ $item->user }}
</td>
<td class="text-center">
{!! $item->getStatus() !!}
</td>
<td class="text-center">
@time($item->updated_at)
</td>
<td>
<a class="btn btn-warning btn-sm"
href="{{ route('module.vcenter.edit',['id' => $item->id]) }}">
<i class="fas fa-user-edit"></i>
</a>
</td>
<td>
@if(auth()->guard('admins')->user()->can("delete module vcenter"))
<form method="post"
class="d-inline"
action="{{ route('module.vcenter.destroy',['id' => $item->id]) }}">
@method('delete')
@csrf
<button type="submit" data-toggle="tooltip" data-placement="top" title=""
class="btn btn-sm btn-danger" data-original-title="Xoá">
<i class="fas fa-trash"></i>
</button>
</form>
@endif
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
</div>
<div class="card-footer">
{{ $modules->withQueryString()->links() }}
</div>
</div>
@else @else
<div class="alert alert-danger alert-dismissible"> <div class="alert alert-danger alert-dismissible">
<h5 class="mb-0"><i class="icon fas fa-ban"></i> Access Denied - You are not assigned to any Module vCenter <h5 class="mb-0"><i class="icon fas fa-ban"></i> Access Denied - You are not assigned to any Module vCenter
management department</h5> management department</h5>
</div> </div>
@endif @endif
@endsection @endsection
@push('plugin-scripts')
@foreach (config('assets.datatable.js') as $datatable)
<script src="{{ asset($datatable) }}"></script>
@endforeach
@endpush
+2 -1
View File
@@ -194,7 +194,7 @@ Route::prefix('administrator')->group(function () {
}); });
//Module vCenter Server //Module vCenter Server
Route::controller(ModulevCenterController::class)->group(function () { Route::controller(ModulevCenterController::class)->prefix('vcenter')->group(function () {
Route::get('list-all-vcenter', 'index')->name('module.vcenter.index'); Route::get('list-all-vcenter', 'index')->name('module.vcenter.index');
Route::post('import-vcenter-from-csv', 'import')->name('module.vcenter.import'); Route::post('import-vcenter-from-csv', 'import')->name('module.vcenter.import');
Route::get('add-new-vcenter', 'create')->name('module.vcenter.create'); Route::get('add-new-vcenter', 'create')->name('module.vcenter.create');
@@ -202,6 +202,7 @@ Route::prefix('administrator')->group(function () {
Route::get('view-vcenter/{id}', 'edit')->name('module.vcenter.edit'); Route::get('view-vcenter/{id}', 'edit')->name('module.vcenter.edit');
Route::put('view-vcenter/{id}', 'update')->name('module.vcenter.update'); Route::put('view-vcenter/{id}', 'update')->name('module.vcenter.update');
Route::delete('remove-vcenter/{id}', 'destroy')->name('module.vcenter.destroy'); Route::delete('remove-vcenter/{id}', 'destroy')->name('module.vcenter.destroy');
Route::get('datatable', 'datatable')->name('module.vcenter.datatable');
}); });