r26218 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26217‎ | r26218 | r26219 >
Date:17:52, 28 September 2007
Author:yaron
Status:old
Tags:
Comment:
Replaced direct access of SMW member variables (bug 11467), added
array_key_exists() calls to eliminate PHP error messages (bug 11466), added
handling for two new input types - "listbox" and "checkboxes", both of which
read and output a delimited list of values (bug 11184), added more class
definitions in HTML tags to allow for more CSS control (bug 10875, to some
extent), made HTML being generated somewhat more W3C-valid.
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc
@@ -23,13 +23,13 @@
2424 // in the form definition
2525 $this->mSemanticTypeHooks = array();
2626 if ($smwgContLang != null) {
27 - $this->setSemanticTypeHook($smwgContLang->smwDatatypeLabels['smw_string'], array('SFFormPrinter', 'textEntryHTML'), array('size' => 35));
28 - $this->setSemanticTypeHook($smwgContLang->smwDatatypeLabels['smw_url'], array('SFFormPrinter', 'textEntryHTML'), array('size' => 100));
29 - $this->setSemanticTypeHook($smwgContLang->smwDatatypeLabels['smw_float'], array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
30 - $this->setSemanticTypeHook($smwgContLang->smwDatatypeLabels['smw_int'], array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
31 - $this->setSemanticTypeHook($smwgContLang->smwDatatypeLabels['smw_bool'], array('SFFormPrinter', 'checkboxHTML'), array());
32 - $this->setSemanticTypeHook($smwgContLang->smwDatatypeLabels['smw_datetime'], array('SFFormPrinter', 'dateEntryHTML'), array());
33 - $this->setSemanticTypeHook($smwgContLang->smwDatatypeLabels['smw_enum'], array('SFFormPrinter', 'dropdownHTML'), array());
 27+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_string'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 35));
 28+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_url'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 100));
 29+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_float'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
 30+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_int'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
 31+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_bool'), array('SFFormPrinter', 'checkboxHTML'), array());
 32+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_datetime'), array('SFFormPrinter', 'dateEntryHTML'), array());
 33+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_enum'), array('SFFormPrinter', 'dropdownHTML'), array());
3434 $this->setSemanticTypeHook('relation', array('SFFormPrinter', 'textEntryWithAutocompleteHTML'), array('size' => 35));
3535 }
3636 $this->mInputTypeHooks = array();
@@ -40,6 +40,8 @@
4141 $this->setInputTypeHook('datetime with timezone', array('SFFormPrinter', 'dateTimeEntryHTML'), array('include_timezone' => true));
4242 $this->setInputTypeHook('checkbox', array('SFFormPrinter', 'checkboxHTML'), array());
4343 $this->setInputTypeHook('radiobutton', array('SFFormPrinter', 'radioButtonHTML'), array());
 44+ $this->setInputTypeHook('listbox', array('SFFormPrinter', 'listboxHTML'), array());
 45+ $this->setInputTypeHook('checkboxes', array('SFFormPrinter', 'checkboxesHTML'), array());
4446
4547 // initialize other variables
4648 $this->standardInputsIncluded = false;
@@ -362,9 +364,9 @@
363365 if ($is_hidden) {
364366 $new_text = $this->hiddenFieldHTML('free_text', '<free_text>');
365367 } else {
366 - if ($field_args['rows'] == null)
 368+ if (! array_key_exists('rows', $field_args))
367369 $field_args['rows'] = 5;
368 - if ($field_args['cols'] == null)
 370+ if (! array_key_exists('cols', $field_args))
369371 $field_args['cols'] = 30;
370372 $sfgTabIndex++;
371373 $sfgFieldNum++;
@@ -377,54 +379,72 @@
378380 $section = substr_replace($section, $new_text, $brackets_loc, $brackets_end_loc + 3 - $brackets_loc);
379381 } else { // this field is part of a template
380382 if (is_array($cur_value)) {
381 - // if it has 1 or 2 elements, assume it's a checkbox; if it has
382 - // 3 elements, assume it's a date
383 - // - this handling will have to get more complex if other
384 - // possibilities get added
385 - if (count($cur_value) == 1) {
386 - $words_for_false = explode(',', wfMsgForContent('smw_false_words'));
387 - // for the various languages, the second word in the 'false'
388 - // series tends to be "no" - go with that one
389 - if (count($words_for_false) > 2) {
390 - $no = $words_for_false[1];
391 - } elseif (count($words_for_false) == 0) {
392 - $no = "0"; // some safe value if no words are found
393 - } else {
394 - $no = $words_for_false[0];
 383+ // first, check if it's a list
 384+ if ($cur_value['is_list'] == 1) {
 385+ $cur_value_in_template = "";
 386+ if (array_key_exists('delimiter', $field_args))
 387+ $delimiter = $field_args['delimiter'];
 388+ else
 389+ $delimiter = ",";
 390+ foreach ($cur_value as $key => $val) {
 391+ if ($key !== "is_list") {
 392+ if ($cur_value_in_template != "") {
 393+ $cur_value_in_template .= $delimiter . " ";
 394+ }
 395+ $cur_value_in_template .= $val;
 396+ }
395397 }
396 - $cur_value_in_template = $no;
397 - } elseif (count($cur_value) == 2) {
398 - $words_for_true = explode(',', wfMsgForContent('smw_true_words'));
399 - // the second word in the 'true' series tends to be "yes" -
400 - // go with that one
401 - if (count($words_for_true) > 2) {
402 - $yes = $words_for_true[1];
403 - } elseif (count($words_for_true) == 0) {
404 - $yes = "1"; // some safe value if no words are found
405 - } else {
406 - $yes = $words_for_true[0];
 398+ } else {
 399+ // otherwise:
 400+ // if it has 1 or 2 elements, assume it's a checkbox; if it has
 401+ // 3 elements, assume it's a date
 402+ // - this handling will have to get more complex if other
 403+ // possibilities get added
 404+ if (count($cur_value) == 1) {
 405+ $words_for_false = explode(',', wfMsgForContent('smw_false_words'));
 406+ // for the various languages, the second word in the 'false'
 407+ // series tends to be "no" - go with that one
 408+ if (count($words_for_false) > 2) {
 409+ $no = $words_for_false[1];
 410+ } elseif (count($words_for_false) == 0) {
 411+ $no = "0"; // some safe value if no words are found
 412+ } else {
 413+ $no = $words_for_false[0];
 414+ }
 415+ $cur_value_in_template = $no;
 416+ } elseif (count($cur_value) == 2) {
 417+ $words_for_true = explode(',', wfMsgForContent('smw_true_words'));
 418+ // the second word in the 'true' series tends to be "yes" -
 419+ // go with that one
 420+ if (count($words_for_true) > 2) {
 421+ $yes = $words_for_true[1];
 422+ } elseif (count($words_for_true) == 0) {
 423+ $yes = "1"; // some safe value if no words are found
 424+ } else {
 425+ $yes = $words_for_true[0];
 426+ }
 427+ $cur_value_in_template = $yes;
 428+ // if it's 3 or greater, assume it's a date or datetime
 429+ } elseif (count($cur_value) >= 3) {
 430+ $month = $cur_value['month'];
 431+ $day = $cur_value['day'];
 432+ $year = $cur_value['year'];
 433+ if (isset($cur_value['hour'])) $hour = $cur_value['hour'];
 434+ if (isset($cur_value['minute'])) $minute = $cur_value['minute'];
 435+ if (isset($cur_value['second'])) $second = $cur_value['second'];
 436+ if (isset($cur_value['ampm24h'])) $ampm24h = $cur_value['ampm24h'];
 437+ if (isset($cur_value['timezone'])) $timezone = $cur_value['timezone'];
 438+ if ($month != '' && $day != '' && $year != '') {
 439+ $cur_value_in_template = "$month $day, $year";
 440+ // include whatever time information we have
 441+ if(isset($hour)) $cur_value_in_template .= " " . str_pad(intval(substr($hour,0,2)),2,'0',STR_PAD_LEFT) . ":" . str_pad(intval(substr($minute,0,2)),2,'0',STR_PAD_LEFT);
 442+ if(isset($second)) $cur_value_in_template .= ":" . str_pad(intval(substr($second,0,2)),2,'0',STR_PAD_LEFT);
 443+ if(isset($ampm24h)) $cur_value_in_template .= " $ampm24h";
 444+ if(isset($timezone)) $cur_value_in_template .= " $timezone";
 445+ } else {
 446+ $cur_value_in_template = "";
 447+ }
407448 }
408 - $cur_value_in_template = $yes;
409 - // if it's 3 or greater, assume it's a date or datetime
410 - } elseif (count($cur_value) >= 3) {
411 - $month = $cur_value['month'];
412 - $day = $cur_value['day'];
413 - $year = $cur_value['year'];
414 - if (isset($cur_value['hour'])) $hour = $cur_value['hour'];
415 - if (isset($cur_value['minute'])) $minute = $cur_value['minute'];
416 - if (isset($cur_value['second'])) $second = $cur_value['second'];
417 - if (isset($cur_value['ampm24h'])) $ampm24h = $cur_value['ampm24h'];
418 - if (isset($cur_value['timezone'])) $timezone = $cur_value['timezone'];
419 - if ($month != '' && $day != '' && $year != '') {
420 - $cur_value_in_template = "$month $day, $year";
421 - # include whatever time information we have
422 - if(isset($hour)) $cur_value_in_template .= " " . str_pad(intval(substr($hour,0,2)),2,'0',STR_PAD_LEFT) . ":" . str_pad(intval(substr($minute,0,2)),2,'0',STR_PAD_LEFT);
423 - if(isset($second)) $cur_value_in_template .= ":" . str_pad(intval(substr($second,0,2)),2,'0',STR_PAD_LEFT);
424 - if(isset($ampm24h)) $cur_value_in_template .= " $ampm24h";
425 - if(isset($timezone)) $cur_value_in_template .= " $timezone";
426 - } else {
427 - $cur_value_in_template = "";
428 - }
429449 }
430450 } else { // value is not an array
431451 $cur_value_in_template = $cur_value;
@@ -545,7 +565,7 @@
546566 $form_text .=<<<END
547567 <div id="wrapper_$sfgFieldNum" class="multiple_template">
548568 $section
549 - <input type="button" onclick="removeInstance('wrapper_$sfgFieldNum');" value="$remove_text" tabindex="$sfgTabIndex" />
 569+ <input type="button" onclick="removeInstance('wrapper_$sfgFieldNum');" value="$remove_text" tabindex="$sfgTabIndex" class="remove" />
550570 </div>
551571
552572 END;
@@ -567,7 +587,7 @@
568588 $add_another = wfMsg('sf_editdata_addanother');
569589 $form_text .=<<<END
570590 <p style="margin-left:10px;">
571 - <p><input type="button" onclick="addInstance('starter_$query_template_name', 'main_$query_template_name', '$sfgFieldNum');" value="$add_another" tabindex="$sfgTabIndex" /></p>
 591+ <p><input type="button" onclick="addInstance('starter_$query_template_name', 'main_$query_template_name', '$sfgFieldNum');" value="$add_another" tabindex="$sfgTabIndex" class="addAnother" /></p>
572592
573593 END;
574594 // if a chooser is being used for this template, add the template
@@ -749,7 +769,7 @@
750770 //Create remove button
751771 var remove_button = document.createElement('input');
752772 remove_button.type = 'button';
753 - remove_button.value = '$remove_text';
 773+ remove_button.value = "$remove_text";
754774 remove_button.tabIndex = tab_index;
755775 remove_button.onclick = removeInstanceEventHandler(div_id);
756776 new_div.appendChild(remove_button);
@@ -913,7 +933,7 @@
914934 list($text, $javascript_text) = $this->textEntryHTML($cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $field_args);
915935 } else { // input type not defined in form, and not a relation
916936 $field_type = $template_field->field_type;
917 - if ($field_type != '' && $this->mSemanticTypeHooks[$field_type] != null) {
 937+ if ($field_type != '' && array_key_exists($field_type, $this->mSemanticTypeHooks)) {
918938 $hook_values = $this->mSemanticTypeHooks[$field_type];
919939 $funcArgs = array();
920940 $funcArgs[] = $cur_value;
@@ -932,7 +952,7 @@
933953 list($text, $javascript_text) = call_user_func_array($hook_values[0], $funcArgs);
934954 } else { // anything else
935955 $other_args = $form_field->field_args;
936 - if ($other_args['size'] == null)
 956+ if (!array_key_exists('size', $other_args))
937957 $other_args['size'] = 35;
938958 list($text, $javascript_text) = $this->textEntryHTML($cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $other_args);
939959 }
@@ -1043,7 +1063,7 @@
10441064
10451065 function textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
10461066 // if it's an autocomplete, call the with-autocomplete function insted
1047 - if ($other_args['autocomplete on'] != null || $other_args['autocomplete on namespace'] != null) {
 1067+ if (array_key_exists('autocomplete on', $other_args) || array_key_exists('autocomplete on namespace', $other_args)) {
10481068 return SFFormPrinter::textEntryWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
10491069 }
10501070
@@ -1070,7 +1090,7 @@
10711091 $input_id = "input_$sfgFieldNum";
10721092 $disabled_text = ($is_disabled) ? "disabled" : "";
10731093 $text =<<<END
1074 - <select id="$input_id" tabindex="$sfgTabIndex" name="$input_name" class="className" $disabled_text>
 1094+ <select id="$input_id" tabindex="$sfgTabIndex" name="$input_name" class="$className" $disabled_text>
10751095
10761096 END;
10771097 // add a blank value at the beginning if it's a non-mandatory field
@@ -1091,6 +1111,87 @@
10921112 return array($text, null);
10931113 }
10941114
 1115+ /*
 1116+ * getValuesArray() - helper function to get an array of values out of
 1117+ * what may be either an array or a delimited string
 1118+ */
 1119+ function getValuesArray($value, $delimiter) {
 1120+ if (is_array($value)) {
 1121+ return $value;
 1122+ } else {
 1123+ // remove extra spaces
 1124+ return array_map('trim', explode($delimiter, $value));
 1125+ }
 1126+ }
 1127+
 1128+ function listboxHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
 1129+ global $sfgTabIndex, $sfgFieldNum;
 1130+
 1131+ $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
 1132+ $input_id = "input_$sfgFieldNum";
 1133+ $hidden_input_name = $input_name . "[is_list]";
 1134+ $input_name .= "[]"; // needed so that this input will send an array
 1135+ $disabled_text = ($is_disabled) ? "disabled" : "";
 1136+ // get list delimiter - default is comma
 1137+ $delimiter = $other_args['delimiter'];
 1138+ if ($delimiter == null)
 1139+ $delimiter = ",";
 1140+ $cur_values = $this->getValuesArray($cur_value, $delimiter);
 1141+
 1142+ $text =<<<END
 1143+ <select id="$input_id" tabindex="$sfgTabIndex" name="$input_name" class="$className" multiple $disabled_text>
 1144+
 1145+END;
 1146+ if (($possible_values = $other_args['possible_values']) == null)
 1147+ $possible_values = array();
 1148+ foreach ($possible_values as $possible_value) {
 1149+ $text .= " <option value=\"$possible_value\"";
 1150+ if (in_array($possible_value, $cur_values)) {$text .= " selected"; }
 1151+ $text .= ">$possible_value</option>\n";
 1152+ }
 1153+ $text .=<<<END
 1154+ </select>
 1155+ <span id="$info_id" class="error_message"></span>
 1156+ <input type="hidden" name="$hidden_input_name" value="1" />
 1157+
 1158+END;
 1159+ return array($text, null);
 1160+ }
 1161+
 1162+ function checkboxesHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
 1163+ global $sfgTabIndex, $sfgFieldNum;
 1164+
 1165+ $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
 1166+ $input_id = "input_$sfgFieldNum";
 1167+ $hidden_input_name = $input_name . "[is_list]";
 1168+ $disabled_text = ($is_disabled) ? "disabled" : "";
 1169+ // get list delimiter - default is comma
 1170+ $delimiter = $other_args['delimiter'];
 1171+ if ($delimiter == null)
 1172+ $delimiter = ",";
 1173+ $cur_values = $this->getValuesArray($cur_value, $delimiter);
 1174+
 1175+ if (($possible_values = $other_args['possible_values']) == null)
 1176+ $possible_values = array();
 1177+ foreach ($possible_values as $key => $possible_value) {
 1178+ $cur_input_name = $input_name . "[" . $key . "]";
 1179+ $checked_text = (in_array($possible_value, $cur_values)) ? 'checked="checked"' : "";
 1180+ $text .=<<<END
 1181+ <span class="checkboxSpan">
 1182+ <input type="checkbox" id="$input_id" tabindex="$sfgTabIndex" name="$cur_input_name" value="$possible_value" class="$className" $checked_text $disabled_text/> $possible_value
 1183+ </span>
 1184+
 1185+END;
 1186+ $sfgTabIndex++;
 1187+ }
 1188+ $text .=<<<END
 1189+ <span id="$info_id" class="error_message"></span>
 1190+ <input type="hidden" name="$hidden_input_name" value="1" />
 1191+
 1192+END;
 1193+ return array($text, null);
 1194+ }
 1195+
10951196 function textEntryWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
10961197 // if 'no autocomplete' was specified, call the regular text entry instead
10971198 if ($other_args['no_autocomplete'] == true)
@@ -1098,13 +1199,13 @@
10991200
11001201 global $sfgTabIndex, $sfgFieldNum;
11011202
1102 - $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
 1203+ $className = ($is_mandatory) ? "mandatoryField" : "autocompleteInput";
11031204 $disabled_text = ($is_disabled) ? "disabled" : "";
11041205 $size = $other_args['size'];
1105 - if ($other_args['autocomplete on'] != null) {
 1206+ if (array_key_exists('autocomplete on', $other_args)) {
11061207 $autocomplete_field_type = "category";
11071208 $semantic_field_name = $other_args['autocomplete on'];
1108 - } elseif ($other_args['autocomplete on namespace'] != null) {
 1209+ } elseif (array_key_exists('autocomplete on namespace', $other_args)) {
11091210 $autocomplete_field_type = "namespace";
11101211 $semantic_field_name = $other_args['autocomplete on namespace'];
11111212 } else {
@@ -1220,8 +1321,8 @@
12211322 }
12221323 $disabled_text = ($is_disabled) ? "disabled" : "";
12231324 $text = $this->monthDropdownHTML($month, $input_name, $is_disabled);
1224 - $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_2" name="' . $input_name . '[day]" type="text" value="' . $day . '" size="2"/ ' . $disabled_text . '>' . "\n";
1225 - $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_3" name="' . $input_name . '[year]" type="text" value="' . $year . '" size="4"/ ' . $disabled_text . '>' . "\n";
 1325+ $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_2" name="' . $input_name . '[day]" type="text" value="' . $day . '" size="2" ' . $disabled_text . '/>' . "\n";
 1326+ $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_3" name="' . $input_name . '[year]" type="text" value="' . $year . '" size="4" ' . $disabled_text . '/>' . "\n";
12261327 $info_id = "info_$sfgFieldNum";
12271328 $text .= " <span id=\"$info_id\" class=\"error_message\"></span>";
12281329 return array($text, null);
@@ -1295,7 +1396,7 @@
12961397 if (! $is_mandatory) {
12971398 $text .= ' <input type="radio" tabindex="' . $sfgTabIndex . '" name="' . $input_name . '" value=""';
12981399 if (! $cur_value) {
1299 - $text .= " checked";
 1400+ $text .= ' checked="checked"';
13001401 $check_set = true;
13011402 }
13021403 $text .= " $disabled_text/> " . wfMsg('sf_editdata_none') . "\n";
@@ -1305,7 +1406,7 @@
13061407 foreach ($possible_values as $i => $possible_value) {
13071408 $text .= ' <input type="radio" tabindex="' . $sfgTabIndex . '" name="' . $input_name . '" value="' . $possible_value . '"';
13081409 if ($cur_value == $possible_value || (! $cur_value && ! $check_set)) {
1309 - $text .= " checked";
 1410+ $text .= ' checked="checked"';
13101411 $check_set = true;
13111412 }
13121413 $text .= " $disabled_text/> $possible_value\n";
@@ -1323,12 +1424,12 @@
13241425 // can show up here either as an array or a string, depending on
13251426 // whether it came from user input or a wiki page
13261427 if (is_array($cur_value)) {
1327 - $checked_str = ($cur_value[value] == 'on') ? " checked" : "";
 1428+ $checked_str = ($cur_value[value] == 'on') ? ' checked="checked"' : "";
13281429 } else {
13291430 // default to false - no need to check if it matches a 'false' word
13301431 $vlc = strtolower(trim($cur_value));
13311432 if (in_array($vlc, explode(',', wfMsgForContent('smw_true_words')), TRUE)) {
1332 - $checked_str = " checked";
 1433+ $checked_str = ' checked="checked"';
13331434 } else {
13341435 $checked_str = "";
13351436 }
@@ -1526,7 +1627,6 @@
15271628 function formBottom($is_disabled) {
15281629 global $wgUser;
15291630
1530 - $disabled_text = ($is_disabled) ? "disabled" : "";
15311631 $summary_text = $this->summaryInputHTML($is_disabled);
15321632 $text =<<<END
15331633 <br /><br />

Status & tagging log