r25964 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25963‎ | r25964 | r25965 >
Date:17:45, 20 September 2007
Author:yaron
Status:old
Tags:
Comment:
'free text' turned into hidden input if not included in form, new
'$sfgInputNum' global variable, used instead of '$sfgTabIndex' when setting
HTML IDs, 'autocomplete on namespace' property added, 'autocomplete on' gets
all the pages that belong to the child categories of the specified category,
removed copyright warning at the bottom of forms, fixed bugs where certain
field properties in forms were getting ignored
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc
@@ -51,9 +51,11 @@
5252 function formHTML($form_def, $form_submitted, $source_is_page, $existing_page_content = null, $page_title = null) {
5353 global $wgRequest, $wgUser, $wgParser;
5454 global $sfgTabIndex; // used to represent the current tab index in the form
 55+ global $sfgFieldNum; // used for setting various HTML IDs
5556
5657 // initialize some variables
57 - $sfgTabIndex = 0;
 58+ $sfgTabIndex = 1;
 59+ $sfgFieldNum = 1;
5860 // disable all form elements if user doesn't have edit permission
5961 $true_page_title = Title::newFromText($page_title);
6062 if ( $true_page_title->userCanEdit() ) {
@@ -122,6 +124,7 @@
123125 // modified; is it necessary?
124126 $section = " " . $form_def_sections[$section_num];
125127
 128+$while_instance = 0;
126129 while ($brackets_loc = strpos($section, '{{{', $start_position)) {
127130 $brackets_end_loc = strpos($section, "}}}", $brackets_loc);
128131 $bracketed_string = substr($section, $brackets_loc + 3, $brackets_end_loc - ($brackets_loc + 3));
@@ -358,6 +361,8 @@
359362 $field_args['rows'] = 5;
360363 if ($field_args['cols'] == null)
361364 $field_args['cols'] = 30;
 365+ $sfgTabIndex++;
 366+ $sfgFieldNum++;
362367 list($new_text, $new_javascript_text) = $this->textAreaHTML('<free_text>', 'free_text', false, $form_is_disabled, $field_args);
363368 }
364369 $free_text_was_included = true;
@@ -461,8 +466,8 @@
462467 $section = substr_replace($section, $new_text, $brackets_loc, $brackets_end_loc + 3 - $brackets_loc);
463468 // also add to Javascript validation code
464469 if ($is_mandatory) {
465 - $input_id = "input_" . $sfgTabIndex;
466 - $info_id = "info_" . $sfgTabIndex;
 470+ $input_id = "input_" . $sfgFieldNum;
 471+ $info_id = "info_" . $sfgFieldNum;
467472 if ($input_type == 'date' || $input_type == 'datetime' || $input_type == 'datetime with timezone') {
468473 $js_validation_calls[] = "validate_mandatory_field ('$input_id" . "_1', '$info_id')";
469474 $js_validation_calls[] = "validate_mandatory_field ('$input_id" . "_2', '$info_id')";
@@ -507,9 +512,9 @@
508513 $section = str_replace('[num]', "[{$instance_num}a]", $section);
509514 $remove_text = wfMsg('sf_editdata_remove');
510515 $form_text .=<<<END
511 - <div id="wrapper_$sfgTabIndex" class="multiple_template">
 516+ <div id="wrapper_$sfgFieldNum" class="multiple_template">
512517 $section
513 - <input type="button" onclick="removeInstance('wrapper_$sfgTabIndex');" value="$remove_text" tabindex="$sfgTabIndex" />
 518+ <input type="button" onclick="removeInstance('wrapper_$sfgFieldNum');" value="$remove_text" tabindex="$sfgTabIndex" />
514519 </div>
515520
516521 END;
@@ -531,13 +536,13 @@
532537 $add_another = wfMsg('sf_editdata_addanother');
533538 $form_text .=<<<END
534539 <p style="margin-left:10px;">
535 - <p><input type="button" onclick="addInstance('starter_$query_template_name', 'main_$query_template_name', '$sfgTabIndex');" value="$add_another" tabindex="$sfgTabIndex" /></p>
 540+ <p><input type="button" onclick="addInstance('starter_$query_template_name', 'main_$query_template_name', '$sfgFieldNum');" value="$add_another" tabindex="$sfgTabIndex" /></p>
536541
537542 END;
538543 // if a chooser is being used for this template, add the template
539544 // to the chooser data array
540545 if ($chooser_name !== false)
541 - $choosers[$chooser_name][]= array($query_template_name, $sfgTabIndex, $chooser_caption);
 546+ $choosers[$chooser_name][]= array($query_template_name, $sfgFieldNum, $chooser_caption);
542547 }
543548 } else {
544549 $form_text .= $section;
@@ -545,14 +550,10 @@
546551
547552 } // end for
548553
549 - // if it wasn't already included in the form definition, put the
550 - // 'free text' input at the bottom of the form
 554+ // if it wasn't included in the form definition, add the
 555+ // 'free text' input as a hidden field at the bottom
551556 if (! $free_text_was_included) {
552 - $form_text .= ' <fieldset><legend>' . wfMsg('sf_editdata_freetextlabel') . "</legend>\n";
553 - $other_args = array('rows' => 5, 'cols' => 30);
554 - list($text, $new_javascript_text) = $this->textAreaHTML('<free_text>', 'free_text', false, $form_is_disabled, $other_args);
555 - $form_text .= $text;
556 - $form_text .= " </fieldset>\n";
 557+ $form_text .= $this->hiddenFieldHTML('free_text', '<free_text>');
557558 }
558559 // get free text, and add to page data, as well as retroactively
559560 // inserting it into the form
@@ -803,7 +804,7 @@
804805 }
805806
806807 function formTemplateFieldHTML($field_name, $input_name, $cur_value, $is_mandatory, $is_hidden, $is_disabled, $input_type, $field_args, $all_fields, $strict_parsing) {
807 - global $sfgTabIndex;
 808+ global $sfgTabIndex, $sfgFieldNum;
808809
809810 // see if this field matches one of the fields defined for this template -
810811 // if it is, use all available information about that field; if it's not,
@@ -823,10 +824,11 @@
824825 }
825826
826827 // if this is not part of a 'multiple' template, incrememt the
827 - // global tab index (used for correct tabbing, and for creating
828 - // unique div IDs.)
 828+ // global tab index (used for correct tabbing)
829829 if (! $field_args['part_of_multiple'])
830830 $sfgTabIndex++;
 831+ // increment the global field number regardless
 832+ $sfgFieldNum++;
831833
832834 // create an SFFormTemplateField object, containing this field as well
833835 // as settings from the form definition file
@@ -868,17 +870,18 @@
869871 $other_args['is_relation'] = true;
870872 $other_args['semantic_field_name'] = $template_field->semantic_field;
871873 }
 874+ // merge default values and values from form - put the default values
 875+ // first, so that if there's a conflict they'll be overridden
