Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentformpro/src/Components/RepeaterField.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
namespace FluentFormPro\Components;
3
+
4
+
if ( ! defined( 'ABSPATH' ) ) {
5
+
exit; // Exit if accessed directly.
6
+
}
7
+
8
+
use FluentForm\App\Helpers\Helper;
9
+
use FluentForm\App\Services\FormBuilder\BaseFieldManager;
10
+
use FluentForm\Framework\Helpers\ArrayHelper;
11
+
12
+
class RepeaterField extends BaseFieldManager
13
+
{
14
+
/**
15
+
* Wrapper class for repeat element
16
+
* @var string
17
+
*/
18
+
protected $wrapperClass = 'ff-el-repeater js-repeater';
19
+
20
+
public function __construct()
21
+
{
22
+
parent::__construct(
23
+
'repeater_field',
24
+
'Repeater Field',
25
+
['repeat', 'list', 'multiple'],
26
+
'advanced'
27
+
);
28
+
29
+
add_filter('fluentform/response_render_repeater_field', array($this, 'renderResponse'), 10, 3);
30
+
31
+
}
32
+
33
+
function getComponent()
34
+
{
35
+
return [
36
+
'index' => 16,
37
+
'element' => 'repeater_field',
38
+
'attributes' => array(
39
+
'name' => 'repeater_field',
40
+
'data-type' => 'repeater_field'
41
+
),
42
+
'settings' => array(
43
+
'label' => __('Repeater Field', 'fluentformpro'),
44
+
'admin_field_label' => '',
45
+
'container_class' => '',
46
+
'label_placement' => '',
47
+
'validation_rules' => array(),
48
+
'conditional_logics' => array(),
49
+
'max_repeat_field' => ''
50
+
),
51
+
'fields' => array(
52
+
array(
53
+
'element' => 'input_text',
54
+
'attributes' => array(
55
+
'type' => 'text',
56
+
'value' => '',
57
+
'placeholder' => '',
58
+
),
59
+
'settings' => array(
60
+
'label' => __('Column 1', 'fluentformpro'),
61
+
'help_message' => '',
62
+
'validation_rules' => array(
63
+
'required' => array(
64
+
'value' => false,
65
+
'global' => true,
66
+
'message' => Helper::getGlobalDefaultMessage('required'),
67
+
'global_message' => Helper::getGlobalDefaultMessage('required'),
68
+
)
69
+
)
70
+
),
71
+
'editor_options' => array(),
72
+
)
73
+
),
74
+
'editor_options' => array(
75
+
'title' => __('Repeat Field', 'fluentformpro'),
76
+
'icon_class' => 'ff-edit-repeat',
77
+
'template' => 'fieldsRepeatSettings'
78
+
),
79
+
];
80
+
}
81
+
82
+
public function getGeneralEditorElements()
83
+
{
84
+
return [
85
+
'label',
86
+
'label_placement',
87
+
'admin_field_label',
88
+
'fields_repeat_settings',
89
+
];
90
+
}
91
+
92
+
public function getAdvancedEditorElements()
93
+
{
94
+
return [
95
+
'container_class',
96
+
'name',
97
+
'conditional_logics',
98
+
'max_repeat_field'
99
+
];
100
+
}
101
+
102
+
public function getEditorCustomizationSettings()
103
+
{
104
+
return [
105
+
'fields_repeat_settings' => array(
106
+
'template' => 'fieldsRepeatSettings',
107
+
'label' => __('Repeat Field Columns', 'fluentformpro'),
108
+
'help_text' => __('Field Columns which a user will be able to repeat.', 'fluentformpro'),
109
+
)
110
+
];
111
+
}
112
+
113
+
/**
114
+
* Compile and echo the html element
115
+
* @param array $data [element data]
116
+
* @param stdClass $form [Form Object]
117
+
* @return void
118
+
*/
119
+
public function render($data, $form)
120
+
{
121
+
$elementName = $data['element'];
122
+
$data = apply_filters_deprecated(
123
+
'fluentform_rendering_field_data_' . $elementName,
124
+
[
125
+
$data,
126
+
$form
127
+
],
128
+
FLUENTFORM_FRAMEWORK_UPGRADE,
129
+
'fluentform/rendering_field_data_' . $elementName,
130
+
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName
131
+
);
132
+
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form);
133
+
134
+
// Test implementation using address component
135
+
$rootName = $data['attributes']['name'];
136
+
$hasConditions = $this->hasConditions($data) ? 'has-conditions ' : '';
137
+
@$data['attributes']['class'] .= ' ff-el-group ' . $this->wrapperClass . ' ' . $hasConditions . ' ' . ArrayHelper::get($data, 'settings.container_class');
138
+
$data['attributes']['class'] = trim($data['attributes']['class']);
139
+
140
+
$maxRepeat = ArrayHelper::get($data, 'settings.max_repeat_field');
141
+
$data['attributes']['data-max_repeat'] = $maxRepeat;
142
+
$data['attributes']['data-root_name'] = $rootName;
143
+
144
+
if ($labelPlacement = ArrayHelper::get($data, 'settings.label_placement')) {
145
+
$data['attributes']['class'] .= ' ff-el-form-' . $labelPlacement;
146
+
}
147
+
148
+
$atts = $this->buildAttributes(
149
+
\FluentForm\Framework\Helpers\ArrayHelper::except($data['attributes'], 'name')
150
+
);
151
+
152
+
$fields = $data['fields'];
153
+
154
+
ob_start();
155
+
?>
156
+
<div <?php echo $atts; ?> >
157
+
<?php echo $this->buildElementLabel($data, $form); ?>
158
+
<div class='ff-el-input--content'>
159
+
<table role="table" aria-label="Repeater Field" data-max_repeat="<?php echo $maxRepeat; ?>" data-root_name="<?php echo $rootName; ?>" class="ff_repeater_table ff_flexible_table">
160
+
<thead>
161
+
<tr>
162
+
<?php foreach ($fields as $field): ?>
163
+
<th><?php echo $this->buildElementLabel($field, $form); ?></th>
164
+
<?php endforeach; ?>
165
+
<th></th>
166
+
</tr>
167
+
</thead>
168
+
<tbody>
169
+
<tr>
170
+
<?php foreach ($fields as $key => $item):
171
+
$itemLabel = fluentform_sanitize_html($item['settings']['label']);
172
+
$item['settings']['label'] = '';
173
+
$item['attributes']['aria-label'] = 'repeater level 1 and field ' . $itemLabel;
174
+
$item['attributes']['name'] = $rootName . '[0][]';
175
+
$item['attributes']['id'] = $this->makeElementId($data, $form) . '_' . $key;
176
+
$item['attributes']['data-repeater_index'] = $key;
177
+
$item['attributes']['data-type'] = 'repeater_item';
178
+
$item['attributes']['data-name'] = $rootName.'_'.$key.'_0';
179
+
$item['attributes']['data-error_index'] = $rootName.'['.$key.']';
180
+
$item['attributes']['data-default'] = ArrayHelper::get($item, 'attributes.value');
181
+
?>
182
+
<td data-label="<?php echo $itemLabel; ?>">
183
+
<?php $item['element'] = $item['element'] == 'input_mask' ? 'input_text' : $item['element'] ; ?>
184
+
<?php
185
+
do_action('fluentform/render_item_' . $item['element'], $item, $form);?>
186
+
</td>
187
+
<?php endforeach; ?>
188
+
<td class="repeat_btn"><?php echo $this->getRepeater($data['element']); ?></td>
189
+
</tr>
190
+
</tbody>
191
+
</table>
192
+
</div>
193
+
</div>
194
+
<?php
195
+
$html = ob_get_clean();
196
+
197
+
echo apply_filters('fluentform/rendering_field_html_' . $elementName, $html, $data, $form);
198
+
\FluentForm\App\Helpers\Helper::getNextTabIndex(50);
199
+
}
200
+
201
+
/**
202
+
* Compile repeater buttons
203
+
* @param string $el [element name]
204
+
* @return string
205
+
*/
206
+
protected function getRepeater($el)
207
+
{
208
+
if ($el == 'repeater_field') {
209
+
$div = '<div class="ff-el-repeat-buttons-list js-repeat-buttons">';
210
+
$div .= '<span role="button" aria-label="add repeater" class="repeat-plus"><svg tabindex="0" width="20" height="20" viewBox="0 0 512 512"><path d="m256 48c-115 0-208 93-208 208 0 115 93 208 208 208 115 0 208-93 208-208 0-115-93-208-208-208z m107 229l-86 0 0 86-42 0 0-86-86 0 0-42 86 0 0-86 42 0 0 86 86 0z"></path></svg></span>';
211
+
$div .= '<span role="button" aria-label="remove repeater" class="repeat-minus"><svg tabindex="0" width="20" height="20" viewBox="0 0 512 512"><path d="m256 48c-115 0-208 93-208 208 0 115 93 208 208 208 115 0 208-93 208-208 0-115-93-208-208-208z m107 229l-214 0 0-42 214 0z""></path></svg></span>';
212
+
$div .= '</div>';
213
+
return $div;
214
+
}
215
+
return '';
216
+
}
217
+
218
+
public function renderResponse($response, $field, $form_id)
219
+
{
220
+
if (defined('FLUENTFORM_RENDERING_ENTRIES')) {
221
+
return __('....', 'fluentformpro');
222
+
}
223
+
224
+
if (is_string($response) || empty($response) || !is_array($response)) {
225
+
return '';
226
+
}
227
+
228
+
$fields = ArrayHelper::get($field, 'raw.fields');
229
+
$columnCount = (count($fields) > count((array)$response[0])) ? count($fields) : count((array)$response[0]);
230
+
$columns = array_fill(0, $columnCount, 'column');
231
+
232
+
if (defined('FLUENTFORM_DOING_CSV_EXPORT')) {
233
+
return self::getResponseAsText($response, $fields, $columns);
234
+
}
235
+
236
+
return $this->getResponseHtml($response, $fields, $columns);
237
+
238
+
}
239
+
240
+
protected function getResponseHtml($response, $fields, $columns)
241
+
{
242
+
ob_start();
243
+
?>
244
+
<div class="ff_entry_table_wrapper">
245
+
<table class="ff_entry_table_field ff-table">
246
+
<thead>
247
+
<tr>
248
+
<?php foreach ($columns as $index => $count): ?>
249
+
<th><?php echo ArrayHelper::get($fields, $index . '.settings.label'); ?></th>
250
+
<?php endforeach; ?>
251
+
</tr>
252
+
</thead>
253
+
<tbody>
254
+
<?php foreach ($response as $responseIndex => $item): ?>
255
+
<tr>
256
+
<?php foreach ($columns as $index => $count): ?>
257
+
<td><?php echo ArrayHelper::get($item, $index); ?></td>
258
+
<?php endforeach; ?>
259
+
</tr>
260
+
<?php endforeach; ?>
261
+
</tbody>
262
+
</table>
263
+
</div>
264
+
<?php
265
+
$response = ob_get_clean();
266
+
return $response;
267
+
}
268
+
269
+
public static function getResponseAsText($response, $fields, $columns = [])
270
+
{
271
+
if (!is_array($response) || !is_array($fields)) {
272
+
return '';
273
+
}
274
+
if (!$columns) {
275
+
$columnCount = (count($fields) > count((array)ArrayHelper::get($response, 0))) ? count($fields) : count((array)ArrayHelper::get($response, 0));
276
+
$columns = array_fill(0, $columnCount, 'column');
277
+
}
278
+
$totalColumns = count($columns);
279
+
$text = '';
280
+
foreach ($columns as $index => $count) {
281
+
$text .= trim(ArrayHelper::get($fields, $index . '.settings.label', ' '));
282
+
if( $index+1 != $totalColumns ) {
283
+
$text .= " | ";
284
+
}
285
+
}
286
+
$text .= "\n";
287
+
foreach ($response as $responseIndex => $item):
288
+
foreach ($columns as $index => $count):
289
+
$text .= ArrayHelper::get($item, $index);
290
+
if( $index+1 != $totalColumns ) {
291
+
$text .= " | ";
292
+
}
293
+
endforeach;
294
+
$text .= "\n";
295
+
endforeach;
296
+
return $text;
297
+
}
298
+
}
299
+