1
0
Files
breadcrumb-the-shire/lib/Repository/Org/UserDepartmentRepository.php

114 lines
3.6 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
2026-02-11 19:28:12 +01:00
namespace MintyPHP\Repository\Org;
2026-02-04 23:31:53 +01:00
use MintyPHP\DB;
class UserDepartmentRepository
{
2026-02-23 12:58:19 +01:00
public function listDepartmentIdsByUserId(int $userId): array
2026-02-04 23:31:53 +01:00
{
$rows = DB::select('select department_id from user_departments where user_id = ?', (string) $userId);
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['user_departments'] ?? $row;
if (is_array($data) && isset($data['department_id'])) {
$ids[] = (int) $data['department_id'];
}
}
return array_values(array_unique($ids));
}
2026-02-23 12:58:19 +01:00
public function replaceForUser(int $userId, array $departmentIds): bool
2026-02-04 23:31:53 +01:00
{
DB::delete('delete from user_departments where user_id = ?', (string) $userId);
if (!$departmentIds) {
return true;
}
foreach ($departmentIds as $departmentId) {
DB::insert(
'insert into user_departments (user_id, department_id, created) values (?,?,NOW())',
(string) $userId,
(string) $departmentId
);
}
return true;
}
2026-02-23 12:58:19 +01:00
public function removeInvalidForDepartment(int $departmentId): int
2026-02-04 23:31:53 +01:00
{
$query = 'delete ud from user_departments ud ' .
'where ud.department_id = ? and not exists (' .
'select 1 from user_tenants ut ' .
'join departments d on d.id = ud.department_id ' .
'where ut.user_id = ud.user_id and ut.tenant_id = d.tenant_id' .
2026-02-04 23:31:53 +01:00
')';
$result = DB::delete($query, (string) $departmentId);
return $result !== false ? (int) $result : 0;
}
2026-02-23 12:58:19 +01:00
public function countUsersByDepartmentIds(array $departmentIds): array
{
2026-02-23 12:58:19 +01:00
return $this->countByDepartmentIds($departmentIds, false);
}
2026-02-23 12:58:19 +01:00
public function countActiveUsersByDepartmentIds(array $departmentIds): array
{
2026-02-23 12:58:19 +01:00
return $this->countByDepartmentIds($departmentIds, true);
}
2026-02-23 12:58:19 +01:00
private function countByDepartmentIds(array $departmentIds, bool $activeOnly): array
{
$departmentIds = array_values(array_unique(array_map('intval', $departmentIds)));
$departmentIds = array_values(array_filter($departmentIds, static fn ($id) => $id > 0));
if (!$departmentIds) {
return [];
}
$placeholders = implode(',', array_fill(0, count($departmentIds), '?'));
$joinUsersSql = $activeOnly ? ' join users u on u.id = ud.user_id and u.active = 1' : '';
$rows = DB::select(
'select ud.department_id, count(distinct ud.user_id) as user_count from user_departments ud' .
$joinUsersSql .
' where ud.department_id in (' . $placeholders . ') group by ud.department_id',
...array_map('strval', $departmentIds)
);
if (!is_array($rows)) {
return [];
}
$result = [];
foreach ($rows as $row) {
2026-02-23 12:58:19 +01:00
$departmentId = $this->extractIntField($row, 'department_id');
if ($departmentId <= 0) {
continue;
}
2026-02-23 12:58:19 +01:00
$result[$departmentId] = $this->extractIntField($row, 'user_count');
}
return $result;
}
2026-02-23 12:58:19 +01:00
private function extractIntField(array $row, string $field): int
{
foreach ($row as $value) {
if (!is_array($value)) {
continue;
}
if (array_key_exists($field, $value)) {
return (int) $value[$field];
}
}
if (array_key_exists($field, $row)) {
return (int) $row[$field];
}
return 0;
}
2026-02-04 23:31:53 +01:00
}