function doSpecialCreateForm() {
		global $wgOut, $wgRequest, $wgUser, $sfgScriptPath;
		$db = wfGetDB( DB_SLAVE );

		// Create Javascript to populate fields to let the user input
		// parameters for the field, based on the input type selected
		// in the dropdown.
		$skin = $wgUser->getSkin();
		$url = $skin->makeSpecialUrl( 'CreateForm', "showinputtypeoptions=' + this.val() + '&formfield=' + this.attr('formfieldid') + '" );
		foreach ( $wgRequest->getValues() as $param => $value ) {
			$url .= '&params[' . Xml::escapeJsString( $param ) . ']=' . Xml::escapeJsString( $value );
		}

		// Only add 'collapsible' ability if the ResourceLoader exists,
		// i.e. for MW 1.17 - adding backwards compatibility doesn't
		// seem worth it for this relatively minor piece of
		// functionality.
		if ( method_exists( $wgOut, 'addModules' ) ) {
			$wgOut->addModules( 'ext.semanticforms.collapsible' );
		}

		$wgOut->addScript("<script>
jQuery.fn.displayInputParams = function() {
	inputParamsDiv = this.closest('.formField').find('.otherInputParams');
	jQuery.ajax({
		url: '$url',
		context: document.body,
		success: function(data){
			inputParamsDiv.html(data);
		}
	});
};
jQuery(document).ready(function() {
	jQuery('.inputTypeSelector').change( function() {
		jQuery(this).displayInputParams();
	});
});
</script>");


		// Get the names of all templates on this site.
		$all_templates = array();
		$res = $db->select(
			'page',
			'page_title',
			array( 'page_namespace' => NS_TEMPLATE, 'page_is_redirect' => 0 ),
			array( 'ORDER BY' => 'page_title' )
		);

		if ( $db->numRows( $res ) > 0 ) {
			while ( $row = $db->fetchRow( $res ) ) {
				$template_name = str_replace( '_', ' ', $row[0] );
				$all_templates[] = $template_name;
			}
		}

		$form_templates = array();
		$i = 1;
		$deleted_template_loc = null;

		# handle inputs
		$form_name = $wgRequest->getVal( 'form_name' );
		foreach ( $wgRequest->getValues() as $var => $val ) {
			# ignore variables that are not of the right form
			if ( strpos( $var, "_" ) != false ) {
				# get the template declarations and work from there
				list ( $action, $id ) = explode( "_", $var, 2 );
				if ( $action == "template" ) {
					// If the button was pressed to remove
					// this template, just don't add it to
					// the array.
					if ( $wgRequest->getVal( "del_$id" ) != null ) {
						$deleted_template_loc = $id;
					} else {
						$form_template = SFTemplateInForm::create( $val,
							$wgRequest->getVal( "label_$id" ),
							$wgRequest->getVal( "allow_multiple_$id" ) );
						$form_templates[] = $form_template;
					}
				}
			}
		}
		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; }
			// @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;
		} else {
			$new_template_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;
					}
				}
				foreach ( $wgRequest->getValues() as $key => $value ) {
					if ( ( $pos = strpos( $key, '_' . $old_i . '_' . $j ) ) != false ) {
						$paramName = substr( $key, 0, $pos );
						// Spaces got replaced by
						// underlines in the query.
						$paramName = str_replace( '_', ' ', $paramName );
					} else {
						continue;
					}

					if ( $paramName == 'label' ) {
						$field->template_field->setLabel( $value );
					} elseif ( $paramName == 'input type' ) {
						$input_type = $wgRequest->getVal( "input_type_" . $old_i . "_" . $j );
						if ( $input_type == 'hidden' ) {
							$field->template_field->setInputType( $input_type );
							$field->setIsHidden( true );
						} elseif ( substr( $input_type, 0, 1 ) == '.' ) {
							// It's the default input type -
							// don't do anything.
						} else {
							$field->template_field->setInputType( $input_type );
						}
					} else {
						if ( ! empty( $value ) ) {
							if ( $value == 'on' ) {
								$value = true;
							}
							$field->setFieldArg( $paramName, $value );
						}
					}
				}
			}
		}
		$form = SFForm::create( $form_name, $form_templates );

		// If a submit button was pressed, create the form-definition
		// file, then redirect.
		$save_page = $wgRequest->getCheck( 'wpSave' );
		$preview_page = $wgRequest->getCheck( 'wpPreview' );
		if ( $save_page || $preview_page ) {
			// Validate form name
			if ( $form->getFormName() == "" ) {
				$form_name_error_str = wfMsg( 'sf_blank_error' );
			} else {
				// Redirect to wiki interface
				$wgOut->setArticleBodyOnly( true );
				$title = Title::makeTitleSafe( SF_NS_FORM, $form->getFormName() );
				$full_text = $form->createMarkup();
				$text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null );
				$wgOut->addHTML( $text );
				return;
			}
		}

		$text = "\t" . '<form action="" method="post">' . "\n";
		// Set 'title' field, in case there's no URL niceness
		$text .= SFFormUtils::hiddenFieldHTML( 'title', $this->getTitle()->getPrefixedText() );
		$text .= "\t<p>" . wfMsg( 'sf_createform_nameinput' ) . ' ' . wfMsg( 'sf_createform_nameinputdesc' ) . ' <input size=25 name="form_name" value="' . $form_name . '" />';
		if ( ! empty( $form_name_error_str ) )
			$text .= "\t" . Html::element( 'font', array( 'color' => 'red' ), $form_name_error_str );
		$text .= "</p>\n";

		$text .= $form->creationHTML();

		$text .= "\t<p>" . wfMsg( 'sf_createform_addtemplate' ) . "\n";

		$select_body = "";
		foreach ( $all_templates as $template ) {
			$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 ) {
			$before_template_msg = wfMsg( 'sf_createform_beforetemplate' );
			$text .= $before_template_msg;
			$select_body = "";
			foreach ( $form_templates as $i => $ft ) {
				$select_body .= "\t" . Html::element( 'option', array( 'value' => $i ), $ft->getTemplateName() ) . "\n";
			}
			$final_index = count( $form_templates );
			$at_end_msg = wfMsg( 'sf_createform_atend' );
			$select_body .= "\t" . Html::element( 'option', array( 'value' => $final_index, 'selected' => 'selected' ), $at_end_msg );
			$text .= Html::rawElement( 'select', array( 'name' => 'before_template' ), $select_body ) . "\n";
		}

		// Disable 'save' and 'preview' buttons if user has not yet
		// added any templates.
		$disabled_text = ( count( $form_templates ) == 0 ) ? "disabled" : "";
		$add_button_text = wfMsg( 'sf_createform_add' );
		$sk = $wgUser->getSkin();
		$create_template_link = SFUtils::linkForSpecialPage( $sk, 'CreateTemplate' );
		$text .= "\t" . Html::input( 'add_field', $add_button_text, 'submit' );
		$text .= <<<END
