1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die;
10
11 JFormHelper::loadFieldClass('list');
12
13 14 15 16 17 18 19
20 class FOFFormFieldModel extends FOFFormFieldList implements FOFFormField
21 {
22 protected $static;
23
24 protected $repeatable;
25
26 27 28 29 30 31 32 33 34
35 public function __get($name)
36 {
37 switch ($name)
38 {
39 case 'static':
40 if (empty($this->static))
41 {
42 $this->static = $this->getStatic();
43 }
44
45 return $this->static;
46 break;
47
48 case 'repeatable':
49 if (empty($this->repeatable))
50 {
51 $this->repeatable = $this->getRepeatable();
52 }
53
54 return $this->repeatable;
55 break;
56
57 default:
58 return parent::__get($name);
59 }
60 }
61
62 63 64 65 66 67 68 69
70 public function getStatic()
71 {
72 $class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
73
74 return '<span id="' . $this->id . '" ' . $class . '>' .
75 htmlspecialchars(FOFFormFieldList::getOptionName($this->getOptions(), $this->value), ENT_COMPAT, 'UTF-8') .
76 '</span>';
77 }
78
79 80 81 82 83 84 85 86
87 public function getRepeatable()
88 {
89 $class = $this->id;
90 $format_string = '';
91 $show_link = false;
92 $link_url = '';
93 $empty_replacement = '';
94
95
96 if ($this->element['class'])
97 {
98 $class = (string) $this->element['class'];
99 }
100
101 if ($this->element['format'])
102 {
103 $format_string = (string) $this->element['format'];
104 }
105
106 if ($this->element['show_link'] == 'true')
107 {
108 $show_link = true;
109 }
110
111 if ($this->element['url'])
112 {
113 $link_url = $this->element['url'];
114 }
115 else
116 {
117 $show_link = false;
118 }
119
120 if ($show_link && ($this->item instanceof FOFTable))
121 {
122 $link_url = $this->parseFieldTags($link_url);
123 }
124 else
125 {
126 $show_link = false;
127 }
128
129 if ($this->element['empty_replacement'])
130 {
131 $empty_replacement = (string) $this->element['empty_replacement'];
132 }
133
134 $value = FOFFormFieldList::getOptionName($this->getOptions(), $this->value);
135
136
137 if (!empty($empty_replacement) && empty($value))
138 {
139 $value = JText::_($empty_replacement);
140 }
141
142 if (empty($format_string))
143 {
144 $value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
145 }
146 else
147 {
148 $value = sprintf($format_string, $value);
149 }
150
151
152 $html = '<span class="' . $class . '">';
153
154 if ($show_link)
155 {
156 $html .= '<a href="' . $link_url . '">';
157 }
158
159 $html .= $value;
160
161 if ($show_link)
162 {
163 $html .= '</a>';
164 }
165
166 $html .= '</span>';
167
168 return $html;
169 }
170
171 172 173 174 175
176 protected function getOptions()
177 {
178 $options = array();
179
180
181 $key = $this->element['key_field'] ? (string) $this->element['key_field'] : 'value';
182 $value = $this->element['value_field'] ? (string) $this->element['value_field'] : (string) $this->element['name'];
183 $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
184 $applyAccess = $this->element['apply_access'] ? (string) $this->element['apply_access'] : 'false';
185 $modelName = (string) $this->element['model'];
186 $nonePlaceholder = (string) $this->element['none'];
187
188 if (!empty($nonePlaceholder))
189 {
190 $options[] = JHtml::_('select.option', null, JText::_($nonePlaceholder));
191 }
192
193
194 $applyAccess = strtolower($applyAccess);
195 $applyAccess = in_array($applyAccess, array('yes', 'on', 'true', '1'));
196
197
198 $parts = FOFInflector::explode($modelName);
199 $mName = ucfirst(array_pop($parts));
200 $mPrefix = FOFInflector::implode($parts);
201
202
203 $config = array('savestate' => 0);
204 $model = FOFModel::getTmpInstance($mName, $mPrefix, $config);
205
206 if ($applyAccess)
207 {
208 $model->applyAccessFiltering();
209 }
210
211
212 foreach ($this->element->children() as $stateoption)
213 {
214
215 if ($stateoption->getName() != 'state')
216 {
217 continue;
218 }
219
220 $stateKey = (string) $stateoption['key'];
221 $stateValue = (string) $stateoption;
222
223 $model->setState($stateKey, $stateValue);
224 }
225
226
227 $items = $model->getItemList(true);
228
229
230 if (!empty($items))
231 {
232 foreach ($items as $item)
233 {
234 if ($translate == true)
235 {
236 $options[] = JHtml::_('select.option', $item->$key, JText::_($item->$value));
237 }
238 else
239 {
240 $options[] = JHtml::_('select.option', $item->$key, $item->$value);
241 }
242 }
243 }
244
245
246 $options = array_merge(parent::getOptions(), $options);
247
248 return $options;
249 }
250
251 252 253 254 255 256 257
258 protected function parseFieldTags($text)
259 {
260 $ret = $text;
261
262
263
264 $keyfield = $this->item->getKeyName();
265 $replace = $this->item->$keyfield;
266 $ret = str_replace('[ITEM:ID]', $replace, $ret);
267
268
269 $ret = str_replace('[ITEMID]', JFactory::getApplication()->input->getInt('Itemid', 0), $ret);
270
271
272 $fields = $this->item->getTableFields();
273
274 foreach ($fields as $fielddata)
275 {
276 $fieldname = $fielddata->Field;
277
278 if (empty($fieldname))
279 {
280 $fieldname = $fielddata->column_name;
281 }
282
283 $search = '[ITEM:' . strtoupper($fieldname) . ']';
284 $replace = $this->item->$fieldname;
285 $ret = str_replace($search, $replace, $ret);
286 }
287
288 return $ret;
289 }
290 }
291