forked from fa/breadcrumb-the-shire
53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Support\Guard;
|
|
use MintyPHP\Service\DepartmentService;
|
|
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);
|
|
$tenantIds = TenantScopeService::getUserTenantIds($currentUserId);
|
|
|
|
$limit = (int) ($_GET['limit'] ?? 10);
|
|
$offset = (int) ($_GET['offset'] ?? 0);
|
|
$search = trim((string) ($_GET['search'] ?? ''));
|
|
$order = (string) ($_GET['order'] ?? 'description');
|
|
$dir = (string) ($_GET['dir'] ?? 'asc');
|
|
$tenant = trim((string) ($_GET['tenant'] ?? ''));
|
|
|
|
$result = DepartmentService::listPaged([
|
|
'limit' => $limit,
|
|
'offset' => $offset,
|
|
'search' => $search,
|
|
'order' => $order,
|
|
'dir' => $dir,
|
|
'tenant' => $tenant,
|
|
'tenantIds' => $tenantIds,
|
|
'tenantUserId' => $currentUserId,
|
|
]);
|
|
|
|
$defaultDepartmentId = SettingService::getDefaultDepartmentId();
|
|
$rows = [];
|
|
foreach ($result['rows'] as $row) {
|
|
$departmentId = (int) ($row['id'] ?? 0);
|
|
$rows[] = [
|
|
'id' => $row['id'] ?? null,
|
|
'uuid' => $row['uuid'] ?? '',
|
|
'is_default' => $departmentId > 0 && $defaultDepartmentId === $departmentId,
|
|
'description' => $row['description'] ?? '',
|
|
'tenants' => $row['tenant_labels'] ?? [],
|
|
'created' => dt($row['created'] ?? ''),
|
|
'modified' => dt($row['modified'] ?? ''),
|
|
];
|
|
}
|
|
|
|
Router::json([
|
|
'data' => $rows,
|
|
'total' => $result['total'] ?? 0,
|
|
]);
|