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         // set default language
00043         //$this->setLanguage();
00044 
00045         //T2008-02-22
00046         //lets try to differentiate cache keys for oxI18n and oxBase
00047         //in order not to load cached structure for the instances of oxbase classe called on same table
00048         if ($this->_sCacheKey) {
00049             $this->_sCacheKey .= "_i18n";
00050         }
00051     }
00052 
00060     public function setLanguage( $iLang = null )
00061     {
00062         $this->_iLanguage = (int) $iLang;
00063     }
00064 
00070     public function getLanguage()
00071     {
00072         if ( $this->_iLanguage === null ) {
00073             $this->_iLanguage = oxLang::getInstance()->getBaseLanguage();
00074         }
00075         return $this->_iLanguage;
00076     }
00077 
00086     public function setEnableMultilang( $blEmployMultilanguage )
00087     {
00088         if ($this->_blEmployMultilanguage != $blEmployMultilanguage) {
00089             $this->_blEmployMultilanguage = $blEmployMultilanguage;
00090             if (!$blEmployMultilanguage) {
00091                 //#63T
00092                 $this->modifyCacheKey("_nonml");
00093             }
00094             if (count($this->_aFieldNames) > 1) {
00095                 $this->_initDataStructure();
00096             }
00097         }
00098     }
00099 
00108     public function isMultilingualField($sFieldName)
00109     {
00110         if (isset($this->_aFieldNames[$sFieldName])) {
00111             return (bool) $this->_aFieldNames[$sFieldName];
00112         }
00113 
00114         //not inited field yet
00115         //and note that this is should be called only in first call after tmp dir is empty
00116         startProfile('!__CACHABLE2__!');
00117         $blIsMultilang = (bool) $this->_getFieldStatus($sFieldName);
00118         stopProfile('!__CACHABLE2__!');
00119         return (bool) $blIsMultilang;
00120     }
00121 
00128     public function isMultilang()
00129     {
00130         return true;
00131     }
00132 
00141     public function loadInLang( $iLanguage, $sOxid)
00142     {
00143         // set new lang to this object
00144         $this->setLanguage($iLanguage);
00145         return $this->load( $sOxid);
00146     }
00147 
00156     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00157     {
00158         if ($blOverride) {
00159             $this->_sCacheKey = $sCacheKey."|i18n";
00160         } else {
00161             $this->_sCacheKey .= $sCacheKey;
00162         }
00163 
00164         if (!$sCacheKey) {
00165             $this->_sCacheKey = null;
00166         }
00167     }
00168 
00176     public function assign($dbRecord)
00177     {
00178         $sLangTag = oxLang::getInstance()->getLanguageTag($this->getLanguage());
00179         if ($this->_blEmployMultilanguage && $sLangTag) {
00180             foreach ($dbRecord as $sField => $sVal) {
00181                 //handling multilang
00182                 if (isset($dbRecord[$sField . $sLangTag])) {
00183                     $dbRecord[$sField] = $dbRecord[$sField . $sLangTag];
00184                 }
00185             }
00186         }
00187 
00188         return parent::assign($dbRecord);
00189     }
00190 
00197     public function getAvailableInLangs()
00198     {
00199         $aLanguages = oxLang::getInstance()->getLanguageNames();
00200 
00201         $aObjFields = $this->_getAllFields(true);
00202         $aMultiLangFields = array();
00203 
00204         //selecting all object multilang fields
00205         foreach ($aObjFields as $sKey => $sValue ) {
00206 
00207             //skipping oxactive field
00208             if ( preg_match('/^oxactive(_(\d{1,2}))?$/', $sKey) ) {
00209                 continue;
00210             }
00211 
00212             $iFieldLang = $this->_getFieldLang( $sKey );
00213 
00214             //checking, if field is multilanguage
00215             if ( $this->isMultilingualField($sKey) || $iFieldLang >  0 ) {
00216                 $sNewKey = preg_replace('/_(\d{1,2})$/', '', $sKey);
00217                 $aMultiLangFields[$sNewKey][] = (int) $iFieldLang;
00218             }
00219         }
00220 
00221         // if no multilanguage fields, return default languages array
00222         if ( count($aMultiLangFields) < 1 ) {
00223             return $aLanguages;
00224         }
00225 
00226         $query = "select * from {$this->_sCoreTable} where oxid = '" . $this->getId() . "'";
00227         $rs = oxDb::getDb( true )->getAll($query);
00228 
00229         $aNotInLang = $aLanguages;
00230 
00231         // checks if object field data is not empty in all available languages
00232         // and formats not available in languages array
00233         if ( is_array($rs) && count($rs[0]) ) {
00234             foreach ( $aMultiLangFields as $sFieldId => $aMultiLangIds ) {
00235 
00236                 foreach ( $aMultiLangIds as $sMultiLangId ) {
00237                     $sFieldName = ( $sMultiLangId == 0 ) ? $sFieldId : $sFieldId.'_'.$sMultiLangId;
00238                     if ( $rs['0'][strtoupper($sFieldName)] ) {
00239                         unset( $aNotInLang[$sMultiLangId] );
00240                         continue;
00241                     }
00242                 }
00243             }
00244         }
00245 
00246         $aIsInLang = array_diff( $aLanguages, $aNotInLang );
00247 
00248         return $aIsInLang;
00249     }
00250 
00258     public function getSqlFieldName($sField)
00259     {
00260         $iLang = $this->getLanguage();
00261         if ($iLang && $this->_blEmployMultilanguage && $this->isMultilingualField($sField)) {
00262             $sField .= "_" . $iLang;
00263         }
00264 
00265         return $sField;
00266     }
00267 
00275     public function getSqlActiveSnippet( $blForceCoreTable = false )
00276     {
00277         $sQ = '';
00278             $sTable = $this->getCoreTableName();
00279 
00280         // has 'active' or 'active_x' field ?
00281         if ( $this->isMultilingualField( 'oxactive' ) ) {
00282             $sQ = " $sTable.oxactive".oxLang::getInstance()->getLanguageTag( $this->getLanguage() )." = 1 ";
00283         } elseif ( isset( $this->_aFieldNames['oxactive'] ) ) {
00284             $sQ = " $sTable.oxactive = 1 ";
00285         }
00286 
00287         // has 'activefrom'/'activeto' fields ?
00288         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00289 
00290             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00291 
00292             $sQ = $sQ?" $sQ or ":'';
00293             $sQ = "( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00294         }
00295 
00296         return $sQ;
00297     }
00298 
00307     protected function _getFieldStatus($sFieldName)
00308     {
00309         $aAllField = $this->_getAllFields(true);
00310         if (isset($aAllField[$sFieldName."_1"])) {
00311             return 1;
00312         }
00313         return 0;
00314     }
00315 
00327     protected function _getNonCachedFieldNames($blForceFullStructure = false)
00328     {
00329         //Tomas
00330         //TODO: this place could be optimized. please check what we can do.
00331         $aFields = parent::_getNonCachedFieldNames($blForceFullStructure);
00332 
00333         if (!$this->_blEmployMultilanguage) {
00334             return $aFields;
00335         }
00336 
00337         //lets do some pointer manipulation
00338         if ($aFields) {
00339             //non admin fields
00340             $aWorkingFields = &$aFields;
00341         } else {
00342             //most likely admin fields so we remove another language
00343             $aWorkingFields = &$this->_aFieldNames;
00344         }
00345 
00346         //we have an array of fields, lets remove multilanguage fields
00347         foreach ($aWorkingFields as $sName => $sVal) {
00348             if ($this->_getFieldLang($sName)) {
00349                 unset($aWorkingFields[$sName]);
00350             } else {
00351                 $aWorkingFields[$sName] = $this->_getFieldStatus($sName);
00352             }
00353         }
00354 
00355         return $aWorkingFields;
00356     }
00357 
00365     protected function _getFieldLang($sFieldName)
00366     {
00367         startProfile('_getFieldLang');
00368         $oStr = getStr();
00369         if ( !$oStr->strstr($sFieldName, '_')) {
00370             return 0;
00371         }
00372         if (preg_match('/_(\d{1,2})$/', $sFieldName, $aRegs)) {
00373             $sRes = $aRegs[1];
00374         } else {
00375             $sRes = 0;
00376         }
00377 
00378         stopProfile('_getFieldLang');
00379         return $sRes;
00380     }
00381 
00391     protected function _update()
00392     {
00393         $blRet = parent::_update();
00394 
00395         // currently only multilanguage objects are SEO
00396         // if current object is managed by SEO and SEO is ON
00397         if ( $blRet && $this->_blIsSeoObject && $this->isAdmin() ) {
00398             // marks all object db entries as expired
00399             oxSeoEncoder::getInstance()->markAsExpired( $this->getId(), $this->getConfig()->getShopId(), 1, $this->getLanguage() );
00400         }
00401 
00402         return $blRet;
00403     }
00404 
00415     /*
00416     protected function _addField($sName, $sStatus, $sType = null, $sLength = null)
00417     {
00418         if ($this->_blEmployMultilanguage && $this->_getFieldLang($sName))
00419             return;
00420 
00421         return parent::_addField($sName, $sStatus, $sType, $sLength);
00422     }*/
00423 }