1 <?php
2
3 4 5
6 class rex_form_container_element extends rex_form_element
7 {
8 private $fields;
9 private $multiple;
10 private $active;
11
12
13
14 public function __construct($tag = '', rex_form_base $table = null, array $attributes = [])
15 {
16 parent::__construct('', $table, $attributes);
17 $this->fields = [];
18 $this->multiple = true;
19 }
20
21 public function setValue($value)
22 {
23 $this->value = $value;
24 }
25
26 public function setMultiple($multiple = true)
27 {
28 $this->multiple = $multiple;
29 }
30
31 public function setActive($group)
32 {
33 $this->active = $group;
34 }
35
36 public function addField($type, $name, $value = null, array $attributes = [])
37 {
38 return $this->addGroupedField('elementContainer', $type, $name, $value, $attributes);
39 }
40
41 public function addGroupedField($group, $type, $name, $value = null, array $attributes = [])
42 {
43 $field = $this->table->createInput($type, $name, $value, $attributes);
44
45 if (!isset($this->fields[$group])) {
46 $this->fields[$group] = [];
47 }
48
49 $field->setAttribute('id', $this->getAttribute('id').'-'.$group.'-'.$field->getFieldName());
50 $field->setAttribute('name', $this->getAttribute('name').'['.$group.']['.$field->getFieldName().']');
51 $field->setValue($value);
52
53 $this->fields[$group][] = $field;
54 return $field;
55 }
56
57 public function getFields()
58 {
59 return $this->fields;
60 }
61
62 protected function prepareInnerFields()
63 {
64 $values = $this->getValue();
65 if (is_string($values)) {
66 $values = json_decode($values, true);
67 if (!$this->multiple) {
68 $values = [$this->active => $values];
69 }
70 }
71
72 foreach ($this->fields as $group => $groupFields) {
73 if (!$this->multiple && $this->active && $this->active !== $group) {
74 continue;
75 }
76
77 foreach ($groupFields as $key => $field) {
78 if (isset($values[$group][$field->getFieldName()])) {
79 $field->setValue($values[$group][$field->getFieldName()]);
80 }
81 }
82 }
83 }
84
85 public function formatElement()
86 {
87 $this->prepareInnerFields();
88
89 $attr = '';
90
91
92
93 $attributeFilter = ['id', 'name'];
94 foreach ($this->getAttributes() as $attributeName => $attributeValue) {
95 if (in_array($attributeName, $attributeFilter)) {
96 continue;
97 }
98
99 $attr .= ' ' . rex_escape($attributeName, 'html_attr') . '="' . rex_escape($attributeValue) . '"';
100 }
101
102 $format = '';
103 foreach ($this->fields as $group => $groupFields) {
104 $format .= '<div id="rex-' . rex_escape($group) . '"' . $attr . '>';
105 foreach ($groupFields as $field) {
106 $format .= $field->get();
107 }
108 $format .= '</div>';
109 }
110 return $format;
111 }
112
113 protected function getFragment()
114 {
115 return 'core/form/container.php';
116 }
117
118 public function getSaveValue()
119 {
120 $this->prepareInnerFields();
121
122 $value = [];
123 if ($this->multiple) {
124 foreach ($this->fields as $group => $groupFields) {
125 foreach ($groupFields as $field) {
126
127 if (strpos($field->getAttribute('class'), 'form-control-static') === false) {
128 $value[$group][$field->getFieldName()] = $field->getSaveValue();
129 }
130 }
131 }
132 } elseif (isset($this->active) && isset($this->fields[$this->active])) {
133 foreach ($this->fields[$this->active] as $field) {
134
135 if (strpos($field->getAttribute('class'), 'form-control-static') === false) {
136 $value[$field->getFieldName()] = $field->getSaveValue();
137 }
138 }
139 }
140 return json_encode($value);
141 }
142 }
143