76 lines
2.6 KiB
PHP
76 lines
2.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use MintyPHP\Buffer;
|
||
|
|
use MintyPHP\Support\Flash;
|
||
|
|
use MintyPHP\Support\Guard;
|
||
|
|
use MintyPHP\Router;
|
||
|
|
use MintyPHP\Service\DepartmentService;
|
||
|
|
use MintyPHP\Service\PermissionService;
|
||
|
|
use MintyPHP\Service\TenantService;
|
||
|
|
use MintyPHP\Service\TenantScopeService;
|
||
|
|
|
||
|
|
Guard::requireLogin();
|
||
|
|
Guard::requirePermissionOrForbidden(PermissionService::DEPARTMENTS_CREATE);
|
||
|
|
|
||
|
|
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
|
||
|
|
$allowedTenantIds = TenantScopeService::getUserTenantIds($currentUserId);
|
||
|
|
if (TenantScopeService::isStrict() && !$allowedTenantIds) {
|
||
|
|
Router::redirect('error/forbidden');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$errors = [];
|
||
|
|
$form = [
|
||
|
|
'description' => '',
|
||
|
|
];
|
||
|
|
$tenants = TenantService::list();
|
||
|
|
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);
|
||
|
|
}));
|
||
|
|
} elseif (TenantScopeService::isStrict()) {
|
||
|
|
$tenants = [];
|
||
|
|
}
|
||
|
|
$selectedTenantIds = [];
|
||
|
|
|
||
|
|
if (isset($_POST['description'])) {
|
||
|
|
$result = DepartmentService::createFromAdmin($_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) {
|
||
|
|
$departmentId = (int) ($result['id'] ?? 0);
|
||
|
|
if ($departmentId) {
|
||
|
|
DepartmentService::syncTenants($departmentId, $selectedTenantIds);
|
||
|
|
}
|
||
|
|
$action = (string) ($_POST['action'] ?? 'create');
|
||
|
|
if ($action === 'create_close') {
|
||
|
|
Flash::success('Department created', 'admin/departments', 'department_created');
|
||
|
|
Router::redirect('admin/departments');
|
||
|
|
} else {
|
||
|
|
$uuid = (string) ($result['uuid'] ?? '');
|
||
|
|
if ($uuid !== '') {
|
||
|
|
$target = "admin/departments/edit/{$uuid}";
|
||
|
|
Flash::success('Department created', $target, 'department_created');
|
||
|
|
Router::redirect($target);
|
||
|
|
} else {
|
||
|
|
Flash::success('Department created', 'admin/departments', 'department_created');
|
||
|
|
Router::redirect('admin/departments');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Buffer::set('title', t('Create department'));
|