872876 $funcArgs[] = array_merge($default_args, $other_args);
873877 list($text, $javascript_text) = call_user_func_array($hook_values[0], $funcArgs);
874878 // escape out here if user doesn't have SMW installed
875879 } elseif ($smwgContLang == null) {
876 - $size = $template_field->size;
 880+ $size = $template_field->field_args['size'];
877881 if ($size == null) $size = 35;
878882 $field_args = array('size' => $size);
879883 list($text, $javascript_text) = $this->textEntryHTML($cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $field_args);
880884 } else { // input type not defined in form, and not a relation
881885 $field_type = $template_field->field_type;
882 - $size = $template_field->size;
883886 if ($field_type != '' && $this->mSemanticTypeHooks[$field_type] != null) {
884887 $hook_values = $this->mSemanticTypeHooks[$field_type];
885888 $funcArgs = array();
@@ -886,7 +889,9 @@
887890 $funcArgs[] = $form_field->input_name;
888891 $funcArgs[] = $form_field->is_mandatory;
889892 $funcArgs[] = $form_field->is_disabled;
890 - $other_args = array_merge($form_field->field_args, $hook_values[1]);
 893+ // merge default values and values from form - put the default values
 894+ // first, so that if there's a conflict they'll be overridden
 895+ $other_args = array_merge($hook_values[1], $form_field->field_args);
891896 $other_args['possible_values'] = $template_field->possible_values;
892897 if ($template_field->is_relation) {
893898 $other_args['is_relation'] = true;
@@ -895,59 +900,125 @@
896901 $funcArgs[] = $other_args;
897902 list($text, $javascript_text) = call_user_func_array($hook_values[0], $funcArgs);
898903 } else { // anything else
899 - if ($field_args['size'] == null)
900 - $field_args['size'] = 35;
901 - list($text, $javascript_text) = $this->textEntryHTML($cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $field_args);
 904+ $other_args = $form_field->field_args;
 905+ if ($other_args['size'] == null)
 906+ $other_args['size'] = 35;
 907+ list($text, $javascript_text) = $this->textEntryHTML($cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $other_args);
902908 }
903909 }
904910 return array($text, $javascript_text);
905911 }
906912
907 - function createAutocompleteValuesString($field_name, $is_relation) {
 913+ /*
 914+ * Get all the pages that belong to a category and all its subcategories,
 915+ * down a certain number of levels - heavily based on SMW's
 916+ * SMWInlineQuery::includeSubcategories()
 917+ */
 918+ private function getAllPagesForCategory($dbr, $top_category, $num_levels) {
 919+ if (0 == $num_levels) return $top_category;
908920 global $sfgMaxAutocompleteValues;
909 - $fname = '$this->createAutocompleteValuesString';
 921+
 922+ $fname = "SFFormPrinter::getAllPagesForCategory";
 923+ $categories = array($top_category);
 924+ $checkcategories = array($top_category);
 925+ $pages = array();
 926+ for ($level = $num_levels; $level > 0; $level--) {
 927+ $newcategories = array();
 928+ foreach ($checkcategories as $category) {
 929+ $res = $dbr->select( // make the query
 930+ array('categorylinks', 'page'),
 931+ array('page_title', 'page_namespace'),
 932+ array('cl_from = page_id',
 933+ 'cl_to = '. $dbr->addQuotes($category)),
 934+ $fname);
 935+ if ($res) {
 936+ while ($res && $row = $dbr->fetchRow($res)) {
 937+ if (array_key_exists('page_title', $row)) {
 938+ $page_namespace = $row['page_namespace'];
 939+ if ($page_namespace == NS_CATEGORY) {
 940+ $new_category = $row[ 'page_title' ];
 941+ if (!in_array($new_category, $categories)) {
 942+ $newcategories[] = $new_category;
 943+ }
 944+ } else {
 945+ $cur_value = str_replace("'", "\'", $row['page_title']);
 946+ $pages[] = str_replace("_", " ", $cur_value);
 947+ // return if we've reached the maximum number of allowed values
 948+ if (count($pages) > $sfgMaxAutocompleteValues)
 949+ return $pages;
 950+ }
 951+ }
 952+ }
 953+ $dbr->freeResult( $res );
 954+ }
 955+ }
 956+ if (count($newcategories) == 0) {
 957+ return $pages;
 958+ } else {
 959+ $categories = array_merge($categories, $newcategories);
 960+ }
 961+ $checkcategories = array_diff($newcategories, array());
 962+ }
 963+ return $pages;
 964+ }
 965+
 966+ function createAutocompleteValuesString($field_name, $autocomplete_field_type) {
 967+ global $sfgMaxAutocompleteValues;
 968+ $fname = 'SFFormPrinter::createAutocompleteValuesString';
 969+
 970+ $names_array = array();
910971 $db = wfGetDB( DB_SLAVE );
911972 $sql_options = array();
912973 $sql_options['LIMIT'] = $sfgMaxAutocompleteValues;
913 - // the query depends on whether this field is a relation or a category
914 - if ($is_relation) {
 974+ // the query depends on whether this field is a relation, category or
 975+ // namespace
 976+ if ($autocomplete_field_type == 'relation') {
915977 $conditions = "relation_title = '$field_name'";
916978 $sql_options['ORDER BY'] = 'object_title';
917979 $res = $db->select( $db->tableName('smw_relations'),
918980 'DISTINCT object_title',
919981 $conditions, $fname, $sql_options);
920 - } else {
921 - $conditions = "cl_from = page_id AND cl_to = '$field_name'";
922 - $sql_options['ORDER BY'] = 'page_title';
923 - $res = $db->select( $db->tableNames('categorylinks', 'page'),
924 - 'page_title',
925 - $conditions, $fname, $sql_options);
926 - }
927 - if ($db->numRows( $res ) > 0) {
928 - $names_array = array();
929982 while ($row = $db->fetchRow($res)) {
930983 $cur_value = str_replace("'", "\'", $row[0]);
931984 $names_array[] = str_replace('_', ' ', $cur_value);
932985 }
933 - $array_str = "['" . implode("', '", $names_array) . "']";
934 - } else {
935 - // return an empty array
936 - $array_str = "[]";
 986+ $db->freeResult($res);
 987+ } elseif ($autocomplete_field_type == 'category') {
 988+ $names_array = $this->getAllPagesForCategory($db, $field_name, 10);
 989+ } else { // i.e., $autocomplete_field_type == 'namespace'
 990+ // cycle through all the namespace names for this language, and if
 991+ // one matches the namespace specified in the form, add the names
 992+ // of all the pages in that namespace to $names_array
 993+ global $wgContLang;
 994+ $namespaces = $wgContLang->getNamespaces();
 995+ foreach ($namespaces as $ns_code => $ns_name) {
 996+ if ($ns_name == $field_name) {
 997+ $conditions = "page_namespace = $ns_code";
 998+ $sql_options['ORDER BY'] = 'page_title';
 999+ $res = $db->select( $db->tableNames('page'),
 1000+ 'page_title',
 1001+ $conditions, $fname, $sql_options);
 1002+ while ($row = $db->fetchRow($res)) {
 1003+ $cur_value = str_replace("'", "\'", $row[0]);
 1004+ $names_array[] = str_replace('_', ' ', $cur_value);
 1005+ }
 1006+ $db->freeResult($res);
 1007+ }
 1008+ }
9371009 }
938 - $db->freeResult($res);
 1010+ $array_str = "['" . implode("', '", $names_array) . "']";
9391011 return $array_str;
9401012 }
9411013
9421014 function textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
9431015 // if it's an autocomplete, call the with-autocomplete function insted
944 - if ($other_args['autocomplete on'] != null) {
 1016+ if ($other_args['autocomplete on'] != null || $other_args['autocomplete on namespace'] != null) {
9451017 return SFFormPrinter::textEntryWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
9461018 }
9471019
948 - global $sfgTabIndex;
949 - $input_id = "input_$sfgTabIndex";
950 - $info_id = "info_$sfgTabIndex";
951 - $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
 1020+ global $sfgTabIndex, $sfgFieldNum;
 1021+ $input_id = "input_$sfgFieldNum";
 1022+ $info_id = "info_$sfgFieldNum";
9521023 $disabled_text = ($is_disabled) ? "disabled" : "";
9531024 $size = $other_args['size'];
9541025
@@ -992,21 +1063,24 @@
9931064 if ($other_args['no_autocomplete'] == true)
9941065 return SFFormPrinter::textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
9951066
996 - global $sfgTabIndex;
 1067+ global $sfgTabIndex, $sfgFieldNum;
9971068
9981069 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
9991070 $disabled_text = ($is_disabled) ? "disabled" : "";
10001071 $size = $other_args['size'];
10011072 if ($other_args['autocomplete on'] != null) {
1002 - $is_relation = false;
 1073+ $autocomplete_field_type = "category";
10031074 $semantic_field_name = $other_args['autocomplete on'];
 1075+ } elseif ($other_args['autocomplete on namespace'] != null) {
 1076+ $autocomplete_field_type = "namespace";
 1077+ $semantic_field_name = $other_args['autocomplete on namespace'];
10041078 } else {
1005 - $is_relation = true;
 1079+ $autocomplete_field_type = "relation";
10061080 $semantic_field_name = $other_args['semantic_field_name'];
10071081 }
1008 - $input_id = "input_" . $sfgTabIndex;
1009 - $info_id = "info_" . $sfgTabIndex;
1010 - $div_name = "div_" . $sfgTabIndex;
 1082+ $input_id = "input_" . $sfgFieldNum;
 1083+ $info_id = "info_" . $sfgFieldNum;
 1084+ $div_name = "div_" . $sfgFieldNum;
10111085 $text =<<<END
10121086 <input tabindex="$sfgTabIndex" id="$input_id" name="$input_name" type="text"
10131087 value="" size="$size" class="$className" $disabled_text/>
@@ -1017,10 +1091,10 @@
10181092 END;
10191093 $autocomplete_values = array();
10201094 $autocomplete_values['mappings'] = array();
1021 - $options_str_key = $semantic_field_name . ',' . $is_relation;
 1095+ $options_str_key = $semantic_field_name . ',' . $autocomplete_field_type;
10221096 if (!$autocomplete_values[$options_str_key])
1023 - $autocomplete_values[$options_str_key] = $this->createAutocompleteValuesString(str_replace(' ', '_', $semantic_field_name), $is_relation);
1024 - $autocomplete_values['mappings'][$sfgTabIndex] = $options_str_key;
 1097+ $autocomplete_values[$options_str_key] = $this->createAutocompleteValuesString(str_replace(' ', '_', $semantic_field_name), $autocomplete_field_type);
 1098+ $autocomplete_values['mappings'][$sfgFieldNum] = $options_str_key;
10251099 foreach ($autocomplete_values as $autocomplete_key => $autocomplete_string) {
10261100 if ($autocomplete_key == "mappings") {
10271101 foreach ($autocomplete_string as $field_number => $field_key) {
@@ -1038,11 +1112,11 @@
10391113 }
10401114
10411115 function textAreaHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
1042 - global $sfgTabIndex;
 1116+ global $sfgTabIndex, $sfgFieldNum;
10431117
10441118 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
1045 - $input_id = "input_$sfgTabIndex";
1046 - $info_id = "info_$sfgTabIndex";
 1119+ $input_id = "input_$sfgFieldNum";
 1120+ $info_id = "info_$sfgFieldNum";
10471121 $disabled_text = ($is_disabled) ? "disabled" : "";
10481122
10491123 $rows = $other_args['rows'];
@@ -1057,11 +1131,11 @@
10581132 }
10591133
10601134 function monthDropdownHTML($cur_month, $input_name, $is_disabled) {
1061 - global $sfgTabIndex;
 1135+ global $sfgTabIndex, $sfgFieldNum;
10621136
10631137 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
10641138 $disabled_text = ($is_disabled) ? "disabled" : "";
1065 - $text = ' <select tabindex="' . $sfgTabIndex . '" id="input_' . $sfgTabIndex . '_1" name="' . $input_name . "[month]\" $disabled_text>\n";
 1139+ $text = ' <select tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_1" name="' . $input_name . "[month]\" $disabled_text>\n";
10661140 $month_names = array(
10671141 wfMsgForContent('sf_january'),
10681142 wfMsgForContent('sf_february'),
@@ -1086,7 +1160,7 @@
10871161 }
10881162
10891163 function dateEntryHTML($date, $input_name, $is_mandatory, $is_disabled, $other_args) {
1090 - global $sfgTabIndex;
 1164+ global $sfgTabIndex, $sfgFieldNum;
10911165
10921166 if ($date) {
10931167 // can show up here either as an array or a string, depending on
@@ -1113,9 +1187,9 @@
11141188 }
11151189 $disabled_text = ($is_disabled) ? "disabled" : "";
11161190 $text = $this->monthDropdownHTML($month, $input_name, $is_disabled);
1117 - $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgTabIndex . '_2" name="' . $input_name . '[day]" type="text" value="' . $day . '" size="2"/ ' . $disabled_text . '>' . "\n";
1118 - $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgTabIndex . '_3" name="' . $input_name . '[year]" type="text" value="' . $year . '" size="4"/ ' . $disabled_text . '>' . "\n";
1119 - $info_id = "info_$sfgTabIndex";
 1191+ $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_2" name="' . $input_name . '[day]" type="text" value="' . $day . '" size="2"/ ' . $disabled_text . '>' . "\n";
 1192+ $text .= ' <input tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_3" name="' . $input_name . '[year]" type="text" value="' . $year . '" size="4"/ ' . $disabled_text . '>' . "\n";
 1193+ $info_id = "info_$sfgFieldNum";
11201194 $text .= " <span id=\"$info_id\" class=\"error_message\"></span>";
11211195 return array($text, null);
11221196 }
@@ -1207,10 +1281,10 @@
12081282 }
12091283
12101284 function checkboxHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
1211 - global $sfgTabIndex;
 1285+ global $sfgTabIndex, $sfgFieldNum;
12121286
12131287 $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
1214 - $info_id = "info_$sfgTabIndex";
 1288+ $info_id = "info_$sfgFieldNum";
12151289 $disabled_text = ($is_disabled) ? "disabled" : "";
12161290
12171291 // can show up here either as an array or a string, depending on
@@ -1228,7 +1302,7 @@
12291303 }
12301304 $text =<<<END
12311305 <input name="{$input_name}[is_checkbox]" type="hidden" value="true" />
1232 - <input id="input_$sfgTabIndex" name="{$input_name}[value]" type="checkbox" class="$className" tabindex="$sfgTabIndex" $checked_str $disabled_text/>
 1306+ <input id="input_$sfgFieldNum" name="{$input_name}[value]" type="checkbox" class="$className" tabindex="$sfgTabIndex" $checked_str $disabled_text/>
12331307 <span id="$info_id" class="error_message"></span>
12341308
12351309 END;
@@ -1291,19 +1365,11 @@
12921366
12931367 // Much of this function is based on MediaWiki's EditPage::showEditForm()
12941368 function formBottom($target_title = null, $is_disabled) {
1295 - global $wgVersion, $wgUser, $wgRightsText, $wgParser;
 1369+ global $wgVersion, $wgUser;
12961370 global $sfgTabIndex;
12971371 $sk = $wgUser->getSkin();
12981372
1299 - $copywarn = "<div id=\"editpage-copywarn\">\n" .
1300 - wfMsg( $wgRightsText ? 'copyrightwarning' : 'copyrightwarning2',
1301 - '[[' . wfMsgForContent( 'copyrightpage' ) . ']]',
1302 - null,
1303 - $wgRightsText ) . "\n</div>";
13041373 $title = new Title();
1305 - $options = new ParserOptions();
1306 - $copywarn_output = $wgParser->parse($copywarn, $title, $options);
1307 - $copywarn_text = $copywarn_output->getText();
13081374 $summary_text = wfMsg('summary');
13091375 if ($target_title == null)
13101376 $cancel = '';
@@ -1328,7 +1394,7 @@
13291395 $sfgTabIndex++;
13301396 $disabled_text = ($is_disabled) ? "disabled" : "";
13311397 $text =<<<END
1332 - $copywarn_text
 1398+ <br /><br />
13331399 <span id='wpSummaryLabel'><label for='wpSummary'>{$summary_text}:</label></span>
13341400 <div class='editOptions'>
13351401 <input tabindex="$sfgTabIndex" type='text' value="" name='wpSummary' id='wpSummary' maxlength='200' size='60' $disabled_text/><br />

Status & tagging log