Skip to content

Commit

Permalink
fix: update usergroup column
Browse files Browse the repository at this point in the history
Signed-off-by: Cleopatra Enjeck M <patrathewhiz@gmail.com>
  • Loading branch information
enjeck committed Jun 4, 2024
1 parent 0828ec0 commit e514050
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
42 changes: 34 additions & 8 deletions lib/Db/Row2Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private function getRows(array $rowIds, array $columnIds): array {
$qbSqlForColumnTypes = null;
foreach ($this->columnsHelper->columns as $columnType) {
$qbTmp = $this->db->getQueryBuilder();
$qbTmp->select('row_id', 'column_id', 'last_edit_at', 'last_edit_by')
$qbTmp->select('row_id', 'column_id', 'value_type', 'last_edit_at', 'last_edit_by')
->selectAlias($qb->expr()->castColumn('value', IQueryBuilder::PARAM_STR), 'value')
->from('tables_row_cells_' . $columnType)
->where($qb->expr()->in('column_id', $qb->createNamedParameter($columnIds, IQueryBuilder::PARAM_INT_ARRAY, ':columnIds')))
Expand All @@ -207,7 +207,7 @@ private function getRows(array $rowIds, array $columnIds): array {
}
$qbSqlForColumnTypes .= ')';

$qb->select('row_id', 'column_id', 'created_by', 'created_at', 't1.last_edit_by', 't1.last_edit_at', 'value', 'table_id')
$qb->select('row_id', 'column_id', 'created_by', 'created_at', 't1.last_edit_by', 't1.last_edit_at', 'value', 'value_type', 'table_id')
->from($qb->createFunction($qbSqlForColumnTypes), 't1')
->innerJoin('t1', 'tables_row_sleeves', 'rs', 'rs.id = t1.row_id');

Expand All @@ -225,7 +225,14 @@ private function getRows(array $rowIds, array $columnIds): array {
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

return $this->parseEntities($result, $sleeves);
try {
$columnTypes = $this->columnMapper->getColumnTypes($columnIds);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

return $this->parseEntities($result, $sleeves, $columnTypes);
}

/**
Expand Down Expand Up @@ -492,7 +499,7 @@ private function resolveSearchValue(string $placeholder, string $userId): string
* @return Row2[]
* @throws InternalError
*/
private function parseEntities(IResult $result, array $sleeves): array {
private function parseEntities(IResult $result, array $sleeves, array $columnTypes): array {
$data = $result->fetchAll();

$rows = [];
Expand All @@ -506,13 +513,32 @@ private function parseEntities(IResult $result, array $sleeves): array {
$rows[$sleeve->getId()]->setTableId($sleeve->getTableId());
}

$rowValues = [];
$keyToColumnId = [];
$keyToRowId = [];

foreach ($data as $rowData) {
if (!isset($rowData['row_id']) || !isset($rows[$rowData['row_id']])) {
break;
}
$value = $this->formatValue($this->columns[$rowData['column_id']], $rowData['value'], 'out', $rowData['value_type']);
$compositeKey = (string)$rowData['row_id'] . ',' . (string)$rowData['column_id'];

if ($columnTypes[$rowData['column_id']] == 'usergroup') {
if (array_key_exists($compositeKey, $rowValues)) {
$rowValues[$compositeKey][] = $value;
} else {
$rowValues[$compositeKey] = [$value];
}
} else {
$rowValues[$compositeKey] = $value;
}
$keyToColumnId[$compositeKey] = $rowData['column_id'];
$keyToRowId[$compositeKey] = $rowData['row_id'];
}

/* @var array $rowData */
$rows[$rowData['row_id']]->addCell($rowData['column_id'], $this->formatValue($this->columns[$rowData['column_id']], $rowData['value']));
foreach ($rowValues as $compositeKey => $value) {
$rows[$keyToRowId[$compositeKey]]->addCell($keyToColumnId[$compositeKey], $value);
}

// format an array without keys
Expand Down Expand Up @@ -766,7 +792,7 @@ private function setColumns(array $columns, array $tableColumns = []): void {
* @return mixed
* @throws InternalError
*/
private function formatValue(Column $column, $value, string $mode = 'out') {
private function formatValue(Column $column, $value, string $mode = 'out', ?int $value_type = null) {
$cellMapperClassName = 'OCA\Tables\Db\RowCell'.ucfirst($column->getType()).'Mapper';
/** @var RowCellMapperSuper $cellMapper */
try {
Expand All @@ -776,7 +802,7 @@ private function formatValue(Column $column, $value, string $mode = 'out') {
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
}
if ($mode === 'out') {
return $cellMapper->parseValueOutgoing($column, $value);
return $cellMapper->parseValueOutgoing($column, $value, $value_type);
} else {
return $cellMapper->parseValueIncoming($column, $value);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/RowCellUsergroupMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function parseValueIncoming(Column $column, $value): array {
/**
* @inheritDoc
*/
public function parseValueOutgoing(Column $column, $value) {
return json_decode($value);
public function parseValueOutgoing(Column $column, $value, ?int $value_type = null): array {
return ['id' => $value, 'type' => $value_type];
}
}

0 comments on commit e514050

Please sign in to comment.