Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
['name' => 'api1#updateColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'PUT'],
['name' => 'api1#getColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'GET'],
['name' => 'api1#deleteColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'DELETE'],
// -> relations
['name' => 'api1#indexTableRelations', 'url' => '/api/1/tables/{tableId}/relations', 'verb' => 'GET'],
['name' => 'api1#indexViewRelations', 'url' => '/api/1/views/{viewId}/relations', 'verb' => 'GET'],
// -> rows
['name' => 'api1#indexTableRowsSimple', 'url' => '/api/1/tables/{tableId}/rows/simple', 'verb' => 'GET'],
['name' => 'api1#indexTableRows', 'url' => '/api/1/tables/{tableId}/rows', 'verb' => 'GET'],
Expand Down
74 changes: 71 additions & 3 deletions lib/Controller/Api1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCA\Tables\ResponseDefinitions;
use OCA\Tables\Service\ColumnService;
use OCA\Tables\Service\ImportService;
use OCA\Tables\Service\RelationService;
use OCA\Tables\Service\RowService;
use OCA\Tables\Service\ShareService;
use OCA\Tables\Service\TableService;
Expand Down Expand Up @@ -57,6 +58,7 @@
private RowService $rowService;
private ImportService $importService;
private ViewService $viewService;
private RelationService $relationService;
private ViewMapper $viewMapper;
private IL10N $l10N;

Expand All @@ -77,6 +79,7 @@
RowService $rowService,
ImportService $importService,
ViewService $viewService,
RelationService $relationService,
ViewMapper $viewMapper,
V1Api $v1Api,
LoggerInterface $logger,
Expand All @@ -90,6 +93,7 @@
$this->rowService = $rowService;
$this->importService = $importService;
$this->viewService = $viewService;
$this->relationService = $relationService;
$this->viewMapper = $viewMapper;
$this->userId = $userId;
$this->v1Api = $v1Api;
Expand Down Expand Up @@ -803,13 +807,77 @@
}
}

/**
* Get all relation data for a table
*
* @param int $tableId Table ID
* @return DataResponse<Http::STATUS_OK, array<string, array<string, array{id: int, label: string}>>, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
*
* 200: Relation data returned
* 403: No permissions
* 404: Not found
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[CORS]
#[RequirePermission(permission: Application::PERMISSION_READ, type: Application::NODE_TYPE_TABLE, idParam: 'tableId')]
public function indexTableRelations(int $tableId): DataResponse {
try {
return new DataResponse($this->relationService->getRelationsForTable($tableId));
} catch (PermissionError $e) {
$this->logger->warning('A permission error occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_FORBIDDEN);
} catch (InternalError $e) {
$this->logger->error('An internal error or exception occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (NotFoundError $e) {
$this->logger->info('A not found error occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_NOT_FOUND);
}
}

/**
* Get all relation data for a view
*
* @param int $viewId View ID
* @return DataResponse<Http::STATUS_OK, array<string, array<string, array{id: int, label: string}>>, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
*
* 200: Relation data returned
* 403: No permissions
* 404: Not found
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[CORS]
#[RequirePermission(permission: Application::PERMISSION_READ, type: Application::NODE_TYPE_VIEW, idParam: 'viewId')]
public function indexViewRelations(int $viewId): DataResponse {
try {
return new DataResponse($this->relationService->getRelationsForView($viewId));
} catch (PermissionError $e) {
$this->logger->warning('A permission error occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_FORBIDDEN);
} catch (InternalError $e) {
$this->logger->error('An internal error or exception occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (NotFoundError $e) {
$this->logger->info('A not found error occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_NOT_FOUND);
}
}

/**
* Create a column
*
* @param int|null $tableId Table ID
* @param int|null $viewId View ID
* @param string $title Title
* @param 'text'|'number'|'datetime'|'select'|'usergroup' $type Column main type
* @param 'text'|'number'|'datetime'|'select'|'usergroup'|'relation' $type Column main type
* @param string|null $subtype Column sub type
* @param bool $mandatory Is the column mandatory
* @param string|null $description Description
Expand Down Expand Up @@ -1311,7 +1379,7 @@
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
public function getRow(int $rowId): DataResponse {
try {
return new DataResponse($this->rowService->find($rowId)->jsonSerialize());
return new DataResponse($this->rowService->find($rowId, $this->userId)->jsonSerialize());

Check failure on line 1382 in lib/Controller/Api1Controller.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable31

TooManyArguments

lib/Controller/Api1Controller.php:1382:47: TooManyArguments: Too many arguments for method OCA\Tables\Service\RowService::find - saw 2 (see https://psalm.dev/026)

Check failure on line 1382 in lib/Controller/Api1Controller.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TooManyArguments

lib/Controller/Api1Controller.php:1382:47: TooManyArguments: Too many arguments for method OCA\Tables\Service\RowService::find - saw 2 (see https://psalm.dev/026)
} catch (PermissionError $e) {
$this->logger->warning('A permission error occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
Expand Down Expand Up @@ -1572,7 +1640,7 @@
*
* @param int $tableId Table ID
* @param string $title Title
* @param 'text'|'number'|'datetime'|'select'|'usergroup' $type Column main type
* @param 'text'|'number'|'datetime'|'select'|'usergroup'|'relation' $type Column main type
* @param string|null $subtype Column sub type
* @param bool $mandatory Is the column mandatory
* @param string|null $description Description
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/RowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#[NoAdminRequired]
public function show(int $id): DataResponse {
return $this->handleError(function () use ($id) {
return $this->service->find($id);
return $this->service->find($id, $this->userId);

Check failure on line 50 in lib/Controller/RowController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable31

TooManyArguments

lib/Controller/RowController.php:50:27: TooManyArguments: Too many arguments for method OCA\Tables\Service\RowService::find - saw 2 (see https://psalm.dev/026)

Check failure on line 50 in lib/Controller/RowController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TooManyArguments

lib/Controller/RowController.php:50:27: TooManyArguments: Too many arguments for method OCA\Tables\Service\RowService::find - saw 2 (see https://psalm.dev/026)
});
}

Expand Down
1 change: 1 addition & 0 deletions lib/Db/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Column extends EntitySuper implements JsonSerializable {
public const TYPE_NUMBER = 'number';
public const TYPE_DATETIME = 'datetime';
public const TYPE_USERGROUP = 'usergroup';
public const TYPE_RELATION = 'relation';

public const SUBTYPE_DATETIME_DATE = 'date';
public const SUBTYPE_DATETIME_TIME = 'time';
Expand Down
19 changes: 19 additions & 0 deletions lib/Db/RowCellRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace OCA\Tables\Db;

class RowCellRelation extends RowCellSuper {

Check failure on line 7 in lib/Db/RowCellRelation.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable31

MissingTemplateParam

lib/Db/RowCellRelation.php:7:7: MissingTemplateParam: OCA\Tables\Db\RowCellRelation has missing template params when extending OCA\Tables\Db\RowCellSuper, expecting 1 (see https://psalm.dev/182)

Check failure on line 7 in lib/Db/RowCellRelation.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

MissingTemplateParam

lib/Db/RowCellRelation.php:7:7: MissingTemplateParam: OCA\Tables\Db\RowCellRelation has missing template params when extending OCA\Tables\Db\RowCellSuper, expecting 1 (see https://psalm.dev/182)
protected ?int $value = null;
protected ?int $valueType = null;

public function __construct() {
parent::__construct();
$this->addType('value', 'integer');
}

public function jsonSerialize(): array {
return parent::jsonSerializePreparation($this->value, $this->valueType);
}
}
37 changes: 37 additions & 0 deletions lib/Db/RowCellRelationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace OCA\Tables\Db;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

class RowCellRelationMapper extends RowCellMapperSuper {

Check failure on line 10 in lib/Db/RowCellRelationMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable31

MissingTemplateParam

lib/Db/RowCellRelationMapper.php:10:7: MissingTemplateParam: OCA\Tables\Db\RowCellRelationMapper has missing template params when extending OCA\Tables\Db\RowCellMapperSuper, expecting 3 (see https://psalm.dev/182)

Check failure on line 10 in lib/Db/RowCellRelationMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

MissingTemplateParam

lib/Db/RowCellRelationMapper.php:10:7: MissingTemplateParam: OCA\Tables\Db\RowCellRelationMapper has missing template params when extending OCA\Tables\Db\RowCellMapperSuper, expecting 3 (see https://psalm.dev/182)
protected string $table = 'tables_row_cells_relation';

public function __construct(IDBConnection $db) {
parent::__construct($db, $this->table, RowCellRelation::class);
}

/**
* @inheritDoc
*/
public function hasMultipleValues(): bool {
return false;
}

/**
* @inheritDoc
*/
public function getDbParamType() {
return IQueryBuilder::PARAM_INT;
}

/**
* @inheritDoc
*/
public function format(Column $column, ?string $value) {
return (int)$value;
}
}
4 changes: 4 additions & 0 deletions lib/Helper/ColumnsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ColumnsHelper {
Column::TYPE_DATETIME,
Column::TYPE_SELECTION,
Column::TYPE_USERGROUP,
Column::TYPE_RELATION,
];