</p>
	<br />

END;
		$saveAttrs = array( 'id' => 'wpSave' );
		if ( count( $form_templates ) == 0 ) {
			$saveAttrs['disabled'] = true;
		}
		$editButtonsText = "\t" . Html::input( 'wpSave', wfMsg( 'savearticle' ), 'submit', $saveAttrs ) . "\n";
		$previewAttrs = array( 'id' => 'wpPreview' );
		if ( count( $form_templates ) == 0 ) {
			$previewAttrs['disabled'] = true;
		}
		$editButtonsText .= "\t" . Html::input( 'wpPreview',  wfMsg( 'preview' ), 'submit', $previewAttrs ) . "\n";
		$text .= "\t" . Html::rawElement( 'div', array( 'class' => 'editButtons' ),
			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, "(" . wfMsg( 'sf_createtemplate_addtemplatebeforesave' ) . ")" );
		}
		$text .= <<<END
	</form>
	<hr /><br />

END;
		$text .= "\t" . Html::rawElement( 'p', null, $create_template_link . '.' );

		$wgOut->addExtensionStyle( $sfgScriptPath . "/skins/SemanticForms.css" );
		$wgOut->addHTML( $text );
	}
	function execute( $query ) {
		global $wgOut, $wgRequest, $wgUser, $sfgScriptPath;
		global $wgLang, $smwgContLang;

		# Check permissions
		if ( !$wgUser->isAllowed( 'createclass' ) ) {
			$this->displayRestrictionError();
			return;
		}

		$this->setHeaders();
		$wgOut->addExtensionStyle( $sfgScriptPath . "/skins/SemanticForms.css" );
		$numStartingRows = 10;
		self::addJavascript( $numStartingRows );

		$property_name_error_str = '';
		$save_page = $wgRequest->getCheck( 'save' );
		if ( $save_page ) {
			$template_name = trim( $wgRequest->getVal( "template_name" ) );
			$form_name = trim( $wgRequest->getVal( "form_name" ) );
			$category_name = trim( $wgRequest->getVal( "category_name" ) );
			if ( $template_name === '' | $form_name === '' || $category_name === '' ) {
				$wgOut->addWikiMsg( 'sf_createclass_missingvalues' );
				return;
			}
			$fields = array();
			$jobs = array();
			// cycle through all the rows passed in
			for ( $i = 1; $wgRequest->getCheck( "property_name_$i" ); $i++ ) {
				// go through the query values, setting the appropriate local variables
				$property_name = trim( $wgRequest->getVal( "property_name_$i" ) );
				if ( empty( $property_name ) ) continue;
				$field_name = trim( $wgRequest->getVal( "field_name_$i" ) );
				if ( $field_name === '' )
					$field_name = $property_name;
				$property_type = $wgRequest->getVal( "property_type_$i" );
				$allowed_values = $wgRequest->getVal( "allowed_values_$i" );
				$is_list = $wgRequest->getCheck( "is_list_$i" );
				// create an SFTemplateField based on these
				// values, and add it to the $fields array
				$field = SFTemplateField::create( $field_name, $field_name, $property_name, $is_list );
				$fields[] = $field;

				// create the property, and make a job for it
				$full_text = SFCreateProperty::createPropertyText( $property_type, '', $allowed_values );
				$property_title = Title::makeTitleSafe( SMW_NS_PROPERTY, $property_name );
				$params = array();
				$params['user_id'] = $wgUser->getId();
				$params['page_text'] = $full_text;
				$jobs[] = new SFCreatePageJob( $property_title, $params );
			}

			// create the template, and save it
			$full_text = SFTemplateField::createTemplateText( $template_name, $fields, null, $category_name, null, null, null );
			$template_title = Title::makeTitleSafe( NS_TEMPLATE, $template_name );
			$template_article = new Article( $template_title, 0 );
			$edit_summary = '';
			$template_article->doEdit( $full_text, $edit_summary );

			// create the form, and make a job for it
			$form_template = SFTemplateInForm::create( $template_name, '', false );
			$form_templates = array( $form_template );
			$form = SFForm::create( $form_name, $form_templates );
			$full_text = $form->createMarkup();
			$form_title = Title::makeTitleSafe( SF_NS_FORM, $form_name );
			$params = array();
			$params['user_id'] = $wgUser->getId();
			$params['page_text'] = $full_text;
			$jobs[] = new SFCreatePageJob( $form_title, $params );

			// create the category, and make a job for it
			$full_text = SFCreateCategory::createCategoryText( $form_name, $category_name, '' );
			$category_title = Title::makeTitleSafe( NS_CATEGORY, $category_name );
			$params = array();
			$params['user_id'] = $wgUser->getId();
			$params['page_text'] = $full_text;
			$jobs[] = new SFCreatePageJob( $category_title, $params );
			Job::batchInsert( $jobs );

			$wgOut->addWikiMsg( 'sf_createclass_success' );
			return;
		}

		$datatype_labels = $smwgContLang->getDatatypeLabels();

		// make links to all the other 'Create...' pages, in order to
		// link to them at the top of the page
		$sk = $wgUser->getSkin();
		$creation_links = array();
		$creation_links[] = SFUtils::linkForSpecialPage( $sk, 'CreateProperty' );
		$creation_links[] = SFUtils::linkForSpecialPage( $sk, 'CreateTemplate' );
		$creation_links[] = SFUtils::linkForSpecialPage( $sk, 'CreateForm' );
		$creation_links[] = SFUtils::linkForSpecialPage( $sk, 'CreateCategory' );
		$create_class_docu = wfMsg( 'sf_createclass_docu', $wgLang->listToText( $creation_links ) );
		$leave_field_blank = wfMsg( 'sf_createclass_leavefieldblank' );
		$form_name_label = wfMsg( 'sf_createclass_nameinput' );
		$template_name_label = wfMsg( 'sf_createtemplate_namelabel' );
		$category_name_label = wfMsg( 'sf_createcategory_name' );
		$property_name_label = wfMsg( 'sf_createproperty_propname' );
		$field_name_label = wfMsg( 'sf_createtemplate_fieldname' );
		$type_label = wfMsg( 'sf_createproperty_proptype' );
		$allowed_values_label = wfMsg( 'sf_createclass_allowedvalues' );
		$list_of_values_label = wfMsg( 'sf_createclass_listofvalues' );

		$text = <<<END
<form action="" method="post">
	<p>$create_class_docu</p>
	<p>$leave_field_blank</p>
	<p>$template_name_label <input type="text" size="30" name="template_name"></p>
	<p>$form_name_label <input type="text" size="30" name="form_name"></p>
	<p>$category_name_label <input type="text" size="30" name="category_name"></p>
	<div>
		<table id="mainTable">
		<tr>
			<th colspan="2">$property_name_label</th>
			<th>$field_name_label</th>
			<th>$type_label</th>
			<th>$allowed_values_label</th>
			<th>$list_of_values_label</th>
		</tr>

END;
		// Make one more row than what we're displaying - use the
		// last row as a "starter row", to be cloned when the
		// "Add another" button is pressed.
		for ( $i = 1; $i <= $numStartingRows + 1; $i++ ) {
			if ( $i == $numStartingRows + 1 ) {
				$rowString = 'id="starterRow" style="display: none"';
				$n = 'starter';
			} else {
				$rowString = '';
				$n = $i;
			}
			$text .= <<<END
		<tr $rowString>
			<td>$n.</td>
			<td><input type="text" size="25" name="property_name_$n" /></td>
			<td><input type="text" size="25" name="field_name_$n" /></td>
			<td>
			<select name="property_type_$n">

END;
			$optionsStr ="";
			foreach ( $datatype_labels as $label ) {
				$text .= "				<option>$label</option>\n";
				$optionsStr .= $label . ",";
			}
			$text .= <<<END
			</select>
			</td>
			<td><input type="text" size="25" name="allowed_values_$n" /></td>
			<td><input type="checkbox" name="is_list_$n" /></td>

END;
		}
		$text .= <<<END
		</tr>
		</table>
	</div>

END;
		$add_another_button = Html::element( 'input',
			array(
				'type' => 'button',
				'value' => wfMsg( 'sf_formedit_addanother' ),
				'onclick' => "createClassAddRow()"
			)
		);
		$text .= Html::rawElement( 'p', null, $add_another_button ) . "\n";
		// Set 'title' as hidden field, in case there's no URL niceness
		$cc = $this->getTitle();
		$text .= SFFormUtils::hiddenFieldHTML( 'title', SFUtils::titleURLString( $cc ) );
		$text .= Html::element( 'input',
			array(
				'type' => 'submit',
				'name' => 'save',
				'value' => wfMsg( 'sf_createclass_create' )
			)
		);
		$text .= "</form>\n";
		$wgOut->addHTML( $text );
	}
