296 lines
11 KiB
PHP
296 lines
11 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Back;
|
||
|
|
|
||
|
|
use App\Http\Requests\Back\Sites\StoreSiteRequest;
|
||
|
|
use App\Http\Requests\Back\Sites\UpdateSiteRequest;
|
||
|
|
use App\Models\GitlabProject;
|
||
|
|
use App\Models\Site;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Arr;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Uwil\LaravelBackend\Controllers\BackendController;
|
||
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
||
|
|
use Uwil\LaravelBackend\Support\UserRoleRegistry;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handles CRUD operations for Site in the backend.
|
||
|
|
*/
|
||
|
|
class SiteController extends BackendController
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Project managers collection.
|
||
|
|
*
|
||
|
|
* @var \Illuminate\Support\Collection<int,\Uwil\LaravelBackend\Models\User>
|
||
|
|
*/
|
||
|
|
private \Illuminate\Support\Collection $userModelsProjectManager;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Project managers collection.
|
||
|
|
*
|
||
|
|
* @var \Illuminate\Support\Collection<int,\Uwil\LaravelBackend\Models\User>
|
||
|
|
*/
|
||
|
|
private \Illuminate\Support\Collection $userModelsClient;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Gitlab projects collection.
|
||
|
|
*
|
||
|
|
* @var \Illuminate\Support\Collection<int,\App\Models\GitlabProject>
|
||
|
|
*/
|
||
|
|
private \Illuminate\Support\Collection $gitlabProjectModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create the controller instance.
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->authorizeResource(Site::class);
|
||
|
|
$this->modelName = Site::modelName();
|
||
|
|
$this->modelPage = Site::modelPage();
|
||
|
|
$userModels = User::getModelsCached()
|
||
|
|
->where("published", true)
|
||
|
|
->where('deleted_at', null)
|
||
|
|
->values();
|
||
|
|
$this->userModelsProjectManager = $userModels
|
||
|
|
->filter(fn(User $user) => $user->role->value === UserRoleRegistry::get('project-manager')->value)
|
||
|
|
->values();
|
||
|
|
$this->userModelsClient = $userModels
|
||
|
|
->filter(fn(User $user) => $user->role->value === UserRoleRegistry::get('client')->value)
|
||
|
|
->values();
|
||
|
|
$this->gitlabProjectModels = GitlabProject::getModelsCached();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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\Site> $query */
|
||
|
|
$query = Site::query()->with(['projectManager', 'gitlabProject']);
|
||
|
|
;
|
||
|
|
|
||
|
|
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',
|
||
|
|
);
|
||
|
|
}
|
||
|
|
$searchFields = \implode(', ', [
|
||
|
|
trans('validation.attributes.name'),
|
||
|
|
]);
|
||
|
|
|
||
|
|
/** Sort columns with a query */
|
||
|
|
$this->sortQuery($query);
|
||
|
|
|
||
|
|
/** Custom pagination */
|
||
|
|
$models = $this->paginate($query);
|
||
|
|
|
||
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Index", [
|
||
|
|
'models' => CommonResource::collection($models),
|
||
|
|
'search' => $search,
|
||
|
|
'searchFields' => $searchFields,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the form for creating a new resource.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Site $site
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function create(Site $site): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Create", [
|
||
|
|
'data' => CommonResource::make($site),
|
||
|
|
'userModelsProjectManager' => $this->userModelsProjectManager,
|
||
|
|
'userModelsClient' => $this->userModelsClient,
|
||
|
|
'gitlabProjectModels' => $this->gitlabProjectModels,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show a specific resource.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Site $site
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function show(Site $site): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Show", [
|
||
|
|
'data' => CommonResource::make($site->load(['projectManager', 'gitlabProject', 'users'])),
|
||
|
|
'userModelsProjectManager' => $this->userModelsProjectManager,
|
||
|
|
'userModelsClient' => $this->userModelsClient,
|
||
|
|
'gitlabProjectModels' => $this->gitlabProjectModels,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created resource.
|
||
|
|
*
|
||
|
|
* @param \App\Http\Requests\Back\Sites\StoreSiteRequest $request
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function store(StoreSiteRequest $request): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
return DB::transaction(function () use ($request) {
|
||
|
|
$validatedData = $request->validated();
|
||
|
|
$site = new Site();
|
||
|
|
$site->fill(Arr::except($validatedData, 'users'));
|
||
|
|
|
||
|
|
if ($site->saveOrFail()) {
|
||
|
|
$site->users()->syncWithoutDetaching(Arr::get($validatedData, 'users'));
|
||
|
|
return redirect()->route("back.$this->modelName.edit", $site)
|
||
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_created', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
return redirect()->back()
|
||
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_created', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the form for editing the specified resource.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Site $site
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function edit(Site $site): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Edit", [
|
||
|
|
'data' => CommonResource::make($site->load(['projectManager', 'gitlabProject', 'users'])),
|
||
|
|
'userModelsProjectManager' => $this->userModelsProjectManager,
|
||
|
|
'userModelsClient' => $this->userModelsClient,
|
||
|
|
'gitlabProjectModels' => $this->gitlabProjectModels,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified resource.
|
||
|
|
*
|
||
|
|
* @param \App\Http\Requests\Back\Sites\UpdateSiteRequest $request
|
||
|
|
* @param \App\Models\Site $site
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function update(
|
||
|
|
UpdateSiteRequest $request,
|
||
|
|
Site $site
|
||
|
|
): \Illuminate\Http\RedirectResponse {
|
||
|
|
return DB::transaction(function () use ($request, $site) {
|
||
|
|
$validatedData = $request->validated();
|
||
|
|
$site->fill(Arr::except($validatedData, 'users'));
|
||
|
|
|
||
|
|
if ($site->saveOrFail()) {
|
||
|
|
$site->users()->sync(Arr::get($validatedData, 'users'));
|
||
|
|
return redirect()->route("back.$this->modelName.edit", $site)
|
||
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_updated', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
return redirect()->route("back.$this->modelName.edit", $site)
|
||
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_updated', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Duplicate the specified resource.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Site $site
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function duplicate(Site $site): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->create($site->replicate());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Restore the specified resource.
|
||
|
|
*
|
||
|
|
* @param integer $site
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function restore(int $site): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
$site = Site::withTrashed()->findOrFail($site);
|
||
|
|
/** @var array<string,string> */
|
||
|
|
$previousQueries = [];
|
||
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
||
|
|
if ($site->restore()) {
|
||
|
|
return redirect()->route("back.$this->modelName.trash", $previousQueries)
|
||
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_restored', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
return redirect()->back()
|
||
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_restored', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Destroy the specified resource.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Site $site
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function destroy(Site $site): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
/** @var array<string,string> */
|
||
|
|
$previousQueries = [];
|
||
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
||
|
|
if ($site->delete()) {
|
||
|
|
return redirect()->route("back.$this->modelName.index", $previousQueries)
|
||
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
return redirect()->back()
|
||
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Destroy the specified resource.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Site|integer $site
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function forceDestroy(Site|int $site): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
if (is_int($site)) {
|
||
|
|
$site = Site::withTrashed()->findOrFail($site);
|
||
|
|
}
|
||
|
|
/** @var array<string,string> */
|
||
|
|
$previousQueries = [];
|
||
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
||
|
|
if ($site->forceDelete()) {
|
||
|
|
return redirect()->route("back.$this->modelName.trash", $previousQueries)
|
||
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
return redirect()->back()
|
||
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
||
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
}
|