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 for formatting money.
14: */
15: class Money extends Table\Column
16: {
17: public array $attr = ['all' => ['class' => ['right aligned single line']]];
18:
19: /** @var bool Should we show zero values in cells? */
20: public $showZeroValues = true;
21:
22: #[\Override]
23: public function getTagAttributes(string $position, array $attr = []): array
24: {
25: $attr = array_merge_recursive($attr, ['class' => ['{$_' . $this->shortName . '_class}']]);
26:
27: return parent::getTagAttributes($position, $attr);
28: }
29:
30: #[\Override]
31: public function getDataCellHtml(Field $field = null, array $attr = []): string
32: {
33: if ($field === null) {
34: throw new Exception('Money column requires a field');
35: }
36:
37: return $this->getTag('body', '{$' . $field->shortName . '}', $attr);
38: }
39:
40: #[\Override]
41: public function getHtmlTags(Model $row, ?Field $field): array
42: {
43: if ($field->get($row) < 0) {
44: return ['_' . $this->shortName . '_class' => 'negative'];
45: } elseif (!$this->showZeroValues && (float) $field->get($row) === 0.0) {
46: return ['_' . $this->shortName . '_class' => '', $field->shortName => '-'];
47: }
48:
49: return ['_' . $this->shortName . '_class' => ''];
50: }
51: }
52: