260 lines
9.1 KiB
PHP
260 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Http\Requests\Back\Companies\StoreCompanyRequest;
|
|
use App\Http\Requests\Back\Companies\UpdateCompanyRequest;
|
|
use App\Models\Company;
|
|
use App\Models\CompanyActivity;
|
|
use App\Models\CompanySector;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Inertia;
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
|
use Uwil\LaravelCommon\Controllers\Controller;
|
|
|
|
class CompanyController extends Controller
|
|
{
|
|
/** @var string $modelName */
|
|
private $modelName = "companies";
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Company::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function index(Request $request): \Inertia\Response
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Company> $query */
|
|
$query = Company::query();
|
|
|
|
if ($request->routeIs("back.$this->modelName.trash")) {
|
|
$query = $query->onlyTrashed();
|
|
}
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$search,
|
|
null,
|
|
'name',
|
|
'latitude',
|
|
'longitude',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.name'),
|
|
trans('validation.custom.latitude'),
|
|
trans('validation.custom.longitude'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
/** Custom pagination */
|
|
$companyModels = $this->paginate($query);
|
|
|
|
return Inertia::render('Companies/Index', [
|
|
'companyModels' => CommonResource::collection($companyModels),
|
|
'search' => $search,
|
|
'searchFields' => $searchFields,
|
|
'modelName' => $this->modelName,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Company $company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function create(Company $company): \Inertia\Response
|
|
{
|
|
$companyActivityModels = CompanyActivity::getModelsCached();
|
|
$companySectorModels = CompanySector::getModelsCached();
|
|
return Inertia::render('Companies/Create', [
|
|
'data' => CommonResource::make($company),
|
|
'companyActivityModels' => $companyActivityModels,
|
|
'companySectorModels' => $companySectorModels,
|
|
'modelName' => $this->modelName,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show a specific resource.
|
|
*
|
|
* @param \App\Models\Company $company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function show(Company $company): \Inertia\Response
|
|
{
|
|
return Inertia::render('Companies/Show', [
|
|
'data' => CommonResource::make($company->load(['companyActivity', 'companySector'])),
|
|
'modelName' => $this->modelName,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\Companies\StoreCompanyRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreCompanyRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$pageConfigModel = new Company();
|
|
$pageConfigModel->inertiaFill($request->validated());
|
|
|
|
if ($pageConfigModel->saveOrFail()) {
|
|
return redirect()->route("back.$this->modelName.edit", $pageConfigModel)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst()
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst()
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Company $company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function edit(Company $company): \Inertia\Response
|
|
{
|
|
$companyActivityModels = CompanyActivity::getModelsCached();
|
|
$companySectorModels = CompanySector::getModelsCached();
|
|
return Inertia::render('Companies/Edit', [
|
|
'data' => CommonResource::make($company),
|
|
'companyActivityModels' => $companyActivityModels,
|
|
'companySectorModels' => $companySectorModels,
|
|
'modelName' => $this->modelName,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\Companies\UpdateCompanyRequest $request
|
|
* @param \App\Models\Company $company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateCompanyRequest $request, Company $company): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $company) {
|
|
$company->inertiaFill($request->validated());
|
|
|
|
if ($company->saveOrFail()) {
|
|
return redirect()->route("back.$this->modelName.edit", $company)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::of(trans('models.classes.company'))->ucfirst()
|
|
]));
|
|
}
|
|
return redirect()->route("back.$this->modelName.edit", $company)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::of(trans('models.classes.company'))->ucfirst()
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Company $company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function duplicate(Company $company): \Inertia\Response
|
|
{
|
|
return $this->create($company->replicate());
|
|
}
|
|
|
|
/**
|
|
* Restore the specified resource.
|
|
*
|
|
* @param integer $company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function restore(int $company): \Illuminate\Http\RedirectResponse
|
|
{
|
|
$company = Company::withTrashed()->findOrFail($company);
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($company->restore()) {
|
|
return redirect()->route("back.$this->modelName.trash", $previousQueries)
|
|
->with('success', __('crud.messages.has_been_restored', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(),
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_restored', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(),
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Destroy the specified resource.
|
|
*
|
|
* @param \App\Models\Company $company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Company $company): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($company->delete()) {
|
|
return redirect()->route("back.$this->modelName.index", $previousQueries)
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(),
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(),
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Destroy the specified resource.
|
|
*
|
|
* @param \App\Models\Company|integer $company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function forceDestroy(Company|int $company): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if (is_int($company)) {
|
|
$company = Company::withTrashed()->findOrFail($company);
|
|
}
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($company->forceDelete()) {
|
|
return redirect()->route("back.$this->modelName.trash", $previousQueries)
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(),
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(),
|
|
]));
|
|
}
|
|
}
|