00001 <?php
00012 class SFTemplateField {
00013 private $mFieldName;
00014 private $mValueLabels;
00015 private $mLabel;
00016 private $mSemanticProperty;
00017 private $mPropertyType;
00018 private $mPossibleValues;
00019 private $mIsList;
00020 private $mDelimiter;
00021 private $mDisplay;
00022 private $mInputType;
00023
00024 static function create( $name, $label, $semanticProperty = null, $isList = null, $delimiter = null, $display = null ) {
00025 $f = new SFTemplateField();
00026 $f->mFieldName = trim( str_replace( '\\', '', $name ) );
00027 $f->mLabel = trim( str_replace( '\\', '', $label ) );
00028 $f->setSemanticProperty( $semanticProperty );
00029 $f->mIsList = $isList;
00030 $f->mDelimiter = $delimiter;
00031 $f->mDisplay = $display;
00032
00033 if ( !empty( $isList ) && empty( $delimiter ) ) {
00034 $f->mDelimiter = ',';
00035 }
00036 return $f;
00037 }
00038
00043 static function createFromList( $field_name, $all_fields, $strict_parsing ) {
00044
00045
00046
00047
00048
00049 $the_field = null;
00050 foreach ( $all_fields as $cur_field ) {
00051 if ( $field_name == $cur_field->mFieldName ) {
00052 $the_field = $cur_field;
00053 break;
00054 }
00055 }
00056 if ( $the_field == null ) {
00057 if ( $strict_parsing ) {
00058 return null;
00059 }
00060 $the_field = new SFTemplateField();
00061 }
00062 return $the_field;
00063 }
00064
00065 function setTypeAndPossibleValues() {
00066 $proptitle = Title::makeTitleSafe( SMW_NS_PROPERTY, $this->mSemanticProperty );
00067 if ( $proptitle === null ) {
00068 return;
00069 }
00070
00071 $store = smwfGetStore();
00072
00073 $allowed_values = SFUtils::getSMWPropertyValues( $store, $proptitle, "Allows value" );
00074 $label_formats = SFUtils::getSMWPropertyValues( $store, $proptitle, "Has field label format" );
00075 if ( class_exists( 'SMWDIProperty' ) ) {
00076
00077 $propValue = SMWDIProperty::newFromUserLabel( $this->mSemanticProperty );
00078 $this->mPropertyType = $propValue->findPropertyTypeID();
00079 } else {
00080 $propValue = SMWPropertyValue::makeUserProperty( $this->mSemanticProperty );
00081 $this->mPropertyType = $propValue->getPropertyTypeID();
00082 }
00083
00084 foreach ( $allowed_values as $allowed_value ) {
00085
00086 $this->mPossibleValues[] = html_entity_decode( $allowed_value );
00087 if ( count( $label_formats ) > 0 ) {
00088 $label_format = $label_formats[0];
00089 $prop_instance = SMWDataValueFactory::findTypeID( $this->mPropertyType );
00090 $label_value = SMWDataValueFactory::newTypeIDValue( $prop_instance, $wiki_value );
00091 $label_value->setOutputFormat( $label_format );
00092 $this->mValueLabels[$wiki_value] = html_entity_decode( $label_value->getWikiValue() );
00093 }
00094 }
00095
00096
00097
00098 if ( count( $this->mPossibleValues ) > 0 ) {
00099 $this->mPropertyType = 'enumeration';
00100 }
00101 }
00102
00107 function setSemanticProperty( $semantic_property ) {
00108 $this->mSemanticProperty = str_replace( '\\', '', $semantic_property );
00109 $this->mPossibleValues = array();
00110
00111 $this->setTypeAndPossibleValues();
00112 }
00113
00114 function getFieldName() {
00115 return $this->mFieldName;
00116 }
00117
00118 function getValueLabels() {
00119 return $this->mValueLabels;
00120 }
00121
00122 function getLabel() {
00123 return $this->mLabel;
00124 }
00125
00126 function getSemanticProperty() {
00127 return $this->mSemanticProperty;
00128 }
00129
00130 function getPropertyType() {
00131 return $this->mPropertyType;
00132 }
00133
00134 function getPossibleValues() {
00135 return $this->mPossibleValues;
00136 }
00137
00138 function isList() {
00139 return $this->mIsList;
00140 }
00141
00142 function getInputType() {
00143 return $this->mInputType;
00144 }
00145
00146 function setTemplateField( $templateField ) {
00147 $this->mTemplateField = $templateField;
00148 }
00149
00150 function setLabel( $label ) {
00151 $this->mLabel = $label;
00152 }
00153
00154 function setInputType( $inputType ) {
00155 $this->mInputType = $inputType;
00156 }
00157
00166 public static function createTemplateText( $template_name, $template_fields, $internal_obj_property, $category, $aggregating_property, $aggregating_label, $template_format ) {
00167 $template_header = wfMsgForContent( 'sf_template_docu', $template_name );
00168 $text = <<<END
00169 <noinclude>
00170 $template_header
00171 <pre>
00172
00173 END;
00174 $text .= '{{' . $template_name;
00175 if ( count( $template_fields ) > 0 ) { $text .= "\n"; }
00176 foreach ( $template_fields as $field ) {
00177 $text .= "|" . $field->mFieldName . "=\n";
00178 }
00179 $template_footer = wfMsgForContent( 'sf_template_docufooter' );
00180 $text .= <<<END
00181 }}
00182 </pre>
00183 $template_footer
00184 </noinclude><includeonly>
00185 END;
00186
00187
00188 if ( !empty( $internal_obj_property ) && class_exists( 'SIOInternalObject' ) ) {
00189 $setInternalText = '{{#set_internal:' . $internal_obj_property;
00190 } else {
00191 $setInternalText = null;
00192 }
00193 $setText = '';
00194
00195
00196 if ( $template_format == 'infobox' ) {
00197
00198
00199
00200 $tableText = <<<END
00201 {| style="width: 30em; font-size: 90%; border: 1px solid #aaaaaa; background-color: #f9f9f9; color: black; margin-bottom: 0.5em; margin-left: 1em; padding: 0.2em; float: right; clear: right; text-align:left;"
00202 ! style="text-align: center; background-color:#ccccff;" colspan="2" |<big>{{PAGENAME}}</big>
00203 |-
00204
00205 END;
00206 } else {
00207 $tableText = '{| class="wikitable"' . "\n";
00208 }
00209
00210 foreach ( $template_fields as $i => $field ) {
00211
00212 if ( is_null( $field->mDisplay ) ) {
00213 if ( $i > 0 ) {
00214 $tableText .= "|-\n";
00215 }
00216 $tableText .= '! ' . $field->mLabel . "\n";
00217 } elseif ( $field->mDisplay == 'nonempty' ) {
00218 $tableText .= '{{#if:{{{' . $field->mFieldName . '|}}}|';
00219 if ( $i > 0 ) {
00220 $tableText .= "{{!}}-\n";
00221 }
00222 $tableText .= '! ' . $field->mLabel . "\n";
00223 }
00224
00225 if ( empty( $field->mSemanticProperty ) ) {
00226 $tableText .= "| {{{" . $field->mFieldName . "|}}}\n";
00227 } elseif ( !is_null( $setInternalText ) ) {
00228 $tableText .= "| {{{" . $field->mFieldName . "|}}}\n";
00229 if ( $field->mIsList ) {
00230 $setInternalText .= '|' . $field->mSemanticProperty . '#list={{{' . $field->mFieldName . '|}}}';
00231 } else {
00232 $setInternalText .= '|' . $field->mSemanticProperty . '={{{' . $field->mFieldName . '|}}}';
00233 }
00234 } elseif ( $field->mDisplay == 'hidden' ) {
00235 if ( $field->mIsList ) {
00236 $setText .= $field->mSemanticProperty . '#list={{{' . $field->mFieldName . '|}}}|';
00237 } else {
00238 $setText .= $field->mSemanticProperty . '={{{' . $field->mFieldName . '|}}}|';
00239 }
00240 } elseif ( $field->mDisplay == 'nonempty' ) {
00241 $tableText .= '{{!}} [[' . $field->mSemanticProperty . '::{{{' . $field->mFieldName . "|}}}]]}}\n";
00242 } elseif ( $field->mIsList ) {
00243
00244
00245
00246
00247
00248
00249 $var = "x";
00250 if ( strstr( $field->mSemanticProperty, $var ) ) {
00251 $var_options = array( 'y', 'z', 'xx', 'yy', 'zz', 'aa', 'bb', 'cc' );
00252 foreach ( $var_options as $option ) {
00253 if ( ! strstr( $field->mSemanticProperty, $option ) ) {
00254 $var = $option;
00255 break;
00256 }
00257 }
00258 }
00259 $tableText .= "| {{#arraymap:{{{" . $field->mFieldName . "|}}}|" . $field->mDelimiter . "|$var|[[" . $field->mSemanticProperty . "::$var]]}}\n";
00260 } else {
00261 $tableText .= "| [[" . $field->mSemanticProperty . "::{{{" . $field->mFieldName . "|}}}]]\n";
00262 }
00263 }
00264
00265
00266
00267 if ( !is_null( $aggregating_property ) && $aggregating_property !== '' ) {
00268 if ( count( $template_fields ) > 0 ) {
00269 $tableText .= "|-\n";
00270 }
00271 $tableText .= <<<END
00272 ! $aggregating_label
00273 | {{#ask:[[$aggregating_property::{{SUBJECTPAGENAME}}]]|format=list}}
00274
00275 END;
00276 }
00277 $tableText .= "|}";
00278
00279
00280
00281 if ( is_null( $setInternalText ) ) {
00282 $tableText .= "\n";
00283 } else {
00284 $setInternalText .= "}}";
00285 $text .= $setInternalText;
00286 }
00287
00288
00289 if ( $setText !== '' ) {
00290 $setText = '{{#set:' . $setText . "}}\n";
00291 $text .= $setText;
00292 }
00293
00294 $text .= $tableText;
00295 if ( $category !== '' ) {
00296 global $wgContLang;
00297 $namespace_labels = $wgContLang->getNamespaces();
00298 $category_namespace = $namespace_labels[NS_CATEGORY];
00299 $text .= "\n[[$category_namespace:$category]]\n";
00300 }
00301 $text .= "</includeonly>\n";
00302
00303 return $text;
00304 }
00305 }