'`key`', 'description' => 'description', 'created' => 'created']; $orderBy = $allowedOrder[$order] ?? $allowedOrder['key']; $where = []; $params = []; if ($search !== '') { $where[] = '(permissions.`key` like ? or permissions.description like ?)'; $params[] = '%' . $search . '%'; $params[] = '%' . $search . '%'; } $whereSql = $where ? ' where ' . implode(' and ', $where) : ''; $total = DB::selectValue('select count(*) from permissions' . $whereSql, ...$params); $query = 'select id, `key`, description, 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, 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, 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'] ?? '')); $result = DB::insert( 'insert into permissions (`key`, description, created) values (?,?,NOW())', $key, $desc !== '' ? $desc : null ); return $result ? (int) $result : null; } public static function update(int $id, array $data): bool { $key = trim((string) ($data['key'] ?? '')); $desc = trim((string) ($data['description'] ?? '')); $result = DB::update( 'update permissions set `key` = ?, description = ? where id = ?', $key, $desc !== '' ? $desc : null, (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; } }