Пример #3
0
 /**
  * Creates a form page, when called from the 'generatepages' page
  * of Page Schemas.
  */
 public static function generateForm($formName, $formTitle, $formTemplates, $formDataFromSchema, $categoryName)
 {
     global $wgUser;
     $form = SFForm::create($formName, $formTemplates);
     $form->setAssociatedCategory($categoryName);
     if (array_key_exists('PageNameFormula', $formDataFromSchema)) {
         $form->setPageNameFormula($formDataFromSchema['PageNameFormula']);
     }
     if (array_key_exists('CreateTitle', $formDataFromSchema)) {
         $form->setCreateTitle($formDataFromSchema['CreateTitle']);
     }
     if (array_key_exists('EditTitle', $formDataFromSchema)) {
         $form->setEditTitle($formDataFromSchema['EditTitle']);
     }
     $formContents = $form->createMarkup();
     $params = array();
     $params['user_id'] = $wgUser->getId();
     $params['page_text'] = $formContents;
     $job = new PSCreatePageJob($formTitle, $params);
     Job::batchInsert(array($job));
 }
Пример #4
0
    function doSpecialCreateForm($query)
    {
        global $wgOut, $wgRequest, $sfgScriptPath;
        $db = wfGetDB(DB_SLAVE);
        if (!is_null($query)) {
            $presetFormName = str_replace('_', ' ', $query);
            $wgOut->setPageTitle(wfMessage('sf-createform-with-name', $presetFormName)->text());
            $form_name = $presetFormName;
        } else {
            $presetFormName = null;
            $form_name = $wgRequest->getVal('form_name');
        }
        // Create Javascript to populate fields to let the user input
        // parameters for the field, based on the input type selected
        // in the dropdown.
        $url = Skin::makeSpecialUrl('CreateForm', "showinputtypeoptions=' + this.val() + '&formfield=' + this.attr('formfieldid') + '");
        foreach ($wgRequest->getValues() as $param => $value) {
            $url .= '&params[' . Xml::escapeJsString($param) . ']=' . Xml::escapeJsString($value);
        }
        $wgOut->addModules('ext.semanticforms.collapsible');
        $section_name_error_str = '<font color="red" id="section_error">' . wfMessage('sf_blank_error')->escaped() . '</font>';
        $wgOut->addScript("<script>\njQuery.fn.displayInputParams = function() {\n\tinputParamsDiv = this.closest('.formField').find('.otherInputParams');\n\tjQuery.ajax({\n\t\turl: '{$url}',\n\t\tcontext: document.body,\n\t\tsuccess: function(data){\n\t\t\tinputParamsDiv.html(data);\n\t\t}\n\t});\n};\njQuery(document).ready(function() {\n\tjQuery('.inputTypeSelector').change( function() {\n\t\tjQuery(this).displayInputParams();\n\t});\n\tjQuery('#addsection').click( function(event) {\n\tif(jQuery('#sectionname').val() == '') {\n\t\t\tevent.preventDefault();\n\t\t\tjQuery('#section_error').remove();\n\t\t\tjQuery('<div/>').append('{$section_name_error_str}').appendTo('#sectionerror');\n\t}\n    });\n});\n</script>");
        // Get the names of all templates on this site.
        $all_templates = array();
        $res = $db->select('page', 'page_title', array('page_namespace' => NS_TEMPLATE, 'page_is_redirect' => 0), __METHOD__, array('ORDER BY' => 'page_title'));
        if ($db->numRows($res) > 0) {
            while ($row = $db->fetchRow($res)) {
                $template_name = str_replace('_', ' ', $row[0]);
                $all_templates[] = $template_name;
            }
        }
        $deleted_template_loc = null;
        $deleted_section_loc = null;
        // To keep the templates and sections
        $form_items = array();
        // Handle inputs.
        foreach ($wgRequest->getValues() as $var => $val) {
            # ignore variables that are not of the right form
            if (strpos($var, "_") != false) {
                # get the template declarations and work from there
                list($action, $id) = explode("_", $var, 2);
                if ($action == "template") {
                    // If the button was pressed to remove
                    // this template, just don't add it to
                    // the array.
                    if ($wgRequest->getVal("del_{$id}") != null) {
                        $deleted_template_loc = $id;
                    } else {
                        $form_template = SFTemplateInForm::create($val, $wgRequest->getVal("label_{$id}"), $wgRequest->getVal("allow_multiple_{$id}"));
                        $form_items[] = array('type' => 'template', 'name' => $form_template->getTemplateName(), 'item' => $form_template);
                    }
                } elseif ($action == "section") {
                    if ($wgRequest->getVal("delsection_{$id}") != null) {
                        $deleted_section_loc = $id;
                    } else {
                        $form_section = SFPageSection::create($val);
                        $form_items[] = array('type' => 'section', 'name' => $form_section->getSectionName(), 'item' => $form_section);
                    }
                }
            }
        }
        if ($wgRequest->getVal('add_field') != null) {
            $form_template = SFTemplateInForm::create($wgRequest->getVal('new_template'), "", false);
            $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_items, $template_loc, 0, "stub");
            $form_items[$template_loc] = array('type' => 'template', 'name' => $form_template->getTemplateName(), 'item' => $form_template);
        } 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_items, $section_loc, 0, "stub");
            $form_items[$section_loc] = array('type' => 'section', 'name' => $form_section->getSectionName(), 'item' => $form_section);
        } else {
            $section_loc = null;
            $new_section_loc = null;
        }
        // Now cycle through the templates and fields, modifying each
        // one per the query variables.
        $templates = 0;
        $sections = 0;
        foreach ($form_items as $fi) {
            if ($fi['type'] == 'template') {
                foreach ($fi['item']->getFields() as $j => $field) {
                    $old_i = SFFormUtils::getChangedIndex($templates, $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);
                            // Spaces got replaced by
                            // underlines in the query.
                            $paramName = str_replace('_', ' ', $paramName);
                        } else {
                            continue;
                        }
                        if ($paramName == 'label') {
                            $field->template_field->setLabel($value);
                        } elseif ($paramName == 'input type') {
                            $input_type = $wgRequest->getVal("input_type_" . $old_i . "_" . $j);
                            if ($input_type == 'hidden') {
                                $field->template_field->setInputType($input_type);
                                $field->setIsHidden(true);
                            } elseif (substr($input_type, 0, 1) == '.') {
                                // It's the default input type -
                                // don't do anything.
                            } else {
                                $field->template_field->setInputType($input_type);
                            }
                        } else {
                            if (!empty($value)) {
                                if ($value == 'on') {
                                    $value = true;
                                }
                                $field->setFieldArg($paramName, $value);
                            }
                        }
                    }
                }
                $templates++;
            } elseif ($fi['type'] == 'section') {
                $section = $fi['item'];
                $old_i = SFFormUtils::getChangedIndex($sections, $new_section_loc, $deleted_section_loc);
                foreach ($wgRequest->getValues() as $key => $value) {
                    if (($pos = strpos($key, '_section_' . $old_i)) != false) {
                        $paramName = substr($key, 0, $pos);
                        $paramName = str_replace('_', ' ', $paramName);
                    } else {
                        continue;
                    }
                    if (!empty($value)) {
                        if ($value == 'on') {
                            $value = true;
                        }
                        if ($paramName == 'level') {
                            $section->setSectionLevel($value);
                        } elseif ($paramName == 'hidden') {
                            $section->setIsHidden($value);
                        } elseif ($paramName == 'restricted') {
                            $section->setIsRestricted($value);
                        } elseif ($paramName == 'mandatory') {
                            $section->setIsMandatory($value);
                        } else {
                            $section->setSectionArgs($paramName, $value);
                        }
                    }
                }
                $sections++;
            }
        }
        $form = SFForm::create($form_name, $form_items);
        // If a submit button was pressed, create the form-definition
        // file, then redirect.
        $save_page = $wgRequest->getCheck('wpSave');
        $preview_page = $wgRequest->getCheck('wpPreview');
        if ($save_page || $preview_page) {
            $validToken = $this->getUser()->matchEditToken($wgRequest->getVal('csrf'), 'CreateForm');
            if (!$validToken) {
                $text = "This appears to be a cross-site request forgery; canceling save.";
                $wgOut->addHTML($text);
                return;
            }
            // Validate form name.
            if ($form->getFormName() == "") {
                $form_name_error_str = wfMessage('sf_blank_error')->text();
            } else {
                // Redirect to wiki interface.
                $wgOut->setArticleBodyOnly(true);
                $title = Title::makeTitleSafe(SF_NS_FORM, $form->getFormName());
                $full_text = $form->createMarkup();
                $text = SFUtils::printRedirectForm($title, $full_text, "", $save_page, $preview_page, false, false, false, null, null);
                $wgOut->addHTML($text);
                return;
            }
        }
        $text = "\t" . '<form action="" method="post">' . "\n";
        if (is_null($presetFormName)) {
            // Set 'title' field, in case there's no URL niceness
            $text .= Html::hidden('title', $this->getTitle()->getPrefixedText());
            $text .= "\n\t<p>" . wfMessage('sf_createform_nameinput')->escaped() . ' ' . wfMessage('sf_createform_nameinputdesc')->escaped() . Html::input('form_name', $form_name, 'text', array('size' => 25));
            if (!empty($form_name_error_str)) {
                $text .= "\t" . Html::element('font', array('color' => 'red'), $form_name_error_str);
            }
            $text .= "</p>\n";
        }
        $text .= $form->creationHTML();
        $text .= "<h2> " . wfMessage('sf_createform_addelements')->escaped() . " </h2>";
        $text .= "\t<p>" . wfMessage('sf_createform_addtemplate')->escaped() . "\n";
        $select_body = "";
        foreach ($all_templates as $template) {
            $select_body .= "\t" . 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.
        $select_body = "";
        foreach ($form_items as $i => $fi) {
            if ($fi['type'] == 'template') {
                $option_str = wfMessage('sf_createform_template')->escaped();
            } elseif ($fi['type'] == 'section') {
                $option_str = wfMessage('sf_createform_pagesection')->escaped();
            }
            $option_str .= $fi['name'];
            $select_body .= "\t" . Html::element('option', array('value' => $i), $option_str) . "\n";
        }
        $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') . "\n";
        // The form HTML for page sections
        $text .= "</br></br>" . Html::rawElement('span', null, wfMessage('sf_createform_addsection')->text() . ":") . "\n";
        $text .= Html::input('sectionname', '', 'text', array('size' => '30', 'placeholder' => wfMessage('sf_createform_sectionname')->text(), '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>
\t<br />

END;
        $text .= "\t" . Html::hidden('csrf', $this->getUser()->getEditToken('CreateForm')) . "\n";
        $saveAttrs = array('id' => 'wpSave');
        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_items) == 0) {
            $previewAttrs['disabled'] = true;
        }
        $editButtonsText .= "\t" . Html::input('wpPreview', wfMessage('preview')->text(), 'submit', $previewAttrs) . "\n";
        $text .= "\t" . Html::rawElement('div', array('class' => 'editButtons'), Html::rawElement('p', array(), $editButtonsText) . "\n") . "\n";
        // Explanatory message if buttons are disabled because no
        // templates have been added.
        if (count($form_items) == 0) {
            $text .= "\t" . Html::element('p', null, "(" . wfMessage('sf_createform_additembeforesave')->text() . ")");
        }
        $text .= <<<END
\t</form>

END;
        $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>');
    }
 function createAllPages()
 {
     $out = $this->getOutput();
     $req = $this->getRequest();
     $user = $this->getUser();
     $template_name = trim($req->getVal("template_name"));
     $template_multiple = $req->getBool("template_multiple");
     // If this is a multiple-instance template, there
     // shouldn't be a corresponding form or category.
     if ($template_multiple) {
         $form_name = null;
         $category_name = null;
     } else {
         $form_name = trim($req->getVal("form_name"));
         $category_name = trim($req->getVal("category_name"));
     }
     if ($template_name === '' || !$template_multiple && ($form_name === '' || $category_name === '')) {
         $out->addWikiMsg('sf_createclass_missingvalues');
         return;
     }
     $fields = array();
     $jobs = array();
     // Cycle through all the rows passed in.
     for ($i = 1; $req->getVal("field_name_{$i}") != ''; $i++) {
         // Go through the query values, setting the appropriate
         // local variables.
         $field_name = trim($req->getVal("field_name_{$i}"));
         $property_name = trim($req->getVal("property_name_{$i}"));
         $property_type = $req->getVal("property_type_{$i}");
         $allowed_values = $req->getVal("allowed_values_{$i}");
         $is_list = $req->getCheck("is_list_{$i}");
         // Create an SFTemplateField object based on these
         // values, and add it to the $fields array.
         $field = SFTemplateField::create($field_name, $field_name, $property_name, $is_list);
         if (defined('CARGO_VERSION')) {
             $field->setFieldType($property_type);
             // Hopefully it's safe to use a Cargo
             // utility method here.
             $possibleValues = CargoUtils::smartSplit(',', $allowed_values);
             $field->setPossibleValues($possibleValues);
         }
         $fields[] = $field;
         // Create the property, and make a job for it.
         if (defined('SMW_VERSION') && !empty($property_name)) {
             $full_text = SFCreateProperty::createPropertyText($property_type, '', $allowed_values);
             $property_title = Title::makeTitleSafe(SMW_NS_PROPERTY, $property_name);
             $params = array();
             $params['user_id'] = $user->getId();
             $params['page_text'] = $full_text;
             $params['edit_summary'] = wfMessage('sf_createproperty_editsummary', $property_type)->inContentLanguage()->text();
             $jobs[] = new SFCreatePageJob($property_title, $params);
         }
     }
     // Also create the "connecting property", if there is one.
     $connectingProperty = trim($req->getVal('connecting_property'));
     if (defined('SMW_VERSION') && $connectingProperty != '') {
         global $smwgContLang;
         $datatypeLabels = $smwgContLang->getDatatypeLabels();
         $property_type = $datatypeLabels['_wpg'];
         $full_text = SFCreateProperty::createPropertyText($property_type, '', $allowed_values);
         $property_title = Title::makeTitleSafe(SMW_NS_PROPERTY, $connectingProperty);
         $params = array();
         $params['user_id'] = $user->getId();
         $params['page_text'] = $full_text;
         $params['edit_summary'] = wfMessage('sf_createproperty_editsummary', $property_type)->inContentLanguage()->text();
         $jobs[] = new SFCreatePageJob($property_title, $params);
     }
     // Create the template, and save it (might as well save
     // one page, instead of just creating jobs for all of them).
     $template_format = $req->getVal("template_format");
     $sfTemplate = new SFTemplate($template_name, $fields);
     if (defined('CARGO_VERSION')) {
         $sfTemplate->mCargoTable = trim($req->getVal("cargo_table"));
     }
     if (defined('SMW_VERSION') && $template_multiple) {
         $sfTemplate->setConnectingProperty($connectingProperty);
     } else {
         $sfTemplate->setCategoryName($category_name);
     }
     $sfTemplate->setFormat($template_format);
     $full_text = $sfTemplate->createText();
     $template_title = Title::makeTitleSafe(NS_TEMPLATE, $template_name);
     $edit_summary = '';
     if (method_exists('WikiPage', 'doEditContent')) {
         // MW 1.21+
         $template_page = new WikiPage($template_title);
         $content = new WikitextContent($full_text);
         $template_page->doEditContent($content, $edit_summary);
     } else {
         // MW <= 1.20
         $template_article = new Article($template_title);
         $template_article->doEdit($full_text, $edit_summary);
     }
     // Create the form, and make a job for it.
     if ($form_name != '') {
         $form_template = SFTemplateInForm::create($template_name, '', false);
         $form_items = array();
         $form_items[] = array('type' => 'template', 'name' => $form_template->getTemplateName(), 'item' => $form_template);
         $form = SFForm::create($form_name, $form_items);
         $full_text = $form->createMarkup();
         $form_title = Title::makeTitleSafe(SF_NS_FORM, $form_name);
         $params = array();
         $params['user_id'] = $user->getId();
         $params['page_text'] = $full_text;
         $jobs[] = new SFCreatePageJob($form_title, $params);
     }
     // Create the category, and make a job for it.
     if ($category_name != '') {
         $full_text = SFCreateCategory::createCategoryText($form_name, $category_name, '');
         $category_title = Title::makeTitleSafe(NS_CATEGORY, $category_name);
         $params = array();
         $params['user_id'] = $user->getId();
         $params['page_text'] = $full_text;
         $jobs[] = new SFCreatePageJob($category_title, $params);
     }
     if (class_exists('JobQueueGroup')) {
         JobQueueGroup::singleton()->push($jobs);
     } else {
         // MW <= 1.20
         Job::batchInsert($jobs);
     }
     $out->addWikiMsg('sf_createclass_success');
 }
