| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui; |
| 6: | |
| 7: | use Atk4\Data\Model; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | class CardTable extends Table |
| 16: | { |
| 17: | protected bool $_bypass = false; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | #[\Override] |
| 23: | public function setModel(Model $entity, array $fields = null): void |
| 24: | { |
| 25: | if ($this->_bypass) { |
| 26: | parent::setModel($entity); |
| 27: | |
| 28: | return; |
| 29: | } |
| 30: | |
| 31: | $entity->assertIsLoaded(); |
| 32: | |
| 33: | if ($fields === null) { |
| 34: | $fields = array_keys($entity->getFields('visible')); |
| 35: | } |
| 36: | |
| 37: | $data = []; |
| 38: | foreach ($entity->get() as $key => $value) { |
| 39: | if (in_array($key, $fields, true)) { |
| 40: | $data[] = [ |
| 41: | 'id' => $key, |
| 42: | 'field' => $entity->getField($key)->getCaption(), |
| 43: | 'value' => $this->getApp()->uiPersistence->typecastSaveField($entity->getField($key), $value), |
| 44: | ]; |
| 45: | } |
| 46: | } |
| 47: | |
| 48: | $this->_bypass = true; |
| 49: | try { |
| 50: | parent::setSource($data); |
| 51: | } finally { |
| 52: | $this->_bypass = false; |
| 53: | } |
| 54: | |
| 55: | $this->addDecorator('value', [Table\Column\Multiformat::class, function (Model $row) use ($entity) { |
| 56: | $field = $entity->getField($row->getId()); |
| 57: | $ret = $this->decoratorFactory( |
| 58: | $field, |
| 59: | $field->type === 'boolean' ? [Table\Column\Status::class, ['positive' => [true, 'Yes'], 'negative' => [false, 'No']]] : [] |
| 60: | ); |
| 61: | if ($ret instanceof Table\Column\Money) { |
| 62: | $ret->attr['all']['class'] = ['single line']; |
| 63: | } |
| 64: | |
| 65: | return [$ret]; |
| 66: | }]); |
| 67: | } |
| 68: | } |
| 69: | |