r25103 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25102‎ | r25103 | r25104 >
Date:06:31, 24 August 2007
Author:yaron
Status:old
Tags:
Comment:
Improved PHP, HTML and Javascript validation of dates, added support for
'radiobutton' input type, made more usable for sites without SMW installed
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc
@@ -135,6 +135,7 @@
136136 // section
137137 $existing_template_text = null;
138138 if ($source_is_page) {
 139+ $matches = array();
139140 if ($allow_multiple) {
140141 // find the number of instances of this template in the page -
141142 // if it's more than one, re-parse this section of the
@@ -285,7 +286,10 @@
286287 }
287288 }
288289 // get the value from the request, if it's there
289 - $cur_value = $template_instance_query_values[$field_name];
 290+ if (isset ($template_instance_query_values[$field_name]))
 291+ $cur_value = $template_instance_query_values[$field_name];
 292+ else
 293+ $cur_value = '';
290294 if ($cur_value && ! is_array($cur_value)) {
291295 $cur_value = Sanitizer::safeEncodeAttribute($cur_value);
292296 }
@@ -297,8 +301,11 @@
298302 // if the user is starting to edit a page, and that page contains
299303 // the current template being processed, get the current template
300304 // field's value in the existing page
301 - if ($source_is_page && ($existing_template_text != null)) {
302 - $cur_value = $template_contents[$field_name];
 305+ if ($source_is_page && (! empty($existing_template_text))) {
 306+ if (isset($template_contents[$field_name]))
 307+ $cur_value = $template_contents[$field_name];
 308+ else
 309+ $cur_value = '';
303310 if ($cur_value) {
304311 $cur_value = Sanitizer::safeEncodeAttribute($cur_value);
305312 }
@@ -420,7 +427,13 @@
421428 if ($is_mandatory) {
422429 $input_id = "input_" . $gTabIndex;
423430 $info_id = "info_" . $gTabIndex;
424 - $js_validation_calls[] = "validate_mandatory_field ('$input_id', '$info_id')";
 431+ if ($input_type == 'date') {
 432+ $js_validation_calls[] = "validate_mandatory_field ('$input_id" . "_1', '$info_id')";
 433+ $js_validation_calls[] = "validate_mandatory_field ('$input_id" . "_2', '$info_id')";
 434+ $js_validation_calls[] = "validate_mandatory_field ('$input_id" . "_3', '$info_id')";
 435+ } else {
 436+ $js_validation_calls[] = "validate_mandatory_field ('$input_id', '$info_id')";
 437+ }
425438 }
426439 } else {
427440 $start_position = $brackets_end_loc;
@@ -820,6 +833,9 @@
821834 $text = SFFormPrinter::dateEntryHTML($template_field->input_name, $cur_value);
822835 } elseif ($template_field->input_type == 'checkbox') {
823836 $text = SFFormPrinter::checkboxHTML($template_field->input_name, $cur_value);
 837+ } elseif ($template_field->input_type == 'radiobutton') {
 838+ $include_none = ! $template_field->is_mandatory;
 839+ $text = SFFormPrinter::radioButtonHTML($template_field->input_name, $cur_value, $include_none, $template_field->possible_values);
824840 } elseif ($template_field->attr_or_rel == "relation") {
825841 $size = $template_field->size;
826842 if ($size == null) $size = 35;
@@ -828,6 +844,10 @@
829845 } else {
830846 $text = SFFormPrinter::textEntryWithAutocompleteHTML($size, $template_field->input_name, $template_field->part_of_multiple, $template_field->semantic_field, true, $cur_value, $out_autocomplete_values);
831847 }
 848+ // escape out here if user doesn't have SMW installed
 849+ } elseif ($smwgContLang == null) {
 850+ if ($size == null) $size = 35;
 851+ $text = SFFormPrinter::textEntryHTML($size, $template_field->input_name, $cur_value);
832852 } else { // input type not defined in form, and not a relation
833853 $attr_type = $template_field->attribute_type;
834854 $size = $template_field->size;
@@ -856,7 +876,7 @@
857877 function createAutocompleteValuesString($field_name, $is_relation) {
858878 global $sfgMaxAutocompleteValues;
859879 $fname = 'SFFormPrinter::createAutocompleteValuesString';
860 - $db = & wfGetDB( DB_SLAVE );
 880+ $db = wfGetDB( DB_SLAVE );
861881 $sql_options = array();
862882 $sql_options['LIMIT'] = $sfgMaxAutocompleteValues;
863883 // the query depends on whether this field is a relation or a category
@@ -907,7 +927,7 @@
908928
909929 $text = ' <select tabindex="' . $gTabIndex . '" name="' . $input_name . '" ' . $gDisabledText . '>';
910930 if ($include_none)
911 - $text .= " <option value=\"\">[None]</option>\n";
 931+ $text .= " <option value=\"\">" . wfMsg('sf_editdata_none') . "</option>\n";
912932 foreach ($possible_values as $possible_value) {
913933 $text .= " <option value=\"$possible_value\"";
914934 if ($possible_value == $cur_value) {$text .= " selected=\"selected\""; }
@@ -953,7 +973,7 @@
954974 $info_id = "info_$gTabIndex";
955975
956976 $text =<<<END
957 - <textarea tabindex="$gTabIndex" id="$input_id" name="$input_name" rows=$rows cols=$cols $gDisabledText/>$cur_value</textarea>
 977+ <textarea tabindex="$gTabIndex" id="$input_id" name="$input_name" rows=$rows cols=$cols $gDisabledText>$cur_value</textarea>
958978 <span id="$info_id" class="error_message"></span>
959979
960980 END;
@@ -963,7 +983,7 @@
964984 function monthDropdownHTML($cur_month, $input_name) {
965985 global $gTabIndex, $gDisabledText;
966986
967 - $text = ' <select tabindex="' . $gTabIndex . '" id="input_' . $gTabIndex . '" name="' . $input_name . "[month]\" $gDisabledText>\n";
 987+ $text = ' <select tabindex="' . $gTabIndex . '" id="input_' . $gTabIndex . '_1" name="' . $input_name . "[month]\" $gDisabledText>\n";
968988 $month_names = array(
969989 wfMsgForContent('sf_january'),
970990 wfMsgForContent('sf_february'),
@@ -1005,27 +1025,36 @@
10061026 }
10071027 } else {
10081028 $cur_date = getdate();
1009 - $year = $cur_date[year];
1010 - $month = $cur_date[month];
 1029+ $year = $cur_date['year'];
 1030+ $month = $cur_date['month'];
10111031 $day = null; // no need for day
10121032 }
1013 - $text .= SFFormPrinter::monthDropdownHTML($month, $input_name);
1014 - $text .= ' <input tabindex="' . $gTabIndex . '" id="input_' . $gTabIndex . '" name="' . $input_name . '[day]" type="text" value="' . $day . '" size="2"/ ' . $gDisabledText . '>' . "\n";
1015 - $text .= ' <input tabindex="' . $gTabIndex . '" id="input_' . $gTabIndex . '" name="' . $input_name . '[year]" type="text" value="' . $year . '" size="4"/ ' . $gDisabledText . '>' . "\n";
 1033+ $text = SFFormPrinter::monthDropdownHTML($month, $input_name);
 1034+ $text .= ' <input tabindex="' . $gTabIndex . '" id="input_' . $gTabIndex . '_2" name="' . $input_name . '[day]" type="text" value="' . $day . '" size="2"/ ' . $gDisabledText . '>' . "\n";
 1035+ $text .= ' <input tabindex="' . $gTabIndex . '" id="input_' . $gTabIndex . '_3" name="' . $input_name . '[year]" type="text" value="' . $year . '" size="4"/ ' . $gDisabledText . '>' . "\n";
10161036 $info_id = "info_$gTabIndex";
10171037 $text .= " <span id=\"$info_id\" class=\"error_message\"></span>";
10181038 return $text;
10191039 }
10201040
1021 - function radioButtonHTML($input_name, $cur_value, $options_array) {
 1041+ function radioButtonHTML($input_name, $cur_value, $include_none, $options_array) {
10221042 global $gTabIndex, $gDisabledText;
10231043
1024 - if ($title)
1025 - $text .= " <strong>$title:</strong>\n";
 1044+ $check_set = false;
 1045+ if ($include_none) {
 1046+ $text .= ' <input type="radio" tabindex="' . $gTabIndex . '" name="' . $input_name . '" value=""';
 1047+ if (! $cur_value) {
 1048+ $text .= " checked";
 1049+ $check_set = true;
 1050+ }
 1051+ $text .= " $gDisabledText/> " . wfMsg('sf_editdata_none') . "\n";
 1052+ }
10261053 foreach ($options_array as $i => $option) {
10271054 $text .= ' <input type="radio" tabindex="' . $gTabIndex . '" name="' . $input_name . '" value="' . $option . '"';
1028 - if ($cur_value == $option || (! $cur_value && $i == 0))
 1055+ if ($cur_value == $option || (! $cur_value && ! $check_set)) {
10291056 $text .= " checked";
 1057+ $check_set = true;
 1058+ }
10301059 $text .= " $gDisabledText/> $option\n";
10311060 }
10321061 return $text;
@@ -1229,22 +1258,22 @@
12301259
12311260 // based on MediaWiki's EditPage::getPreloadedText()
12321261 function getPreloadedText($preload) {
1233 - if ( $preload === '' )
1234 - return '';
1235 - else {
 1262+ if ( $preload === '' ) {
 1263+ return '';
 1264+ } else {
12361265 $preloadTitle = Title::newFromText( $preload );
1237 - if ( isset( $preloadTitle ) && $preloadTitle->userCanRead() ) {
1238 - $rev=Revision::newFromTitle($preloadTitle);
 1266+ if ( isset( $preloadTitle ) && $preloadTitle->userCanRead() ) {
 1267+ $rev = Revision::newFromTitle($preloadTitle);
12391268 if ( is_object( $rev ) ) {
12401269 $text = $rev->getText();
12411270 // Remove <noinclude> sections and <includeonly> tags from text
12421271 $text = StringUtils::delimiterReplace('<noinclude>', '</noinclude>', '', $text);
12431272 $text = strtr($text, array('<includeonly>' => '', '</includeonly>' => ''));
12441273 return $text;
1245 - } else
1246 - return '';
12471274 }
1248 - }
 1275+ }
 1276+ return '';
 1277+ }
12491278 }
12501279
12511280 }

Status & tagging log