From 45dbc4a41a56bd4a7fc9d53e923e9e65131b6c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 8 Feb 2024 09:56:12 +0100 Subject: [PATCH] fix: Pass all table columns along as we need them for filtering the rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/Row2Mapper.php | 23 +++++++++++++++++------ lib/Service/RowService.php | 9 ++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/Db/Row2Mapper.php b/lib/Db/Row2Mapper.php index f8603ddee..474e0c0d6 100644 --- a/lib/Db/Row2Mapper.php +++ b/lib/Db/Row2Mapper.php @@ -26,19 +26,23 @@ class Row2Mapper { private IDBConnection $db; private LoggerInterface $logger; protected UserHelper $userHelper; + protected ColumnMapper $columnMapper; /* @var Column[] $columns */ private array $columns = []; + /* @var Column[] $columns */ + private array $allColumns = []; private ColumnsHelper $columnsHelper; - public function __construct(?string $userId, IDBConnection $db, LoggerInterface $logger, UserHelper $userHelper, RowSleeveMapper $rowSleeveMapper, ColumnsHelper $columnsHelper) { + public function __construct(?string $userId, IDBConnection $db, LoggerInterface $logger, UserHelper $userHelper, RowSleeveMapper $rowSleeveMapper, ColumnsHelper $columnsHelper, ColumnMapper $columnMapper) { $this->rowSleeveMapper = $rowSleeveMapper; $this->userId = $userId; $this->db = $db; $this->logger = $logger; $this->userHelper = $userHelper; $this->columnsHelper = $columnsHelper; + $this->columnMapper = $columnMapper; } /** @@ -166,8 +170,8 @@ private function getWantedRowIds(string $userId, int $tableId, ?array $filter = * @return Row2[] * @throws InternalError */ - public function findAll(array $columns, int $tableId, int $limit = null, int $offset = null, array $filter = null, array $sort = null, string $userId = null): array { - $this->setColumns($columns); + public function findAll(array $tableColumns, array $columns, int $tableId, int $limit = null, int $offset = null, array $filter = null, array $sort = null, string $userId = null): array { + $this->setColumns($columns, $tableColumns); $columnIdsArray = array_map(fn (Column $column) => $column->getId(), $columns); $wantedRowIdsArray = $this->getWantedRowIds($userId, $tableId, $filter, $limit, $offset); @@ -267,12 +271,13 @@ private function getFilter(IQueryBuilder &$qb, array $filterGroup): array { $filterExpressions = []; foreach ($filterGroup as $filter) { $columnId = $filter['columnId']; + $column = $this->columns[$columnId] ?? $this->allColumns[$columnId]; // if is normal column if ($columnId >= 0) { $sql = $qb->expr()->in( 'id', - $qb->createFunction($this->getFilterExpression($qb, $this->columns[$filter['columnId']], $filter['operator'], $filter['value'])->getSQL()) + $qb->createFunction($this->getFilterExpression($qb, $column, $filter['operator'], $filter['value'])->getSQL()) ); // if is meta data column @@ -284,7 +289,7 @@ private function getFilter(IQueryBuilder &$qb, array $filterGroup): array { // if column id is unknown } else { - $e = new Exception("Needed column (" . $filter['columnId'] . ") not found."); + $e = new Exception("Needed column (" . $columnId . ") not found."); $this->logger->error($e->getMessage(), ['exception' => $e]); throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage()); } @@ -670,10 +675,16 @@ private function insertOrUpdateCell(int $rowId, int $columnId, $value): void { /** * @param Column[] $columns */ - private function setColumns(array $columns): void { + private function setColumns(array $columns, array $tableColumns = []): void { foreach ($columns as $column) { $this->columns[$column->getId()] = $column; } + + // We hold a list of all table columns to be used in filter expression building for those not visible in the view + foreach ($tableColumns as $column) { + $this->allColumns[$column->getId()] = $column; + } + } /** diff --git a/lib/Service/RowService.php b/lib/Service/RowService.php index c9b04934c..3586220ec 100644 --- a/lib/Service/RowService.php +++ b/lib/Service/RowService.php @@ -65,7 +65,8 @@ public function formatRows(array $rows): array { public function findAllByTable(int $tableId, string $userId, ?int $limit = null, ?int $offset = null): array { try { if ($this->permissionsService->canReadRowsByElementId($tableId, 'table', $userId)) { - return $this->row2Mapper->findAll($this->columnMapper->findAllByTable($tableId), $tableId, $limit, $offset, null, null, $userId); + $tableColumns = $this->columnMapper->findAllByTable($tableId); + return $this->row2Mapper->findAll($tableColumns, $tableColumns, $tableId, $limit, $offset, null, null, $userId); } else { throw new PermissionError('no read access to table id = '.$tableId); } @@ -91,8 +92,10 @@ public function findAllByView(int $viewId, string $userId, ?int $limit = null, ? if ($this->permissionsService->canReadRowsByElementId($viewId, 'view', $userId)) { $view = $this->viewMapper->find($viewId); $columnsArray = $view->getColumnsArray(); - $columns = $this->columnMapper->findAll($columnsArray); - return $this->row2Mapper->findAll($columns, $view->getTableId(), $limit, $offset, $view->getFilterArray(), $view->getSortArray(), $userId); + $filterColumns = $this->columnMapper->findAll($columnsArray); + $tableColumns = $this->columnMapper->findAllByTable($view->getTableId()); + + return $this->row2Mapper->findAll($tableColumns, $filterColumns, $view->getTableId(), $limit, $offset, $view->getFilterArray(), $view->getSortArray(), $userId); } else { throw new PermissionError('no read access to view id = '.$viewId); }