185 lines
6.0 KiB
PHP
185 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin\package;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Package\OsGroupRequest;
|
|
use App\Http\Services\Upload;
|
|
use App\Imports\GroupImport;
|
|
use App\Models\ModuleDatastore;
|
|
use App\Models\ModuleHost;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\OsGroup;
|
|
use App\Repositories\OsGroup\OsGroupRepositoryInterface;
|
|
use Exception;
|
|
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
|
|
{
|
|
public function __construct(
|
|
public OsGroupRepositoryInterface $osGroupRepository
|
|
) {}
|
|
//
|
|
public function index(Request $request)
|
|
{
|
|
$group = $this->osGroupRepository->filter(
|
|
[
|
|
'page' => $request->get('page') ?? 1
|
|
],
|
|
['host']
|
|
);
|
|
$title = 'List all group Os for all datastore';
|
|
|
|
return view('administrator.pages.package.group.index', compact('group', '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],
|
|
'thumbnail' => $row[2],
|
|
'module_host_id' => $row[3]
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! isset($allData) || (isset($allData) && count($allData) < 1)) {
|
|
return back()->withErrors('No data imported');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->osGroupRepository->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 Group OS';
|
|
|
|
return view('administrator.pages.package.group.add', compact('title'));
|
|
}
|
|
public function store(OsGroupRequest $request)
|
|
{
|
|
$request->validated();
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->osGroupRepository->create($request->all());
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.group.index')->with('success', 'Add new Os Group successfully');
|
|
}
|
|
public function edit(int $id = 0)
|
|
{
|
|
|
|
$group = $this->osGroupRepository->findWithRelationships($id, ['host.datacenter']);
|
|
if (! $group) {
|
|
return back()->withErrors('Can\'t find your group by id ' . $id);
|
|
}
|
|
$title = 'Edit Group OS';
|
|
|
|
return view('administrator.pages.package.group.edit', compact('title', 'group'));
|
|
}
|
|
public function update(int $id = 0, OsGroupRequest $request)
|
|
{
|
|
$group = $this->osGroupRepository->find($id);
|
|
if (! $group) {
|
|
return redirect()->route('package.group.index')->withErrors('Invalid Request');
|
|
}
|
|
$request->validated();
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->osGroupRepository->update($id, $request->all());
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return redirect()->route('package.group.index')->with('success', 'Update successfully');
|
|
}
|
|
public function destroy(int $id = 0)
|
|
{
|
|
$group = $this->osGroupRepository->find($id);
|
|
if (! $group) {
|
|
return redirect()->route('package.group.index')->withErrors('Invalid Request');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->osGroupRepository->delete($id);
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
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 '<img src="' . $d->thumbnail . '"
|
|
alt="' . e($d->name) . '"
|
|
width="50" height="50"
|
|
style="object-fit: cover; border-radius: 6px;">';
|
|
})
|
|
->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 '<a href="' . route('package.group.edit', $d->id) . '"
|
|
class="btn btn-primary btn-sm">
|
|
Edit
|
|
</a>';
|
|
})
|
|
->filterColumn('host', function ($query, $keyword) {
|
|
$query->whereHas('host', function ($q) use ($keyword) {
|
|
$q->where('host', 'like', "%{$keyword}%");
|
|
});
|
|
})
|
|
->rawColumns(['thumbnail', 'action', 'status'])
|
|
->make(true);
|
|
}
|
|
}
|