oxshopmetadata.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxShopMetaData extends oxSuperCfg
00008 {
00014     private static $_instance = null;
00015 
00021     const SHOP_FIELD_SET_SIZE = 64;
00022 
00030     public static function getInstance()
00031     {
00032         return oxRegistry::get("oxShopMetadata");
00033     }
00034 
00044     public function getShopBit( $iShopId )
00045     {
00046         // get shop field set id for shop field set
00047         $iShopId = $this->_getShopFieldSetId((int) $iShopId);
00048 
00049         //this works for large numbers when $sShopNr is up to (inclusive) 128
00050         $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00051 
00052         //as php ints supports only 32 bits, we return string.
00053         return $iRes;
00054     }
00055 
00063     public function getShopBits( $iShopId )
00064     {
00065         $aShopBits = array();
00066         $iFieldCount = $this->_getShopFieldCount();
00067         if ($iFieldCount) {
00068             // Fill array with 0 values
00069             $aShopBits = array_fill(0, $iFieldCount, 0);
00070             // Calculate shop bit for current field set
00071             $aShopBits[$this->getShopFieldSet($iShopId)] = $this->getShopBit($iShopId);
00072         }
00073         return $aShopBits;
00074     }
00075 
00081     public function getMultiShopBits()
00082     {
00083         $aShopBits = array();
00084         $iFieldCount = $this->_getShopFieldCount();
00085         if ($iFieldCount) {
00086             // Fill array with max 64bit int values
00087             $aShopBits = array_fill(0, $iFieldCount, MAX_64BIT_INTEGER);
00088         }
00089         return $aShopBits;
00090     }
00091 
00100     public function getParentShopBits( $sShopId, $blIsInherited = null )
00101     {
00102         $iCnt  = 0;
00103         $iMax  = $this->_getMaxShopId();
00104         $aShopSetBits = array();
00105         do {
00106             //collects inherited shop set bit array up to uninherited parent.
00107             $aInheritedShops = array();
00108             $sQ = 'select oxid, oxparentid, oxisinherited from oxshops where oxid = "'.$sShopId.'" ';
00109             $rs = oxDb::getDb()->select( $sQ );
00110             if ( $rs && $rs->recordCount()> 0 && $rs->fields[0] ) {
00111                 $iOXID     = $rs->fields[0];
00112                 $sParentID = $rs->fields[1];
00113                 //T2007-03-14
00114                 //taking oxparentid field not oxisherineted as in single case of oxobject2category table case when we call it
00115                 //this is exactly what we need
00116                 if ( !$blIsInherited ) {
00117                     $blIsInherited = $rs->fields[2];
00118                 }
00119                 $aShopSetBits[$this->getShopFieldSet($iOXID)][] = $this->getShopBit($iOXID);
00120             }
00121             $sShopId = $sParentID;
00122 
00123         } while ( ( $blIsInherited && $sParentID ) && $iCnt++ < $iMax );
00124 
00125         $aFieldSets = $this->_combineShopSetBits($aShopSetBits);
00126         return $aFieldSets;
00127     }
00128 
00138     public function isIncludedInShop($iShopId, $sTable, $sOXID)
00139     {
00140         $oDb      = oxDb::getDb();
00141         $sField   = $this->getShopFieldName('oxshopincl', $iShopId);
00142         $iShopBit = $this->getShopBit($iShopId);
00143         $sSelect  = "select ({$iShopBit} & {$sField}) as isIncluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00144 
00145         return (bool)  $oDb->getOne( $sSelect );
00146     }
00147 
00157     public function isExcludedFromShop($iShopId, $sTable, $sOXID)
00158     {
00159         $oDb      = oxDb::getDb();
00160         $sField   = $this->getShopFieldName('oxshopexcl', $iShopId);
00161         $iShopBit = $this->getShopBit($iShopId);
00162         $sSelect  = "select ({$iShopBit} & {$sField}) as isExcluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00163 
00164         return (bool)  $oDb->getOne( $sSelect );
00165     }
00166 
00174     public function getShopFieldSet($iShopId)
00175     {
00176         return ceil($iShopId / self::SHOP_FIELD_SET_SIZE)-1;
00177     }
00178 
00184     public function getShopFields()
00185     {
00186         $iCount = $this->_getShopFieldCount();
00187         $aFields = array();
00188         for ($iSet=0; $iSet<$iCount; $iSet++) {
00189             $aFields[] = $this->getShopFieldSetName('oxshopincl', $iSet);
00190             $aFields[] = $this->getShopFieldSetName('oxshopexcl', $iSet);
00191         }
00192         return $aFields;
00193     }
00194 
00195 
00196 
00205     public function shopFieldSetExist( $iShopId, $sTable = 'oxarticles')
00206     {
00207         $blExists = true;
00208         $sFieldSet = $this->getShopFieldSet($iShopId);
00209         if ( $sFieldSet > 0 ) {
00210             $sFieldName = "oxshopincl".$sFieldSet;
00211             $oDbMetadata = oxNew('oxDbMetaDataHandler');
00212             $blExists = $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sFieldName, $sTable );
00213         }
00214         return $blExists;
00215     }
00216 
00224     public function addShopFieldSets( $iShopId )
00225     {
00226         set_time_limit(0);
00227         $aSql = array();
00228         $aMultiShopTables = $this->getConfig()->getConfigParam( 'aMultiShopTables' );
00229         $iFieldSet = $this->getShopFieldSet($iShopId);
00230         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00231 
00232         foreach ( $aMultiShopTables as $sTable ) {
00233             foreach ( array( "OXSHOPINCL", "OXSHOPEXCL" ) as $sField ) {
00234                 $sNewFieldName = $sField . $iFieldSet;
00235                 if ($iFieldSet > 1) {
00236                     $iPrevLang = $iFieldSet-1;
00237                     $sPrevField = $sField.$iPrevLang;
00238                 } else {
00239                     $sPrevField = $sField;
00240                 }
00241                 if ( !$oDbMetadata->fieldExists( $sNewFieldName, $sTable ) ) {
00242 
00243                     //getting add field sql
00244                     $aSql[] = $oDbMetadata->getAddFieldSql( $sTable, $sField, $sNewFieldName, $sPrevField );
00245 
00246 
00247                     //getting add index sql on added field
00248                     $aSql = array_merge($aSql, (array) $oDbMetadata->getAddFieldIndexSql($sTable, $sField, $sNewFieldName));
00249                 }
00250             }
00251         }
00252         $oDbMetadata->executeSql($aSql);
00253     }
00254 
00262     public function getShopFieldSetSuffix( $iSet )
00263     {
00264         $sSuffix = ( $iSet > 0 ) ? $iSet : '';
00265         return $sSuffix;
00266     }
00267 
00275     public function getShopFieldSuffix( $iShopId )
00276     {
00277         $iSet = $this->getShopFieldSet( $iShopId );
00278         return $this->getShopFieldSetSuffix($iSet);
00279     }
00280 
00289     public function getShopFieldSetName( $sField, $iSet )
00290     {
00291         $sSuffix = $this->getShopFieldSetSuffix($iSet);
00292         return $sField.$sSuffix;
00293     }
00294 
00303     public function getShopFieldName( $sField, $iShopId )
00304     {
00305         $iSet = $this->getShopFieldSet( $iShopId );
00306         return $this->getShopFieldSetName($sField, $iSet);
00307     }
00308 
00317     public function getSqlSetIncludeSnippet( $iShopId )
00318     {
00319         $iBit     = $this->getShopBit( $iShopId );
00320         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId);
00321         $sSnippet = "{$sField} = {$sField} | $iBit";
00322 
00323         return $sSnippet;
00324     }
00325 
00334     public function getSqlUnsetIncludeSnippet( $iShopId )
00335     {
00336         $iBit     = $this->getShopBit( $iShopId );
00337         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId );
00338         $sSnippet = "{$sField} = {$sField} & ~{$iBit}";
00339 
00340         return $sSnippet;
00341     }
00342 
00351     public function getSqlSetExcludeSnippet( $iShopId )
00352     {
00353         $iBit     = $this->getShopBit( $iShopId );
00354         $sField   = $this->getShopFieldName( 'oxshopexcl', $iShopId );
00355         $sSnippet = "{$sField} = {$sField} | $iBit";
00356 
00357         return $sSnippet;
00358     }
00359 
00368     public function getSqlUnsetExcludeSnippet( $iShopId )
00369     {
00370         $iBit     = $this->getShopBit( $iShopId );
00371         $iField   = $this->getShopFieldName( 'oxshopexcl', $iShopId);
00372         $sSnippet = "{$iField} = {$iField} & ~{$iBit}";
00373 
00374         return $sSnippet;
00375     }
00376 
00384     protected function _getShopFieldSetId($iShopId)
00385     {
00386         return 1 + (($iShopId - 1) % self::SHOP_FIELD_SET_SIZE);
00387     }
00388 
00394     protected function _getShopFieldCount()
00395     {
00396         return 1 + $this->getShopFieldSet($this->_getMaxShopId());
00397     }
00398 
00404     protected function _getMaxShopId()
00405     {
00406         return oxDb::getDb()->getOne( "select max(oxid) as maxid from oxshops" );
00407     }
00408 
00417     protected function _combineShopSetBits( $aShopSetBits )
00418     {
00419         $aFieldSets = array_fill(0, $this->_getShopFieldCount(), 0);
00420         foreach ( $aShopSetBits as $iShopSet => $aBits ) {
00421             //this works for large numbers when $sShopNr is up to (inclusive) 64
00422             $iRes = oxDb::getDb()->getOne( "select (".implode(" | ", $aBits).") as bitwiseOr" );
00423             $aFieldSets[$iShopSet] = $iRes;
00424         }
00425 
00426         //for more than 64 shop, we return array.
00427         return $aFieldSets;
00428     }
00429 }