Index: trunk/extensions/SemanticForms/includes/SF_FormInputs.inc |
— | — | @@ -1,6 +1,6 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * Handles the creation and running of a user-created form. |
| 4 | + * Helper functions to display the various inputs of a user-generated form |
5 | 5 | * |
6 | 6 | * @author Yaron Koren |
7 | 7 | * @author Jeffrey Stuckman |
— | — | @@ -10,34 +10,31 @@ |
11 | 11 | |
12 | 12 | class SFFormInputs { |
13 | 13 | |
14 | | - function createAutocompleteValuesArray($field_name, $autocomplete_field_type) { |
| 14 | + /** |
| 15 | + * Create a comma-delimited string of values that match the specified |
| 16 | + * source name and type, for use by Javascript autocompletion. |
| 17 | + */ |
| 18 | + static function createAutocompleteValuesArray($source_name, $source_type) { |
15 | 19 | $names_array = array(); |
16 | | - // the query depends on whether this field is a relation (property of type |
17 | | - // Page), attribute (property of any other type), category or namespace |
18 | | - if ($autocomplete_field_type == 'relation' || $autocomplete_field_type == 'attribute' || $autocomplete_field_type == 'property') { |
19 | | - $smw_version = SMW_VERSION; |
20 | | - if (version_compare(SMW_VERSION, '1.2', '>=' ) || |
21 | | - substr($smw_version, 0, 3) == '1.2') { // temporary hack |
22 | | - $names_array = SFUtils::getAllValuesForProperty_1_2($field_name); |
23 | | - } else { |
24 | | - $is_relation = ($autocomplete_field_type == 'relation'); |
25 | | - $names_array = SFUtils::getAllValuesForProperty_orig($is_relation, $field_name); |
26 | | - } |
27 | | - } elseif ($autocomplete_field_type == 'category') { |
28 | | - $names_array = SFUtils::getAllPagesForCategory($field_name, 10); |
29 | | - } elseif ($autocomplete_field_type == 'concept') { |
30 | | - $names_array = SFUtils::getAllPagesForConcept($field_name); |
31 | | - } else { // i.e., $autocomplete_field_type == 'namespace' |
| 20 | + // the query depends on whether this is a property, category, concept |
| 21 | + // or namespace |
| 22 | + if ($source_type == 'property' || $source_type == 'attribute' || $source_type == 'relation') { |
| 23 | + $names_array = SFUtils::getAllValuesForProperty($source_name); |
| 24 | + } elseif ($source_type == 'category') { |
| 25 | + $names_array = SFUtils::getAllPagesForCategory($source_name, 10); |
| 26 | + } elseif ($source_type == 'concept') { |
| 27 | + $names_array = SFUtils::getAllPagesForConcept($source_name); |
| 28 | + } else { // i.e., $source_type == 'namespace' |
32 | 29 | // switch back to blank for main namespace |
33 | | - if ($field_name == "main") |
34 | | - $field_name = ""; |
35 | | - $names_array = SFUtils::getAllPagesForNamespace($field_name); |
| 30 | + if ($source_name == "main") |
| 31 | + $source_name = ""; |
| 32 | + $names_array = SFUtils::getAllPagesForNamespace($source_name); |
36 | 33 | } |
37 | 34 | // escape quotes, to avoid Javascript errors |
38 | 35 | return array_map('addslashes', $names_array); |
39 | 36 | } |
40 | 37 | |
41 | | - function uploadLinkHTML($input_id, $delimiter = null) { |
| 38 | + static function uploadLinkHTML($input_id, $delimiter = null) { |
42 | 39 | $upload_window_page = SpecialPage::getPage('UploadWindow'); |
43 | 40 | $query_string = "sfInputID=$input_id"; |
44 | 41 | if ($delimiter != null) |
— | — | @@ -48,24 +45,10 @@ |
49 | 46 | return $text; |
50 | 47 | } |
51 | 48 | |
52 | | - function textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 49 | + static function textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
53 | 50 | // if it's an autocomplete, call the with-autocomplete function instead |
54 | | - // we test to make sure that autocompletion hasn't been disabled, and |
55 | | - // that either the property is a relation (i.e. it's of type Page) or |
56 | | - // autocompletion has been specified in the form |
57 | | - $autocompletion_disabled = (array_key_exists('autocomplete on', $other_args) && $other_args['autocomplete on'] == "") || |
58 | | - (array_key_exists('no autocomplete', $other_args) && $other_args['no autocomplete'] == true); |
59 | | - if (! $autocompletion_disabled) { |
60 | | - if ((array_key_exists('is_relation', $other_args) && $other_args['is_relation'] == true) || |
61 | | - array_key_exists('autocomplete', $other_args) || |
62 | | - array_key_exists('autocomplete on property', $other_args) || |
63 | | - array_key_exists('autocomplete on category', $other_args) || |
64 | | - array_key_exists('autocomplete on concept', $other_args) || |
65 | | - array_key_exists('autocomplete on', $other_args) || |
66 | | - array_key_exists('autocomplete on namespace', $other_args) || |
67 | | - array_key_exists('remote autocompletion', $other_args)) { |
68 | | - return SFFormInputs::textInputWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args); |
69 | | - } |
| 51 | + if (array_key_exists('autocompletion source', $other_args)) { |
| 52 | + return self::textInputWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args); |
70 | 53 | } |
71 | 54 | |
72 | 55 | // if there are possible values specified, call the dropdown function |
— | — | @@ -139,7 +122,7 @@ |
140 | 123 | return array($text, null); |
141 | 124 | } |
142 | 125 | |
143 | | - function dropdownHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 126 | + static function dropdownHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
144 | 127 | global $sfgTabIndex, $sfgFieldNum; |
145 | 128 | |
146 | 129 | $className = ($is_mandatory) ? "mandatoryField" : "createboxInput"; |
— | — | @@ -170,11 +153,11 @@ |
171 | 154 | return array($text, null); |
172 | 155 | } |
173 | 156 | |
174 | | - /* |
175 | | - * getValuesArray() - helper function to get an array of values out of |
176 | | - * what may be either an array or a delimited string |
| 157 | + /** |
| 158 | + * Helper function to get an array of values out of what may be either |
| 159 | + * an array or a delimited string |
177 | 160 | */ |
178 | | - function getValuesArray($value, $delimiter) { |
| 161 | + static function getValuesArray($value, $delimiter) { |
179 | 162 | if (is_array($value)) { |
180 | 163 | return $value; |
181 | 164 | } else { |
— | — | @@ -183,7 +166,7 @@ |
184 | 167 | } |
185 | 168 | } |
186 | 169 | |
187 | | - function listboxHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 170 | + static function listboxHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
188 | 171 | global $sfgTabIndex, $sfgFieldNum; |
189 | 172 | |
190 | 173 | $className = ($is_mandatory) ? "mandatoryField" : "createboxInput"; |
— | — | @@ -204,7 +187,7 @@ |
205 | 188 | } else { |
206 | 189 | $delimiter = ","; |
207 | 190 | } |
208 | | - $cur_values = SFFormInputs::getValuesArray($cur_value, $delimiter); |
| 191 | + $cur_values = self::getValuesArray($cur_value, $delimiter); |
209 | 192 | |
210 | 193 | $text =<<<END |
211 | 194 | <select id="$input_id" tabindex="$sfgTabIndex" name="$input_name" class="$className" multiple $size_text $disabled_text> |
— | — | @@ -226,7 +209,7 @@ |
227 | 210 | return array($text, null); |
228 | 211 | } |
229 | 212 | |
230 | | - function checkboxesHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 213 | + static function checkboxesHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
231 | 214 | global $sfgTabIndex, $sfgFieldNum; |
232 | 215 | |
233 | 216 | $className = ($is_mandatory) ? "mandatoryField" : "createboxInput"; |
— | — | @@ -242,7 +225,7 @@ |
243 | 226 | } else { |
244 | 227 | $delimiter = ","; |
245 | 228 | } |
246 | | - $cur_values = SFFormInputs::getValuesArray($cur_value, $delimiter); |
| 229 | + $cur_values = self::getValuesArray($cur_value, $delimiter); |
247 | 230 | |
248 | 231 | if (($possible_values = $other_args['possible_values']) == null) |
249 | 232 | $possible_values = array(); |
— | — | @@ -273,51 +256,26 @@ |
274 | 257 | return array($text, null); |
275 | 258 | } |
276 | 259 | |
277 | | - function textInputWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 260 | + static function textInputWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
278 | 261 | // if 'no autocomplete' was specified, print a regular text entry instead |
279 | 262 | if (array_key_exists('no autocomplete', $other_args) && |
280 | | - $other_args['no autocomplete'] == true) |
| 263 | + $other_args['no autocomplete'] == true) { |
| 264 | + unset($other_args['autocompletion source']); |
281 | 265 | return SFFormInputs::textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args); |
| 266 | + } |
282 | 267 | // if a set of values was specified, print a dropdown instead |
283 | 268 | if (array_key_exists('possible_values', $other_args) && $other_args['possible_values'] != null) |
284 | 269 | return SFFormInputs::dropdownHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args); |
285 | 270 | |
286 | 271 | global $sfgTabIndex, $sfgFieldNum; |
287 | 272 | |
288 | | - $className = ($is_mandatory) ? "autocompleteInput mandatoryField" : "autocompleteInput"; |
| 273 | + $className = ($is_mandatory) ? "autocompleteInput mandatoryField" : "autocompleteInput createboxInput"; |
289 | 274 | if (array_key_exists('class', $other_args)) |
290 | 275 | $className .= " " . $other_args['class']; |
291 | 276 | $disabled_text = ($is_disabled) ? "disabled" : ""; |
292 | | - if (array_key_exists('autocomplete on property', $other_args)) { |
293 | | - $autocomplete_field_type = 'property'; |
294 | | - $semantic_field_name = $other_args['autocomplete on property']; |
295 | | - } elseif (array_key_exists('autocomplete on', $other_args)) { // for backwards-compatibility |
296 | | - $autocomplete_field_type = 'category'; |
297 | | - $semantic_field_name = $other_args['autocomplete on']; |
298 | | - } elseif (array_key_exists('autocomplete on category', $other_args)) { |
299 | | - $autocomplete_field_type = 'category'; |
300 | | - $semantic_field_name = $other_args['autocomplete on category']; |
301 | | - } elseif (array_key_exists('autocomplete on concept', $other_args)) { |
302 | | - $autocomplete_field_type = 'concept'; |
303 | | - $semantic_field_name = $other_args['autocomplete on concept']; |
304 | | - } elseif (array_key_exists('autocomplete on namespace', $other_args)) { |
305 | | - $autocomplete_field_type = 'namespace'; |
306 | | - $semantic_field_name = $other_args['autocomplete on namespace']; |
307 | | - // special handling for "main" (blank) namespace |
308 | | - if ($semantic_field_name == "") |
309 | | - $semantic_field_name = "main"; |
310 | | - } elseif (array_key_exists('is_relation', $other_args) || |
311 | | - (array_key_exists('field_type', $other_args) && $other_args['field_type'] == 'page')) { |
312 | | - $autocomplete_field_type = 'relation'; |
313 | | - $semantic_field_name = $other_args['semantic_field_name']; |
314 | | - } else { |
315 | | - $autocomplete_field_type = 'attribute'; |
316 | | - if (array_key_exists('semantic_field_name', $other_args)) |
317 | | - $semantic_field_name = $other_args['semantic_field_name']; |
318 | | - else |
319 | | - // there's some discrepancy between the form and template calls and |
320 | | - // the property for this field |
321 | | - $semantic_field_name = ""; |
| 277 | + if (array_key_exists('autocomplete field type', $other_args)) { |
| 278 | + $autocomplete_field_type = $other_args['autocomplete field type']; |
| 279 | + $autocompletion_source = $other_args['autocompletion source']; |
322 | 280 | } |
323 | 281 | $input_id = "input_" . $sfgFieldNum; |
324 | 282 | $info_id = "info_" . $sfgFieldNum; |
— | — | @@ -374,7 +332,7 @@ |
375 | 333 | <script type="text/javascript">/* <![CDATA[ */ |
376 | 334 | |
377 | 335 | END; |
378 | | - $options_str_key = str_replace("'", "\'", $semantic_field_name); |
| 336 | + $options_str_key = str_replace("'", "\'", $autocompletion_source); |
379 | 337 | if ($is_list) { |
380 | 338 | $options_str_key .= ",list"; |
381 | 339 | if ($delimiter != ",") { |
— | — | @@ -385,8 +343,8 @@ |
386 | 344 | if (array_key_exists('remote autocompletion', $other_args) && |
387 | 345 | $other_args['remote autocompletion'] == true) { |
388 | 346 | $javascript_text .= "autocompletedatatypes['$options_str_key'] = '$autocomplete_field_type';\n"; |
389 | | - } elseif ($semantic_field_name != '') { |
390 | | - $autocomplete_values = SFFormInputs::createAutocompleteValuesArray($semantic_field_name, $autocomplete_field_type); |
| 347 | + } elseif ($autocompletion_source != '') { |
| 348 | + $autocomplete_values = self::createAutocompleteValuesArray($autocompletion_source, $autocomplete_field_type); |
391 | 349 | $autocomplete_string = "[['" . implode("'], ['", $autocomplete_values) . "']]"; |
392 | 350 | // replace any newlines in the string, just to avoid breaking the Javascript |
393 | 351 | $autocomplete_string = str_replace("\n", ' ', $autocomplete_string); |
— | — | @@ -399,7 +357,7 @@ |
400 | 358 | return array($text, $javascript_text); |
401 | 359 | } |
402 | 360 | |
403 | | - function textAreaHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 361 | + static function textAreaHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
404 | 362 | // set size values |
405 | 363 | if (! array_key_exists('rows', $other_args)) |
406 | 364 | $other_args['rows'] = 5; |
— | — | @@ -407,19 +365,9 @@ |
408 | 366 | $other_args['cols'] = 80; |
409 | 367 | |
410 | 368 | // if it's an autocomplete, call the with-autocomplete function instead |
411 | | - $autocompletion_disabled = array_key_exists('autocomplete on', $other_args) && $other_args['autocomplete on'] == ""; |
412 | | - if (! $autocompletion_disabled) { |
413 | | - if ((array_key_exists('is_relation', $other_args) && $other_args['is_relation'] == true) || |
414 | | - array_key_exists('autocomplete', $other_args) || |
415 | | - array_key_exists('autocomplete on', $other_args) || |
416 | | - array_key_exists('autocomplete on property', $other_args) || |
417 | | - array_key_exists('autocomplete on category', $other_args) || |
418 | | - array_key_exists('autocomplete on concept', $other_args) || |
419 | | - array_key_exists('autocomplete on namespace', $other_args) || |
420 | | - array_key_exists('remote autocompletion', $other_args)) { |
| 369 | + if (array_key_exists('autocompletion source', $other_args)) { |
421 | 370 | $other_args['input_type'] = "textarea"; |
422 | 371 | return SFFormInputs::textInputWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args); |
423 | | - } |
424 | 372 | } |
425 | 373 | |
426 | 374 | global $sfgTabIndex, $sfgFieldNum; |
— | — | @@ -452,7 +400,7 @@ |
453 | 401 | return array($text, null); |
454 | 402 | } |
455 | 403 | |
456 | | - function monthDropdownHTML($cur_month, $input_name, $is_disabled) { |
| 404 | + static function monthDropdownHTML($cur_month, $input_name, $is_disabled) { |
457 | 405 | global $sfgTabIndex, $sfgFieldNum, $wgAmericanDates; |
458 | 406 | |
459 | 407 | $disabled_text = ($is_disabled) ? "disabled" : ""; |
— | — | @@ -469,7 +417,7 @@ |
470 | 418 | return $text; |
471 | 419 | } |
472 | 420 | |
473 | | - function dateEntryHTML($date, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 421 | + static function dateEntryHTML($date, $input_name, $is_mandatory, $is_disabled, $other_args) { |
474 | 422 | global $sfgTabIndex, $sfgFieldNum, $sfgJSValidationCalls, $wgAmericanDates; |
475 | 423 | |
476 | 424 | $input_id = "input_$sfgFieldNum"; |
— | — | @@ -514,7 +462,7 @@ |
515 | 463 | return array($text, null); |
516 | 464 | } |
517 | 465 | |
518 | | - function dateTimeEntryHTML($datetime, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 466 | + static function dateTimeEntryHTML($datetime, $input_name, $is_mandatory, $is_disabled, $other_args) { |
519 | 467 | global $sfgTabIndex, $sfg24HourTime; |
520 | 468 | |
521 | 469 | $include_timezone = $other_args['include_timezone']; |
— | — | @@ -581,7 +529,7 @@ |
582 | 530 | return array($text, $javascript_text); |
583 | 531 | } |
584 | 532 | |
585 | | - function radioButtonHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 533 | + static function radioButtonHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
586 | 534 | global $sfgTabIndex; |
587 | 535 | |
588 | 536 | $disabled_text = ($is_disabled) ? "disabled" : ""; |
— | — | @@ -614,7 +562,7 @@ |
615 | 563 | return array($text, null); |
616 | 564 | } |
617 | 565 | |
618 | | - function checkboxHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
| 566 | + static function checkboxHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) { |
619 | 567 | global $sfgTabIndex, $sfgFieldNum; |
620 | 568 | |
621 | 569 | $className = ($is_mandatory) ? "mandatoryField" : "createboxInput"; |