1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Form\Layout;
6:
7: use Atk4\Data\Model;
8: use Atk4\Ui\Columns as UiColumns;
9: use Atk4\Ui\Form;
10: use Atk4\Ui\View;
11:
12: /**
13: * Layout that automatically arranges itself into multiple columns.
14: * Well suitable for large number of fields on a form.
15: */
16: class Columns extends Form\Layout
17: {
18: /** @var int count of columns */
19: public $col;
20:
21: /** @var string size CSS class */
22: public $size = '';
23:
24: #[\Override]
25: public function setModel(Model $entity, array $fields = null): void
26: {
27: // dont add any fields automatically
28: parent::setModel($entity, []);
29:
30: if ($fields === null) {
31: $fields = $this->getModelFields($entity);
32: }
33:
34: $cnt = count($fields);
35:
36: if ($this->col !== null) {
37: $col = $this->col;
38: $size = $this->size;
39: } elseif ($cnt < 10) {
40: $col = 1;
41: $size = '';
42: } elseif ($cnt < 15) {
43: $col = 2;
44: $size = '';
45: } elseif ($cnt < 20) {
46: $col = 2;
47: $size = 'small';
48: } elseif ($cnt < 32) {
49: $col = 3;
50: $size = 'small';
51: } else {
52: $col = 4;
53: $size = 'tiny';
54: }
55:
56: if ($size) {
57: $this->form->addClass($size);
58: }
59:
60: $c = UiColumns::addTo($this);
61:
62: $chunks = array_chunk($fields, (int) ceil($cnt / $col));
63: foreach ($chunks as $chunk) {
64: $cc = $c->addColumn();
65: Form\Layout::addTo($cc, ['form' => $this->form])
66: ->setModel($entity, $chunk);
67: }
68:
69: View::addTo($this, ['ui' => 'clearing hidden divider']);
70: }
71: }
72: