From ddac8c8a2951bdb3299e8ea889b4137321e414ab Mon Sep 17 00:00:00 2001 From: DarkSide Date: Mon, 7 Mar 2022 00:22:49 +0200 Subject: [PATCH 01/11] Adds Table\Column\Flag decorator --- demos/collection/grid.php | 8 ++++++ src/Table/Column/Flag.php | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/Table/Column/Flag.php diff --git a/demos/collection/grid.php b/demos/collection/grid.php index ac27e76a8f..9e21215afc 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -14,6 +14,7 @@ use Atk4\Ui\Message; use Atk4\Ui\Table; use Atk4\Ui\UserAction\BasicExecutor; +use Atk4\Ui\Table; /** @var \Atk4\Ui\App $app */ require_once __DIR__ . '/../init-app.php'; @@ -26,6 +27,13 @@ $grid->setModel($model); +// add country flag column +$grid->addColumn('flag', [ + Table\Column\Flag::class, + 'code_field' => $model->fieldName()->iso, + 'name_field' => $model->fieldName()->name, +]); + // Adding Quicksearch on Name field using auto query. $grid->addQuickSearch([$model->fieldName()->name], true); diff --git a/src/Table/Column/Flag.php b/src/Table/Column/Flag.php new file mode 100644 index 0000000000..2507eea1f8 --- /dev/null +++ b/src/Table/Column/Flag.php @@ -0,0 +1,51 @@ +code_field) { + throw new Exception('Country code field must be defined'); + } + } + + public function getHtmlTags(Model $row, $field) + { + if ($row->hasField($this->code_field)) { + $code = $row->get($this->code_field) ?: null; + $name = $this->name_field ? $row->get($this->name_field) : null; + + return [ + $field->short_name => empty($code) ? '' : $this->getApp()->getTag('i', ['class' => strtolower($code) . ' flag', 'title' => $code . ($name ? ': ' . $name : '')]) + ]; + } + + return [$field->short_name => $field->get($row)]; + } +} From 1dbeacc149b37c8e2dc4f6bc77ea6d83afdf681f Mon Sep 17 00:00:00 2001 From: DarkSide Date: Mon, 7 Mar 2022 00:27:55 +0200 Subject: [PATCH 02/11] fix style --- demos/collection/grid.php | 1 - src/Table/Column/Flag.php | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 9e21215afc..6c4b98c7ee 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -14,7 +14,6 @@ use Atk4\Ui\Message; use Atk4\Ui\Table; use Atk4\Ui\UserAction\BasicExecutor; -use Atk4\Ui\Table; /** @var \Atk4\Ui\App $app */ require_once __DIR__ . '/../init-app.php'; diff --git a/src/Table/Column/Flag.php b/src/Table/Column/Flag.php index 2507eea1f8..8027b5f5a5 100644 --- a/src/Table/Column/Flag.php +++ b/src/Table/Column/Flag.php @@ -1,11 +1,12 @@ name_field ? $row->get($this->name_field) : null; return [ - $field->short_name => empty($code) ? '' : $this->getApp()->getTag('i', ['class' => strtolower($code) . ' flag', 'title' => $code . ($name ? ': ' . $name : '')]) + $field->short_name => empty($code) ? '' : $this->getApp()->getTag('i', ['class' => strtolower($code) . ' flag', 'title' => $code . ($name ? ': ' . $name : '')]), ]; } From 02a86d24a096d5ad176649ebf98db9c8395f45a8 Mon Sep 17 00:00:00 2001 From: DarkSide Date: Mon, 7 Mar 2022 00:45:33 +0200 Subject: [PATCH 03/11] remove useless ternary --- src/Table/Column/Flag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Table/Column/Flag.php b/src/Table/Column/Flag.php index 8027b5f5a5..c09cfc0289 100644 --- a/src/Table/Column/Flag.php +++ b/src/Table/Column/Flag.php @@ -39,7 +39,7 @@ protected function init(): void public function getHtmlTags(Model $row, $field) { if ($row->hasField($this->code_field)) { - $code = $row->get($this->code_field) ?: null; + $code = $row->get($this->code_field); $name = $this->name_field ? $row->get($this->name_field) : null; return [ From 98a9918bfe68f4fedc7f7dde08450afd6b6a21f4 Mon Sep 17 00:00:00 2001 From: DarkSide Date: Mon, 7 Mar 2022 01:01:20 +0200 Subject: [PATCH 04/11] Adding test what fails - see in demos/collection/test.php --- demos/collection/test.php | 27 +++++++++++++++++++++++++++ src/Table/Column/Flag.php | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 demos/collection/test.php diff --git a/demos/collection/test.php b/demos/collection/test.php new file mode 100644 index 0000000000..68f29ec579 --- /dev/null +++ b/demos/collection/test.php @@ -0,0 +1,27 @@ +db); +$m->addField('flag', [ + 'ui' => [ + 'table' => [ + Table\Column\Flag::class, + [ + 'code_field' => $m->fieldName()->iso, + 'name_field' => $m->fieldName()->name, + ] + ], + ], +]); +$e = $m->loadAny(); +$t = CardTable::addTo($app); +$t->setModel($e); diff --git a/src/Table/Column/Flag.php b/src/Table/Column/Flag.php index c09cfc0289..80c7084479 100644 --- a/src/Table/Column/Flag.php +++ b/src/Table/Column/Flag.php @@ -38,14 +38,14 @@ protected function init(): void public function getHtmlTags(Model $row, $field) { - if ($row->hasField($this->code_field)) { + //if ($row->hasField($this->code_field)) { $code = $row->get($this->code_field); $name = $this->name_field ? $row->get($this->name_field) : null; return [ $field->short_name => empty($code) ? '' : $this->getApp()->getTag('i', ['class' => strtolower($code) . ' flag', 'title' => $code . ($name ? ': ' . $name : '')]), ]; - } + //} return [$field->short_name => $field->get($row)]; } From 65415d839e8757bf1070a3b6ed919601797dfcc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 28 Aug 2022 19:26:26 +0200 Subject: [PATCH 05/11] fix cs/stan & simplify code --- demos/collection/test.php | 2 +- src/Table/Column/Flag.php | 44 ++++++++++++--------------------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/demos/collection/test.php b/demos/collection/test.php index 68f29ec579..8464bf9de6 100644 --- a/demos/collection/test.php +++ b/demos/collection/test.php @@ -18,7 +18,7 @@ [ 'code_field' => $m->fieldName()->iso, 'name_field' => $m->fieldName()->name, - ] + ], ], ], ]); diff --git a/src/Table/Column/Flag.php b/src/Table/Column/Flag.php index 80c7084479..1055d97543 100644 --- a/src/Table/Column/Flag.php +++ b/src/Table/Column/Flag.php @@ -4,8 +4,8 @@ namespace Atk4\Ui\Table\Column; +use Atk4\Data\Field; use Atk4\Data\Model; -use Atk4\Ui\Exception; use Atk4\Ui\Table; /** @@ -13,40 +13,22 @@ */ class Flag extends Table\Column { - /** - * Name of model field which contains country ALPHA-2 (2 letter) codes. - * - * @var string - */ + /** @var string Name of model field which contains country ALPHA-2 (2 letter) codes. */ public $code_field; - /** - * Optional name of model field which contains country names. - * - * @var string - */ + /** @var string|null Optional name of model field which contains country names. */ public $name_field; - protected function init(): void + public function getHtmlTags(Model $row, ?Field $field): array { - parent::init(); - - if (!$this->code_field) { - throw new Exception('Country code field must be defined'); - } - } - - public function getHtmlTags(Model $row, $field) - { - //if ($row->hasField($this->code_field)) { - $code = $row->get($this->code_field); - $name = $this->name_field ? $row->get($this->name_field) : null; - - return [ - $field->short_name => empty($code) ? '' : $this->getApp()->getTag('i', ['class' => strtolower($code) . ' flag', 'title' => $code . ($name ? ': ' . $name : '')]), - ]; - //} - - return [$field->short_name => $field->get($row)]; + $countryCode = $row->get($this->code_field); + $countryName = $this->name_field ? $row->get($this->name_field) : null; + + return [ + $field->shortName => $countryCode === null ? '' : $this->getApp()->getTag('i', [ + 'class' => strtolower($countryCode) . ' flag', + 'title' => $countryCode . ($countryName === null ? '' : ' - ' . $countryName), + ]), + ]; } } From 3709c3c2319aa17e05eb6d087896f466137539dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 7 Mar 2022 11:08:57 +0100 Subject: [PATCH 06/11] remove other unsafe hasField uses in atk4/ui --- src/Table.php | 2 +- src/Table/Column/Link.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Table.php b/src/Table.php index 5d48559b38..c63d9bfb6a 100644 --- a/src/Table.php +++ b/src/Table.php @@ -481,7 +481,7 @@ public function renderRow(): void if (!is_array($columns)) { $columns = [$columns]; } - $field = !is_int($name) && $this->model->hasField($name) ? $this->model->getField($name) : null; + $field = is_int($name) ? null : $this->model->getField($name); foreach ($columns as $column) { $html_tags = array_merge($column->getHtmlTags($this->model, $field), $html_tags); } diff --git a/src/Table/Column/Link.php b/src/Table/Column/Link.php index 37d192a8e1..e82907e567 100644 --- a/src/Table/Column/Link.php +++ b/src/Table/Column/Link.php @@ -146,7 +146,7 @@ public function getHtmlTags(Model $row, ?Field $field): array $p = $this->page ?? []; foreach ($this->args as $key => $val) { - if (is_numeric($key)) { + if (is_int($key)) { $key = $val; } From e04677967d528572e1c87ef9c81284819c9e0d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 7 Mar 2022 13:14:18 +0100 Subject: [PATCH 07/11] POC of value /w entity link --- src/CardTable.php | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/CardTable.php b/src/CardTable.php index b303ff7f42..76bd17fb13 100644 --- a/src/CardTable.php +++ b/src/CardTable.php @@ -34,30 +34,18 @@ public function setModel(Model $model, array $columns = null): void } $data = []; - foreach ($model->get() as $key => $value) { - if (in_array($key, $columns, true)) { + foreach (array_keys($model->get()) as $fieldName) { + if (in_array($fieldName, $columns, true)) { $data[] = [ - 'id' => $key, - 'field' => $model->getField($key)->getCaption(), - 'value' => $this->getApp()->uiPersistence->typecastSaveField($model->getField($key), $value), + 'id' => $fieldName, + 'field' => $model->getField($fieldName)->getCaption(), + 'value' => new Model\EntityFieldPair($model, $fieldName), ]; } } $this->_bypass = true; - $mm = parent::setSource($data); - $this->addDecorator('value', [Table\Column\Multiformat::class, function (Model $row, $field) use ($model) { - $field = $model->getField($row->getId()); - $ret = $this->decoratorFactory( - $field, - $field->type === 'boolean' ? [Table\Column\Status::class, ['positive' => [true, 'Yes'], 'negative' => [false, 'No']]] : [] - ); - if ($ret instanceof Table\Column\Money) { - $ret->attr['all']['class'] = ['single line']; - } - - return [$ret]; - }]); + parent::setSource($data); $this->_bypass = false; } } From 01596934f9724265d8560c1c2f804c526faaca0c Mon Sep 17 00:00:00 2001 From: Imants Horsts Date: Thu, 22 Dec 2022 13:31:44 +0200 Subject: [PATCH 08/11] Update grid.php --- demos/collection/grid.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 6c4b98c7ee..ac31b35820 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -29,8 +29,8 @@ // add country flag column $grid->addColumn('flag', [ Table\Column\Flag::class, - 'code_field' => $model->fieldName()->iso, - 'name_field' => $model->fieldName()->name, + 'codeField' => $model->fieldName()->iso, + 'nameField' => $model->fieldName()->name, ]); // Adding Quicksearch on Name field using auto query. From 3aef068fd739bf8f4b6adbd74244d7f99fc8b80d Mon Sep 17 00:00:00 2001 From: Imants Horsts Date: Thu, 22 Dec 2022 13:32:03 +0200 Subject: [PATCH 09/11] Update test.php --- demos/collection/test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/collection/test.php b/demos/collection/test.php index 8464bf9de6..d0aaab4042 100644 --- a/demos/collection/test.php +++ b/demos/collection/test.php @@ -16,8 +16,8 @@ 'table' => [ Table\Column\Flag::class, [ - 'code_field' => $m->fieldName()->iso, - 'name_field' => $m->fieldName()->name, + 'codeField' => $m->fieldName()->iso, + 'nameField' => $m->fieldName()->name, ], ], ], From 190002fff0e51f50695586148fcea12aa582b8bf Mon Sep 17 00:00:00 2001 From: Imants Horsts Date: Thu, 22 Dec 2022 13:33:08 +0200 Subject: [PATCH 10/11] Update Flag.php --- src/Table/Column/Flag.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Table/Column/Flag.php b/src/Table/Column/Flag.php index 1055d97543..16c15f948f 100644 --- a/src/Table/Column/Flag.php +++ b/src/Table/Column/Flag.php @@ -14,15 +14,15 @@ class Flag extends Table\Column { /** @var string Name of model field which contains country ALPHA-2 (2 letter) codes. */ - public $code_field; + public $codeField; /** @var string|null Optional name of model field which contains country names. */ - public $name_field; + public $nameField; public function getHtmlTags(Model $row, ?Field $field): array { - $countryCode = $row->get($this->code_field); - $countryName = $this->name_field ? $row->get($this->name_field) : null; + $countryCode = $row->get($this->codeField); + $countryName = $this->nameField ? $row->get($this->nameField) : null; return [ $field->shortName => $countryCode === null ? '' : $this->getApp()->getTag('i', [ From e474216cc6e377f51adcfec5de916eed3aaaa44a Mon Sep 17 00:00:00 2001 From: DarkSide Date: Tue, 27 Dec 2022 23:34:39 +0200 Subject: [PATCH 11/11] fixes --- demos/collection/test.php | 4 +++- src/CardTable.php | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/demos/collection/test.php b/demos/collection/test.php index d0aaab4042..edf3efdd1b 100644 --- a/demos/collection/test.php +++ b/demos/collection/test.php @@ -1,5 +1,5 @@ db); $m->addField('flag', [ + 'neverPersist' => true, // no need for actual value in this field 'ui' => [ 'table' => [ Table\Column\Flag::class, @@ -22,6 +23,7 @@ ], ], ]); + $e = $m->loadAny(); $t = CardTable::addTo($app); $t->setModel($e); diff --git a/src/CardTable.php b/src/CardTable.php index 6a2e982c69..338366764c 100644 --- a/src/CardTable.php +++ b/src/CardTable.php @@ -46,8 +46,8 @@ public function setModel(Model $model, array $columns = null): void $this->_bypass = true; - //parent::setSource($data); -// + parent::setSource($data); + /* $mm = parent::setSource($data); $this->addDecorator('value', [Table\Column\Multiformat::class, function (Model $row) use ($model) { $field = $model->getField($row->getId()); @@ -61,7 +61,8 @@ public function setModel(Model $model, array $columns = null): void return [$ret]; }]); -// + */ + $this->_bypass = false; } }