forked from fa/breadcrumb-the-shire
112 lines
4.4 KiB
PHP
112 lines
4.4 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Buffer;
|
|
use MintyPHP\Support\Flash;
|
|
use MintyPHP\Support\Guard;
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Service\DepartmentService;
|
|
use MintyPHP\Repository\TenantDepartmentRepository;
|
|
use MintyPHP\Service\TenantService;
|
|
use MintyPHP\Service\UserService;
|
|
use MintyPHP\Service\SettingService;
|
|
use MintyPHP\Service\PermissionService;
|
|
use MintyPHP\Service\TenantScopeService;
|
|
|
|
Guard::requireLogin();
|
|
Guard::requirePermissionOrForbidden(PermissionService::DEPARTMENTS_VIEW);
|
|
|
|
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
|
|
if ($currentUserId > 0) {
|
|
PermissionService::getUserPermissions($currentUserId);
|
|
}
|
|
$allowedTenantIds = TenantScopeService::getUserTenantIds($currentUserId);
|
|
|
|
$uuid = trim((string) ($id ?? ''));
|
|
$department = $uuid !== '' ? DepartmentService::findByUuid($uuid) : null;
|
|
if (!$department) {
|
|
Flash::error('Department not found', 'admin/departments', 'department_not_found');
|
|
Router::redirect('admin/departments');
|
|
}
|
|
|
|
$departmentId = (int) ($department['id'] ?? 0);
|
|
if (!TenantScopeService::canAccess('departments', $departmentId, $currentUserId)) {
|
|
Router::redirect('error/forbidden');
|
|
return;
|
|
}
|
|
$creatorId = (int) ($department['created_by'] ?? 0);
|
|
if ($creatorId > 0) {
|
|
$creator = UserService::findById($creatorId);
|
|
if ($creator) {
|
|
$creatorName = trim(($creator['first_name'] ?? '') . ' ' . ($creator['last_name'] ?? ''));
|
|
$department['created_by_label'] = $creatorName !== '' ? $creatorName : ($creator['email'] ?? '');
|
|
$department['created_by_uuid'] = $creator['uuid'] ?? null;
|
|
}
|
|
}
|
|
$modifierId = (int) ($department['modified_by'] ?? 0);
|
|
if ($modifierId > 0) {
|
|
$modifier = UserService::findById($modifierId);
|
|
if ($modifier) {
|
|
$modifierName = trim(($modifier['first_name'] ?? '') . ' ' . ($modifier['last_name'] ?? ''));
|
|
$department['modified_by_label'] = $modifierName !== '' ? $modifierName : ($modifier['email'] ?? '');
|
|
$department['modified_by_uuid'] = $modifier['uuid'] ?? null;
|
|
}
|
|
}
|
|
|
|
$errors = [];
|
|
$form = $department;
|
|
$tenants = TenantService::list();
|
|
$selectedTenantIds = TenantDepartmentRepository::listTenantIdsByDepartmentId($departmentId);
|
|
if ($allowedTenantIds) {
|
|
$tenants = array_values(array_filter($tenants, static function (array $tenant) use ($allowedTenantIds): bool {
|
|
$tenantId = (int) ($tenant['id'] ?? 0);
|
|
return $tenantId > 0 && in_array($tenantId, $allowedTenantIds, true);
|
|
}));
|
|
$selectedTenantIds = array_values(array_intersect($selectedTenantIds, $allowedTenantIds));
|
|
} elseif (TenantScopeService::isStrict()) {
|
|
$tenants = [];
|
|
$selectedTenantIds = [];
|
|
}
|
|
|
|
if (isset($_POST['description'])) {
|
|
if (!PermissionService::userHas($currentUserId, PermissionService::DEPARTMENTS_UPDATE)) {
|
|
Router::redirect('error/forbidden');
|
|
return;
|
|
}
|
|
$result = DepartmentService::updateFromAdmin($departmentId, $_POST, $currentUserId);
|
|
$form = $result['form'] ?? $form;
|
|
$errors = $result['errors'] ?? [];
|
|
$tenantIds = $_POST['tenant_ids'] ?? [];
|
|
if (!is_array($tenantIds)) {
|
|
$tenantIds = [$tenantIds];
|
|
}
|
|
$selectedTenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
|
$selectedTenantIds = TenantScopeService::filterTenantIdsForUser($selectedTenantIds, $currentUserId);
|
|
|
|
if (TenantScopeService::isStrict() && !$selectedTenantIds) {
|
|
$errors[] = t('Please select at least one tenant');
|
|
}
|
|
|
|
if (($result['ok'] ?? false) && !$errors) {
|
|
$existingTenantIds = TenantDepartmentRepository::listTenantIdsByDepartmentId($departmentId);
|
|
$finalTenantIds = TenantScopeService::mergeTenantIdsPreservingOutOfScope(
|
|
$selectedTenantIds,
|
|
$existingTenantIds,
|
|
$allowedTenantIds
|
|
);
|
|
$cleaned = DepartmentService::syncTenants($departmentId, $finalTenantIds);
|
|
$action = (string) ($_POST['action'] ?? 'save');
|
|
if ($cleaned > 0) {
|
|
Flash::info(t('Department assignments cleaned: %d', $cleaned), "admin/departments/edit/{$uuid}", 'department_assignments_cleaned');
|
|
}
|
|
if ($action === 'save_close') {
|
|
Flash::success('Department updated', 'admin/departments', 'department_updated');
|
|
Router::redirect('admin/departments');
|
|
} else {
|
|
Flash::success('Department updated', "admin/departments/edit/{$uuid}", 'department_updated');
|
|
Router::redirect("admin/departments/edit/{$uuid}");
|
|
}
|
|
}
|
|
}
|
|
|
|
Buffer::set('title', t('Edit department'));
|