Files
breadcrumb-the-shire/lib/Repository/Access/PermissionRepository.php
2026-02-11 19:28:12 +01:00

141 lines
4.8 KiB
PHP

<?php
namespace MintyPHP\Repository\Access;
use MintyPHP\DB;
use MintyPHP\Repository\Support\RepoQuery;
class PermissionRepository
{
public static function list(): array
{
$rows = DB::select('select id, `key`, description, active, is_system, created from permissions order by `key` asc');
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$data = $row['permissions'] ?? $row;
if (is_array($data)) {
$list[] = $data;
}
}
return $list;
}
public static function listActive(): array
{
$rows = DB::select(
'select id, `key`, description, active, is_system, created from permissions where active = 1 order by `key` asc'
);
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$data = $row['permissions'] ?? $row;
if (is_array($data)) {
$list[] = $data;
}
}
return $list;
}
public static function listPaged(array $options): array
{
$search = trim((string) ($options['search'] ?? ''));
$allowedOrder = [
'key' => '`key`',
'description' => 'description',
'active' => 'active',
'created' => 'created',
];
[$limit, $offset] = RepoQuery::sanitizeLimitOffset($options);
[$order, $dir] = RepoQuery::sanitizeOrder($options, array_keys($allowedOrder), 'key', 'asc');
$orderBy = $allowedOrder[$order] ?? $allowedOrder['key'];
$where = [];
$params = [];
RepoQuery::addLikeFilter($where, $params, ['permissions.`key`', 'permissions.description'], $search);
$activeValue = array_key_exists('active', $options) ? $options['active'] : null;
RepoQuery::addEnumFilter($where, $params, $activeValue, [
['aliases' => ['1', 'true', 'active'], 'sql' => 'permissions.active = 1'],
['aliases' => ['0', 'false', 'inactive'], 'sql' => 'permissions.active = 0'],
]);
$whereSql = $where ? ' where ' . implode(' and ', $where) : '';
$total = DB::selectValue('select count(*) from permissions' . $whereSql, ...$params);
$query = 'select id, `key`, description, active, is_system, created from permissions' . $whereSql .
' order by ' . $orderBy . ' ' . $dir . ' limit ? offset ?';
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
$rows = DB::select($query, ...$queryParams);
$list = [];
foreach ($rows as $row) {
$data = $row['permissions'] ?? $row;
if (is_array($data)) {
$list[] = $data;
}
}
return ['data' => $list, 'total' => (int) $total];
}
public static function find(int $id): ?array
{
$row = DB::selectOne(
'select id, `key`, description, active, is_system, created from permissions where id = ? limit 1',
(string) $id
);
$data = $row['permissions'] ?? $row;
return is_array($data) ? $data : null;
}
public static function findByKey(string $key): ?array
{
$row = DB::selectOne(
'select id, `key`, description, active, is_system, created from permissions where `key` = ? limit 1',
$key
);
$data = $row['permissions'] ?? $row;
return is_array($data) ? $data : null;
}
public static function create(array $data): ?int
{
$key = trim((string) ($data['key'] ?? ''));
$desc = trim((string) ($data['description'] ?? ''));
$active = (int) ($data['active'] ?? 1);
$isSystem = (int) ($data['is_system'] ?? 0);
$result = DB::insert(
'insert into permissions (`key`, description, active, is_system, created) values (?,?,?,?,NOW())',
$key,
$desc !== '' ? $desc : null,
(string) $active,
(string) $isSystem
);
return $result ? (int) $result : null;
}
public static function update(int $id, array $data): bool
{
$key = trim((string) ($data['key'] ?? ''));
$desc = trim((string) ($data['description'] ?? ''));
$active = (int) ($data['active'] ?? 1);
$isSystem = (int) ($data['is_system'] ?? 0);
$result = DB::update(
'update permissions set `key` = ?, description = ?, active = ?, is_system = ? where id = ?',
$key,
$desc !== '' ? $desc : null,
(string) $active,
(string) $isSystem,
(string) $id
);
return $result !== false;
}
public static function delete(int $id): bool
{
$result = DB::delete('delete from permissions where id = ?', (string) $id);
return (bool) $result;
}
}