Skip to content

Commit 6a4e073

Browse files
committed
feat: add relation column type
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent cc96280 commit 6a4e073

13 files changed

Lines changed: 172 additions & 102 deletions

File tree

lib/Controller/Api1Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ public function createRowInTable(int $tableId, $data): DataResponse {
13791379
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
13801380
public function getRow(int $rowId): DataResponse {
13811381
try {
1382-
return new DataResponse($this->rowService->find($rowId, $this->userId)->jsonSerialize());
1382+
return new DataResponse($this->rowService->find($rowId)->jsonSerialize());
13831383
} catch (PermissionError $e) {
13841384
$this->logger->warning('A permission error occurred: ' . $e->getMessage(), ['exception' => $e]);
13851385
$message = ['message' => $e->getMessage()];

lib/Controller/RowController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function indexView(int $viewId): DataResponse {
4747
#[NoAdminRequired]
4848
public function show(int $id): DataResponse {
4949
return $this->handleError(function () use ($id) {
50-
return $this->service->find($id, $this->userId);
50+
return $this->service->find($id);
5151
});
5252
}
5353

lib/Db/RowCellRelation.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66

77
class RowCellRelation extends RowCellSuper {
88
protected ?int $value = null;
9-
protected ?int $valueType = null;
109

1110
public function __construct() {
1211
parent::__construct();
1312
$this->addType('value', 'integer');
1413
}
1514

1615
public function jsonSerialize(): array {
17-
return parent::jsonSerializePreparation($this->value, $this->valueType);
16+
return parent::jsonSerializePreparation($this->value);
1817
}
1918
}

lib/Db/RowCellRelationMapper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ public function getDbParamType() {
2828
return IQueryBuilder::PARAM_INT;
2929
}
3030

31-
/**
32-
* @inheritDoc
33-
*/
34-
public function format(Column $column, ?string $value) {
31+
public function formatRowData(Column $column, array $row) {
32+
$value = $row['value'];
3533
return (int)$value;
3634
}
3735
}

lib/Migration/Version002001Date20260109000000.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
1515
/** @var ISchemaWrapper $schema */
1616
$schema = $schemaClosure();
1717

18-
$changes = $this->createRelationTable($schema, 'relation', Types::INTEGER);
18+
$changes = $this->createRowValueTable($schema, 'relation', Types::INTEGER);
1919
return $changes;
2020
}
2121

22-
private function createRelationTable(ISchemaWrapper $schema, string $name, string $type): ?ISchemaWrapper {
22+
private function createRowValueTable(ISchemaWrapper $schema, string $name, string $type): ?ISchemaWrapper {
2323
if (!$schema->hasTable('tables_row_cells_' . $name)) {
2424
$table = $schema->createTable('tables_row_cells_' . $name);
2525
$table->addColumn('id', Types::INTEGER, [

lib/Service/ColumnTypes/RelationBusiness.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
class RelationBusiness extends SuperBusiness implements IColumnTypeBusiness {
1515

16-
public function __construct(LoggerInterface $logger, private RelationService $relationService) {
17-
parent::__construct($logger);
18-
}
16+
public function __construct(
17+
LoggerInterface $logger,
18+
private RelationService $relationService,
19+
) {
20+
parent::__construct($logger);
21+
}
1922

2023
/**
2124
* @param mixed $value (array|string|null)
@@ -28,18 +31,18 @@ public function parseValue($value, ?Column $column = null): string {
2831
return '';
2932
}
3033

31-
$relationData = $this->relationService->getRelationData($column);
34+
$relationData = $this->relationService->getRelationData($column);
3235

33-
if (is_array($value) && isset($value['context']) && $value['context'] === 'import') {
34-
$matchingRelation = array_filter($relationData, fn($relation) => $relation['label'] === $value['value']);
36+
if (is_array($value) && isset($value['context']) && $value['context'] === 'import') {
37+
$matchingRelation = array_filter($relationData, fn ($relation) => $relation['label'] === $value['value']);
3538
if (!empty($matchingRelation)) {
3639
return json_encode(reset($matchingRelation)['id']);
3740
}
38-
} else {
39-
if (isset($relationData[$value])) {
40-
return json_encode($relationData[$value]['id']);
41-
}
42-
}
41+
} else {
42+
if (isset($relationData[$value])) {
43+
return json_encode($relationData[$value]['id']);
44+
}
45+
}
4346

4447
return '';
4548
}
@@ -58,18 +61,18 @@ public function canBeParsed($value, ?Column $column = null): bool {
5861
return true;
5962
}
6063

61-
$relationData = $this->relationService->getRelationData($column);
64+
$relationData = $this->relationService->getRelationData($column);
6265

63-
if (is_array($value) && isset($value['context']) && $value['context'] === 'import') {
64-
$matchingRelation = array_filter($relationData, fn($relation) => $relation['label'] === $value['value']);
66+
if (is_array($value) && isset($value['context']) && $value['context'] === 'import') {
67+
$matchingRelation = array_filter($relationData, fn ($relation) => $relation['label'] === $value['value']);
6568
if (!empty($matchingRelation)) {
6669
return true;
6770
}
68-
} else {
69-
if (isset($relationData[$value])) {
70-
return true;
71-
}
72-
}
71+
} else {
72+
if (isset($relationData[$value])) {
73+
return true;
74+
}
75+
}
7376

7477
return false;
7578
}

lib/Service/RelationService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
Row2Mapper $row2Mapper,
3636
ColumnService $columnService,
3737
LoggerInterface $logger,
38-
?string $userId
38+
?string $userId,
3939
) {
4040
$this->columnMapper = $columnMapper;
4141
$this->viewMapper = $viewMapper;
@@ -58,7 +58,7 @@ public function getRelationsForTable(int $tableId): array {
5858
// Check table permissions through ColumnService
5959
$columns = $this->columnService->findAllByTable($tableId);
6060

61-
$relationColumns = array_filter($columns, function($column) {
61+
$relationColumns = array_filter($columns, function ($column) {
6262
return $column->getType() === Column::TYPE_RELATION;
6363
});
6464

@@ -78,7 +78,7 @@ public function getRelationsForView(int $viewId): array {
7878
// Check view permissions through ColumnService
7979
$columns = $this->columnService->findAllByView($viewId);
8080

81-
$relationColumns = array_filter($columns, function($column) {
81+
$relationColumns = array_filter($columns, function ($column) {
8282
return $column->getType() === Column::TYPE_RELATION;
8383
});
8484

@@ -215,7 +215,7 @@ private function getRelationDataForTarget(string $target, Column $column): array
215215
$result = [];
216216
foreach ($rows as $row) {
217217
$data = $row->getData();
218-
$displayFieldData = array_filter($data, function($item) use ($settings) {
218+
$displayFieldData = array_filter($data, function ($item) use ($settings) {
219219
return $item['columnId'] === (int)$settings['labelColumn'];
220220
});
221221
$value = reset($displayFieldData)['value'] ?? null;

openapi.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,7 @@
138138
"type": "string"
139139
},
140140
"type": {
141-
"type": "string",
142-
"enum": [
143-
"text",
144-
"number",
145-
"datetime",
146-
"select",
147-
"usergroup",
148-
"relation"
149-
],
150-
"description": "Column main type"
141+
"type": "string"
151142
},
152143
"subtype": {
153144
"type": "string"

src/modules/modals/CreateColumn.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ export default {
307307
} else if (this.column.type === 'relation' && !this.column.customSettings?.relationType) {
308308
showInfo(t('tables', 'Please select a relation type.'))
309309
} else if (this.column.type === 'relation' && !this.column.customSettings?.targetId) {
310-
showInfo(t('tables', 'Please select a entity.'))
310+
showInfo(t('tables', 'Please select a target.'))
311311
} else if (this.column.type === 'relation' && !this.column.customSettings?.labelColumn) {
312-
showInfo(t('tables', 'Please select a display field.'))
312+
showInfo(t('tables', 'Please select a value selection label.'))
313313
} else {
314314
this.$emit('save', this.prepareSubmitData())
315315
if (this.isCustomSave) {
@@ -383,7 +383,6 @@ export default {
383383
data.customSettings.relationType = this.column.customSettings.relationType
384384
data.customSettings.targetId = this.column.customSettings.targetId
385385
data.customSettings.labelColumn = this.column.customSettings.labelColumn
386-
data.customSettings.displayColumns = this.column.customSettings.displayColumns
387386
}
388387
return data
389388
},

src/shared/components/ncTable/mixins/columnsTypes/relation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class RelationColumn extends AbstractColumn {
6060
const columnRelations = dataStore.getRelations(this.id)
6161
const option = columnRelations[rowId]
6262

63-
return option ? option.label : ''
63+
return option ? option.label : undefined
6464
} catch (error) {
6565
console.warn('Failed to get relation label:', error)
6666
return ''

0 commit comments

Comments
 (0)