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 = oxRegistry::getLang()->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 = oxRegistry::getLang()->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( oxDb::FETCH_MODE_ASSOC );
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, $this->getUseSkipSaveFields() ) .
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             oxRegistry::get("oxSeoEncoder")->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, $this->getUseSkipSaveFields() );
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         return getViewName( $sTable, $this->getLanguage(), $sShopID);
00502     }
00503 
00515     protected function _getAllFields( $blReturnSimple = false )
00516     {
00517         if ( $this->_blEmployMultilanguage ) {
00518             return parent::_getAllFields( $blReturnSimple );
00519         } else {
00520             $sViewName = $this->getViewName();
00521             if ( !$sViewName ) {
00522                 return array();
00523             }
00524             return $this->_getTableFields( $sViewName, $blReturnSimple );
00525         }
00526     }
00527 
00538     protected function _addField($sName, $sStatus, $sType = null, $sLength = null)
00539     {
00540         if ($this->_blEmployMultilanguage && $this->_getFieldLang($sName)) {
00541             return;
00542         }
00543 
00544         return parent::_addField($sName, $sStatus, $sType, $sLength);
00545     }
00546 
00558     protected function _canFieldBeNull( $sFieldName )
00559     {
00560         $sFieldName = preg_replace( '/_\d{1,2}$/', '', $sFieldName );
00561         return parent::_canFieldBeNull( $sFieldName );
00562     }
00563 
00571     public function delete( $sOXID = null )
00572     {
00573         $blDeleted = parent::delete( $sOXID );
00574         if ( $blDeleted ) {
00575             $oDB = oxDb::getDb();
00576             $sOXID = $oDB->quote( $sOXID );
00577 
00578             //delete the record
00579             foreach ( $this->_getLanguageSetTables() as $sSetTbl ) {
00580                 $oDB->execute( "delete from {$sSetTbl} where oxid = {$sOXID}" );
00581             }
00582         }
00583         return $blDeleted;
00584     }
00585 }