239 lines
8.7 KiB
PHP
239 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Http\Requests\Back\CompanyActivities\StoreCompanyActivityRequest;
|
|
use App\Http\Requests\Back\CompanyActivities\UpdateCompanyActivityRequest;
|
|
use App\Models\CompanyActivity;
|
|
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 CompanyActivityController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(CompanyActivity::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\CompanyActivity> $query */
|
|
$query = CompanyActivity::query();
|
|
|
|
if ($request->routeIs("back.company-activities.trash")) {
|
|
$query = $query->onlyTrashed();
|
|
}
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.name'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
/** Custom pagination */
|
|
$companyActivityModels = $this->paginate($query);
|
|
|
|
return Inertia::render('CompanyActivities/Index', [
|
|
'companyActivityModels' => CommonResource::collection($companyActivityModels),
|
|
'search' => $search,
|
|
'searchFields' => $searchFields,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\CompanyActivity $company_activity
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function create(CompanyActivity $company_activity): \Inertia\Response
|
|
{
|
|
return Inertia::render('CompanyActivities/Create', [
|
|
'data' => CommonResource::make($company_activity),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show a specific resource.
|
|
*
|
|
* @param \App\Models\CompanyActivity $company_activity
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function show(CompanyActivity $company_activity): \Inertia\Response
|
|
{
|
|
return Inertia::render('CompanyActivities/Show', [
|
|
'data' => CommonResource::make($company_activity),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\CompanyActivities\StoreCompanyActivityRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreCompanyActivityRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$pageConfigModel = new CompanyActivity();
|
|
$pageConfigModel->fill($request->validated());
|
|
|
|
if ($pageConfigModel->saveOrFail()) {
|
|
return redirect()->route('back.company-activities.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\CompanyActivity $company_activity
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function edit(CompanyActivity $company_activity): \Inertia\Response
|
|
{
|
|
return Inertia::render('CompanyActivities/Edit', [
|
|
'data' => CommonResource::make($company_activity),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\CompanyActivities\UpdateCompanyActivityRequest $request
|
|
* @param \App\Models\CompanyActivity $company_activity
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateCompanyActivityRequest $request, CompanyActivity $company_activity): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $company_activity) {
|
|
$company_activity->fill($request->validated());
|
|
|
|
if ($company_activity->saveOrFail()) {
|
|
return redirect()->route('back.company-activities.edit', $company_activity)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::of(trans('models.classes.company'))->ucfirst()
|
|
]));
|
|
}
|
|
return redirect()->route('back.company-activities.edit', $company_activity)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::of(trans('models.classes.company'))->ucfirst()
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\CompanyActivity $company_activity
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function duplicate(CompanyActivity $company_activity): \Inertia\Response
|
|
{
|
|
return $this->create($company_activity->replicate());
|
|
}
|
|
|
|
/**
|
|
* Restore the specified resource.
|
|
*
|
|
* @param integer $company_activity
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function restore(int $company_activity): \Illuminate\Http\RedirectResponse
|
|
{
|
|
$company_activity = CompanyActivity::withTrashed()->findOrFail($company_activity);
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($company_activity->restore()) {
|
|
return redirect()->route('back.company-activities.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\CompanyActivity $company_activity
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(CompanyActivity $company_activity): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($company_activity->delete()) {
|
|
return redirect()->route('back.company-activities.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\CompanyActivity|integer $company_activity
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function forceDestroy(CompanyActivity|int $company_activity): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if (is_int($company_activity)) {
|
|
$company_activity = CompanyActivity::withTrashed()->findOrFail($company_activity);
|
|
}
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($company_activity->forceDelete()) {
|
|
return redirect()->route('back.company-activities.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(),
|
|
]));
|
|
}
|
|
}
|