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\Exception;
10: use Atk4\Ui\Table;
11:
12: /**
13: * Column to add a little icon to show on hover a text
14: * text is taken by the Row Model in $tooltipField.
15: *
16: * $crud->addDecorator('paid_date', new Table\Column\Tooltip('note'));
17: * $crud->addDecorator('paid_date', new Table\Column\Tooltip('note', 'error red'));
18: */
19: class Tooltip extends Table\Column
20: {
21: /** @var string */
22: public $icon;
23:
24: public string $tooltipField;
25:
26: #[\Override]
27: protected function init(): void
28: {
29: parent::init();
30:
31: if (!$this->icon) {
32: $this->icon = 'info circle';
33: }
34: }
35:
36: #[\Override]
37: public function getDataCellHtml(Field $field = null, array $attr = []): string
38: {
39: if ($field === null) {
40: throw new Exception('Tooltip can be used only with model field');
41: }
42:
43: $bodyAttr = $this->getTagAttributes('body');
44:
45: $attr = array_merge_recursive($bodyAttr, $attr, ['class' => '{$_' . $field->shortName . '_tooltip}']);
46:
47: if (is_array($attr['class'] ?? null)) {
48: $attr['class'] = implode(' ', $attr['class']);
49: }
50:
51: return $this->getApp()->getTag('td', $attr, [
52: ' {$' . $field->shortName . '}',
53: ['span', [
54: 'class' => 'ui icon link {$_' . $field->shortName . '_data_visible_class}',
55: 'data-tooltip' => '{$_' . $field->shortName . '_data_tooltip}',
56: ], [
57: ['i', ['class' => 'ui icon {$_' . $field->shortName . '_icon}']],
58: ]],
59: ]);
60: }
61:
62: #[\Override]
63: public function getHtmlTags(Model $row, ?Field $field): array
64: {
65: // @TODO remove popup tooltip when null
66: $tooltip = $row->get($this->tooltipField);
67:
68: if ($tooltip === null || $tooltip === '') {
69: return [
70: '_' . $field->shortName . '_data_visible_class' => 'transition hidden',
71: '_' . $field->shortName . '_data_tooltip' => '',
72: '_' . $field->shortName . '_icon' => '',
73: ];
74: }
75:
76: return [
77: '_' . $field->shortName . '_data_visible_class' => '',
78: '_' . $field->shortName . '_data_tooltip' => $tooltip,
79: '_' . $field->shortName . '_icon' => $this->icon,
80: ];
81: }
82: }
83: