2025-05-20 16:20:23 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2025-05-26 11:33:47 +02:00
|
|
|
use App\Http\Requests\Bo\Tenants\StoreTenantRequest;
|
|
|
|
|
use App\Http\Requests\Bo\Tenants\UpdateTenantRequest;
|
2025-05-20 16:20:23 +02:00
|
|
|
use App\Models\Tenant;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2025-05-26 11:46:46 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2025-05-20 16:20:23 +02:00
|
|
|
|
|
|
|
|
class TenantController extends Controller
|
|
|
|
|
{
|
2025-05-26 11:33:47 +02:00
|
|
|
/**
|
|
|
|
|
* Create the controller instance.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeResource(Tenant::class);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-20 16:20:23 +02:00
|
|
|
/**
|
|
|
|
|
* 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 $query */
|
|
|
|
|
$query = Tenant::query();
|
|
|
|
|
|
|
|
|
|
if ($request->search) {
|
|
|
|
|
$this->searchQuery(
|
|
|
|
|
$query,
|
|
|
|
|
$request->search,
|
|
|
|
|
null,
|
|
|
|
|
'name',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$searchFields = trans('validation.attributes.name');
|
|
|
|
|
|
|
|
|
|
/** Sort columns with a query */
|
|
|
|
|
$this->sortQuery($query);
|
|
|
|
|
|
2025-05-26 11:33:47 +02:00
|
|
|
$tenantModels = $this->paginate($query);
|
2025-05-20 16:20:23 +02:00
|
|
|
|
2025-05-26 11:33:47 +02:00
|
|
|
return view('bo.pages.tenants.index', compact('tenantModels', 'searchFields'));
|
2025-05-20 16:20:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
2025-05-26 11:33:47 +02:00
|
|
|
* @param \App\Models\Tenant $tenant
|
2025-05-20 16:20:23 +02:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
2025-05-26 11:33:47 +02:00
|
|
|
public function create(Tenant $tenant): \Illuminate\Contracts\View\View
|
2025-05-20 16:20:23 +02:00
|
|
|
{
|
2025-05-26 11:33:47 +02:00
|
|
|
return view('bo.pages.tenants.create', ['tenantModel' => $tenant]);
|
2025-05-20 16:20:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
2025-05-26 11:33:47 +02:00
|
|
|
* @param \App\Http\Requests\Bo\Tenants\StoreTenantRequest $request
|
2025-05-20 16:20:23 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
2025-05-26 11:33:47 +02:00
|
|
|
public function store(StoreTenantRequest $request): \Illuminate\Http\RedirectResponse
|
2025-05-20 16:20:23 +02:00
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request) {
|
|
|
|
|
$tenant = new Tenant();
|
|
|
|
|
$tenant->fill($request->validated());
|
|
|
|
|
if ($tenant->saveOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('bo.tenants.edit', ['tenant' => $tenant])
|
|
|
|
|
->with('success', trans(
|
|
|
|
|
':model a été créé',
|
2025-05-26 11:33:47 +02:00
|
|
|
['model' => trans('models.classes.tenant')]
|
2025-05-20 16:20:23 +02:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
|
|
|
|
':model n\'a pas été créé',
|
2025-05-26 11:33:47 +02:00
|
|
|
['model' => trans('models.classes.tenant')]
|
2025-05-20 16:20:23 +02:00
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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('bo.pages.tenants.edit', ['tenantModel' => $tenant]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
2025-05-26 11:33:47 +02:00
|
|
|
* @param \App\Http\Requests\Bo\Tenants\UpdateTenantRequest $request
|
|
|
|
|
* @param \App\Models\Tenant $tenant
|
2025-05-20 16:20:23 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
2025-05-26 11:33:47 +02:00
|
|
|
public function update(UpdateTenantRequest $request, Tenant $tenant): \Illuminate\Http\RedirectResponse
|
2025-05-20 16:20:23 +02:00
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request, $tenant) {
|
|
|
|
|
$tenant->fill($request->validated());
|
|
|
|
|
|
|
|
|
|
if ($tenant->saveOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('bo.tenants.edit', ['tenant' => $tenant])
|
|
|
|
|
->with('success', trans(
|
|
|
|
|
':model a été mis à jour',
|
2025-05-26 11:33:47 +02:00
|
|
|
['model' => trans('models.classes.tenant')]
|
2025-05-20 16:20:23 +02:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
|
|
|
|
':model n\'a pas été mis à jour',
|
2025-05-26 11:33:47 +02:00
|
|
|
['model' => trans('models.classes.tenant')]
|
2025-05-20 16:20:23 +02:00
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Tenant $tenant
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function destroy(Tenant $tenant): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
/** @var array<string,string> */
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
|
|
|
return DB::transaction(function () use ($tenant, $previousQueries) {
|
|
|
|
|
if ($tenant->deleteOrFail()) {
|
2025-05-26 11:46:46 +02:00
|
|
|
/** @var string $tenantKey */
|
|
|
|
|
$tenantKey = \tenant()->getKey();
|
|
|
|
|
Storage::disk('public')->deleteDirectory("modelfiles/{$tenantKey}");
|
|
|
|
|
Storage::disk('public')->deleteDirectory("docs/{$tenantKey}");
|
2025-05-20 16:20:23 +02:00
|
|
|
return redirect()
|
2025-05-26 11:33:47 +02:00
|
|
|
->route('bo.tenants.index', $previousQueries)
|
2025-05-20 16:20:23 +02:00
|
|
|
->with('warning', trans(
|
|
|
|
|
':model a bien été supprimé',
|
2025-05-26 11:33:47 +02:00
|
|
|
['model' => trans('models.classes.tenant')]
|
2025-05-20 16:20:23 +02:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
|
|
|
|
':model n\'a pas été supprimé',
|
2025-05-26 11:33:47 +02:00
|
|
|
['model' => trans('models.classes.tenant')]
|
2025-05-20 16:20:23 +02:00
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-05-26 11:33:47 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Duplicate the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Tenant $tenant
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function duplicate(Tenant $tenant): \Illuminate\Contracts\View\View
|
|
|
|
|
{
|
|
|
|
|
return $this->create($tenant->replicate());
|
|
|
|
|
}
|
2025-05-20 16:20:23 +02:00
|
|
|
}
|