Пример #6
0
 /**
  * Creates a form page, when called from the 'generatepages' page
  * of Page Schemas.
  */
 public static function generateForm($formName, $formTitle, $formItems, $formDataFromSchema, $categoryName)
 {
     global $wgUser;
     $input = array();
     if (array_key_exists('inputFreeText', $formDataFromSchema)) {
         $input['free text'] = '{{{standard input|free text|rows=10}}}';
     }
     if (array_key_exists('inputSummary', $formDataFromSchema)) {
         $input['summary'] = '{{{standard input|summary}}}';
     }
     if (array_key_exists('inputMinorEdit', $formDataFromSchema)) {
         $input['minor edit'] = '{{{standard input|minor edit}}}';
     }
     if (array_key_exists('inputWatch', $formDataFromSchema)) {
         $input['watch'] = '{{{standard input|watch}}}';
     }
     if (array_key_exists('inputSave', $formDataFromSchema)) {
         $input['save'] = '{{{standard input|save}}}';
     }
     if (array_key_exists('inputPreview', $formDataFromSchema)) {
         $input['preview'] = '{{{standard input|preview}}}';
     }
     if (array_key_exists('inputChanges', $formDataFromSchema)) {
         $input['changes'] = '{{{standard input|changes}}}';
     }
     if (array_key_exists('inputCancel', $formDataFromSchema)) {
         $input['cancel'] = '{{{standard input|cancel}}}';
     }
     $freeTextLabel = null;
     if (array_key_exists('freeTextLabel', $formDataFromSchema)) {
         $freeTextLabel = $formDataFromSchema['freeTextLabel'];
     }
     $form = SFForm::create($formName, $formItems);
     $form->setAssociatedCategory($categoryName);
     if (array_key_exists('PageNameFormula', $formDataFromSchema)) {
         $form->setPageNameFormula($formDataFromSchema['PageNameFormula']);
     }
     if (array_key_exists('CreateTitle', $formDataFromSchema)) {
         $form->setCreateTitle($formDataFromSchema['CreateTitle']);
     }
     if (array_key_exists('EditTitle', $formDataFromSchema)) {
         $form->setEditTitle($formDataFromSchema['EditTitle']);
     }
     $formContents = $form->createMarkup($input, $freeTextLabel);
     $params = array();
     $params['user_id'] = $wgUser->getId();
     $params['page_text'] = $formContents;
     $job = new PSCreatePageJob($formTitle, $params);
     $jobs = array($job);
     if (class_exists('JobQueueGroup')) {
         JobQueueGroup::singleton()->push($jobs);
     } else {
         // MW <= 1.20
         Job::batchInsert($jobs);
     }
 }
