| 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: | |
| 14: | |
| 15: | class Status extends Table\Column |
| 16: | { |
| 17: | |
| 18: | public $states = []; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | public function __construct($states) |
| 28: | { |
| 29: | parent::__construct(); |
| 30: | |
| 31: | $this->states = $states; |
| 32: | } |
| 33: | |
| 34: | #[\Override] |
| 35: | public function getDataCellHtml(Field $field = null, array $attr = []): string |
| 36: | { |
| 37: | if ($field === null) { |
| 38: | throw new Exception('Status can be used only with model field'); |
| 39: | } |
| 40: | |
| 41: | $bodyAttr = $this->getTagAttributes('body'); |
| 42: | |
| 43: | $attr = array_merge_recursive($bodyAttr, $attr, ['class' => '{$_' . $field->shortName . '_status}']); |
| 44: | |
| 45: | if (is_array($attr['class'] ?? null)) { |
| 46: | $attr['class'] = implode(' ', $attr['class']); |
| 47: | } |
| 48: | |
| 49: | return $this->getApp()->getTag('td', $attr, [ |
| 50: | ['i', ['class' => 'icon {$_' . $field->shortName . '_icon}'], ''], |
| 51: | ' {$' . $field->shortName . '}', |
| 52: | ]); |
| 53: | } |
| 54: | |
| 55: | #[\Override] |
| 56: | public function getHtmlTags(Model $row, ?Field $field): array |
| 57: | { |
| 58: | $cl = ''; |
| 59: | |
| 60: | |
| 61: | foreach ($this->states as $class => $values) { |
| 62: | if (in_array($field->get($row), $values, true)) { |
| 63: | $cl = $class; |
| 64: | |
| 65: | break; |
| 66: | } |
| 67: | } |
| 68: | |
| 69: | if (!$cl) { |
| 70: | return []; |
| 71: | } |
| 72: | |
| 73: | switch ($cl) { |
| 74: | case 'positive': |
| 75: | $ic = 'checkmark'; |
| 76: | |
| 77: | break; |
| 78: | case 'negative': |
| 79: | $ic = 'close'; |
| 80: | |
| 81: | break; |
| 82: | case 'default': |
| 83: | $ic = 'question'; |
| 84: | |
| 85: | break; |
| 86: | default: |
| 87: | $ic = ''; |
| 88: | } |
| 89: | |
| 90: | return [ |
| 91: | '_' . $field->shortName . '_status' => $cl . ' single line', |
| 92: | '_' . $field->shortName . '_icon' => $ic, |
| 93: | ]; |
| 94: | } |
| 95: | } |
| 96: | |