181 lines
5.9 KiB
PHP
181 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Tenants\StoreTenantRequest;
|
|
use App\Http\Requests\Bo\Tenants\UpdateTenantRequest;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class TenantController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Tenant::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $tenantModels */
|
|
$tenantModels = Tenant::query()->with('domains');
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$tenantModels,
|
|
$search,
|
|
null,
|
|
'id',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.custom.theme'),
|
|
trans('validation.custom.domain'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($tenantModels);
|
|
|
|
/** Custom pagination */
|
|
$tenantModels = $this->paginate($tenantModels);
|
|
|
|
return view('back::pages.tenants.index', compact('tenantModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param \App\Models\Tenant $tenant
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(Tenant $tenant): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('back::pages.tenants.show', ['tenantModel' => $tenant]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Tenant $tenant
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Tenant $tenant): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('back::pages.tenants.create', [
|
|
'tenantModel' => $tenant
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Tenants\StoreTenantRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreTenantRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$tenant = new Tenant();
|
|
$tenant->fill(Arr::except($request->validated(), 'domain'));
|
|
if ($tenant->saveOrFail()) {
|
|
$tenant->createDomain(Arr::only($request->validated(), 'domain'));
|
|
return redirect()
|
|
->route('bo.tenants.edit', $tenant)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(__('models.classes.tenant'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(__('models.classes.tenant'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Tenant $tenant
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Tenant $tenant): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('back::pages.tenants.edit', [
|
|
'tenantModel' => $tenant->load('domains'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Tenants\UpdateTenantRequest $request
|
|
* @param \App\Models\Tenant $tenant
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateTenantRequest $request, Tenant $tenant): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $tenant) {
|
|
$tenant->fill(Arr::except($request->validated(), 'domain'));
|
|
if ($tenant->saveOrFail()) {
|
|
$domainModel = $tenant->domains->first();
|
|
$domainModel->domain = Arr::get($request->validated(), 'domain');
|
|
$domainModel->saveOrFail();
|
|
return redirect()
|
|
->route('bo.tenants.edit', $tenant)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(__('models.classes.tenant'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(__('models.classes.tenant'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Tenant $tenant
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Tenant $tenant): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($tenant) {
|
|
if ($tenant->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.tenants.index')
|
|
->with('success', __(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(__('models.classes.tenant'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', __(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(__('models.classes.tenant'))]
|
|
));
|
|
});
|
|
}
|
|
}
|