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 
00114                 //the default value is taking from the shop settings
00115                 if ( is_null($blIsInherited) ) {
00116                     $blIsInherited = $rs->fields[2];
00117                 }
00118                 $aShopSetBits[$this->getShopFieldSet($iOXID)][] = $this->getShopBit($iOXID);
00119             }
00120             $sShopId = $sParentID;
00121 
00122         } while ( ( $blIsInherited && $sParentID ) && $iCnt++ < $iMax );
00123 
00124         $aFieldSets = $this->_combineShopSetBits($aShopSetBits);
00125         return $aFieldSets;
00126     }
00127 
00137     public function isIncludedInShop($iShopId, $sTable, $sOXID)
00138     {
00139         $oDb      = oxDb::getDb();
00140         $sField   = $this->getShopFieldName('oxshopincl', $iShopId);
00141         $iShopBit = $this->getShopBit($iShopId);
00142         $sSelect  = "select ({$iShopBit} & {$sField}) as isIncluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00143 
00144         return (bool)  $oDb->getOne( $sSelect );
00145     }
00146 
00156     public function isExcludedFromShop($iShopId, $sTable, $sOXID)
00157     {
00158         $oDb      = oxDb::getDb();
00159         $sField   = $this->getShopFieldName('oxshopexcl', $iShopId);
00160         $iShopBit = $this->getShopBit($iShopId);
00161         $sSelect  = "select ({$iShopBit} & {$sField}) as isExcluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00162 
00163         return (bool)  $oDb->getOne( $sSelect );
00164     }
00165 
00173     public function getShopFieldSet($iShopId)
00174     {
00175         return ceil($iShopId / self::SHOP_FIELD_SET_SIZE)-1;
00176     }
00177 
00183     public function getShopFields()
00184     {
00185         $iCount = $this->_getShopFieldCount();
00186         $aFields = array();
00187         for ($iSet=0; $iSet<$iCount; $iSet++) {
00188             $aFields[] = $this->getShopFieldSetName('oxshopincl', $iSet);
00189             $aFields[] = $this->getShopFieldSetName('oxshopexcl', $iSet);
00190         }
00191         return $aFields;
00192     }
00193 
00194 
00195 
00204     public function shopFieldSetExist( $iShopId, $sTable = 'oxarticles')
00205     {
00206         $blExists = true;
00207         $sFieldSet = $this->getShopFieldSet($iShopId);
00208         if ( $sFieldSet > 0 ) {
00209             $sFieldName = "oxshopincl".$sFieldSet;
00210             $oDbMetadata = oxNew('oxDbMetaDataHandler');
00211             $blExists = $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sFieldName, $sTable );
00212         }
00213         return $blExists;
00214     }
00215 
00223     public function addShopFieldSets( $iShopId )
00224     {
00225         set_time_limit(0);
00226         $aSql = array();
00227         $aMultiShopTables = $this->getConfig()->getConfigParam( 'aMultiShopTables' );
00228         $iFieldSet = $this->getShopFieldSet($iShopId);
00229         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00230 
00231         foreach ( $aMultiShopTables as $sTable ) {
00232             foreach ( array( "OXSHOPINCL", "OXSHOPEXCL" ) as $sField ) {
00233                 $sNewFieldName = $sField . $iFieldSet;
00234                 if ($iFieldSet > 1) {
00235                     $iPrevLang = $iFieldSet-1;
00236                     $sPrevField = $sField.$iPrevLang;
00237                 } else {
00238                     $sPrevField = $sField;
00239                 }
00240                 if ( !$oDbMetadata->fieldExists( $sNewFieldName, $sTable ) ) {
00241 
00242                     //getting add field sql
00243                     $aSql[] = $oDbMetadata->getAddFieldSql( $sTable, $sField, $sNewFieldName, $sPrevField );
00244 
00245 
00246                     //getting add index sql on added field
00247                     $aSql = array_merge($aSql, (array) $oDbMetadata->getAddFieldIndexSql($sTable, $sField, $sNewFieldName));
00248                 }
00249             }
00250         }
00251         $oDbMetadata->executeSql($aSql);
00252     }
00253 
00261     public function getShopFieldSetSuffix( $iSet )
00262     {
00263         $sSuffix = ( $iSet > 0 ) ? $iSet : '';
00264         return $sSuffix;
00265     }
00266 
00274     public function getShopFieldSuffix( $iShopId )
00275     {
00276         $iSet = $this->getShopFieldSet( $iShopId );
00277         return $this->getShopFieldSetSuffix($iSet);
00278     }
00279 
00288     public function getShopFieldSetName( $sField, $iSet )
00289     {
00290         $sSuffix = $this->getShopFieldSetSuffix($iSet);
00291         return $sField.$sSuffix;
00292     }
00293 
00302     public function getShopFieldName( $sField, $iShopId )
00303     {
00304         $iSet = $this->getShopFieldSet( $iShopId );
00305         return $this->getShopFieldSetName($sField, $iSet);
00306     }
00307 
00316     public function getSqlSetIncludeSnippet( $iShopId )
00317     {
00318         $iBit     = $this->getShopBit( $iShopId );
00319         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId);
00320         $sSnippet = "{$sField} = {$sField} | $iBit";
00321 
00322         return $sSnippet;
00323     }
00324 
00333     public function getSqlUnsetIncludeSnippet( $iShopId )
00334     {
00335         $iBit     = $this->getShopBit( $iShopId );
00336         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId );
00337         $sSnippet = "{$sField} = {$sField} & ~{$iBit}";
00338 
00339         return $sSnippet;
00340     }
00341 
00350     public function getSqlSetExcludeSnippet( $iShopId )
00351     {
00352         $iBit     = $this->getShopBit( $iShopId );
00353         $sField   = $this->getShopFieldName( 'oxshopexcl', $iShopId );
00354         $sSnippet = "{$sField} = {$sField} | $iBit";
00355 
00356         return $sSnippet;
00357     }
00358 
00367     public function getSqlUnsetExcludeSnippet( $iShopId )
00368     {
00369         $iBit     = $this->getShopBit( $iShopId );
00370         $iField   = $this->getShopFieldName( 'oxshopexcl', $iShopId);
00371         $sSnippet = "{$iField} = {$iField} & ~{$iBit}";
00372 
00373         return $sSnippet;
00374     }
00375 
00383     protected function _getShopFieldSetId($iShopId)
00384     {
00385         return 1 + (($iShopId - 1) % self::SHOP_FIELD_SET_SIZE);
00386     }
00387 
00393     protected function _getShopFieldCount()
00394     {
00395         return 1 + $this->getShopFieldSet($this->_getMaxShopId());
00396     }
00397 
00403     protected function _getMaxShopId()
00404     {
00405         return oxDb::getDb()->getOne( "select max(oxid) as maxid from oxshops" );
00406     }
00407 
00416     protected function _combineShopSetBits( $aShopSetBits )
00417     {
00418         $aFieldSets = array_fill(0, $this->_getShopFieldCount(), 0);
00419         foreach ( $aShopSetBits as $iShopSet => $aBits ) {
00420             //this works for large numbers when $sShopNr is up to (inclusive) 64
00421             $iRes = oxDb::getDb()->getOne( "select (".implode(" | ", $aBits).") as bitwiseOr" );
00422             $aFieldSets[$iShopSet] = $iRes;
00423         }
00424 
00425         //for more than 64 shop, we return array.
00426         return $aFieldSets;
00427     }
00428 }