00001 <?php
00002
00010 class oxI18n extends oxBase
00011 {
00012
00017 protected $_sClassName = 'oxI18n';
00018
00023 protected $_iLanguage = null;
00024
00030 protected $_blEmployMultilanguage = true;
00031
00035 public function __construct()
00036 {
00037 parent::__construct();
00038
00039 $this->setLanguage();
00040
00041
00042
00043
00044 if ($this->_sCacheKey) {
00045 $this->_sCacheKey .= "_i18n";
00046 }
00047 }
00048
00056 public function setLanguage( $iLang = null )
00057 {
00058 $myConfig = $this->getConfig();
00059 if ( isset($iLang)) {
00060 $this->_iLanguage = (int) $iLang;
00061 } else {
00062 $this->_iLanguage = (int) oxLang::getInstance()->getBaseLanguage();
00063 }
00064 }
00065
00071 public function getLanguage()
00072 {
00073 return $this->_iLanguage;
00074 }
00075
00084 public function setEnableMultilang( $blEmployMultilanguage )
00085 {
00086 if ($this->_blEmployMultilanguage != $blEmployMultilanguage) {
00087 $this->_blEmployMultilanguage = $blEmployMultilanguage;
00088 if (!$blEmployMultilanguage) {
00089
00090 $this->modifyCacheKey("_nonml");
00091 }
00092 if (count($this->_aFieldNames) > 1) {
00093 $this->_initDataStructure();
00094 }
00095 }
00096 }
00097
00106 public function isMultilingualField($sFieldName)
00107 {
00108 if (isset($this->_aFieldNames[$sFieldName])) {
00109 return (bool) $this->_aFieldNames[$sFieldName];
00110 }
00111
00112
00113
00114 startProfile('!__CACHABLE2__!');
00115 $blIsMultilang = (bool) $this->_getFieldStatus($sFieldName);
00116 stopProfile('!__CACHABLE2__!');
00117 return (bool) $blIsMultilang;
00118 }
00119
00126 public function isMultilang()
00127 {
00128 return true;
00129 }
00130
00139 public function loadInLang( $iLanguage, $sOxid)
00140 {
00141
00142 $this->setLanguage($iLanguage);
00143 return parent::load( $sOxid);
00144 }
00145
00154 public function modifyCacheKey( $sCacheKey, $blOverride = false )
00155 {
00156 if ($blOverride) {
00157 $this->_sCacheKey = $sCacheKey."|i18n";
00158 } else {
00159 $this->_sCacheKey .= $sCacheKey;
00160 }
00161
00162 if (!$sCacheKey) {
00163 $this->_sCacheKey = null;
00164 }
00165 }
00166
00175 public function load( $sOXID)
00176 {
00177 $this->setLanguage( oxLang::getInstance()->getBaseLanguage() );
00178 return parent::load($sOXID);
00179 }
00180
00188 public function assign($dbRecord)
00189 {
00190 $oLang = oxLang::getInstance();
00191 $oLang->getBaseLanguage();
00192 $sLangTag = $oLang->getLanguageTag($this->getLanguage());
00193 if ($this->_blEmployMultilanguage && $sLangTag) {
00194 foreach ($dbRecord as $sField => $sVal) {
00195
00196 if (isset($dbRecord[$sField . $sLangTag])) {
00197 $dbRecord[$sField] = $dbRecord[$sField . $sLangTag];
00198 }
00199 }
00200 }
00201
00202 return parent::assign($dbRecord);
00203 }
00204
00211 public function getAvailableInLangs()
00212 {
00213 $aLanguages = oxLang::getInstance()->getLanguageNames();
00214
00215 $aObjFields = $this->_getAllFields(true);
00216 $aMultiLangFields = array();
00217
00218
00219 foreach ($aObjFields as $sKey => $sValue ) {
00220
00221
00222 if ( preg_match('/^oxactive(_(\d{1,2}))?$/', $sKey) ) {
00223 continue;
00224 }
00225
00226 $iFieldLang = $this->_getFieldLang( $sKey );
00227
00228
00229 if ( $this->isMultilingualField($sKey) || $iFieldLang > 0 ) {
00230 $sNewKey = preg_replace('/_(\d{1,2})$/', '', $sKey);
00231 $aMultiLangFields[$sNewKey][] = (int) $iFieldLang;
00232 }
00233 }
00234
00235
00236 if ( count($aMultiLangFields) < 1 ) {
00237 return $aLanguages;
00238 }
00239
00240 $query = "select * from {$this->_sCoreTable} where oxid = '" . $this->getId() . "'";
00241 $rs = oxDb::getDb( true )->getAll($query);
00242
00243 $aNotInLang = $aLanguages;
00244
00245
00246
00247 if ( is_array($rs) && count($rs[0]) ) {
00248 foreach ( $aMultiLangFields as $sFieldId => $aMultiLangIds ) {
00249
00250 foreach ( $aMultiLangIds as $sMultiLangId ) {
00251 $sFieldName = ( $sMultiLangId == 0 ) ? $sFieldId : $sFieldId.'_'.$sMultiLangId;
00252 if ( $rs['0'][strtoupper($sFieldName)] ) {
00253 unset( $aNotInLang[$sMultiLangId] );
00254 continue;
00255 }
00256 }
00257 }
00258 }
00259
00260 $aIsInLang = array_diff( $aLanguages, $aNotInLang );
00261
00262 return $aIsInLang;
00263 }
00264
00272 public function getSqlFieldName($sField)
00273 {
00274 $iLang = (int) $this->_iLanguage;
00275 if ($iLang && $this->_blEmployMultilanguage && $this->isMultilingualField($sField)) {
00276 $sField .= "_" . $iLang;
00277 }
00278
00279 return $sField;
00280 }
00281
00289 public function getSqlActiveSnippet( $blForceCoreTable = false )
00290 {
00291 $sQ = '';
00292 $sTable = $this->getCoreTableName();
00293
00294
00295 if ( $this->isMultilingualField( 'oxactive' ) ) {
00296 $sQ = " $sTable.oxactive".oxLang::getInstance()->getLanguageTag( $this->getLanguage() )." = 1 ";
00297 } elseif ( isset( $this->_aFieldNames['oxactive'] ) ) {
00298 $sQ = " $sTable.oxactive = 1 ";
00299 }
00300
00301
00302 if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00303
00304 $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00305
00306 $sQ = $sQ?" $sQ or ":'';
00307 $sQ = "( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00308 }
00309
00310 return $sQ;
00311 }
00312
00321 protected function _getFieldStatus($sFieldName)
00322 {
00323 $aAllField = $this->_getAllFields(true);
00324 if (isset($aAllField[$sFieldName."_1"])) {
00325 return 1;
00326 }
00327 return 0;
00328 }
00329
00341 protected function _getNonCachedFieldNames($blForceFullStructure = false)
00342 {
00343
00344
00345 $aFields = parent::_getNonCachedFieldNames($blForceFullStructure);
00346
00347 if (!$this->_blEmployMultilanguage) {
00348 return $aFields;
00349 }
00350
00351
00352 if ($aFields) {
00353
00354 $aWorkingFields = &$aFields;
00355 } else {
00356
00357 $aWorkingFields = &$this->_aFieldNames;
00358 }
00359
00360
00361 foreach ($aWorkingFields as $sName => $sVal) {
00362 if ($this->_getFieldLang($sName)) {
00363 unset($aWorkingFields[$sName]);
00364 } else {
00365 $aWorkingFields[$sName] = $this->_getFieldStatus($sName);
00366 }
00367 }
00368
00369 return $aWorkingFields;
00370 }
00371
00379 protected function _getFieldLang($sFieldName)
00380 {
00381 startProfile('_getFieldLang');
00382 $oStr = getStr();
00383 if ( !$oStr->strstr($sFieldName, '_')) {
00384 return 0;
00385 }
00386 if (preg_match('/_(\d{1,2})$/', $sFieldName, $aRegs)) {
00387 $sRes = $aRegs[1];
00388 } else {
00389 $sRes = 0;
00390 }
00391
00392 stopProfile('_getFieldLang');
00393 return $sRes;
00394 }
00395
00405 protected function _update()
00406 {
00407 $blRet = parent::_update();
00408
00409
00410
00411 if ( $blRet && $this->_blIsSeoObject && $this->isAdmin() ) {
00412
00413 oxSeoEncoder::getInstance()->markAsExpired( $this->getId() );
00414 }
00415
00416 return $blRet;
00417 }
00418
00429
00430
00431
00432
00433
00434
00435
00436
00437 }