oxi18n.php

Go to the documentation of this file.
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         // set default language
00039         $this->setLanguage();
00040 
00041         //T2008-02-22
00042         //lets try to differentiate cache keys for oxI18n and oxBase
00043         //in order not to load cached structure for the instances of oxbase classe called on same table
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                 //#63T
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         //not inited field yet
00113         //and note that this is should be called only in first call after tmp dir is empty
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         // set new lang to this object
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                 //handling multilang
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         //selecting all object multilang fields
00219         foreach ($aObjFields as $sKey => $sValue ) {
00220 
00221             //skipping oxactive field
00222             if ( preg_match('/^oxactive(_(\d{1,2}))?$/', $sKey) ) {
00223                 continue;
00224             }
00225 
00226             $iFieldLang = $this->_getFieldLang( $sKey );
00227 
00228             //checking, if field is multilanguage
00229             if ( $this->isMultilingualField($sKey) || $iFieldLang >  0 ) {
00230                 $sNewKey = preg_replace('/_(\d{1,2})$/', '', $sKey);
00231                 $aMultiLangFields[$sNewKey][] = (int) $iFieldLang;
00232             }
00233         }
00234 
00235         // if no multilanguage fields, return default languages array
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         // checks if object field data is not empty in all available languages
00246         // and formats not available in languages array
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         // has 'active' or 'active_x' field ?
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         // has 'activefrom'/'activeto' fields ?
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         //Tomas
00344         //TODO: clean this
00345         $aFields = parent::_getNonCachedFieldNames($blForceFullStructure);
00346 
00347         if (!$this->_blEmployMultilanguage) {
00348             return $aFields;
00349         }
00350 
00351         //lets do some pointer manipulation
00352         if ($aFields) {
00353             //non admin fields
00354             $aWorkingFields = &$aFields;
00355         } else {
00356             //most likely admin fields so we remove another language
00357             $aWorkingFields = &$this->_aFieldNames;
00358         }
00359 
00360         //we have an array of fields, lets remove multilanguage fields
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         // currently only multilanguage objects are SEO
00409         // if current object is managed by SEO and SEO is ON
00410         if ( $blRet && $this->_blIsSeoObject && $this->isAdmin() ) {
00411             // marks all object db entries as expired
00412             oxSeoEncoder::getInstance()->markAsExpired( $this->getId() );
00413         }
00414 
00415         return $blRet;
00416     }
00417 
00428     /*
00429     protected function _addField($sName, $sStatus, $sType = null, $sLength = null)
00430     {
00431         if ($this->_blEmployMultilanguage && $this->_getFieldLang($sName))
00432             return;
00433 
00434         return parent::_addField($sName, $sStatus, $sType, $sLength);
00435     }*/
00436 }

Generated on Tue Apr 21 15:45:44 2009 for OXID eShop CE by  doxygen 1.5.5