Himeshi has uploaded a new change for review.
https://gerrit.wikimedia.org/r/75343
Change subject: Modified Special:CreateForm for page sections
......................................................................
Modified Special:CreateForm for page sections
Modified the interface and added implementation for page sections to be added
via the Special:CreateForm page.
Bug: 46662
Change-Id: I6b63e2dbfc992127d8eb570471bf4039a96ef996
---
M SemanticForms.php
M includes/SF_Form.php
M includes/SF_FormUtils.php
A includes/SF_PageSection.php
M languages/SF_Messages.php
M specials/SF_CreateForm.php
6 files changed, 366 insertions(+), 37 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticForms
refs/changes/43/75343/1
diff --git a/SemanticForms.php b/SemanticForms.php
index 3d9bcd2..7968090 100644
--- a/SemanticForms.php
+++ b/SemanticForms.php
@@ -156,6 +156,7 @@
$wgAutoloadClasses['SFAutoeditAPI'] = $sfgIP . '/includes/SF_AutoeditAPI.php';
$wgAutoloadClasses['SFFormEditAction'] = $sfgIP .
'/includes/SF_FormEditAction.php';
$wgAutoloadClasses['SFHelperFormAction'] = $sfgIP .
'/includes/SF_HelperFormAction.php';
+$wgAutoloadClasses['SFPageSection'] = $sfgIP . '/includes/SF_PageSection.php';
// Form inputs
$wgAutoloadClasses['SFFormInput'] = $sfgIP .
'/includes/forminputs/SF_FormInput.php';
diff --git a/includes/SF_Form.php b/includes/SF_Form.php
index 8d9e0f7..a74a5a7 100644
--- a/includes/SF_Form.php
+++ b/includes/SF_Form.php
@@ -13,12 +13,16 @@
private $mCreateTitle;
private $mEditTitle;
private $mAssociatedCategory;
+ private $mSections;
+ private $mItems;
- static function create( $formName, $templates ) {
+ static function create( $formName, $items, $templates, $sections ) {
$form = new SFForm();
$form->mFormName = ucfirst( str_replace( '_', ' ', $formName )
);
$form->mTemplates = $templates;
$form->mAssociatedCategory = null;
+ $form->mSections = $sections;
+ $form->mItems = $items;
return $form;
}
@@ -44,9 +48,19 @@
function creationHTML() {
$text = "";
- foreach ( $this->mTemplates as $i => $ft ) {
- $text .= $ft->creationHTML( $i );
+ $template_count = 0; $section_count = 0;
+ foreach ( $this->mItems as $item ) {
+ if ( $item['type'] == 'template' ) {
+ $template = $this->mTemplates[$template_count];
+ $text .= $template->creationHTML(
$template_count );
+ $template_count++;
+ } elseif ( $item['type'] == 'section' ) {
+ $section = $this->mSections[$section_count];
+ $text .= $section->creationHTML( $section_count
);
+ $section_count++;
+ }
}
+
return $text;
}
@@ -86,9 +100,19 @@
<div id="wikiPreview" style="display: none; padding-bottom: 25px;
margin-bottom: 25px; border-bottom: 1px solid #AAAAAA;"></div>
END;
- foreach ( $this->mTemplates as $template ) {
- $text .= $template->createMarkup() . "\n";
+ $template_count = 0; $section_count = 0;
+ foreach ( $this->mItems as $item ) {
+ if ( $item['type'] == 'template' ) {
+ $template = $this->mTemplates[$template_count];
+ $text .= $template->createMarkup() . "\n";
+ $template_count++;
+ } elseif ( $item['type'] == 'section' ) {
+ $section = $this->mSections[$section_count];
+ $text .= $section->createMarkup() . "\n";
+ $section_count++;
+ }
}
+
$free_text_label = wfMessage( 'sf_form_freetextlabel'
)->inContentLanguage()->text();
$text .= <<<END
'''$free_text_label:'''
diff --git a/includes/SF_FormUtils.php b/includes/SF_FormUtils.php
index 5bb6a19..4c2e28c 100644
--- a/includes/SF_FormUtils.php
+++ b/includes/SF_FormUtils.php
@@ -569,4 +569,25 @@
return $text;
}
+ /**
+ * Get the changed index if a new template or section was
+ * inserted before the end, or one was deleted in the form
+ */
+ static function getChangedIndex( $i, $new_item_loc, $deleted_item_loc )
{
+ $old_i = $i;
+ if ( $new_item_loc != null ) {
+ if ( $i > $new_item_loc ) {
+ $old_i = $i - 1;
+ } elseif ( $i == $new_item_loc ) {
+ // it's the new template; it shouldn't
+ // get any query-string data
+ $old_i = - 1;
+ }
+ } elseif ( $deleted_item_loc != null ) {
+ if ( $i >= $deleted_item_loc ) {
+ $old_i = $i + 1;
+ }
+ }
+ return $old_i;
+ }
}
diff --git a/includes/SF_PageSection.php b/includes/SF_PageSection.php
new file mode 100644
index 0000000..aa8fb51
--- /dev/null
+++ b/includes/SF_PageSection.php
@@ -0,0 +1,131 @@
+<?php
+/**
+ * Represents a page section in a user-defined form.
+ * @author Himeshi
+ * @file
+ * @ingroup SF
+ */
+class SFPageSection {
+ private $mSectionName;
+ private $mSectionLevel;
+ private $mIsMandatory;
+ private $mIsHidden;
+ private $mIsRestricted;
+
+ static function create( $section_name ) {
+ $ps = new SFPageSection();
+ $ps->mSectionName = $section_name;
+ $ps->mSectionLevel = 2;
+ $ps->mIsMandatory = false;
+ $ps->mIsHidden = false;
+ $ps->mIsRestricted = false;
+
+ return $ps;
+ }
+
+ public function getSectionName() {
+ return $this->mSectionName;
+ }
+
+ public function getSectionLevel() {
+ return $this->mSectionLevel;
+ }
+
+ public function setSectionLevel( $section_level ) {
+ $this->mSectionLevel = $section_level;
+ }
+
+ public function setIsMandatory( $isMandatory ) {
+ $this->mIsMandatory = $isMandatory;
+ }
+
+ public function setIsHidden( $isHidden ) {
+ $this->mIsHidden = $isHidden;
+ }
+
+ public function setIsRestricted( $isRestricted ) {
+ $this->mIsRestricted = $isRestricted;
+ }
+
+ function creationHTML( $section_count ) {
+ $section_name = $this->mSectionName;
+ $section_level = $this->mSectionLevel;
+
+ $section_str = wfMessage( 'sf_createform_pagesection' )->text()
. " '" . $section_name . "'";
+ $text = <<<END
+ <input type="hidden" name="section_$section_count"
value="$section_name">
+ <div class="sectionForm">
+ <h2>$section_str</h2>
+
+END;
+ $paramValues = $this->getParamValues();
+ $header_options = '';
+ $text .= Html::rawElement( 'span', null, wfMessage(
'sf_createform_sectionlevel' )->text() ) . "\n";
+ for ( $i = 1; $i < 7; $i++ ) {
+ if ( $section_level == $i ) {
+ $header_options .= " " . Html::element(
'option', array( 'value' => $i, 'selected' ), 'Level '.$i ) . "\n";
+ } else {
+ $header_options .= " " . Html::element(
'option', array( 'value' => $i ), 'Level '.$i ) . "\n";
+ }
+ }
+ $text .= Html::rawElement( 'select', array( 'name' =>
"sectionlevel_" . $section_count ), $header_options ) . "\n";
+ $text .= Html::rawElement( 'div', array(),
+ SFCreateForm::showSectionParameters( $section_count,
$paramValues ) ) . "\n";
+
+ $removeSectionButton = Html::input( 'delsection_' .
$section_count, wfMessage( 'sf_createform_removesection' )->text(), 'submit' )
. "\n";
+ $text .= "\n\n</br>" . Html::rawElement( 'p', null,
$removeSectionButton ) . "\n";
+ $text .= " </div>\n";
+
+ return $text;
+ }
+
+ function createMarkup() {
+ $section_name = $this->mSectionName;
+ $section_level = $this->mSectionLevel;
+ // Set default section level to 2
+ if ( $section_level == '' ){
+ $section_level = 2;
+ }
+ $text = SFFormUtils::headerHTML( $section_name, $section_level
) . "\n";
+ $text .= "{{{section|" . $section_name . "|level=" .
$section_level;
+
+ if ( $this->mIsMandatory ) {
+ $text .= "|mandatory";
+ } elseif ( $this->mIsRestricted ) {
+ $text .= "|restricted";
+ } elseif ( $this->mIsHidden ) {
+ $text .= "|hidden";
+ }
+ $text .= "}}}\n";
+
+ return $text;
+ }
+
+ public static function getParameters() {
+ $params = array();
+ $params['mandatory'] = array(
+ 'name' => 'mandatory',
+ 'type' => 'boolean',
+ 'description' => wfMessage( 'sf_forminputs_mandatory'
)->text()
+ );
+ $params['restricted'] = array(
+ 'name' => 'restricted',
+ 'type' => 'boolean',
+ 'description' => wfMessage( 'sf_forminputs_restricted'
)->text()
+ );
+ $params['hidden'] = array(
+ 'name' => 'hidden',
+ 'type' => 'boolean',
+ 'description' => wfMessage(
'sf_createform_hiddensection' )->text()
+ );
+ return $params;
+ }
+
+ function getParamValues() {
+ $paramValues = array();
+ $paramValues['restricted'] = $this->mIsRestricted;
+ $paramValues['hidden'] = $this->mIsHidden;
+ $paramValues['mandatory'] = $this->mIsMandatory;
+ return $paramValues;
+ }
+}
diff --git a/languages/SF_Messages.php b/languages/SF_Messages.php
index 5d81665..d32ae81 100644
--- a/languages/SF_Messages.php
+++ b/languages/SF_Messages.php
@@ -102,6 +102,15 @@
'sf_createform_atend' => 'At end',
'sf_createform_add' => 'Add',
'sf_createform_choosefield' => 'Choose a field to add',
+ 'sf_createform_pagesection' => 'Page section:',
+ 'sf_createform_addsection' => 'Add section',
+ 'sf_createform_removesection' => 'Remove section',
+ 'sf_createform_before' => 'Before:',
+ 'sf_createform_pagesections' => 'Page Sections',
+ 'sf_createform_templates' => 'Templates',
+ 'sf_createform_hiddensection' => 'This input is hidden in the
form',
+ 'sf_createform_sectionlevel' => 'Section level:',
+ 'sf_createform_additembeforesave' => 'You must add at least one
template or page section to this form before you can save it.',
'createcategory' => 'Create a category',
'sf-createcategory-with-name' => 'Create category: $1',
'sf_createcategory_name' => 'Category name:',
diff --git a/specials/SF_CreateForm.php b/specials/SF_CreateForm.php
index 78c00b8..65cbaee 100644
--- a/specials/SF_CreateForm.php
+++ b/specials/SF_CreateForm.php
@@ -68,6 +68,7 @@
}
$wgOut->addModules( 'ext.semanticforms.collapsible' );
+ $section_name_error_str = '<font color="red"
id="section_error">' . wfMessage( 'sf_blank_error' )->escaped() . '</font>';
$wgOut->addScript("<script>
jQuery.fn.displayInputParams = function() {
@@ -84,6 +85,13 @@
jQuery('.inputTypeSelector').change( function() {
jQuery(this).displayInputParams();
});
+ jQuery('#addsection').click( function(event) {
+ if(jQuery('#sectionname').val() == '') {
+ event.preventDefault();
+ jQuery('#section_error').remove();
+
jQuery('<div/>').append('$section_name_error_str').appendTo('#sectionerror');
+ }
+ });
});
</script>");
@@ -107,6 +115,10 @@
$form_templates = array();
$deleted_template_loc = null;
+ $form_sections = array();
+ $deleted_section_loc = null;
+ // To keep the order of templates and sections
+ $form_items = array();
// Handle inputs.
foreach ( $wgRequest->getValues() as $var => $val ) {
@@ -125,43 +137,81 @@
$wgRequest->getVal(
"label_$id" ),
$wgRequest->getVal(
"allow_multiple_$id" ) );
$form_templates[] =
$form_template;
+ $form_items[] = array( 'type'
=> 'template', 'name' => $form_template->getTemplateName() );
+ }
+ } elseif ( $action == "section" ) {
+ if ( $wgRequest->getVal(
"delsection_$id" ) != null ) {
+ $deleted_section_loc = $id;
+ } else {
+ $form_section =
SFPageSection::create( $val );
+ $form_sections[] =
$form_section;
+ $form_items[] = array( 'type'
=> 'section', 'name' => $form_section->getSectionName() );
}
}
}
}
if ( $wgRequest->getVal( 'add_field' ) != null ) {
$form_template = SFTemplateInForm::create(
$wgRequest->getVal( 'new_template' ), "", false );
- $new_template_loc = $wgRequest->getVal(
'before_template' );
- if ( $new_template_loc === null ) { $new_template_loc =
0; }
+ $template_loc = $wgRequest->getVal( 'before_template' );
+ $template_count = 0;
+ if ( $template_loc === null ) {
+ $new_template_loc = 0;
+ $template_loc = 0;
+ } else {
+ // Count the number of templates before the
+ // location of the template to be added
+ for ( $i = 0; $i < $template_loc; $i++ ) {
+ if ( $form_items[$i]['type'] ==
'template' ) {
+ $template_count++;
+ }
+ }
+ $new_template_loc = $template_count;
+ }
// @HACK - array_splice() doesn't work for objects, so
// we have to first insert a stub element into the
// array, then replace that with the actual object.
array_splice( $form_templates, $new_template_loc, 0,
"stub" );
$form_templates[$new_template_loc] = $form_template;
+ array_splice( $form_items, $template_loc, 0, "stub" );
+ $form_items[$template_loc] = array( 'type' =>
'template', 'name' => $form_template->getTemplateName() );
} else {
+ $template_loc = null;
$new_template_loc = null;
+ }
+
+ if ( $wgRequest->getVal( 'add_section' ) != null ) {
+ $form_section = SFPageSection::create(
$wgRequest->getVal( 'sectionname' ) );
+ $section_loc = $wgRequest->getVal( 'before_section' );
+ $section_count = 0;
+ if ( $section_loc === null ) {
+ $new_section_loc = 0;
+ $section_loc = 0;
+ } else {
+ // Count the number of sections before
the
+ // location of the section to be added
+ for ( $i = 0; $i < $section_loc; $i++ )
{
+ if ( $form_items[$i]['type'] ==
'section' ) {
+ $section_count++;
+ }
+ }
+ $new_section_loc = $section_count;
+ }
+ // The same used hack for templates
+ array_splice( $form_sections, $new_section_loc, 0,
"stub" );
+ $form_sections[$new_section_loc] = $form_section;
+ array_splice( $form_items, $section_loc, 0, "stub" );
+ $form_items[$section_loc] = array( 'type' => 'section',
'name' => $form_section->getSectionName() );
+ } else {
+ $section_loc = null;
+ $new_section_loc = null;
}
// Now cycle through the templates and fields, modifying each
// one per the query variables.
foreach ( $form_templates as $i => $ft ) {
foreach ( $ft->getFields() as $j => $field ) {
- // handle the change in indexing if a new
template was
- // inserted before the end, or one was deleted
- $old_i = $i;
- if ( $new_template_loc != null ) {
- if ( $i > $new_template_loc ) {
- $old_i = $i - 1;
- } elseif ( $i == $new_template_loc ) {
- // it's the new template; it
shouldn't
- // get any query-string data
- $old_i = - 1;
- }
- } elseif ( $deleted_template_loc != null ) {
- if ( $i >= $deleted_template_loc ) {
- $old_i = $i + 1;
- }
- }
+
+ $old_i = SFFormUtils::getChangedIndex( $i,
$new_template_loc, $deleted_template_loc );
foreach ( $wgRequest->getValues() as $key =>
$value ) {
if ( ( $pos = strpos( $key, '_' .
$old_i . '_' . $j ) ) != false ) {
$paramName = substr( $key, 0,
$pos );
@@ -196,7 +246,32 @@
}
}
}
- $form = SFForm::create( $form_name, $form_templates );
+
+ foreach ( $form_sections as $i => $section ) {
+ $old_i = SFFormUtils::getChangedIndex( $i,
$new_section_loc, $deleted_section_loc );
+ foreach ( $wgRequest->getValues() as $key => $value ) {
+ if ( ( $pos = strpos( $key, '_' . $old_i ) ) !=
false ) {
+ $paramName = substr( $key, 0,
$pos );
+ } else {
+ continue;
+ }
+
+ if ( $paramName == 'sectionlevel' ) {
+ $section->setSectionLevel(
$value );
+ }
+ if ( $value == 'on' ) {
+ $value = true;
+ }
+ if ( $paramName == 'hidden' ) {
+ $section->setIsHidden( $value );
+ } elseif ( $paramName == 'restricted' )
{
+ $section->setIsRestricted(
$value );
+ } elseif ( $paramName == 'mandatory' ) {
+ $section->setIsMandatory(
$value );
+ }
+ }
+ }
+ $form = SFForm::create( $form_name, $form_items,
$form_templates, $form_sections );
// If a submit button was pressed, create the form-definition
// file, then redirect.
@@ -230,6 +305,7 @@
$text .= $form->creationHTML();
+ $text .= "<h2> " . wfMessage( 'sf_createform_templates'
)->escaped() . " </h2>";
$text .= "\t<p>" . wfMessage( 'sf_createform_addtemplate'
)->escaped() . "\n";
$select_body = "";
@@ -237,36 +313,64 @@
$select_body .= " " . Html::element( 'option',
array( 'value' => $template ), $template ) . "\n";
}
$text .= "\t" . Html::rawElement( 'select', array( 'name' =>
'new_template' ), $select_body ) . "\n";
+
// If a template has already been added, show a dropdown letting
// the user choose where in the list to add a new dropdown.
- if ( count( $form_templates ) > 0 ) {
- $text .= wfMessage( 'sf_createform_beforetemplate'
)->escaped();
- $select_body = "";
- foreach ( $form_templates as $i => $ft ) {
- $select_body .= "\t" . Html::element( 'option',
array( 'value' => $i ), $ft->getTemplateName() ) . "\n";
+ $select_body = "";
+ $template_count = 0;
+ foreach ( $form_items as $i => $fi ) {
+ if ( $fi['type'] == 'template' ) {
+ $option_str = wfMessage(
'sf_createform_template' )->escaped();
+ $option_str .=
$form_templates[$template_count]->getTemplateName();
+ $select_body .= "\t" . Html::element( 'option',
array( 'value' => $i ), $option_str ) . "\n";
+ $template_count++;
+ } elseif ( $fi['type'] == 'section' ) {
+ $option_str = wfMessage(
'sf_createform_pagesection' )->escaped();
+ $option_str .= "\t" . $fi['name'];
+ $select_body .= "\t" . Html::element( 'option',
array( 'value' => $i ), $option_str ) . "\n";
}
- $final_index = count( $form_templates );
- $at_end_msg = wfMessage( 'sf_createform_atend'
)->escaped();
- $select_body .= "\t" . Html::element( 'option', array(
'value' => $final_index, 'selected' => 'selected' ), $at_end_msg );
+ }
+ $final_index = count( $form_items );
+ $at_end_msg = wfMessage( 'sf_createform_atend' )->escaped();
+ $select_body .= "\t" . Html::element( 'option', array( 'value'
=> $final_index, 'selected' => 'selected' ), $at_end_msg );
+
+ // Selection for before which item this template should be
placed
+ if ( count( $form_items ) > 0 ) {
+ $text .= wfMessage( 'sf_createform_before' )->escaped();
$text .= Html::rawElement( 'select', array( 'name' =>
'before_template' ), $select_body ) . "\n";
}
// Disable 'save' and 'preview' buttons if user has not yet
// added any templates.
$add_button_text = wfMessage( 'sf_createform_add' )->text();
- $text .= "\t" . Html::input( 'add_field', $add_button_text,
'submit' );
+ $text .= "\t" . Html::input( 'add_field', $add_button_text,
'submit' ) . "\n";
+
+ // The form HTML for page sections
+ $text .= "<br/> <h2> " . wfMessage(
'sf_createform_pagesections' )->escaped() . " </h2>";
+ $text .= Html::input( 'sectionname', '', 'text', array( 'size'
=> '60', 'placeholder' => 'Section Name', 'id' => 'sectionname' ) ) . "\n";
+
+ // Selection for before which item this section should be placed
+ if ( count( $form_items ) > 0 ) {
+ $text .= wfMessage( 'sf_createform_before' )->escaped();
+ $text .= Html::rawElement( 'select', array( 'name' =>
'before_section' ), $select_body ) . "\n";
+ }
+
+ $add_section_text = wfMessage( 'sf_createform_addsection'
)->text();
+ $text .= "\t" . Html::input( 'add_section', $add_section_text,
'submit', array( 'id' => 'addsection' ) );
+ $text .= "\n\t" . Html::rawElement( 'div', array( 'id' =>
'sectionerror' ) );
$text .= <<<END
</p>
<br />
END;
+
$saveAttrs = array( 'id' => 'wpSave' );
- if ( count( $form_templates ) == 0 ) {
+ if ( count( $form_items ) == 0 ) {
$saveAttrs['disabled'] = true;
}
$editButtonsText = "\t" . Html::input( 'wpSave', wfMessage(
'savearticle' )->text(), 'submit', $saveAttrs ) . "\n";
$previewAttrs = array( 'id' => 'wpPreview' );
- if ( count( $form_templates ) == 0 ) {
+ if ( count( $form_items ) == 0 ) {
$previewAttrs['disabled'] = true;
}
$editButtonsText .= "\t" . Html::input( 'wpPreview',
wfMessage( 'preview' )->text(), 'submit', $previewAttrs ) . "\n";
@@ -274,8 +378,8 @@
Html::rawElement( 'p', array(), $editButtonsText ) .
"\n" ) . "\n";
// Explanatory message if buttons are disabled because no
// templates have been added.
- if ( count( $form_templates ) == 0 ) {
- $text .= "\t" . Html::element( 'p', null, "(" .
wfMessage( 'sf_createtemplate_addtemplatebeforesave' )->text() . ")" );
+ if ( count( $form_items ) == 0 ) {
+ $text .= "\t" . Html::element( 'p', null, "(" .
wfMessage( 'sf_createform_additembeforesave' )->text() . ")" );
}
$text .= <<<END
</form>
@@ -284,6 +388,11 @@
$wgOut->addExtensionStyle( $sfgScriptPath .
"/skins/SemanticForms.css" );
$wgOut->addHTML( $text );
+
+ //Don't submit the form if enter is pressed on a text input box
or a select
+ $wgOut->addScript('<script>
+ jQuery("input,select").keypress(function(event) { return
event.keyCode != 13; });
+ </script>');
}
/**
@@ -396,4 +505,38 @@
}
return $text;
}
+
+ /**
+ * Display other parameters for a page section
+ *
+ * @return string
+ */
+ public static function showSectionParameters( $section_count,
$paramValues ) {
+ global $wgParser;
+
+ $text = '';
+ $descriptiontext = '';
+ $section_text = $section_count;
+
+ $params = SFPageSection::getParameters();
+
+ foreach ( $params as $param ) {
+ $paramName = $param['name'];
+ $type = $param['type'];
+ $desc = $wgParser->parse( $param['description'], new
Title(), new ParserOptions() )->getText();
+
+ if ( array_key_exists( $paramName, $paramValues ) ) {
+ $cur_value = $paramValues[$paramName];
+ } elseif ( array_key_exists( 'default', $param ) ) {
+ $cur_value = $param['default'];
+ } else {
+ $cur_value = '';
+ }
+ $text .= "<div style=\"width: 30%; padding: 5px; float:
left;\">$paramName:\n";
+
+ $text .= self::inputTypeParamInput( $type, $paramName,
$cur_value, $param, array(), $section_text );
+ $text .= "\n<br />" . Html::rawElement( 'em', null,
$desc ) . "\n</div>\n";
+ }
+ return $text;
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/75343
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b63e2dbfc992127d8eb570471bf4039a96ef996
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticForms
Gerrit-Branch: master
Gerrit-Owner: Himeshi <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits