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
00043
00044
00045 if ($this->_sCacheKey) {
00046 $this->_sCacheKey .= "_i18n";
00047 }
00048 }
00049
00057 public function setLanguage( $iLang = null )
00058 {
00059 $this->_iLanguage = (int) $iLang;
00060
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
00091 $this->modifyCacheKey("_nonml");
00092 }
00093
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
00117
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
00146 $this->setLanguage($iLanguage);
00147
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
00190 foreach ($aObjFields as $sKey => $sValue ) {
00191
00192
00193 if ( preg_match('/^oxactive(_(\d{1,2}))?$/', $sKey) ) {
00194 continue;
00195 }
00196
00197 $iFieldLang = $this->_getFieldLang( $sKey );
00198
00199
00200 if ( $this->isMultilingualField($sKey) || $iFieldLang > 0 ) {
00201 $sNewKey = preg_replace('/_(\d{1,2})$/', '', $sKey);
00202 $aMultiLangFields[$sNewKey][] = (int) $iFieldLang;
00203 }
00204 }
00205
00206
00207 if ( count($aMultiLangFields) < 1 ) {
00208 return $aLanguages;
00209 }
00210
00211
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
00219
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
00269
00270 $aFields = parent::_getNonCachedFieldNames($blForceFullStructure);
00271
00272 if (!$this->_blEmployMultilanguage) {
00273 return $aFields;
00274 }
00275
00276
00277 if ($aFields) {
00278
00279 $aWorkingFields = &$aFields;
00280 } else {
00281
00282 $aWorkingFields = &$this->_aFieldNames;
00283 }
00284
00285
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
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
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
00444
00445 if ( $blRet && $this->_blIsSeoObject && $this->isAdmin() ) {
00446
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
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
00579 foreach ( $this->_getLanguageSetTables() as $sSetTbl ) {
00580 $oDB->execute( "delete from {$sSetTbl} where oxid = {$sOXID}" );
00581 }
00582 }
00583 return $blDeleted;
00584 }
00585 }