oxi18n.php

Go to the documentation of this file.
00001 <?php
00002 
00010 class oxI18n extends oxBase
00011 {
00012 
00018     protected $_sClassName = 'oxI18n';
00019 
00025     protected $_iLanguage = null;
00026 
00033     protected $_blEmployMultilanguage = true;
00034 
00038     public function __construct()
00039     {
00040         parent::__construct();
00041 
00042         //T2008-02-22
00043         //lets try to differentiate cache keys for oxI18n and oxBase
00044         //in order not to load cached structure for the instances of oxbase classe called on same table
00045         if ($this->_sCacheKey) {
00046             $this->_sCacheKey .= "_i18n";
00047         }
00048     }
00049 
00057     public function setLanguage( $iLang = null )
00058     {
00059         $this->_iLanguage = (int) $iLang;
00060         // reset
00061         $this->_sViewTable = false;
00062     }
00063 
00069     public function getLanguage()
00070     {
00071         if ( $this->_iLanguage === null ) {
00072             $this->_iLanguage = oxLang::getInstance()->getBaseLanguage();
00073         }
00074         return $this->_iLanguage;
00075     }
00076 
00085     public function setEnableMultilang( $blEmployMultilanguage )
00086     {
00087         if ($this->_blEmployMultilanguage != $blEmployMultilanguage) {
00088             $this->_blEmployMultilanguage = $blEmployMultilanguage;
00089             if (!$blEmployMultilanguage) {
00090                 //#63T
00091                 $this->modifyCacheKey("_nonml");
00092             }
00093             // reset
00094             $this->_sViewTable = false;
00095             if (count($this->_aFieldNames) > 1) {
00096                 $this->_initDataStructure();
00097             }
00098         }
00099     }
00100 
00109     public function isMultilingualField( $sFieldName )
00110     {
00111         $sFieldName = strtolower( $sFieldName );
00112         if ( isset( $this->_aFieldNames[$sFieldName] ) ) {
00113             return (bool) $this->_aFieldNames[$sFieldName];
00114         }
00115 
00116         //not inited field yet
00117         //and note that this is should be called only in first call after tmp dir is empty
00118         startProfile('!__CACHABLE2__!');
00119         $blIsMultilang = (bool) $this->_getFieldStatus( $sFieldName );
00120         stopProfile('!__CACHABLE2__!');
00121         return (bool) $blIsMultilang;
00122     }
00123 
00130     public function isMultilang()
00131     {
00132         return true;
00133     }
00134 
00143     public function loadInLang( $iLanguage, $sOxid)
00144     {
00145         // set new lang to this object
00146         $this->setLanguage($iLanguage);
00147         // reset
00148         $this->_sViewTable = false;
00149         return $this->load( $sOxid);
00150     }
00151 
00160     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00161     {
00162         if ($blOverride) {
00163             $this->_sCacheKey = $sCacheKey."|i18n";
00164         } else {
00165             $this->_sCacheKey .= $sCacheKey;
00166         }
00167 
00168         if (!$sCacheKey) {
00169             $this->_sCacheKey = null;
00170         }
00171     }
00172 
00179     public function getAvailableInLangs()
00180     {
00181         $aLanguages = oxLang::getInstance()->getLanguageNames();
00182 
00183         $aObjFields = $this->_getTableFields(
00184             getViewName($this->_sCoreTable, -1, -1),
00185             true
00186         );
00187         $aMultiLangFields = array();
00188 
00189         //selecting all object multilang fields
00190         foreach ($aObjFields as $sKey => $sValue ) {
00191 
00192             //skipping oxactive field
00193             if ( preg_match('/^oxactive(_(\d{1,2}))?$/', $sKey) ) {
00194                 continue;
00195             }
00196 
00197             $iFieldLang = $this->_getFieldLang( $sKey );
00198 
00199             //checking, if field is multilanguage
00200             if ( $this->isMultilingualField($sKey) || $iFieldLang >  0 ) {
00201                 $sNewKey = preg_replace('/_(\d{1,2})$/', '', $sKey);
00202                 $aMultiLangFields[$sNewKey][] = (int) $iFieldLang;
00203             }
00204         }
00205 
00206         // if no multilanguage fields, return default languages array
00207         if ( count($aMultiLangFields) < 1 ) {
00208             return $aLanguages;
00209         }
00210 
00211         // select from non-multilanguage core view (all ml tables joined to one)
00212         $oDb = oxDb::getDb( true );
00213         $query = "select * from ".getViewName($this->_sCoreTable, -1, -1)." where oxid = " . $oDb->quote( $this->getId() );
00214         $rs = $oDb->getAll($query);
00215 
00216         $aNotInLang = $aLanguages;
00217 
00218         // checks if object field data is not empty in all available languages
00219         // and formats not available in languages array
00220         if ( is_array($rs) && count($rs[0]) ) {
00221             foreach ( $aMultiLangFields as $sFieldId => $aMultiLangIds ) {
00222 
00223                 foreach ( $aMultiLangIds as $sMultiLangId ) {
00224                     $sFieldName = ( $sMultiLangId == 0 ) ? $sFieldId : $sFieldId.'_'.$sMultiLangId;
00225                     if ( $rs['0'][strtoupper($sFieldName)] ) {
00226                         unset( $aNotInLang[$sMultiLangId] );
00227                         continue;
00228                     }
00229                 }
00230             }
00231         }
00232 
00233         $aIsInLang = array_diff( $aLanguages, $aNotInLang );
00234 
00235         return $aIsInLang;
00236     }
00237 
00246     protected function _getFieldStatus( $sFieldName )
00247     {
00248         $aAllField = $this->_getAllFields( true );
00249         if ( isset( $aAllField[strtolower( $sFieldName ) . "_1"] ) ) {
00250             return 1;
00251         }
00252         return 0;
00253     }
00254 
00266     protected function _getNonCachedFieldNames($blForceFullStructure = false)
00267     {
00268         //Tomas
00269         //TODO: this place could be optimized. please check what we can do.
00270         $aFields = parent::_getNonCachedFieldNames($blForceFullStructure);
00271 
00272         if (!$this->_blEmployMultilanguage) {
00273             return $aFields;
00274         }
00275 
00276         //lets do some pointer manipulation
00277         if ($aFields) {
00278             //non admin fields
00279             $aWorkingFields = &$aFields;
00280         } else {
00281             //most likely admin fields so we remove another language
00282             $aWorkingFields = &$this->_aFieldNames;
00283         }
00284 
00285         //we have an array of fields, lets remove multilanguage fields
00286         foreach ($aWorkingFields as $sName => $sVal) {
00287             if ($this->_getFieldLang($sName)) {
00288                 unset($aWorkingFields[$sName]);
00289             } else {
00290                 $aWorkingFields[$sName] = $this->_getFieldStatus($sName);
00291             }
00292         }
00293 
00294         return $aWorkingFields;
00295     }
00296 
00304     protected function _getFieldLang($sFieldName)
00305     {
00306         if ( false === strpos($sFieldName, '_')) {
00307             return 0;
00308         }
00309         if (preg_match('/_(\d{1,2})$/', $sFieldName, $aRegs)) {
00310             return $aRegs[1];
00311         } else {
00312             return 0;
00313         }
00314     }
00315 
00323     public function getUpdateSqlFieldName($sField)
00324     {
00325         $iLang = $this->getLanguage();
00326         if ($iLang && $this->_blEmployMultilanguage && $this->isMultilingualField($sField)) {
00327             $sField .= "_" . $iLang;
00328         }
00329 
00330         return $sField;
00331     }
00332 
00341     protected function _getUpdateFieldsForTable( $sTable, $blUseSkipSaveFields = true )
00342     {
00343         $sCoreTable = $this->getCoreTableName();
00344 
00345         $blSkipMultilingual = false;
00346         $blSkipCoreFields = false;
00347 
00348         if ($sTable != $sCoreTable ) {
00349             $blSkipCoreFields = true;
00350         }
00351         if ($this->_blEmployMultilanguage) {
00352             if ( $sTable != getLangTableName($sCoreTable, $this->getLanguage() ) ) {
00353                 $blSkipMultilingual = true;
00354             }
00355         }
00356 
00357         $sSql = '';
00358         $blSep  = false;
00359         foreach (array_keys($this->_aFieldNames) as $sKey) {
00360             $sKeyLowercase = strtolower($sKey);
00361             if ($sKeyLowercase != 'oxid') {
00362                 if ($this->_blEmployMultilanguage) {
00363                     if ($blSkipMultilingual && $this->isMultilingualField($sKey)) {
00364                         continue;
00365                     }
00366                     if ($blSkipCoreFields && !$this->isMultilingualField($sKey)) {
00367                         continue;
00368                     }
00369                 } else {
00370                     // need to explicitly check field language
00371                     $iFieldLang = $this->_getFieldLang($sKey);
00372                     if ($iFieldLang) {
00373                         if ($sTable != getLangTableName($sCoreTable, $iFieldLang)) {
00374                             continue;
00375                         }
00376                     } elseif ($blSkipCoreFields) {
00377                         continue;
00378                     }
00379                 }
00380             }
00381 
00382             $sLongName = $this->_getFieldLongName($sKey);
00383             $oField = $this->$sLongName;
00384 
00385             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array($sKeyLowercase, $this->_aSkipSaveFields)) ) {
00386                 $sKey = $this->getUpdateSqlFieldName($sKey);
00387                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
00388                 $blSep = true;
00389             }
00390         }
00391 
00392         return $sSql;
00393     }
00394 
00404     protected function _getUpdateFields( $blUseSkipSaveFields = true )
00405     {
00406         return $this->_getUpdateFieldsForTable( $this->getCoreTableName(), $blUseSkipSaveFields );
00407     }
00408 
00419     protected function _update()
00420     {
00421         $blRet = parent::_update();
00422 
00423         if ($blRet) {
00424             //also update multilang table if it is separate
00425             $aUpdateTables = array();
00426             if ($this->_blEmployMultilanguage) {
00427                 $sCoreTable = $this->getCoreTableName();
00428                 $sLangTable = getLangTableName($sCoreTable, $this->getLanguage() );
00429                 if ($sCoreTable != $sLangTable) {
00430                     $aUpdateTables[] = $sLangTable;
00431                 }
00432             } else {
00433                 $aUpdateTables = $this->_getLanguageSetTables();
00434             }
00435             foreach ($aUpdateTables as $sLangTable) {
00436                 $sUpdate= "insert into $sLangTable set ".$this->_getUpdateFieldsForTable( $sLangTable, false ) .
00437                           " on duplicate key update ".$this->_getUpdateFieldsForTable( $sLangTable );
00438 
00439                 $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
00440             }
00441         }
00442 
00443         // currently only multilanguage objects are SEO
00444         // if current object is managed by SEO and SEO is ON
00445         if ( $blRet && $this->_blIsSeoObject && $this->isAdmin() ) {
00446             // marks all object db entries as expired
00447             oxSeoEncoder::getInstance()->markAsExpired( $this->getId(), null, 1, $this->getLanguage() );
00448         }
00449 
00450         return $blRet;
00451     }
00452 
00460     protected function _getLanguageSetTables( $sCoreTableName = null )
00461     {
00462         $sCoreTableName = $sCoreTableName ? $sCoreTableName : $this->getCoreTableName();
00463         return oxNew('oxDbMetaDataHandler')->getAllMultiTables( $sCoreTableName );
00464     }
00465 
00473     protected function _insert()
00474     {
00475         $blRet = parent::_insert();
00476 
00477         if ($blRet) {
00478             //also insert to multilang tables if it is separate
00479             foreach ($this->_getLanguageSetTables() as $sTable) {
00480                 $sSq = "insert into $sTable set ".$this->_getUpdateFieldsForTable( $sTable, false );
00481                 $blRet = $blRet && (bool) oxDB::getDb()->execute( $sSq );
00482             }
00483         }
00484 
00485         return $blRet;
00486     }
00487 
00496     protected function _getObjectViewName( $sTable, $sShopID = null)
00497     {
00498         if (!$this->_blEmployMultilanguage) {
00499             return parent::_getObjectViewName($sTable, $sShopID);
00500         }
00501 
00502         if ( $this->_blForceCoreTableUsage ) {
00503             $sShopID = -1;
00504         }
00505         return getViewName( $sTable, $this->getLanguage(), $sShopID);
00506     }
00507 
00515     public function getViewName($blForceCoreTableUsage = null)
00516     {
00517         if (!$this->_blEmployMultilanguage) {
00518             return parent::getViewName($blForceCoreTableUsage);
00519         }
00520         if (!$this->_sViewTable || ($blForceCoreTableUsage !== null)) {
00521             if ( ($blForceCoreTableUsage !== null)?$blForceCoreTableUsage:$this->_blForceCoreTableUsage ) {
00522                 $iShopId = -1;
00523             } else {
00524                 $iShopId = oxConfig::getInstance()->getShopId();
00525             }
00526             $sViewName = getViewName($this->_sCoreTable, $this->getLanguage(), $iShopId);
00527             if ($blForceCoreTableUsage !== null) {
00528                 return $sViewName;
00529             }
00530             $this->_sViewTable = $sViewName;
00531         }
00532         return $this->_sViewTable;
00533     }
00534 
00546     protected function _getAllFields( $blReturnSimple = false )
00547     {
00548         if ( $this->_blEmployMultilanguage ) {
00549             return parent::_getAllFields( $blReturnSimple );
00550         } else {
00551             $sViewName = $this->getViewName();
00552             if ( !$sViewName ) {
00553                 return array();
00554             }
00555             return $this->_getTableFields( $sViewName, $blReturnSimple );
00556         }
00557     }
00558 
00569     protected function _addField($sName, $sStatus, $sType = null, $sLength = null)
00570     {
00571         if ($this->_blEmployMultilanguage && $this->_getFieldLang($sName)) {
00572             return;
00573         }
00574 
00575         return parent::_addField($sName, $sStatus, $sType, $sLength);
00576     }
00577 
00589     protected function _canFieldBeNull( $sFieldName )
00590     {
00591         $sFieldName = preg_replace( '/_\d{1,2}$/', '', $sFieldName );
00592         return parent::_canFieldBeNull( $sFieldName );
00593     }
00594 
00602     public function delete( $sOXID = null )
00603     {
00604         $blDeleted = parent::delete( $sOXID );
00605         if ( $blDeleted ) {
00606             $oDB = oxDb::getDb();
00607             $sOXID = $oDB->quote( $sOXID );
00608 
00609             //delete the record
00610             foreach ( $this->_getLanguageSetTables() as $sSetTbl ) {
00611                 $oDB->execute( "delete from {$sSetTbl} where oxid = {$sOXID}" );
00612             }
00613         }
00614         return $blDeleted;
00615     }
00616 }