public function __construct(
Expand All @@ -37,6 +38,9 @@ public function resolveSearchValue(string $placeholder, string $userId, ?Column
if (str_starts_with($placeholder, '@selection-id-')) {
return substr($placeholder, 14);
}
if (str_starts_with($placeholder, '@relation-id-')) {
return substr($placeholder, 13);
}

$placeholderParts = explode(':', $placeholder, 2);
$placeholderName = ltrim($placeholderParts[0], '@');
Expand Down
42 changes: 42 additions & 0 deletions lib/Migration/Version002001Date20260109000000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace OCA\Tables\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version002001Date20260109000000 extends SimpleMigrationStep {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$changes = $this->createRelationTable($schema, 'relation', Types::INTEGER);
return $changes;
}

private function createRelationTable(ISchemaWrapper $schema, string $name, string $type): ?ISchemaWrapper {
if (!$schema->hasTable('tables_row_cells_' . $name)) {
$table = $schema->createTable('tables_row_cells_' . $name);
$table->addColumn('id', Types::INTEGER, [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('column_id', Types::INTEGER, ['notnull' => true]);
$table->addColumn('row_id', Types::INTEGER, ['notnull' => true]);
$table->addColumn('value', $type, ['notnull' => false]);
$table->addColumn('last_edit_at', Types::DATETIME, ['notnull' => true]);
$table->addColumn('last_edit_by', Types::STRING, ['notnull' => true, 'length' => 64]);
$table->addIndex(['column_id', 'row_id']);
$table->addIndex(['column_id', 'value']);
$table->setPrimaryKey(['id']);
return $schema;
}

return null;
}
}
76 changes: 76 additions & 0 deletions lib/Service/ColumnTypes/RelationBusiness.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Tables\Service\ColumnTypes;

use OCA\Tables\Db\Column;
use OCA\Tables\Service\RelationService;
use Psr\Log\LoggerInterface;

class RelationBusiness extends SuperBusiness implements IColumnTypeBusiness {

public function __construct(LoggerInterface $logger, private RelationService $relationService) {
parent::__construct($logger);
}

/**
* @param mixed $value (array|string|null)
* @param Column|null $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
if (!$column) {
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
return '';
}

$relationData = $this->relationService->getRelationData($column);

if (is_array($value) && isset($value['context']) && $value['context'] === 'import') {
$matchingRelation = array_filter($relationData, fn($relation) => $relation['label'] === $value['value']);
if (!empty($matchingRelation)) {
return json_encode(reset($matchingRelation)['id']);
}
} else {
if (isset($relationData[$value])) {
return json_encode($relationData[$value]['id']);
}
}

return '';
}

/**
* @param mixed $value (array|string|null)
* @param Column|null $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
if (!$column) {
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
return false;
}
if ($value === null) {
return true;
}

$relationData = $this->relationService->getRelationData($column);

if (is_array($value) && isset($value['context']) && $value['context'] === 'import') {
$matchingRelation = array_filter($relationData, fn($relation) => $relation['label'] === $value['value']);
if (!empty($matchingRelation)) {
return true;
}
} else {
if (isset($relationData[$value])) {
return true;
}
}

return false;
}
}
4 changes: 4 additions & 0 deletions lib/Service/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ private function getPreviewData(Worksheet $worksheet): array {
$value = $cell->getValue();
// $cellIterator`s index is based on 1, not 0.
$colIndex = $cellIterator->getCurrentColumnIndex() - 1;
if (!array_key_exists($colIndex, $this->columns)) {
continue;
}

$column = $this->columns[$colIndex];

if (!array_key_exists($colIndex, $columns)) {
Expand Down
Loading
Loading