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

Generated on Tue Sep 29 16:45:12 2009 for OXID eShop CE by  doxygen 1.5.5