Пример #7
0
 static function createAllPages()
 {
     global $wgOut, $wgRequest, $wgUser;
     $template_name = trim($wgRequest->getVal("template_name"));
     $template_multiple = $wgRequest->getBool("template_multiple");
     // If this is a multiple-instance template, there
     // shouldn't be a corresponding form or category.
     if ($template_multiple) {
         $form_name = null;
         $category_name = null;
     } else {
         $form_name = trim($wgRequest->getVal("form_name"));
         $category_name = trim($wgRequest->getVal("category_name"));
     }
     if ($template_name === '' || !$template_multiple && ($form_name === '' || $category_name === '')) {
         $wgOut->addWikiMsg('sf_createclass_missingvalues');
         return;
     }
     $fields = array();
     $jobs = array();
     // Cycle through all the rows passed in.
     for ($i = 1; $wgRequest->getCheck("field_name_{$i}"); $i++) {
         // go through the query values, setting the appropriate local variables
         $property_name = trim($wgRequest->getVal("property_name_{$i}"));
         $field_name = trim($wgRequest->getVal("field_name_{$i}"));
         $property_type = $wgRequest->getVal("property_type_{$i}");
         $allowed_values = $wgRequest->getVal("allowed_values_{$i}");
         $is_list = $wgRequest->getCheck("is_list_{$i}");
         // Create an SFTemplateField object based on these
         // values, and add it to the $fields array.
         $field = SFTemplateField::create($field_name, $field_name, $property_name, $is_list);
         $fields[] = $field;
         // Create the property, and make a job for it.
         if (!empty($property_name)) {
             $full_text = SFCreateProperty::createPropertyText($property_type, '', $allowed_values);
             $property_title = Title::makeTitleSafe(SMW_NS_PROPERTY, $property_name);
             $params = array();
             $params['user_id'] = $wgUser->getId();
             $params['page_text'] = $full_text;
             $jobs[] = new SFCreatePageJob($property_title, $params);
         }
     }
     // Create the template, and save it (might as well save
     // one page, instead of just creating jobs for all of them).
     $template_format = $wgRequest->getVal("template_format");
     $full_text = SFTemplateField::createTemplateText($template_name, $fields, null, $category_name, null, null, $template_format);
     $template_title = Title::makeTitleSafe(NS_TEMPLATE, $template_name);
     $template_article = new Article($template_title, 0);
     $edit_summary = '';
     $template_article->doEdit($full_text, $edit_summary);
     // Create the form, and make a job for it.
     $form_template = SFTemplateInForm::create($template_name, '', false);
     $form_templates = array($form_template);
     $form = SFForm::create($form_name, $form_templates);
     $full_text = $form->createMarkup();
     $form_title = Title::makeTitleSafe(SF_NS_FORM, $form_name);
     $params = array();
     $params['user_id'] = $wgUser->getId();
     $params['page_text'] = $full_text;
     $jobs[] = new SFCreatePageJob($form_title, $params);
     // Create the category, and make a job for it.
     $full_text = SFCreateCategory::createCategoryText($form_name, $category_name, '');
     $category_title = Title::makeTitleSafe(NS_CATEGORY, $category_name);
     $params = array();
     $params['user_id'] = $wgUser->getId();
     $params['page_text'] = $full_text;
     $jobs[] = new SFCreatePageJob($category_title, $params);
     Job::batchInsert($jobs);
     $wgOut->addWikiMsg('sf_createclass_success');
 }