repeatable.php
3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* @package JCE
* @subpackage Component
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @copyright Copyright (C) 2006 - 2020 Ryan Demmer. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Form Field class for the JCE.
* Display a field with a repeatable set of defined sub fields
*
* @since 2.7
*/
class JFormFieldRepeatable extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 2.7
*/
protected $type = 'Repeatable';
/**
* Method to get the field input markup.
*
* @return string The field input markup.
*
* @since 2.7
*/
protected function getInput()
{
$subForm = new JForm($this->name, array('control' => $this->formControl));
$children = $this->element->children();
$subForm->load($children);
$subForm->setFields($children);
// And finaly build a main container
$str = array();
$values = $this->value;
// explode to array if string
if (is_string($values)) {
$values = explode(',', $values);
}
$fields = $subForm->getFieldset();
foreach ($values as $value) {
$class = '';
// highlight grouped fields
if (count($fields) > 1) {
$class = ' well well-small p-2 bg-light';
}
$str[] = '<div class="form-field-repeatable-item">';
$str[] = ' <div class="form-field-repeatable-item-group' . $class . '">';
$n = 0;
foreach ($fields as $field) {
$field->element['multiple'] = true;
// substitute for repeatable element
$field->element['name'] = (string) $this->element['name'];
if (is_array($value)) {
$value = isset($value[$n]) ? $value[$n] : $value[0];
}
// escape value
$field->value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
$field->setup($field->element, $field->value, $this->group);
// reset id
$field->id .= '_' . $n;
if (strpos($field->name, '[]') === false) {
$field->name .= '[]';
}
$str[] = $field->renderField();
$n++;
}
$str[] = ' </div>';
$str[] = ' <div class="form-field-repeatable-item-control">';
$str[] = ' <button class="btn btn-link form-field-repeatable-add" aria-label="' . JText::_('JGLOBAL_FIELD_ADD') . '"><i class="icon icon-plus pull-right float-right"></i></button>';
$str[] = ' <button class="btn btn-link form-field-repeatable-remove" aria-label="' . JText::_('JGLOBAL_FIELD_REMOVE') . '"><i class="icon icon-trash pull-right float-right"></i></button>';
$str[] = ' </div>';
$str[] = '</div>';
}
return implode("", $str);
}
}