00001 <?php
00002
00011 class oxI18n extends oxBase
00012 {
00013
00018 protected $_sClassName = 'oxI18n';
00019
00024 protected $_iLanguage = null;
00025
00031 protected $_blEmployMultilanguage = true;
00032
00036 public function __construct()
00037 {
00038 parent::__construct();
00039
00040 $this->setLanguage();
00041
00042
00043
00044
00045 if ($this->_sCacheKey) {
00046 $this->_sCacheKey .= "_i18n";
00047 }
00048 }
00049
00057 public function setLanguage( $iLang = null )
00058 {
00059 $myConfig = $this->getConfig();
00060 if( isset($iLang)) {
00061 $this->_iLanguage = (int) $iLang;
00062 } else {
00063 $this->_iLanguage = (int) oxLang::getInstance()->getBaseLanguage();
00064 }
00065 }
00066
00072 public function getLanguage()
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 if (count($this->_aFieldNames) > 1) {
00094 $this->_initDataStructure();
00095 }
00096 }
00097 }
00098
00107 public function isMultilingualField($sFieldName)
00108 {
00109 if (isset($this->_aFieldNames[$sFieldName])) {
00110 return (bool) $this->_aFieldNames[$sFieldName];
00111 }
00112
00113
00114
00115 startProfile('!__CACHABLE2__!');
00116 $blIsMultilang = (bool) $this->_getFieldStatus($sFieldName);
00117 stopProfile('!__CACHABLE2__!');
00118 return (bool) $blIsMultilang;
00119 }
00120
00127 public function isMultilang()
00128 {
00129 return true;
00130 }
00131
00140 public function loadInLang( $iLanguage, $sOxid)
00141 {
00142
00143 $this->setLanguage($iLanguage);
00144 return parent::load( $sOxid);
00145 }
00146
00155 public function modifyCacheKey( $sCacheKey, $blOverride = false )
00156 {
00157 if ($blOverride) {
00158 $this->_sCacheKey = $sCacheKey."|i18n";
00159 } else {
00160 $this->_sCacheKey .= $sCacheKey;
00161 }
00162
00163 if (!$sCacheKey) {
00164 $this->_sCacheKey = null;
00165 }
00166 }
00167
00176 public function load( $sOXID)
00177 {
00178 $this->setLanguage( oxLang::getInstance()->getBaseLanguage() );
00179 return parent::load($sOXID);
00180 }
00181
00189 public function assign($dbRecord)
00190 {
00191 oxLang::getInstance()->getBaseLanguage();
00192 $sLangTag = oxLang::getInstance()->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 if (!strstr($sFieldName, '_')) {
00383 return 0;
00384 }
00385 if (preg_match('/_(\d{1,2})$/', $sFieldName, $aRegs)) {
00386 $sRes = $aRegs[1];
00387 } else {
00388 $sRes = 0;
00389 }
00390
00391 stopProfile('_getFieldLang');
00392 return $sRes;
00393 }
00394
00404 protected function _update()
00405 {
00406 $blRet = parent::_update();
00407
00408
00409
00410 if ( $blRet && $this->_blIsSeoObject && $this->isAdmin() ) {
00411
00412 oxSeoEncoder::getInstance()->markAsExpired( $this->getId() );
00413 }
00414
00415 return $blRet;
00416 }
00417
00428
00429
00430
00431
00432
00433
00434
00435
00436 }