forked from fa/breadcrumb-the-shire
big restructure
This commit is contained in:
54
lib/Repository/Org/UserDepartmentRepository.php
Normal file
54
lib/Repository/Org/UserDepartmentRepository.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\Org;
|
||||
|
||||
use MintyPHP\DB;
|
||||
|
||||
class UserDepartmentRepository
|
||||
{
|
||||
public static function listDepartmentIdsByUserId(int $userId): array
|
||||
{
|
||||
$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));
|
||||
}
|
||||
|
||||
public static function replaceForUser(int $userId, array $departmentIds): bool
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public static function removeInvalidForDepartment(int $departmentId): int
|
||||
{
|
||||
$query = 'delete ud from user_departments ud ' .
|
||||
'where ud.department_id = ? and not exists (' .
|
||||
'select 1 from user_tenants ut ' .
|
||||
'join tenant_departments td on td.tenant_id = ut.tenant_id ' .
|
||||
'where ut.user_id = ud.user_id and td.department_id = ud.department_id' .
|
||||
')';
|
||||
$result = DB::delete($query, (string) $departmentId);
|
||||
return $result !== false ? (int) $result : 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user