ldc-cocotte/app/Traits/Controllers/HasMassActions.php
Jérémy Munsch 74ab9b3041
refactor: reorganize imports across multiple controllers and jobs
This commit refactors the import statements in various controllers and jobs
to improve code organization and readability. Unused imports were removed,
and necessary imports were added to ensure proper functionality.
2026-11-10 18:15:32 +01:00

50 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Traits\Controllers;
use App\Contracts\Executor;
use App\Cqs\Actions\Common\ExecuteMassAction;
use App\Enums\MassActionType;
use App\Http\Requests\Bo\MassActionRequest;
use Illuminate\Http\RedirectResponse;
/**
* This trait works with Route::bind in App service provider
* to resolve the model and the DB row
* It does not work with binder Service
*
* @mixin \Illuminate\Routing\Controller
* @mixin \App\Interfaces\MassActions
*/
trait HasMassActions
{
/** Toggles model status */
public function massActions(
MassActionRequest $request,
ExecuteMassAction $action,
Executor $executor,
): RedirectResponse {
$modelClass = $this->getModelClass();
$model = new $modelClass();
$type = $request->getMassActionType();
if (in_array($type, [MassActionType::Star, MassActionType::Unstar], true)) {
$this->authorize('toggleStar', $model);
}
if (in_array($type, [MassActionType::Publish, MassActionType::Unpublish], true)) {
$this->authorize('toggleStatus', $model);
}
$executor->execute($action, $request->toExecuteMassActionInput($modelClass));
$message = match ($type) {
MassActionType::Star, MassActionType::Unstar => trans('crud.messages.star_saved'),
MassActionType::Publish, MassActionType::Unpublish => trans('crud.messages.publish_status_saved'),
};
return back()->with('info', $message);
}
}