- Changed view extends from "admin.main" to "administrator.layout.master" across multiple files. - Updated route names in admin.php and administrator.php to reflect new structure for user, order, invoice, and other management functionalities. - Adjusted view includes to point to the new directory structure for blocks and components. - Ensured all permission checks remain intact while refactoring the views.
142 lines
5.0 KiB
PHP
142 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Http\Facade\SignRSA;
|
|
use App\Http\Requests\ModuleHosting\CreateHostingRequest;
|
|
use App\Http\Requests\ModuleHosting\UpdateHostingRequest;
|
|
use App\Repositories\Module\HostingServer\HostingServerRepositoryInterface;
|
|
use Exception;
|
|
|
|
class ModuleHostingController extends Controller
|
|
{
|
|
public function __construct(
|
|
public HostingServerRepositoryInterface $hostingServerRepository
|
|
) {}
|
|
// show all hosting module
|
|
public function index(Request $request)
|
|
{
|
|
$modules = $this->hostingServerRepository->filter(
|
|
[
|
|
'page' => $request->get('page') ?? 1
|
|
]
|
|
);
|
|
$title = 'List all server hosting';
|
|
return view('administrator.pages.module.hosting.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[] = [
|
|
'name' => $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->hostingServerRepository->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 server hosting';
|
|
return view('administrator.pages.module.hosting.add', compact('title'));
|
|
}
|
|
public function store(CreateHostingRequest $request)
|
|
{
|
|
$request->validated();
|
|
$isExitsHost = $this->hostingServerRepository->findByName($request->get('name'));
|
|
if ($isExitsHost) {
|
|
return back()->withErrors('The server already exists');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$data = $request->all();
|
|
$data['password'] = SignRSA::EncryptFunc($request->password);
|
|
$this->hostingServerRepository->create($data);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('module.hosting.index')->with('success', 'Added successfully');
|
|
}
|
|
public function edit(int $id = 0)
|
|
{
|
|
|
|
$server = $this->hostingServerRepository->find($id);
|
|
if (! $server) return back()->withErrors('Can\'t find server by id ' . $id);
|
|
$title = 'View server hosting information';
|
|
return view('administrator.pages.module.hosting.edit', compact('title', 'server'));
|
|
}
|
|
public function update(UpdateHostingRequest $request, int $id = 0)
|
|
{
|
|
$server = $this->hostingServerRepository->find($id);
|
|
if (! $server) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
$request->validated();
|
|
if ($request->get('name') != $server->name) {
|
|
$isExitsHost = $this->hostingServerRepository->findByName($request->get('name'));
|
|
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->hostingServerRepository->update($id, $data);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('module.hosting.index')->with('success', 'Updated successfully.');
|
|
}
|
|
public function destroy(int $id = 0)
|
|
{
|
|
$server = $this->hostingServerRepository->find($id);
|
|
if (! $server) {
|
|
return back()->withErrors('Invalid Request');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
|
|
$this->hostingServerRepository->delete($id);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('module.hosting.index')->with('success', 'Deleted successfully.');
|
|
}
|
|
}
|