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 
00028     public static function getInstance()
00029     {
00030         // disable caching for test modules
00031         if ( defined( 'OXID_PHP_UNIT' ) ) {
00032             self::$_instance = modInstances::getMod( __CLASS__ );
00033         }
00034 
00035         if ( !(self::$_instance instanceof oxShopMetaData) ) {
00036             self::$_instance = oxNew( 'oxShopMetaData' );
00037             if ( defined( 'OXID_PHP_UNIT' ) ) {
00038                 modInstances::addMod( __CLASS__, self::$_instance);
00039             }
00040         }
00041         return self::$_instance;
00042     }
00043 
00053     public function getShopBit( $iShopId )
00054     {
00055         // get shop field set id for shop field set
00056         $iShopId = $this->_getShopFieldSetId((int) $iShopId);
00057 
00058         //this works for large numbers when $sShopNr is up to (inclusive) 128
00059         $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00060 
00061         //as php ints supports only 32 bits, we return string.
00062         return $iRes;
00063     }
00064 
00072     public function getShopBits( $iShopId )
00073     {
00074         $aShopBits = array();
00075         $iFieldCount = $this->_getShopFieldCount();
00076         if ($iFieldCount) {
00077             // Fill array with 0 values
00078             $aShopBits = array_fill(0, $iFieldCount, 0);
00079             // Calculate shop bit for current field set
00080             $aShopBits[$this->getShopFieldSet($iShopId)] = $this->getShopBit($iShopId);
00081         }
00082         return $aShopBits;
00083     }
00084 
00090     public function getMultiShopBits()
00091     {
00092         $aShopBits = array();
00093         $iFieldCount = $this->_getShopFieldCount();
00094         if ($iFieldCount) {
00095             // Fill array with max 64bit int values
00096             $aShopBits = array_fill(0, $iFieldCount, MAX_64BIT_INTEGER);
00097         }
00098         return $aShopBits;
00099     }
00100 
00109     public function getParentShopBits( $sShopId, $blIsInherited = null )
00110     {
00111         $iCnt  = 0;
00112         $iMax  = $this->_getMaxShopId();
00113         $aShopSetBits = array();
00114         do {
00115             //collects inherited shop set bit array up to uninherited parent.
00116             $aInheritedShops = array();
00117             $sQ = 'select oxid, oxparentid, oxisinherited from oxshops where oxid = "'.$sShopId.'" ';
00118             $rs = oxDb::getDb()->select( $sQ );
00119             if ( $rs && $rs->recordCount()> 0 && $rs->fields[0] ) {
00120                 $iOXID     = $rs->fields[0];
00121                 $sParentID = $rs->fields[1];
00122                 //T2007-03-14
00123                 //taking oxparentid field not oxisherineted as in single case of oxobject2category table case when we call it
00124                 //this is exactly what we need
00125                 if ( !$blIsInherited ) {
00126                     $blIsInherited = $rs->fields[2];
00127                 }
00128                 $aShopSetBits[$this->getShopFieldSet($iOXID)][] = $this->getShopBit($iOXID);
00129             }
00130             $sShopId = $sParentID;
00131 
00132         } while ( ( $blIsInherited && $sParentID ) && $iCnt++ < $iMax );
00133 
00134         $aFieldSets = $this->_combineShopSetBits($aShopSetBits);
00135         return $aFieldSets;
00136     }
00137 
00147     public function isIncludedInShop($iShopId, $sTable, $sOXID)
00148     {
00149         $oDb      = oxDb::getDb();
00150         $sField   = $this->getShopFieldName('oxshopincl', $iShopId);
00151         $iShopBit = $this->getShopBit($iShopId);
00152         $sSelect  = "select ({$iShopBit} & {$sField}) as isIncluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00153 
00154         return (bool)  $oDb->getOne( $sSelect );
00155     }
00156 
00166     public function isExcludedFromShop($iShopId, $sTable, $sOXID)
00167     {
00168         $oDb      = oxDb::getDb();
00169         $sField   = $this->getShopFieldName('oxshopexcl', $iShopId);
00170         $iShopBit = $this->getShopBit($iShopId);
00171         $sSelect  = "select ({$iShopBit} & {$sField}) as isExcluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00172 
00173         return (bool)  $oDb->getOne( $sSelect );
00174     }
00175 
00183     public function getShopFieldSet($iShopId)
00184     {
00185         return ceil($iShopId / self::SHOP_FIELD_SET_SIZE)-1;
00186     }
00187 
00193     public function getShopFields()
00194     {
00195         $iCount = $this->_getShopFieldCount();
00196         $aFields = array();
00197         for ($iSet=0; $iSet<$iCount; $iSet++) {
00198             $aFields[] = $this->getShopFieldSetName('oxshopincl', $iSet);
00199             $aFields[] = $this->getShopFieldSetName('oxshopexcl', $iSet);
00200         }
00201         return $aFields;
00202     }
00203 
00204 
00205 
00214     public function shopFieldSetExist( $iShopId, $sTable = 'oxarticles')
00215     {
00216         $blExists = true;
00217         $sFieldSet = $this->getShopFieldSet($iShopId);
00218         if ( $sFieldSet > 0 ) {
00219             $sFieldName = "oxshopincl".$sFieldSet;
00220             $oDbMetadata = oxNew('oxDbMetaDataHandler');
00221             $blExists = $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sFieldName, $sTable );
00222         }
00223         return $blExists;
00224     }
00225 
00233     public function addShopFieldSets( $iShopId )
00234     {
00235         set_time_limit(0);
00236         $aSql = array();
00237         $aMultiShopTables = $this->getConfig()->getConfigParam( 'aMultiShopTables' );
00238         $iFieldSet = $this->getShopFieldSet($iShopId);
00239         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00240 
00241         foreach ( $aMultiShopTables as $sTable ) {
00242             foreach ( array( "OXSHOPINCL", "OXSHOPEXCL" ) as $sField ) {
00243                 $sNewFieldName = $sField . $iFieldSet;
00244                 if ($iFieldSet > 1) {
00245                     $iPrevLang = $iFieldSet-1;
00246                     $sPrevField = $sField.$iPrevLang;
00247                 } else {
00248                     $sPrevField = $sField;
00249                 }
00250                 if ( !$oDbMetadata->fieldExists( $sNewFieldName, $sTable ) ) {
00251 
00252                     //getting add field sql
00253                     $aSql[] = $oDbMetadata->getAddFieldSql( $sTable, $sField, $sNewFieldName, $sPrevField );
00254 
00255 
00256                     //getting add index sql on added field
00257                     $aSql = array_merge($aSql, (array) $oDbMetadata->getAddFieldIndexSql($sTable, $sField, $sNewFieldName));
00258                 }
00259             }
00260         }
00261         $oDbMetadata->executeSql($aSql);
00262     }
00263 
00271     public function getShopFieldSetSuffix( $iSet )
00272     {
00273         $sSuffix = ( $iSet > 0 ) ? $iSet : '';
00274         return $sSuffix;
00275     }
00276 
00284     public function getShopFieldSuffix( $iShopId )
00285     {
00286         $iSet = $this->getShopFieldSet( $iShopId );
00287         return $this->getShopFieldSetSuffix($iSet);
00288     }
00289 
00298     public function getShopFieldSetName( $sField, $iSet )
00299     {
00300         $sSuffix = $this->getShopFieldSetSuffix($iSet);
00301         return $sField.$sSuffix;
00302     }
00303 
00312     public function getShopFieldName( $sField, $iShopId )
00313     {
00314         $iSet = $this->getShopFieldSet( $iShopId );
00315         return $this->getShopFieldSetName($sField, $iSet);
00316     }
00317 
00326     public function getSqlSetIncludeSnippet( $iShopId )
00327     {
00328         $iBit     = $this->getShopBit( $iShopId );
00329         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId);
00330         $sSnippet = "{$sField} = {$sField} | $iBit";
00331 
00332         return $sSnippet;
00333     }
00334 
00343     public function getSqlUnsetIncludeSnippet( $iShopId )
00344     {
00345         $iBit     = $this->getShopBit( $iShopId );
00346         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId );
00347         $sSnippet = "{$sField} = {$sField} & ~{$iBit}";
00348 
00349         return $sSnippet;
00350     }
00351 
00360     public function getSqlSetExcludeSnippet( $iShopId )
00361     {
00362         $iBit     = $this->getShopBit( $iShopId );
00363         $sField   = $this->getShopFieldName( 'oxshopexcl', $iShopId );
00364         $sSnippet = "{$sField} = {$sField} | $iBit";
00365 
00366         return $sSnippet;
00367     }
00368 
00377     public function getSqlUnsetExcludeSnippet( $iShopId )
00378     {
00379         $iBit     = $this->getShopBit( $iShopId );
00380         $iField   = $this->getShopFieldName( 'oxshopexcl', $iShopId);
00381         $sSnippet = "{$iField} = {$iField} & ~{$iBit}";
00382 
00383         return $sSnippet;
00384     }
00385 
00393     protected function _getShopFieldSetId($iShopId)
00394     {
00395         return 1 + (($iShopId - 1) % self::SHOP_FIELD_SET_SIZE);
00396     }
00397 
00403     protected function _getShopFieldCount()
00404     {
00405         return 1 + $this->getShopFieldSet($this->_getMaxShopId());
00406     }
00407 
00413     protected function _getMaxShopId()
00414     {
00415         return oxDb::getDb()->getOne( "select max(oxid) as maxid from oxshops" );
00416     }
00417 
00426     protected function _combineShopSetBits( $aShopSetBits )
00427     {
00428         $aFieldSets = array_fill(0, $this->_getShopFieldCount(), 0);
00429         foreach ( $aShopSetBits as $iShopSet => $aBits ) {
00430             //this works for large numbers when $sShopNr is up to (inclusive) 64
00431             $iRes = oxDb::getDb()->getOne( "select (".implode(" | ", $aBits).") as bitwiseOr" );
00432             $aFieldSets[$iShopSet] = $iRes;
00433         }
00434 
00435         //for more than 64 shop, we return array.
00436         return $aFieldSets;
00437     }
00438 }