2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-03-22 18:25:33 +01:00
|
|
|
use MintyPHP\Http\Request;
|
2026-03-06 11:28:22 +01:00
|
|
|
use MintyPHP\Http\SessionStoreInterface;
|
2026-02-04 23:31:53 +01:00
|
|
|
use MintyPHP\Router;
|
2026-02-24 08:49:40 +01:00
|
|
|
use MintyPHP\Service\Access\DepartmentAuthorizationPolicy;
|
2026-02-23 12:58:19 +01:00
|
|
|
use MintyPHP\Support\Flash;
|
|
|
|
|
use MintyPHP\Support\Guard;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-06 11:28:22 +01:00
|
|
|
$session = app(SessionStoreInterface::class)->all();
|
2026-02-04 23:31:53 +01:00
|
|
|
Guard::requireLogin();
|
2026-03-04 15:56:58 +01:00
|
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
$uuid = trim((string) ($id ?? ''));
|
|
|
|
|
if ($uuid === '') {
|
|
|
|
|
Router::redirect('admin/departments');
|
2026-02-24 08:49:40 +01:00
|
|
|
return;
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 11:28:22 +01:00
|
|
|
$currentUserId = (int) ($session['user']['id'] ?? 0);
|
2026-03-04 15:56:58 +01:00
|
|
|
$department = app(\MintyPHP\Service\Org\DepartmentService::class)->findByUuid($uuid);
|
2026-02-24 08:49:40 +01:00
|
|
|
if (!$department) {
|
2026-03-22 18:25:33 +01:00
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code(404);
|
|
|
|
|
Router::json(['error' => 'not_found']);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-24 08:49:40 +01:00
|
|
|
Flash::error('Department not found', 'admin/departments', 'department_not_found');
|
|
|
|
|
Router::redirect('admin/departments');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$departmentId = (int) ($department['id'] ?? 0);
|
|
|
|
|
$decision = $authorizationService->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_DELETE, [
|
|
|
|
|
'actor_user_id' => $currentUserId,
|
|
|
|
|
'target_department_id' => $departmentId,
|
|
|
|
|
]);
|
|
|
|
|
if (!$decision->isAllowed()) {
|
2026-03-22 18:25:33 +01:00
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code($decision->status());
|
|
|
|
|
Router::json(['error' => $decision->error()]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-04 23:31:53 +01:00
|
|
|
Router::redirect('error/forbidden');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$result = app(\MintyPHP\Service\Org\DepartmentService::class)->deleteByUuid($uuid);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!($result['ok'] ?? false)) {
|
2026-03-22 18:25:33 +01:00
|
|
|
$error = $result['error'] ?? 'not_found';
|
|
|
|
|
$status = $result['status'] ?? 500;
|
|
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code($status);
|
|
|
|
|
Router::json(['error' => $error]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($error === 'department_has_users') {
|
|
|
|
|
Flash::error(t('Department can not be deleted while users are assigned'), 'admin/departments', 'department_has_users');
|
|
|
|
|
} else {
|
|
|
|
|
Flash::error(t('Department not found'), 'admin/departments', 'department_not_found');
|
|
|
|
|
}
|
2026-02-04 23:31:53 +01:00
|
|
|
Router::redirect('admin/departments');
|
2026-02-24 08:49:40 +01:00
|
|
|
return;
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($currentUserId > 0) {
|
2026-03-04 15:56:58 +01:00
|
|
|
app(\MintyPHP\Service\Auth\AuthService::class)->loadTenantDataIntoSession($currentUserId);
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 18:25:33 +01:00
|
|
|
if (Request::wantsJson()) {
|
|
|
|
|
http_response_code(204);
|
|
|
|
|
Router::json([]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-04 23:31:53 +01:00
|
|
|
Flash::success('Department deleted', 'admin/departments', 'department_deleted');
|
|
|
|
|
Router::redirect('admin/departments');
|