185 lines
6.2 KiB
PHP
185 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Facade\SignRSA;
|
|
use App\Http\Requests\vCenter\CreatevCenterRequest;
|
|
use App\Http\Requests\vCenter\UpdatevCenterRequest;
|
|
use App\Http\Services\Upload;
|
|
use App\Imports\ModuleDNSImport;
|
|
use App\Imports\vCenterImport;
|
|
use App\Models\vCenter;
|
|
use App\Repositories\vCenter\vCenterRepositoryInterface;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
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 ModulevCenterController extends Controller
|
|
{
|
|
public function __construct(
|
|
public vCenterRepositoryInterface $vCenterRepository
|
|
) {
|
|
}
|
|
// show all hosting module
|
|
public function index(Request $request)
|
|
{
|
|
$modules = $this->vCenterRepository->filter(
|
|
[
|
|
'page' => $request->get('page') ?? 1
|
|
]
|
|
);
|
|
$title = 'List all vCenter';
|
|
return view('administrator.pages.module.vcenter.index', compact('modules', 'title'));
|
|
}
|
|
public function import(Request $request)
|
|
{
|
|
if (!$request->file) {
|
|
return back()->withErrors("Please choose your file");
|
|
}
|
|
$data = Excel::toArray([], $request->file);
|
|
if (count($data) > 0) {
|
|
$data = array_slice($data[0], 1);
|
|
$allData = [];
|
|
foreach ($data as $row) {
|
|
if (isset($row[1]) && isset($row[2]) && isset($row[3]) && !empty($row[1]) && !empty($row[1]) && !empty($row[1])) {
|
|
$allData[] = [
|
|
'host' => $row[1],
|
|
'user' => $row[2],
|
|
'password' => SignRSA::EncryptFunc($row[3])
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!isset($allData) || (isset($allData) && count($allData) < 1)) {
|
|
return back()->withErrors('No data imported');
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->vCenterRepository->insert($allData);
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return back()->with('success', 'Imported successfully');
|
|
}
|
|
public function create()
|
|
{
|
|
$title = 'Add new vCenter';
|
|
return view('administrator.pages.module.vcenter.add', compact('title'));
|
|
}
|
|
public function store(CreatevCenterRequest $request)
|
|
{
|
|
$request->validated();
|
|
|
|
$isExitsHost = $this->vCenterRepository->findByName($request->get('host'));
|
|
if ($isExitsHost) {
|
|
return back()->withErrors('The server already exists');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$data = $request->all();
|
|
$data['password'] = SignRSA::EncryptFunc($request->password);
|
|
$this->vCenterRepository->create($data);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('module.vcenter.index')->with('success', 'Added Successfully');
|
|
}
|
|
public function edit(int $id = 0)
|
|
{
|
|
$server = $this->vCenterRepository->find($id);
|
|
if (!$server)
|
|
return back()->withErrors('Can\'t find server by id ' . $id);
|
|
$title = 'View vCenter information';
|
|
return view('administrator.pages.module.vcenter.edit', compact('title', 'server'));
|
|
}
|
|
public function update(UpdatevCenterRequest $request, int $id = 0)
|
|
{
|
|
$server = $this->vCenterRepository->find($id);
|
|
if (!$server)
|
|
return back()->withErrors('Invalid Request');
|
|
$request->validated();
|
|
if ($request->get('host') != $server->name) {
|
|
$isExitsHost = $this->vCenterRepository->findByName($request->get('host'));
|
|
if ($isExitsHost) {
|
|
return back()->withErrors('The server already exists');
|
|
}
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$data = $request->all();
|
|
if (!empty($request->get('password'))) {
|
|
$data['password'] = SignRSA::EncryptFunc($request->password);
|
|
}
|
|
$this->vCenterRepository->update($id, $data);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('module.vcenter.index')->with('success', 'Updated Successfully');
|
|
}
|
|
|
|
public function destroy(int $id = 0)
|
|
{
|
|
$server = $this->vCenterRepository->find($id);
|
|
if (!$server) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
|
|
$this->vCenterRepository->delete($id);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
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);
|
|
}
|
|
}
|