baseline
This commit is contained in:
111
pages/address-book/view($id).php
Normal file
111
pages/address-book/view($id).php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
use MintyPHP\Buffer;
|
||||
use MintyPHP\DB;
|
||||
use MintyPHP\Router;
|
||||
use MintyPHP\Support\Flash;
|
||||
use MintyPHP\Support\Guard;
|
||||
use MintyPHP\Service\PermissionService;
|
||||
use MintyPHP\Service\TenantScopeService;
|
||||
use MintyPHP\Service\UserService;
|
||||
|
||||
Guard::requireLogin();
|
||||
Guard::requirePermissionOrForbidden(PermissionService::ADDRESS_BOOK_VIEW);
|
||||
|
||||
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
|
||||
$uuid = trim((string) ($id ?? ''));
|
||||
if ($uuid === '') {
|
||||
Router::redirect('address-book');
|
||||
}
|
||||
|
||||
$user = UserService::findByUuid($uuid);
|
||||
if (!$user || !isset($user['id'])) {
|
||||
Flash::error('User not found', 'address-book', 'user_not_found');
|
||||
Router::redirect('address-book');
|
||||
}
|
||||
|
||||
$userId = (int) ($user['id'] ?? 0);
|
||||
if ($currentUserId !== $userId && !TenantScopeService::canAccess('users', $userId, $currentUserId)) {
|
||||
Router::redirect('error/forbidden');
|
||||
}
|
||||
|
||||
$getRowValue = static function (array $row, string $key): ?string {
|
||||
foreach ($row as $value) {
|
||||
if (is_array($value) && array_key_exists($key, $value)) {
|
||||
return (string) $value[$key];
|
||||
}
|
||||
}
|
||||
if (array_key_exists($key, $row) && !is_array($row[$key])) {
|
||||
return (string) $row[$key];
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
$extractLabels = static function (array $rows): array {
|
||||
$labels = [];
|
||||
foreach ($rows as $row) {
|
||||
$label = '';
|
||||
foreach ($row as $value) {
|
||||
if (is_array($value) && isset($value['description'])) {
|
||||
$label = (string) $value['description'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($label === '' && isset($row['description'])) {
|
||||
$label = (string) $row['description'];
|
||||
}
|
||||
if ($label !== '') {
|
||||
$labels[] = $label;
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($labels));
|
||||
};
|
||||
|
||||
$tenantRows = DB::select(
|
||||
'select t.id as id, t.description as description from user_tenants ut join tenants t on t.id = ut.tenant_id where ut.user_id = ? order by t.description asc',
|
||||
(string) $userId
|
||||
);
|
||||
$departmentRows = DB::select(
|
||||
'select td.tenant_id as tenant_id, d.description as description from user_departments ud join tenant_departments td on td.department_id = ud.department_id join departments d on d.id = ud.department_id where ud.user_id = ? order by d.description asc',
|
||||
(string) $userId
|
||||
);
|
||||
$roleRows = DB::select(
|
||||
'select r.description as description from user_roles ur join roles r on r.id = ur.role_id where ur.user_id = ? order by r.description asc',
|
||||
(string) $userId
|
||||
);
|
||||
|
||||
$departmentMap = [];
|
||||
foreach (is_array($departmentRows) ? $departmentRows : [] as $row) {
|
||||
$tenantId = (int) ($getRowValue($row, 'tenant_id') ?? 0);
|
||||
$label = (string) ($getRowValue($row, 'description') ?? '');
|
||||
if ($tenantId > 0 && $label !== '') {
|
||||
$departmentMap[$tenantId][] = $label;
|
||||
}
|
||||
}
|
||||
foreach ($departmentMap as $tenantId => $labels) {
|
||||
$departmentMap[$tenantId] = array_values(array_unique($labels));
|
||||
}
|
||||
|
||||
$primaryTenantId = (int) ($user['primary_tenant_id'] ?? 0);
|
||||
$tenantGroups = [];
|
||||
foreach (is_array($tenantRows) ? $tenantRows : [] as $row) {
|
||||
$tenantId = (int) ($getRowValue($row, 'id') ?? 0);
|
||||
$tenantLabel = (string) ($getRowValue($row, 'description') ?? '');
|
||||
if ($tenantId <= 0 || $tenantLabel === '') {
|
||||
continue;
|
||||
}
|
||||
$tenantGroups[] = [
|
||||
'label' => $tenantLabel,
|
||||
'is_primary' => $primaryTenantId > 0 && $tenantId === $primaryTenantId,
|
||||
'departments' => $departmentMap[$tenantId] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
$roleLabels = $extractLabels(is_array($roleRows) ? $roleRows : []);
|
||||
|
||||
$user['tenant_groups'] = $tenantGroups;
|
||||
$user['role_labels'] = $roleLabels;
|
||||
|
||||
$name = trim(($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? ''));
|
||||
$title = $name !== '' ? $name : ($user['email'] ?? t('Address book'));
|
||||
Buffer::set('title', $title);
|
||||
Reference in New Issue
Block a user