1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16 17 18 19
20 class JFormFieldRepeatable extends JFormField
21 {
22 23 24 25 26 27
28 protected $type = 'Repeatable';
29
30 31 32 33 34 35 36
37 protected function getInput()
38 {
39 JLog::add('JFormFieldRepeatable is deprecated. Use JFormFieldSubform instead.', JLog::WARNING, 'deprecated');
40
41
42 $subForm = new JForm($this->name, array('control' => 'jform'));
43 $xml = $this->element->children()->asXml();
44 $subForm->load($xml);
45
46
47
48 $subForm->repeatCounter = (int) @$this->form->repeatCounter;
49
50 $children = $this->element->children();
51 $subForm->setFields($children);
52
53
54 $maximum = $this->element['maximum'] ? (int) $this->element['maximum'] : '999';
55
56
57 $head_row_str = array();
58 $body_row_str = array();
59 $head_row_str[] = '<th></th>';
60 $body_row_str[] = '<td><span class="sortable-handler " style="cursor: move;"><span class="icon-menu" aria-hidden="true"></span></span></td>';
61 foreach ($subForm->getFieldset() as $field)
62 {
63
64 $field->name = (string) $field->element['name'];
65
66
67 $head_row_str[] = '<th>' . strip_tags($field->getLabel($field->name));
68 $head_row_str[] = '<br /><small style="font-weight:normal">' . JText::_($field->description) . '</small>';
69 $head_row_str[] = '</th>';
70
71
72 $body_row_str[] = '<td>' . $field->getInput() . '</td>';
73 }
74
75
76 $head_row_str[] = '<th><div class="btn-group"><a href="#" class="add btn button btn-success"><span class="icon-plus"></span> </a></div></th>';
77 $body_row_str[] = '<td><div class="btn-group">';
78 $body_row_str[] = '<a class="add btn button btn-success"><span class="icon-plus"></span> </a>';
79 $body_row_str[] = '<a class="remove btn button btn-danger"><span class="icon-minus"></span> </a>';
80 $body_row_str[] = '</div></td>';
81
82
83 $table = '<table id="' . $this->id . '_table" class="adminlist ' . $this->element['class'] . ' table table-striped">'
84 . '<thead><tr>' . implode("\n", $head_row_str) . '</tr></thead>'
85 . '<tbody><tr>' . implode("\n", $body_row_str) . '</tr></tbody>'
86 . '</table>';
87
88
89 $str = array();
90 $str[] = '<div id="' . $this->id . '_container">';
91
92
93 $str[] = '<div id="' . $this->id . '_modal" class="modal hide">';
94 $str[] = $table;
95 $str[] = '<div class="modal-footer">';
96 $str[] = '<button class="close-modal btn button btn-link">' . JText::_('JCANCEL') . '</button>';
97 $str[] = '<button class="save-modal-data btn button btn-primary">' . JText::_('JAPPLY') . '</button>';
98 $str[] = '</div>';
99
100
101 $str[] = '</div>';
102
103
104 $str[] = '</div>';
105
106
107 $select = (string) $this->element['select'] ? JText::_((string) $this->element['select']) : JText::_('JLIB_FORM_BUTTON_SELECT');
108 $icon = $this->element['icon'] ? '<span class="icon-' . $this->element['icon'] . '"></span> ' : '';
109 $str[] = '<button class="open-modal btn" id="' . $this->id . '_button" >' . $icon . $select . '</button>';
110
111 if (is_array($this->value))
112 {
113 $this->value = array_shift($this->value);
114 }
115
116
117 $data = array();
118 $data[] = 'data-container="#' . $this->id . '_container"';
119 $data[] = 'data-modal-element="#' . $this->id . '_modal"';
120 $data[] = 'data-repeatable-element="table tbody tr"';
121 $data[] = 'data-bt-add="a.add"';
122 $data[] = 'data-bt-remove="a.remove"';
123 $data[] = 'data-bt-modal-open="#' . $this->id . '_button"';
124 $data[] = 'data-bt-modal-close="button.close-modal"';
125 $data[] = 'data-bt-modal-save-data="button.save-modal-data"';
126 $data[] = 'data-maximum="' . $maximum . '"';
127 $data[] = 'data-input="#' . $this->id . '"';
128
129
130 $value = htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8');
131 $str[] = '<input type="hidden" name="' . $this->name . '" id="' . $this->id . '" value="' . $value
132 . '" class="form-field-repeatable" ' . implode(' ', $data) . ' />';
133
134
135 JHtml::_('bootstrap.framework');
136
137
138 JHtml::_('jquery.ui', array('core', 'sortable'));
139
140 JHtml::_('script', 'jui/sortablelist.js', array('version' => 'auto', 'relative' => true));
141 JHtml::_('stylesheet', 'jui/sortablelist.css', array('version' => 'auto', 'relative' => true));
142 JHtml::_('script', 'system/repeatable.js', array('framework' => true, 'version' => 'auto', 'relative' => true));
143
144 $javascript = 'jQuery(document).ready(function($) { $("#' . $this->id . '_table tbody").sortable(); });';
145
146 JFactory::getDocument()->addScriptDeclaration($javascript);
147
148 return implode("\n", $str);
149 }
150 }
151