r46853 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46852‎ | r46853 | r46854 >
Date:17:12, 5 February 2009
Author:yaron
Status:deferred
Tags:
Comment:
Made all functions static, simplified handling of autocompletion using new 'autocomplete field type' and 'autocompletion source' parameters
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormInputs.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormInputs.inc
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * Handles the creation and running of a user-created form.
 4+ * Helper functions to display the various inputs of a user-generated form
55 *
66 * @author Yaron Koren
77 * @author Jeffrey Stuckman
@@ -10,34 +10,31 @@
1111
1212 class SFFormInputs {
1313
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) {
1519 $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'
3229 // 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);
3633 }
3734 // escape quotes, to avoid Javascript errors
3835 return array_map('addslashes', $names_array);
3936 }
4037
41 - function uploadLinkHTML($input_id, $delimiter = null) {
 38+ static function uploadLinkHTML($input_id, $delimiter = null) {
4239 $upload_window_page = SpecialPage::getPage('UploadWindow');
4340 $query_string = "sfInputID=$input_id";
4441 if ($delimiter != null)
@@ -48,24 +45,10 @@
4946 return $text;
5047 }
5148
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) {
5350 // 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);
7053 }
7154
7255 // if there are possible values specified, call the dropdown function
@@ -139,7 +122,7 @@
140123 return array($text, null);
141124 }
142125
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) {
144127 global $sfgTabIndex, $sfgFieldNum;
145128
146129 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
@@ -170,11 +153,11 @@
171154 return array($text, null);
172155 }
173156
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
177160 */
178 - function getValuesArray($value, $delimiter) {
 161+ static function getValuesArray($value, $delimiter) {
179162 if (is_array($value)) {
180163 return $value;
181164 } else {
@@ -183,7 +166,7 @@
184167 }
185168 }
186169
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) {
188171 global $sfgTabIndex, $sfgFieldNum;
189172
190173 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
@@ -204,7 +187,7 @@
205188 } else {
206189 $delimiter = ",";
207190 }
208 - $cur_values = SFFormInputs::getValuesArray($cur_value, $delimiter);
 191+ $cur_values = self::getValuesArray($cur_value, $delimiter);
209192
210193 $text =<<<END
211194 <select id="$input_id" tabindex="$sfgTabIndex" name="$input_name" class="$className" multiple $size_text $disabled_text>
@@ -226,7 +209,7 @@
227210 return array($text, null);
228211 }
229212
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) {
231214 global $sfgTabIndex, $sfgFieldNum;
232215
233216 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
@@ -242,7 +225,7 @@
243226 } else {
244227 $delimiter = ",";
245228 }
246 - $cur_values = SFFormInputs::getValuesArray($cur_value, $delimiter);
 229+ $cur_values = self::getValuesArray($cur_value, $delimiter);
247230
248231 if (($possible_values = $other_args['possible_values']) == null)
249232 $possible_values = array();
@@ -273,51 +256,26 @@
274257 return array($text, null);
275258 }
276259
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) {
278261 // if 'no autocomplete' was specified, print a regular text entry instead
279262 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']);
281265 return SFFormInputs::textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
 266+ }
282267 // if a set of values was specified, print a dropdown instead
283268 if (array_key_exists('possible_values', $other_args) && $other_args['possible_values'] != null)
284269 return SFFormInputs::dropdownHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
285270
286271 global $sfgTabIndex, $sfgFieldNum;
287272
288 - $className = ($is_mandatory) ? "autocompleteInput mandatoryField" : "autocompleteInput";
 273+ $className = ($is_mandatory) ? "autocompleteInput mandatoryField" : "autocompleteInput createboxInput";
289274 if (array_key_exists('class', $other_args))
290275 $className .= " " . $other_args['class'];
291276 $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'];
322280 }
323281 $input_id = "input_" . $sfgFieldNum;
324282 $info_id = "info_" . $sfgFieldNum;
@@ -374,7 +332,7 @@
375333 <script type="text/javascript">/* <![CDATA[ */
376334
377335 END;
378 - $options_str_key = str_replace("'", "\'", $semantic_field_name);
 336+ $options_str_key = str_replace("'", "\'", $autocompletion_source);
379337 if ($is_list) {
380338 $options_str_key .= ",list";
381339 if ($delimiter != ",") {
@@ -385,8 +343,8 @@
386344 if (array_key_exists('remote autocompletion', $other_args) &&
387345 $other_args['remote autocompletion'] == true) {
388346 $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);
391349 $autocomplete_string = "[['" . implode("'], ['", $autocomplete_values) . "']]";
392350 // replace any newlines in the string, just to avoid breaking the Javascript
393351 $autocomplete_string = str_replace("\n", ' ', $autocomplete_string);
@@ -399,7 +357,7 @@
400358 return array($text, $javascript_text);
401359 }
402360
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) {
404362 // set size values
405363 if (! array_key_exists('rows', $other_args))
406364 $other_args['rows'] = 5;
@@ -407,19 +365,9 @@
408366 $other_args['cols'] = 80;
409367
410368 // 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)) {
421370 $other_args['input_type'] = "textarea";
422371 return SFFormInputs::textInputWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
423 - }
424372 }
425373
426374 global $sfgTabIndex, $sfgFieldNum;
@@ -452,7 +400,7 @@
453401 return array($text, null);
454402 }
455403
456 - function monthDropdownHTML($cur_month, $input_name, $is_disabled) {
 404+ static function monthDropdownHTML($cur_month, $input_name, $is_disabled) {
457405 global $sfgTabIndex, $sfgFieldNum, $wgAmericanDates;
458406
459407 $disabled_text = ($is_disabled) ? "disabled" : "";
@@ -469,7 +417,7 @@
470418 return $text;
471419 }
472420
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) {
474422 global $sfgTabIndex, $sfgFieldNum, $sfgJSValidationCalls, $wgAmericanDates;
475423
476424 $input_id = "input_$sfgFieldNum";
@@ -514,7 +462,7 @@
515463 return array($text, null);
516464 }
517465
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) {
519467 global $sfgTabIndex, $sfg24HourTime;
520468
521469 $include_timezone = $other_args['include_timezone'];
@@ -581,7 +529,7 @@
582530 return array($text, $javascript_text);
583531 }
584532
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) {
586534 global $sfgTabIndex;
587535
588536 $disabled_text = ($is_disabled) ? "disabled" : "";
@@ -614,7 +562,7 @@
615563 return array($text, null);
616564 }
617565
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) {
619567 global $sfgTabIndex, $sfgFieldNum;
620568
621569 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";

Status & tagging log