Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.php |
— | — | @@ -144,6 +144,16 @@ |
145 | 145 | } |
146 | 146 | } |
147 | 147 | |
| 148 | + /** |
| 149 | + * This function is the real heart of the entire Semantic Forms |
| 150 | + * extension. It handles two main actions: (1) displaying a form on the |
| 151 | + * screen, given a form definition and possibly page contents (if an |
| 152 | + * existing page is being edited); and (2) creating actual page contents, |
| 153 | + * if the form was already submitted by the user. |
| 154 | + * |
| 155 | + * It also does some related tasks, like figuring out the page name (if |
| 156 | + * only a page formula exists). |
| 157 | + */ |
148 | 158 | function formHTML( $form_def, $form_submitted, $source_is_page, $form_id = null, $existing_page_content = null, $page_name = null, $page_name_formula = null, $is_query = false, $embedded = false ) { |
149 | 159 | global $wgRequest, $wgUser, $wgParser; |
150 | 160 | global $sfgTabIndex; // used to represent the current tab index in the form |
— | — | @@ -184,9 +194,9 @@ |
185 | 195 | } |
186 | 196 | } |
187 | 197 | |
188 | | - // disable all form elements if user doesn't have edit permission - |
| 198 | + // Disable all form elements if user doesn't have edit permission - |
189 | 199 | // two different checks are needed, because editing permissions can be |
190 | | - // set in different ways |
| 200 | + // set in different ways. |
191 | 201 | // HACK - sometimes we don't know the page name in advance, but we still |
192 | 202 | // need to set a title here for testing permissions |
193 | 203 | if ( $embedded ) { |
— | — | @@ -218,9 +228,9 @@ |
219 | 229 | $form_text = ""; |
220 | 230 | if ( $userCanEditPage || $is_query ) { |
221 | 231 | $form_is_disabled = false; |
222 | | - // show "Your IP address will be recorded" warning if user is |
| 232 | + // Show "Your IP address will be recorded" warning if user is |
223 | 233 | // anonymous, and it's not a query - |
224 | | - // wikitext for bolding has to be replaced with HTML |
| 234 | + // wiki-text for bolding has to be replaced with HTML. |
225 | 235 | if ( $wgUser->isAnon() && ! $is_query ) { |
226 | 236 | $anon_edit_warning = preg_replace( "/'''(.*)'''/", "<strong>$1</strong>", wfMsg( 'anoneditwarning' ) ); |
227 | 237 | $form_text .= "<p>$anon_edit_warning</p>\n"; |
— | — | @@ -231,14 +241,14 @@ |
232 | 242 | $wgOut->addHTML( "\n<hr />\n" ); |
233 | 243 | } |
234 | 244 | |
235 | | - // Remove <noinclude> sections and <includeonly> tags from form definition |
| 245 | + // Remove <noinclude> sections and <includeonly> tags from form definition. |
236 | 246 | $form_def = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $form_def ); |
237 | 247 | $form_def = strtr( $form_def, array( '<includeonly>' => '', '</includeonly>' => '' ) ); |
238 | 248 | |
239 | | - // parse wiki-text |
240 | | - // add '<nowiki>' tags around every triple-bracketed form definition |
| 249 | + // Parse wiki-text. |
| 250 | + // Add '<nowiki>' tags around every triple-bracketed form definition |
241 | 251 | // element, so that the wiki parser won't touch it - the parser will |
242 | | - // remove the '<nowiki>' tags, leaving us with what we need |
| 252 | + // remove the '<nowiki>' tags, leaving us with what we need. |
243 | 253 | $form_def = "__NOEDITSECTION__" . strtr( $form_def, array( '{{{' => '<nowiki>{{{', '}}}' => '}}}</nowiki>' ) ); |
244 | 254 | $old_strip_state = $wgParser->mStripState; |
245 | 255 | $wgParser->mStripState = new StripState(); |
— | — | @@ -272,13 +282,13 @@ |
273 | 283 | $free_text_preload_page = null; |
274 | 284 | $free_text_components = array(); |
275 | 285 | $all_values_for_template = array(); |
276 | | - // unencode and HTML-encoded representations of curly brackets and |
| 286 | + // Unencode any HTML-encoded representations of curly brackets and |
277 | 287 | // pipes - this is a hack to allow for forms to include templates |
278 | | - // that themselves contain form elements - the escaping is needed |
279 | | - // to make sure that those elements don't get parsed too early |
| 288 | + // that themselves contain form elements - the escaping was needed |
| 289 | + // to make sure that those elements don't get parsed too early. |
280 | 290 | $form_def = str_replace( array( '{', '|', '}' ), array( '{', '|', '}' ), $form_def ); |
281 | | - // and another hack - replace the 'free text' standard input with |
282 | | - // a field declaration to get it to be handled as a field |
| 291 | + // And another hack - replace the 'free text' standard input with |
| 292 | + // a field declaration to get it to be handled as a field. |
283 | 293 | $form_def = str_replace( 'standard input|free text', 'field|<freetext>', $form_def ); |
284 | 294 | while ( $brackets_loc = strpos( $form_def, "{{{", $start_position ) ) { |
285 | 295 | $brackets_end_loc = strpos( $form_def, "}}}", $brackets_loc ); |
— | — | @@ -327,10 +337,10 @@ |
328 | 338 | $tif->template_name = $template_name; |
329 | 339 | $query_template_name = str_replace( ' ', '_', $template_name ); |
330 | 340 | $add_button_text = wfMsg( 'sf_formedit_addanother' ); |
331 | | - // also replace periods with underlines, since that's what |
332 | | - // POST does to strings anyway |
| 341 | + // Also replace periods with underlines, since that's what |
| 342 | + // POST does to strings anyway. |
333 | 343 | $query_template_name = str_replace( '.', '_', $query_template_name ); |
334 | | - // cycle through the other components |
| 344 | + // Cycle through the other components. |
335 | 345 | for ( $i = 2; $i < count( $tag_components ); $i++ ) { |
336 | 346 | $component = $tag_components[$i]; |
337 | 347 | if ( $component == 'multiple' ) $allow_multiple = true; |
— | — | @@ -344,7 +354,8 @@ |
345 | 355 | } |
346 | 356 | } |
347 | 357 | } |
348 | | - // if this is the first instance, add the label in the form |
| 358 | + // If this is the first instance, add the label into the form, if |
| 359 | + // there is one. |
349 | 360 | if ( ( $old_template_name != $template_name ) && isset( $template_label ) ) { |
350 | 361 | $form_text .= "<fieldset>\n"; |
351 | 362 | $form_text .= "<legend>$template_label</legend>\n"; |
— | — | @@ -383,12 +394,12 @@ |
384 | 395 | } |
385 | 396 | // get the first instance of this template on the page being edited, |
386 | 397 | // even if there are more |
387 | | - if ( $found_instance ) { |
| 398 | + if ( $found_instance ) { |
388 | 399 | $matches = array(); |
389 | 400 | $search_pattern = '/{{' . $preg_match_template_str . '\s*[\|}]/i'; |
390 | 401 | $content_str = str_replace( '_', ' ', $existing_page_content ); |
391 | 402 | preg_match($search_pattern, $content_str, $matches, PREG_OFFSET_CAPTURE); |
392 | | - // is this check necessary? |
| 403 | + // is this check necessary? |
393 | 404 | if ( array_key_exists( 0, $matches ) && array_key_exists( 1, $matches[0] ) ) { |
394 | 405 | $start_char = $matches[0][1]; |
395 | 406 | $fields_start_char = $start_char + 2 + strlen( $search_template_str ); |
— | — | @@ -450,7 +461,7 @@ |
451 | 462 | } else { |
452 | 463 | $existing_page_content = self::strReplaceFirst( $existing_template_text, '', $existing_page_content ); |
453 | 464 | } |
454 | | - // if this is not a multiple-instance template, and we've found |
| 465 | + // If this is not a multiple-instance template, and we've found |
455 | 466 | // a match in the source page, there's a good chance that this |
456 | 467 | // page was created with this form - note that, so we don't |
457 | 468 | // send the user a warning |
— | — | @@ -459,19 +470,19 @@ |
460 | 471 | // - on second thought, allow even the presence of multiple- |
461 | 472 | // instance templates to validate that this is the correct |
462 | 473 | // form: the problem is that some forms contain *only* mutliple- |
463 | | - // instance templates |
| 474 | + // instance templates. |
464 | 475 | // if (! $allow_multiple) { |
465 | 476 | $source_page_matches_this_form = true; |
466 | 477 | // } |
467 | 478 | } |
468 | 479 | } |
469 | 480 | } |
470 | | - // if the input is from the form (meaning the user has hit one |
| 481 | + // If the input is from the form (meaning the user has hit one |
471 | 482 | // of the bottom row of buttons), and we're dealing with a |
472 | 483 | // multiple template, get the values for this instance of this |
473 | 484 | // template, then delete them from the array, so we can get the |
474 | 485 | // next group next time - the next() command for arrays doesn't |
475 | | - // seem to work here |
| 486 | + // seem to work here. |
476 | 487 | if ( ( ! $source_is_page ) && $allow_multiple && $wgRequest ) { |
477 | 488 | $all_instances_printed = true; |
478 | 489 | if ( $old_template_name != $template_name ) { |
— | — | @@ -590,10 +601,11 @@ |
591 | 602 | $property_name = $sub_components[1]; |
592 | 603 | $dummy_field = new SFTemplateField(); |
593 | 604 | $dummy_field->setSemanticProperty( $property_name ); |
594 | | - if ( $dummy_field->propertyIsOfType( '_wpg' ) ) |
| 605 | + if ( $dummy_field->propertyIsOfType( '_wpg' ) ) { |
595 | 606 | $field_args['autocomplete field type'] = 'relation'; |
596 | | - else |
| 607 | + } else { |
597 | 608 | $field_args['autocomplete field type'] = 'attribute'; |
| 609 | + } |
598 | 610 | $field_args['autocompletion source'] = $sub_components[1]; |
599 | 611 | } elseif ( $sub_components[0] == 'autocomplete on' ) { // for backwards-compatibility |
600 | 612 | $field_args['autocomplete field type'] = 'category'; |
— | — | @@ -657,7 +669,7 @@ |
658 | 670 | } |
659 | 671 | } else { |
660 | 672 | $cur_value = ''; |
661 | | - } |
| 673 | + } |
662 | 674 | |
663 | 675 | if ( empty( $cur_value ) ) { |
664 | 676 | if ( $default_value ) { |
— | — | @@ -683,14 +695,14 @@ |
684 | 696 | } |
685 | 697 | } |
686 | 698 | |
687 | | - // handle the free text field - if it was declared as |
| 699 | + // Handle the free text field - if it was declared as |
688 | 700 | // "field|free text" (a deprecated usage), it has to be outside |
689 | | - // of a template |
| 701 | + // of a template. |
690 | 702 | if ( ( $template_name == '' && $field_name == 'free text' ) || |
691 | 703 | $field_name == '<freetext>' ) { |
692 | | - // add placeholders for the free text in both the form and |
| 704 | + // Add placeholders for the free text in both the form and |
693 | 705 | // the page, using <free_text> tags - once all the free text |
694 | | - // is known (at the end), it will get substituted in |
| 706 | + // is known (at the end), it will get substituted in. |
695 | 707 | if ( $is_hidden ) { |
696 | 708 | $new_text = SFFormUtils::hiddenFieldHTML( 'free_text', '!free_text!' ); |
697 | 709 | } else { |
— | — | @@ -858,13 +870,14 @@ |
859 | 871 | // it's a restricted field and user doesn't have sysop privileges |
860 | 872 | $is_disabled = ( $form_is_disabled || |
861 | 873 | ( $is_restricted && ( ! $wgUser || ! $wgUser->isAllowed( 'editrestrictedfields' ) ) ) ); |
862 | | - // create an SFFormField instance based on all the |
863 | | - // parameters in the form definition, and any information from |
864 | | - // the template definition (contained in the $all_fields parameter) |
865 | | - $form_field = SFFormField::createFromDefinition( $field_name, $input_name, |
866 | | - $is_mandatory, $is_hidden, $is_uploadable, $possible_values, $is_disabled, |
867 | | - $is_list, $input_type, $field_args, $all_fields, $strict_parsing ); |
868 | | - // if a property was set in the form definition, overwrite whatever |
| 874 | + // Create an SFFormField instance based on all the parameters |
| 875 | + // in the form definition, and any information from the template |
| 876 | + // definition (contained in the $all_fields parameter). |
| 877 | + $form_field = SFFormField::createFromDefinition( $field_name, |
| 878 | + $input_name, $is_mandatory, $is_hidden, $is_uploadable, |
| 879 | + $possible_values, $is_disabled, $is_list, $input_type, |
| 880 | + $field_args, $all_fields, $strict_parsing ); |
| 881 | + // If a property was set in the form definition, overwrite whatever |
869 | 882 | // is set in the template field - this is somewhat of a hack, since |
870 | 883 | // parameters set in the form definition are meant to go into the |
871 | 884 | // SFFormField object, not the SFTemplateField object it contains; |
— | — | @@ -1115,21 +1128,21 @@ |
1116 | 1129 | $remove_text = wfMsg( 'sf_formedit_remove' ); |
1117 | 1130 | $form_text .= <<<END |
1118 | 1131 | <div class="multipleTemplate"> |
1119 | | - $section |
1120 | | - <input type="button" value="$remove_text" tabindex="$sfgTabIndex" class="remover" /> |
1121 | | - </div> |
| 1132 | + $section |
| 1133 | + <input type="button" value="$remove_text" tabindex="$sfgTabIndex" class="remover" /> |
| 1134 | + </div> |
1122 | 1135 | |
1123 | 1136 | END; |
1124 | 1137 | // this will cause the section to be re-parsed on the next go |
1125 | 1138 | $section_num--; |
1126 | | - } else { |
| 1139 | + } else { |
1127 | 1140 | // this is the last instance of this template - stick an 'add' |
1128 | 1141 | // button in the form |
1129 | | - $form_text .= <<<END |
| 1142 | + $form_text .= <<<END |
1130 | 1143 | <div id="starter_$query_template_name" class="multipleTemplateStarter" style="display: none;"> |
1131 | | - $section |
1132 | | - </div> |
1133 | | - <div id="main_$query_template_name"></div> |
| 1144 | + $section |
| 1145 | + </div> |
| 1146 | + <div id="main_$query_template_name"></div> |
1134 | 1147 | |
1135 | 1148 | END; |
1136 | 1149 | $adderID = "adder_$sfgFieldNum"; |
— | — | @@ -1209,7 +1222,7 @@ |
1210 | 1223 | // add a warning in, if we're editing an existing page and that page |
1211 | 1224 | // appears to not have been created with this form |
1212 | 1225 | if ( $this->mPageTitle->exists() && ( $existing_page_content != '' ) && ! $source_page_matches_this_form ) { |
1213 | | - $form_text = ' <div class="warningMessage">' . wfMsg( 'sf_formedit_formwarning', $this->mPageTitle->getFullURL() ) . "</div>\n" . $form_text; |
| 1226 | + $form_text = "\t" . '<div class="warningMessage">' . wfMsg( 'sf_formedit_formwarning', $this->mPageTitle->getFullURL() ) . "</div>\n" . $form_text; |
1214 | 1227 | } |
1215 | 1228 | |
1216 | 1229 | // add form bottom, if no custom "standard inputs" have been defined |
— | — | @@ -1222,12 +1235,13 @@ |
1223 | 1236 | $starttime = wfTimestampNow(); |
1224 | 1237 | $page_article = new Article( $this->mPageTitle ); |
1225 | 1238 | $edittime = $page_article->getTimestamp(); |
1226 | | - if ( !$is_query ) |
1227 | | - $form_text .= <<<END |
| 1239 | + if ( !$is_query ) { |
| 1240 | + $form_text .= <<<END |
1228 | 1241 | |
1229 | 1242 | <input type="hidden" value="$starttime" name="wpStarttime" /> |
1230 | 1243 | <input type="hidden" value="$edittime" name="wpEdittime" /> |
1231 | 1244 | END; |
| 1245 | + } |
1232 | 1246 | $form_text .= <<<END |
1233 | 1247 | </form> |
1234 | 1248 | |
— | — | @@ -1245,25 +1259,25 @@ |
1246 | 1260 | } |
1247 | 1261 | } |
1248 | 1262 | |
1249 | | - // send the autocomplete values to the browser, along with the mappings |
1250 | | - // of which values should apply to which fields |
1251 | | - // if doing a replace, the data text is actually the modified original page |
| 1263 | + // Send the autocomplete values to the browser, along with the mappings |
| 1264 | + // of which values should apply to which fields. |
| 1265 | + // If doing a replace, the data text is actually the modified original page |
1252 | 1266 | if ( $wgRequest->getCheck( 'partial' ) ) |
1253 | 1267 | $data_text = $existing_page_content; |
1254 | 1268 | |
1255 | 1269 | global $wgParser; |
1256 | 1270 | $new_text = ""; |
1257 | | - if ( !$embedded ) |
| 1271 | + if ( !$embedded ) { |
1258 | 1272 | $new_text = $wgParser->preprocess( str_replace( "{{!}}", "|", $form_page_title ), $this->mPageTitle, new ParserOptions() ); |
| 1273 | + } |
1259 | 1274 | |
1260 | | - // keep it simple - if the form has already been submitted, i.e. this is |
1261 | | - // just the redirect page, get rid of all the Javascript, to avoid JS errors |
| 1275 | + // If the form has already been submitted, i.e. this is just the redirect |
| 1276 | + // page, get rid of all the Javascript, to avoid JS errors. |
1262 | 1277 | if ( $form_submitted ) { |
1263 | 1278 | $javascript_text = ''; |
1264 | 1279 | } |
1265 | 1280 | |
1266 | | - return array( $form_text, $javascript_text, |
1267 | | - $data_text, $new_text, $generated_page_name ); |
| 1281 | + return array( $form_text, $javascript_text, $data_text, $new_text, $generated_page_name ); |
1268 | 1282 | } |
1269 | 1283 | |
1270 | 1284 | /** |