Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable0.7] Fix filters for meta columns #1028

Merged
merged 6 commits into from
Apr 29, 2024
Merged
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
7 changes: 7 additions & 0 deletions lib/Db/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
* @method setDatetimeDefault(?string $datetimeDefault)
*/
class Column extends Entity implements JsonSerializable {
// Meta column types
public const TYPE_META_ID = -1;
public const TYPE_META_CREATED_BY = -2;
public const TYPE_META_UPDATED_BY = -3;
public const TYPE_META_CREATED_AT = -4;
public const TYPE_META_UPDATED_AT = -5;

public const TYPE_SELECTION = 'selection';
public const TYPE_TEXT = 'text';
public const TYPE_NUMBER = 'number';
Expand Down
10 changes: 5 additions & 5 deletions lib/Db/ColumnMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ public function getColumnTypes(array $neededColumnIds): array {

// Initialise return array with column types of the meta columns: id, created_by, created_at, last_edit_by, last_edit_at
$out = [
-1 => 'number',
-2 => 'text-line',
-3 => 'datetime',
-4 => 'text-line',
-5 => 'datetime',
Column::TYPE_META_ID => 'number',
Column::TYPE_META_CREATED_BY => 'text-line',
Column::TYPE_META_CREATED_AT => 'datetime',
Column::TYPE_META_UPDATED_BY => 'text-line',
Column::TYPE_META_UPDATED_AT => 'datetime',
];
$result = $qb->executeQuery();
try {
Expand Down
11 changes: 6 additions & 5 deletions lib/Db/ColumnTypes/SuperColumnQB.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\Tables\Db\ColumnTypes;

use OCA\Tables\Db\Column;
use OCA\Tables\Errors\InternalError;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
Expand Down Expand Up @@ -96,15 +97,15 @@ private function getFormattedDataCellValue(string $columnPlaceHolder, int $colum
*/
public static function getMetaColumnName(int $metaId): string {
switch ($metaId) {
case -1:
case Column::TYPE_META_ID:
return 'id';
case -2:
case Column::TYPE_META_CREATED_BY:
return 'created_by';
case -3:
case Column::TYPE_META_UPDATED_BY:
return 'last_edit_by';
case -4:
case Column::TYPE_META_CREATED_AT:
return 'created_at';
case -5:
case Column::TYPE_META_UPDATED_AT:
return 'last_edit_at';
default:
throw new InternalError('No meta data column exists with id ' . $metaId);
Expand Down
22 changes: 9 additions & 13 deletions lib/Db/Row2Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,15 @@ private function getFilter(IQueryBuilder &$qb, array $filterGroup): array {
$filterExpressions = [];
foreach ($filterGroup as $filter) {
$columnId = $filter['columnId'];
if (!isset($this->columns[$columnId]) && !isset($this->allColumns[$columnId])) {
// Fail if the filter is for a column that is not in the list and no meta column
if (!isset($this->columns[$columnId]) && !isset($this->allColumns[$columnId]) && $columnId > 0) {
throw new InternalError('No column found to build filter with for id ' . $columnId);
}
$column = $this->columns[$columnId] ?? $this->allColumns[$columnId];

// if is normal column
if ($columnId >= 0) {
$column = $this->columns[$columnId] ?? $this->allColumns[$columnId];

$sql = $qb->expr()->in(
'id',
$qb->createFunction($this->getFilterExpression($qb, $column, $filter['operator'], $filter['value'])->getSQL())
Expand Down Expand Up @@ -392,12 +394,6 @@ private function getFilterExpression(IQueryBuilder $qb, Column $column, string $
}

/**
*
* -1 => 'number', ID
* -2 => 'text-line', Created
* -3 => 'datetime', At
* -4 => 'text-line', LastEdit
* -5 => 'datetime', At
* @throws InternalError
*/
private function getMetaFilterExpression(IQueryBuilder $qb, int $columnId, string $operator, string $value): IQueryBuilder {
Expand All @@ -406,20 +402,20 @@ private function getMetaFilterExpression(IQueryBuilder $qb, int $columnId, strin
$qb2->from('tables_row_sleeves');

switch ($columnId) {
case -1: // row ID
case Column::TYPE_META_ID:
$qb2->where($this->getSqlOperator($operator, $qb, 'id', (int)$value, IQueryBuilder::PARAM_INT));
break;
case -2: // created by
case Column::TYPE_META_CREATED_BY:
$qb2->where($this->getSqlOperator($operator, $qb, 'created_by', $value, IQueryBuilder::PARAM_STR));
break;
case -3: // created at
case Column::TYPE_META_CREATED_AT:
$value = new \DateTimeImmutable($value);
$qb2->where($this->getSqlOperator($operator, $qb, 'created_at', $value, IQueryBuilder::PARAM_DATE));
break;
case -4: // last edit by
case Column::TYPE_META_UPDATED_BY:
$qb2->where($this->getSqlOperator($operator, $qb, 'last_edit_by', $value, IQueryBuilder::PARAM_STR));
break;
case -5: // last edit at
case Column::TYPE_META_UPDATED_AT:
$value = new \DateTimeImmutable($value);
$qb2->where($this->getSqlOperator($operator, $qb, 'last_edit_at', $value, IQueryBuilder::PARAM_DATE));
break;
Expand Down
10 changes: 5 additions & 5 deletions lib/Service/TableTemplateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ private function makeVacationRequests(Table $table):void {
'title' => $this->l->t('Create Vacation Request'),
'emoji' => '️➕',
'columns' => json_encode([$columns['employee']->getId(), $columns['from']->getId(), $columns['to']->getId(), $columns['workingDays']->getId(), $columns['dateRequest']->getId()]),
'sort' => json_encode([["columnId" => $columns['from']->getId(), "mode" => "ASC"]]),
'filter' => json_encode([[["columnId" => $columns['employee']->getId(), "operator" => "is-equal", "value" => "@my-name"], ["columnId" => $columns['approved']->getId(), "operator" => "is-empty", "value" => ""]]]),
'sort' => json_encode([["columnId" => Column::TYPE_META_UPDATED_AT, "mode" => "ASC"]]),
'filter' => json_encode([[["columnId" => Column::TYPE_META_CREATED_BY, "operator" => "is-equal", "value" => "@my-name"], ["columnId" => $columns['approved']->getId(), "operator" => "is-empty", "value" => ""]]]),
]
);
$this->createView($table,
Expand All @@ -491,8 +491,8 @@ private function makeVacationRequests(Table $table):void {
'columns' => json_encode(array_values(array_map(function ($col) {
return $col->getId();
}, $columns))),
'sort' => json_encode([["columnId" => $columns['dateRequest']->getId(), "mode" => "ASC"]]),
'filter' => json_encode([[["columnId" => $columns['employee']->getId(), "operator" => "is-equal", "value" => "@my-name"]]]),
'sort' => json_encode([["columnId" => Column::TYPE_META_UPDATED_BY, "mode" => "ASC"]]),
'filter' => json_encode([[["columnId" => Column::TYPE_META_CREATED_BY, "operator" => "is-equal", "value" => "@my-name"]]]),
]
);
$this->createView($table,
Expand All @@ -502,7 +502,7 @@ private function makeVacationRequests(Table $table):void {
'columns' => json_encode(array_values(array_map(function ($col) {
return $col->getId();
}, $columns))),
'sort' => json_encode([["columnId" => $columns['dateRequest']->getId(), "mode" => "ASC"]]),
'sort' => json_encode([["columnId" => Column::TYPE_META_UPDATED_BY, "mode" => "ASC"]]),
'filter' => json_encode([[["columnId" => $columns['approved']->getId(), "operator" => "is-equal", "value" => "@checked"]], [["columnId" => $columns['approved']->getId(), "operator" => "is-equal", "value" => "@unchecked"]]]),
]
);
Expand Down
13 changes: 8 additions & 5 deletions src/shared/components/ncTable/mixins/exportTableMixin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import moment from '@nextcloud/moment'
import generalHelper from '../../../mixins/generalHelper.js'
import {
TYPE_META_ID, TYPE_META_CREATED_BY, TYPE_META_CREATED_AT, TYPE_META_UPDATED_BY, TYPE_META_UPDATED_AT,
} from '../../../../shared/constants.js'

export default {

Expand All @@ -23,19 +26,19 @@ export default {
} else {
// if is a meta data column (id < 0)
switch (column.id) {
case -1:
case TYPE_META_ID:
rowData[column.title] = row.id
break
case -2:
case TYPE_META_CREATED_BY:
rowData[column.title] = row.createdBy
break
case -3:
case TYPE_META_UPDATED_BY:
rowData[column.title] = row.lastEditBy
break
case -4:
case TYPE_META_CREATED_AT:
rowData[column.title] = row.createdAt
break
case -5:
case TYPE_META_UPDATED_AT:
rowData[column.title] = row.lastEditAt
break
}
Expand Down
13 changes: 8 additions & 5 deletions src/shared/components/ncTable/partials/TableRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import TableCellMultiSelection from './TableCellMultiSelection.vue'
import TableCellTextRich from './TableCellEditor.vue'
import { ColumnTypes } from './../mixins/columnHandler.js'
import { translate as t } from '@nextcloud/l10n'
import {
TYPE_META_ID, TYPE_META_CREATED_BY, TYPE_META_CREATED_AT, TYPE_META_UPDATED_BY, TYPE_META_UPDATED_AT,
} from '../../../../shared/constants.js'

export default {
name: 'TableRow',
Expand Down Expand Up @@ -110,19 +113,19 @@ export default {
// See metaColumns.js for mapping
let value
switch (columnId) {
case -1:
case TYPE_META_ID:
value = this.row.id
break
case -2:
case TYPE_META_CREATED_BY:
value = this.row.createdBy
break
case -3:
case TYPE_META_UPDATED_BY:
value = this.row.lastEditBy
break
case -4:
case TYPE_META_CREATED_AT:
value = this.row.createdAt
break
case -5:
case TYPE_META_UPDATED_AT:
value = this.row.lastEditAt
break
}
Expand Down
13 changes: 8 additions & 5 deletions src/shared/components/ncTable/sections/CustomTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import TableHeader from '../partials/TableHeader.vue'
import TableRow from '../partials/TableRow.vue'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
import { MagicFields } from '../mixins/magicFields.js'
import {
TYPE_META_ID, TYPE_META_CREATED_BY, TYPE_META_CREATED_AT, TYPE_META_UPDATED_BY, TYPE_META_UPDATED_AT,
} from '../../../../shared/constants.js'

export default {
name: 'CustomTable',
Expand Down Expand Up @@ -140,19 +143,19 @@ export default {
if (column.id < 0) {
cell = { columnId: column.id }
switch (column.id) {
case -1:
case TYPE_META_ID:
cell.value = row.id
break
case -2:
case TYPE_META_CREATED_BY:
cell.value = row.createdBy
break
case -3:
case TYPE_META_UPDATED_BY:
cell.value = row.editedBy
break
case -4:
case TYPE_META_CREATED_AT:
cell.value = row.createdAt
break
case -5:
case TYPE_META_UPDATED_AT:
cell.value = row.editedAt
break
}
Expand Down
11 changes: 11 additions & 0 deletions src/shared/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ export const PERMISSION_UPDATE = 4
export const PERMISSION_DELETE = 8
export const PERMISSION_MANAGE = 16
export const PERMISSION_ALL = 31

export const TYPE_META_ID = -1
export const TYPE_META_CREATED_BY = -2
export const TYPE_META_UPDATED_BY = -3
export const TYPE_META_CREATED_AT = -4
export const TYPE_META_UPDATED_AT = -5

export const TYPE_SELECTION = 'selection'
export const TYPE_TEXT = 'text'
export const TYPE_NUMBER = 'number'
export const TYPE_DATETIME = 'datetime'
Loading