diff --git a/lib/Api/V1Api.php b/lib/Api/V1Api.php index 960900641..1b68f37c8 100644 --- a/lib/Api/V1Api.php +++ b/lib/Api/V1Api.php @@ -3,9 +3,12 @@ namespace OCA\Tables\Api; use OCA\Tables\Errors\InternalError; +use OCA\Tables\Errors\NotFoundError; use OCA\Tables\Errors\PermissionError; use OCA\Tables\Service\ColumnService; use OCA\Tables\Service\RowService; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; class V1Api { private RowService $rowService; @@ -19,17 +22,30 @@ public function __construct(ColumnService $columnService, RowService $rowService } /** - * @param int $tableId + * @param int $nodeId * @param int|null $limit * @param int|null $offset + * @param string|null $userId + * @param string|null $nodeType * @return array + * @throws DoesNotExistException * @throws InternalError + * @throws MultipleObjectsReturnedException + * @throws NotFoundError * @throws PermissionError */ - public function getData(int $tableId, ?int $limit, ?int $offset): array { - $columns = $this->columnService->findAllByTable($tableId); - - $rows = $this->rowService->findAllByTable($tableId, $this->userId, $limit, $offset); + public function getData(int $nodeId, ?int $limit, ?int $offset, ?string $userId, ?string $nodeType = null): array { + if ($userId) { + $this->userId = $userId; + } + if ($nodeType === 'view') { + $columns = $this->columnService->findAllByView($nodeId, $this->userId); + $rows = $this->rowService->findAllByView($nodeId, $this->userId, $limit, $offset); + } else { + // if no nodeType is provided, the old table selection is used to not break anything + $columns = $this->columnService->findAllByTable($nodeId, null, $this->userId); + $rows = $this->rowService->findAllByTable($nodeId, $this->userId, $limit, $offset); + } $data = []; diff --git a/lib/Controller/Api1Controller.php b/lib/Controller/Api1Controller.php index bf5c76578..09b719b04 100644 --- a/lib/Controller/Api1Controller.php +++ b/lib/Controller/Api1Controller.php @@ -468,7 +468,7 @@ public function deleteColumn(int $columnId): DataResponse { */ public function indexTableRowsSimple(int $tableId, ?int $limit, ?int $offset): DataResponse { return $this->handleError(function () use ($tableId, $limit, $offset) { - return $this->v1Api->getData($tableId, $limit, $offset); + return $this->v1Api->getData($tableId, $limit, $offset, $this->userId, 'table'); }); } diff --git a/lib/Datasource/AnalyticsDatasource.php b/lib/Datasource/AnalyticsDatasource.php index b095882c6..783f29e94 100644 --- a/lib/Datasource/AnalyticsDatasource.php +++ b/lib/Datasource/AnalyticsDatasource.php @@ -14,8 +14,12 @@ use OCA\Analytics\Datasource\IDatasource; use OCA\Tables\Api\V1Api; use OCA\Tables\Errors\InternalError; +use OCA\Tables\Errors\NotFoundError; use OCA\Tables\Errors\PermissionError; use OCA\Tables\Service\TableService; +use OCA\Tables\Service\ViewService; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\IL10N; use Psr\Log\LoggerInterface; @@ -23,6 +27,7 @@ class AnalyticsDatasource implements IDatasource { private LoggerInterface $logger; private IL10N $l10n; private TableService $tableService; + private ViewService $viewService; private V1Api $api; protected ?string $userId; @@ -31,12 +36,14 @@ public function __construct( IL10N $l10n, LoggerInterface $logger, TableService $tableService, + ViewService $viewService, V1Api $api, - string $userId + ?string $userId ) { $this->l10n = $l10n; $this->logger = $logger; $this->tableService = $tableService; + $this->viewService = $viewService; $this->api = $api; $this->userId = $userId; } @@ -45,7 +52,7 @@ public function __construct( * @return string Display Name of the datasource */ public function getName(): string { - return $this->l10n->t('Nextcloud tables'); + return $this->l10n->t('Nextcloud Tables'); } /** @@ -75,25 +82,36 @@ public function getId(): int { * {['id' => 'datatype', 'name' => 'Type of Data', 'type' => 'tf', 'placeholder' => 'adaptation/absolute']} * * @return array + * @throws DoesNotExistException * @throws InternalError + * @throws MultipleObjectsReturnedException + * @throws NotFoundError + * @throws PermissionError */ public function getTemplate(): array { $tableString = ''; $template = []; + $tables = []; - // get all tables for the current user and concatenate the placeholder string + // get all tables for the user if ($this->userId) { $tables = $this->tableService->findAll($this->userId); - } else { - $tables = []; } + + // concatenate the option-string. The format is tableId:viewId-title foreach ($tables as $table) { - $tableString = $tableString . $table->jsonSerialize()['id'] . '-' . $table->jsonSerialize()['title'] . '/'; + $tableString = $tableString . $table->getId() . '-' . $table->getTitle() . '/'; + // get all views per table + $views = $this->viewService->findAll($this->tableService->find($table->getId())); + foreach ($views as $view) { + $tableString = $tableString . $table->getId() . ':' . $view->getId() . '-' . $view->getTitle() . '/'; + } } // add the tables to a dropdown in the data source settings $template[] = ['id' => 'tableId', 'name' => $this->l10n->t('Select table'), 'type' => 'tf', 'placeholder' => $tableString]; $template[] = ['id' => 'columns', 'name' => $this->l10n->t('Select columns'), 'placeholder' => $this->l10n->t('e.g. 1,2,4 or leave empty'), 'type' => 'columnPicker']; + $template[] = ['id' => 'timestamp', 'name' => $this->l10n->t('Timestamp of data load'), 'placeholder' => 'false-' . $this->l10n->t('No') . '/true-' . $this->l10n->t('Yes'), 'type' => 'tf']; return $template; } @@ -112,11 +130,22 @@ public function getTemplate(): array { * @return array available options of the data source * @throws InternalError * @throws PermissionError + * @throws NotFoundError + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException */ public function readData($option): array { - // get the data for the selected table - $tableId = $option['tableId']; - $data = $this->api->getData($tableId, null, null); + // get the ids which come in the format tableId:viewId + $ids = explode(':', $option['tableId']); + $this->userId = $option['user_id']; + + if (count($ids) === 1) { + // it's a table + $data = $this->api->getData((int) $ids[0], null, null, $this->userId); + } elseif (count($ids) === 2) { + // it's a view + $data = $this->api->getData((int) $ids[1], null, null, $this->userId, 'view'); + } // extract the header from the first row $header = $data[0]; @@ -147,7 +176,7 @@ public function readData($option): array { 'header' => $header, 'dimensions' => array_slice($header, 0, count($header) - 1), 'data' => $data, - 'rawdata' => $data, + //'rawdata' => $data, 'error' => 0, ]; } @@ -162,7 +191,7 @@ public function readData($option): array { private function minimizeRow(array $selectedColumns, array $row): array { $rowMinimized = []; foreach ($selectedColumns as $selectedColumn) { - if ((int) $selectedColumn) { + if ((int)$selectedColumn) { $rowMinimized[] = $row[$selectedColumn - 1]; } else { $rowMinimized[] = $selectedColumn; diff --git a/lib/Service/ColumnService.php b/lib/Service/ColumnService.php index 0bc694fe2..53e5a1d69 100644 --- a/lib/Service/ColumnService.php +++ b/lib/Service/ColumnService.php @@ -65,6 +65,7 @@ public function findAllByTable(int $tableId, ?int $viewId = null, ?string $userI /** * @param int $viewId + * @param string|null $userId * @return array * @throws DoesNotExistException * @throws InternalError @@ -72,10 +73,10 @@ public function findAllByTable(int $tableId, ?int $viewId = null, ?string $userI * @throws NotFoundError * @throws PermissionError */ - public function findAllByView(int $viewId): array { + public function findAllByView(int $viewId, ?string $userId = null): array { try { // No need to check for columns outside the view since they cannot be addressed - $view = $this->viewService->find($viewId, true); + $view = $this->viewService->find($viewId, true, $userId); $viewColumnIds = $view->getColumnsArray(); $viewColumns = []; foreach ($viewColumnIds as $viewColumnId) { @@ -391,12 +392,13 @@ public function delete(int $id, bool $skipRowCleanup = false, ?string $userId = } /** - * @param int $tableId - * @param int $viewId + * @param int|null $tableId + * @param int|null $viewId * @param array $titles example ['Test column 1', 'And so on', '3rd column title'] * @param string|null $userId * @param bool $createUnknownColumns * @param int $countCreatedColumns + * @param int $countMatchingColumns * @return array with column object or null for given columns * @throws DoesNotExistException * @throws InternalError @@ -411,7 +413,7 @@ public function findOrCreateColumnsByTitleForTableAsArray(?int $tableId, ?int $v if($userId === null) { $userId = $this->userId; } - $allColumns = $viewId !== null ? $this->findAllByView($viewId) : $this->findAllByTable($tableId); + $allColumns = $viewId !== null ? $this->findAllByView($viewId, $userId) : $this->findAllByTable($tableId, null, $userId); $i = -1; foreach ($titles as $title) { $i++;