00001 <?php
00016 class SMWLinearValue extends SMWNumberValue {
00017
00019 protected $m_unitfactors = false;
00021 protected $m_unitids = false;
00023 protected $m_displayunits = false;
00025 protected $m_mainunit = false;
00026
00027 protected function convertToMainUnit( $number, $unit ) {
00028 $this->initConversionData();
00029 if ( array_key_exists( $unit, $this->m_unitids ) ) {
00030 $this->m_unitin = $this->m_unitids[$unit];
00031 $this->m_dataitem = new SMWDINumber( $number / $this->m_unitfactors[$this->m_unitin], $this->m_typeid );
00032 return true;
00033 } else {
00034 return false;
00035 }
00036 }
00037
00038 protected function makeConversionValues() {
00039 if ( $this->m_unitvalues !== false ) return;
00040 $this->m_unitvalues = array();
00041 if ( !$this->isValid() ) return;
00042 $this->initDisplayData();
00043 if ( count( $this->m_displayunits ) == 0 ) {
00044 foreach ( $this->m_unitfactors as $unit => $factor ) {
00045 if ( $unit != '' ) {
00046 $this->m_unitvalues[$unit] = $this->m_dataitem->getNumber() * $factor;
00047 }
00048 }
00049 } else {
00050 foreach ( $this->m_displayunits as $unit ) {
00054 $unitkey = ( $this->m_unitids[$unit] == $this->m_unitin ) ? $this->m_unitids[$unit] : $unit;
00055 $this->m_unitvalues[$unitkey] = $this->m_dataitem->getNumber() * $this->m_unitfactors[$this->m_unitids[$unit]];
00056 }
00057 }
00058 }
00059
00060 protected function makeUserValue() {
00061 $printunit = false;
00062
00063 if ( ( $this->m_outformat ) && ( $this->m_outformat != '-' ) &&
00064 ( $this->m_outformat != '-n' ) && ( $this->m_outformat != '-u' )) {
00065 $wantedunit = SMWNumberValue::normalizeUnit( $this->m_outformat );
00066 if ( array_key_exists( $wantedunit, $this->m_unitids ) ) {
00067 $printunit = $wantedunit;
00068 }
00069 }
00070
00071 if ( $printunit === false ) {
00072 $this->initDisplayData();
00073 if ( count( $this->m_displayunits ) > 0 ) {
00074 $printunit = reset( $this->m_displayunits );
00075 }
00076 }
00077
00078 if ( $printunit === false ) {
00079 $printunit = $this->getUnit();
00080 }
00081
00082 $this->m_unitin = $this->m_unitids[$printunit];
00083 $this->m_unitvalues = false;
00084 $value = $this->m_dataitem->getNumber() * $this->m_unitfactors[$this->m_unitin];
00085
00086 $this->m_caption = '';
00087 if ( $this->m_outformat != '-u' ) {
00088 $this->m_caption .= ( ( $this->m_outformat != '-' ) && ( $this->m_outformat != '-n' ) ? smwfNumberFormat( $value ) : $value );
00089 }
00090 if ( ( $printunit != '' ) && ( $this->m_outformat != '-n' ) ) {
00091 if ( $this->m_outformat != '-u' ) {
00092 $this->m_caption .= ( $this->m_outformat != '-' ? ' ' : ' ' );
00093 }
00094 $this->m_caption .= $printunit;
00095 }
00096 }
00097
00098 public function getUnitList() {
00099 $this->initConversionData();
00100 return array_keys( $this->m_unitfactors );
00101 }
00102
00103 public function getUnit() {
00104 $this->initConversionData();
00105 return $this->m_mainunit;
00106 }
00107
00111
00115 protected function initConversionData() {
00116 if ( $this->m_unitids !== false ) return;
00117 $this->m_unitids = array();
00118 $this->m_unitfactors = array();
00119 $this->m_mainunit = false;
00120
00121 try {
00122 $typeDiWikiPage = new SMWDIWikiPage( $this->m_typeid, SMW_NS_TYPE, '' );
00123 } catch ( SMWDataItemException $e ) {
00124 smwfLoadExtensionMessages( 'SemanticMediaWiki' );
00125 $this->addError( wfMsgForContent( 'smw_unknowntype', SMWDataValueFactory::findTypeLabel( $this->getTypeID() ) ) );
00126 return;
00127 }
00128 $factors = smwfGetStore()->getPropertyValues( $typeDiWikiPage, new SMWDIProperty( '_CONV' ) );
00129 if ( count( $factors ) == 0 ) {
00130 smwfLoadExtensionMessages( 'SemanticMediaWiki' );
00131 $this->addError( wfMsgForContent( 'smw_unknowntype', SMWDataValueFactory::findTypeLabel( $this->getTypeID() ) ) );
00132 return;
00133 }
00134 $number = $unit = '';
00135 foreach ( $factors as $di ) {
00136 if ( ( $di->getDIType() !== SMWDataItem::TYPE_STRING ) ||
00137 ( SMWNumberValue::parseNumberValue( $di->getString(), $number, $unit ) != 0 ) ) {
00138 continue;
00139 }
00140 $unit_aliases = preg_split( '/\s*,\s*/u', $unit );
00141 $first = true;
00142 foreach ( $unit_aliases as $unit ) {
00143 $unit = SMWNumberValue::normalizeUnit( $unit );
00144 if ( $first ) {
00145 $unitid = $unit;
00146 if ( $number == 1 ) {
00147 $this->m_mainunit = $unit;
00148 $this->m_unitfactors = array( $unit => 1 ) + $this->m_unitfactors;
00149 } else {
00150 $this->m_unitfactors[$unit] = $number;
00151 }
00152 $first = false;
00153 }
00154
00155 $this->m_unitids[$unit] = $unitid;
00156 }
00157 }
00158 if ( $this->m_mainunit === false ) {
00159 $this->m_mainunit = '';
00160 }
00161
00162
00163
00164 $this->m_unitfactors = array( '' => 1 ) + $this->m_unitfactors;
00165 $this->m_unitids[''] = '';
00166 }
00167
00171 protected function initDisplayData() {
00172 if ( $this->m_displayunits !== false ) return;
00173 $this->initConversionData();
00174 $this->m_displayunits = array();
00175 if ( ( $this->m_property === null ) || ( $this->m_property->getDIWikiPage() === null ) ) {
00176 return;
00177 }
00178 $dataItems = smwfGetStore()->getPropertyValues( $this->m_property->getDIWikiPage(), new SMWDIProperty( '_UNIT' ) );
00179 $units = array();
00180 foreach ( $dataItems as $di ) {
00181 if ( $di->getDIType() === SMWDataItem::TYPE_STRING ) {
00182 $units = $units + preg_split( '/\s*,\s*/u', $di->getString() );
00183 }
00184 }
00185 foreach ( $units as $unit ) {
00186 $unit = SMWNumberValue::normalizeUnit( $unit );
00187 if ( array_key_exists( $unit, $this->m_unitids ) ) {
00188 $this->m_displayunits[] = $unit;
00189 }
00190 }
00191 }
00192
00193 }