mTemplateName = str_replace( '_', ' ', $name ); $tif->mFields = array(); if ( is_null( $formFields ) ) { $template = PFTemplate::newFromName( $tif->mTemplateName ); $fields = $template->getTemplateFields(); foreach ( $fields as $field ) { $tif->mFields[] = PFFormField::create( $field ); } } else { $tif->mFields = $formFields; } $tif->mLabel = $label; $tif->mAllowMultiple = $allowMultiple; $tif->mMaxAllowed = $maxAllowed; return $tif; } public static function newFromFormTag( $tag_components ) { global $wgParser; $template_name = str_replace( '_', ' ', trim( $wgParser->recursiveTagParse( $tag_components[1] ) ) ); $tif = new PFTemplateInForm(); $tif->mTemplateName = str_replace( '_', ' ', $template_name ); $tif->mAddButtonText = wfMessage( 'pf_formedit_addanother' )->text(); // Cycle through the other components. for ( $i = 2; $i < count( $tag_components ); $i++ ) { $component = $tag_components[$i]; if ( $component == 'multiple' ) { $tif->mAllowMultiple = true; } elseif ( $component == 'strict' ) { $tif->mStrictParsing = true; } $sub_components = array_map( 'trim', explode( '=', $component, 2 ) ); if ( count( $sub_components ) == 2 ) { if ( $sub_components[0] == 'label' ) { $tif->mLabel = $wgParser->recursiveTagParse( $sub_components[1] ); } elseif ( $sub_components[0] == 'minimum instances' ) { $tif->mMinAllowed = $sub_components[1]; } elseif ( $sub_components[0] == 'maximum instances' ) { $tif->mMaxAllowed = $sub_components[1]; } elseif ( $sub_components[0] == 'add button text' ) { $tif->mAddButtonText = $wgParser->recursiveTagParse( $sub_components[1] ); } elseif ( $sub_components[0] == 'embed in field' ) { // Placeholder on form template level. Assume that the template form def // will have a multiple+placeholder parameters, and get the placeholder value. // We expect something like TemplateName[fieldName], and convert it to the // TemplateName___fieldName form used internally. preg_match( '/\s*(.*)\[(.*)\]\s*/', $sub_components[1], $matches ); if ( count( $matches ) > 2 ) { $tif->mEmbedInTemplate = $matches[1]; $tif->mEmbedInField = $matches[2]; $tif->mPlaceholder = PFFormPrinter::placeholderFormat( $tif->mEmbedInTemplate, $tif->mEmbedInField ); } } elseif ( $sub_components[0] == 'display' ) { $tif->mDisplay = $sub_components[1]; } elseif ( $sub_components[0] == 'height' ) { $tif->mHeight = $sub_components[1]; } } } return $tif; } function getTemplateName() { return $this->mTemplateName; } function getHeight() { return $this->mHeight; } function getFields() { return $this->mFields; } function getEmbedInTemplate() { return $this->mEmbedInTemplate; } function getEmbedInField() { return $this->mEmbedInField; } function getLabel() { return $this->mLabel; } function getAddButtonText() { return $this->mAddButtonText; } function getDisplay() { return $this->mDisplay; } function getPlaceholder() { return $this->mPlaceholder; } function allowsMultiple() { return $this->mAllowMultiple; } function strictParsing() { return $this->mStrictParsing; } function getMinInstancesAllowed() { return $this->mMinAllowed; } function getMaxInstancesAllowed() { return $this->mMaxAllowed; } function createMarkup() { $text = "{{{for template|" . $this->mTemplateName; if ( $this->mAllowMultiple ) { $text .= "|multiple"; } if ( $this->mLabel != '' ) { $text .= "|label=" . $this->mLabel; } $text .= "}}}\n"; // For now, HTML for templates differs for multiple-instance // templates; this may change if handling of form definitions // gets more sophisticated. if ( ! $this->mAllowMultiple ) { $text .= "{| class=\"formtable\"\n"; } foreach ( $this->mFields as $i => $field ) { $is_last_field = ( $i == count( $this->mFields ) - 1 ); $text .= $field->createMarkup( $this->mAllowMultiple, $is_last_field ); } if ( ! $this->mAllowMultiple ) { $text .= "|}\n"; } $text .= "{{{end template}}}\n"; return $text; } // The remaining functions here are intended for an instance of a // template in a specific form, and perhaps should be moved into // another class. function getFullTextInPage() { return $this->mFullTextInPage; } function pageCallsThisTemplate() { return $this->mPageCallsThisTemplate; } function hasValueFromPageForField( $field_name ) { return array_key_exists( $field_name, $this->mValuesFromPage ); } function getAndRemoveValueFromPageForField( $field_name ) { $value = $this->mValuesFromPage[$field_name]; unset( $this->mValuesFromPage[$field_name] ); return $value; } function getValuesFromPage() { return $this->mValuesFromPage; } function getInstanceNum() { return $this->mInstanceNum; } function getGridValues() { return $this->mGridValues; } function incrementInstanceNum() { $this->mInstanceNum++; } function allInstancesPrinted() { return $this->mAllInstancesPrinted; } function addGridValue( $field_name, $cur_value ) { if ( ! array_key_exists( $this->mInstanceNum, $this->mGridValues ) ) { $this->mGridValues[$this->mInstanceNum] = array(); } $this->mGridValues[$this->mInstanceNum][$field_name] = $cur_value; } function addField( $form_field ) { $this->mFields[] = $form_field; } function setFieldValuesFromSubmit() { global $wgRequest; $this->mValuesFromSubmit = null; $query_template_name = str_replace( ' ', '_', $this->mTemplateName ); // Also replace periods with underlines, since that's what // POST does to strings anyway. $query_template_name = str_replace( '.', '_', $query_template_name ); // ...and escape apostrophes. // (Or don't.) // $query_template_name = str_replace( "'", "\'", $query_template_name ); $allValuesFromSubmit = $wgRequest->getArray( $query_template_name ); if ( is_null( $allValuesFromSubmit ) ) { return; } // If this is a multiple-instance template, get the values for // this instance of the template. if ( $this->mAllowMultiple ) { $valuesFromSubmitKeys = array(); foreach ( array_keys( $allValuesFromSubmit ) as $key ) { if ( $key != 'num' ) { $valuesFromSubmitKeys[] = $key; } } $this->mNumInstancesFromSubmit = count( $valuesFromSubmitKeys ); if ( $this->mNumInstancesFromSubmit > $this->mInstanceNum ) { $instanceKey = $valuesFromSubmitKeys[$this->mInstanceNum]; $this->mValuesFromSubmit = $allValuesFromSubmit[$instanceKey]; } } else { $this->mValuesFromSubmit = $allValuesFromSubmit; } } function getValuesFromSubmit() { return $this->mValuesFromSubmit; } /** * Change "non-template pipes", i.e. pipes that do not separate * between template params but rather are contained within tag * functions, into another character, so that they don't get * handled and can be changed back into pipes later. * (This doesn't include pipes contained within curly bracket * parser functions - those are handled separately.) * * @param string $str * @return string */ static function escapeNonTemplatePipes( $str ) { $startAndEndTags = array( array( '
' ), array( '' ), array( '