1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Table\Column;
6:
7: use Atk4\Data\Field;
8: use Atk4\Data\Model;
9: use Atk4\Ui\Table;
10:
11: /**
12: * Take the field value as string in CSV format or array of IDs and transforms into Fomantic-UI labels.
13: * If model field values property is set, then will use titles instead of IDs as label text.
14: *
15: * from => label1,label2 | to => div.ui.label[label1] div.ui.label[label2]
16: */
17: class Labels extends Table\Column
18: {
19: /** @var array<string|int, string>|null Allowed values, prioritized over Field::$values */
20: public ?array $values = null;
21:
22: #[\Override]
23: public function getHtmlTags(Model $row, ?Field $field): array
24: {
25: $values = $this->values ?? $field->values;
26:
27: $v = $field->get($row);
28: $v = explode(',', $v ?? '');
29:
30: $labelsHtml = [];
31: foreach ($v as $id) {
32: // if field values is set, then use titles instead of IDs
33: $label = $values[$id] ?? $id;
34:
35: if ($label !== '') {
36: $labelsHtml[] = $this->getApp()->getTag('div', ['class' => 'ui label'], $label);
37: }
38: }
39:
40: return [$field->shortName => implode('', $labelsHtml)];
41: }
42: }
43: