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 
00123                 //the default value is taking from the shop settings
00124                 if ( is_null($blIsInherited) ) {
00125                     $blIsInherited = $rs->fields[2];
00126                 }
00127                 $aShopSetBits[$this->getShopFieldSet($iOXID)][] = $this->getShopBit($iOXID);
00128             }
00129             $sShopId = $sParentID;
00130 
00131         } while ( ( $blIsInherited && $sParentID ) && $iCnt++ < $iMax );
00132 
00133         $aFieldSets = $this->_combineShopSetBits($aShopSetBits);
00134         return $aFieldSets;
00135     }
00136 
00146     public function isIncludedInShop($iShopId, $sTable, $sOXID)
00147     {
00148         $oDb      = oxDb::getDb();
00149         $sField   = $this->getShopFieldName('oxshopincl', $iShopId);
00150         $iShopBit = $this->getShopBit($iShopId);
00151         $sSelect  = "select ({$iShopBit} & {$sField}) as isIncluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00152 
00153         return (bool)  $oDb->getOne( $sSelect );
00154     }
00155 
00165     public function isExcludedFromShop($iShopId, $sTable, $sOXID)
00166     {
00167         $oDb      = oxDb::getDb();
00168         $sField   = $this->getShopFieldName('oxshopexcl', $iShopId);
00169         $iShopBit = $this->getShopBit($iShopId);
00170         $sSelect  = "select ({$iShopBit} & {$sField}) as isExcluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
00171 
00172         return (bool)  $oDb->getOne( $sSelect );
00173     }
00174 
00182     public function getShopFieldSet($iShopId)
00183     {
00184         return ceil($iShopId / self::SHOP_FIELD_SET_SIZE)-1;
00185     }
00186 
00192     public function getShopFields()
00193     {
00194         $iCount = $this->_getShopFieldCount();
00195         $aFields = array();
00196         for ($iSet=0; $iSet<$iCount; $iSet++) {
00197             $aFields[] = $this->getShopFieldSetName('oxshopincl', $iSet);
00198             $aFields[] = $this->getShopFieldSetName('oxshopexcl', $iSet);
00199         }
00200         return $aFields;
00201     }
00202 
00203 
00204 
00213     public function shopFieldSetExist( $iShopId, $sTable = 'oxarticles')
00214     {
00215         $blExists = true;
00216         $sFieldSet = $this->getShopFieldSet($iShopId);
00217         if ( $sFieldSet > 0 ) {
00218             $sFieldName = "oxshopincl".$sFieldSet;
00219             $oDbMetadata = oxNew('oxDbMetaDataHandler');
00220             $blExists = $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sFieldName, $sTable );
00221         }
00222         return $blExists;
00223     }
00224 
00232     public function addShopFieldSets( $iShopId )
00233     {
00234         set_time_limit(0);
00235         $aSql = array();
00236         $aMultiShopTables = $this->getConfig()->getConfigParam( 'aMultiShopTables' );
00237         $iFieldSet = $this->getShopFieldSet($iShopId);
00238         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00239 
00240         foreach ( $aMultiShopTables as $sTable ) {
00241             foreach ( array( "OXSHOPINCL", "OXSHOPEXCL" ) as $sField ) {
00242                 $sNewFieldName = $sField . $iFieldSet;
00243                 if ($iFieldSet > 1) {
00244                     $iPrevLang = $iFieldSet-1;
00245                     $sPrevField = $sField.$iPrevLang;
00246                 } else {
00247                     $sPrevField = $sField;
00248                 }
00249                 if ( !$oDbMetadata->fieldExists( $sNewFieldName, $sTable ) ) {
00250 
00251                     //getting add field sql
00252                     $aSql[] = $oDbMetadata->getAddFieldSql( $sTable, $sField, $sNewFieldName, $sPrevField );
00253 
00254 
00255                     //getting add index sql on added field
00256                     $aSql = array_merge($aSql, (array) $oDbMetadata->getAddFieldIndexSql($sTable, $sField, $sNewFieldName));
00257                 }
00258             }
00259         }
00260         $oDbMetadata->executeSql($aSql);
00261     }
00262 
00270     public function getShopFieldSetSuffix( $iSet )
00271     {
00272         $sSuffix = ( $iSet > 0 ) ? $iSet : '';
00273         return $sSuffix;
00274     }
00275 
00283     public function getShopFieldSuffix( $iShopId )
00284     {
00285         $iSet = $this->getShopFieldSet( $iShopId );
00286         return $this->getShopFieldSetSuffix($iSet);
00287     }
00288 
00297     public function getShopFieldSetName( $sField, $iSet )
00298     {
00299         $sSuffix = $this->getShopFieldSetSuffix($iSet);
00300         return $sField.$sSuffix;
00301     }
00302 
00311     public function getShopFieldName( $sField, $iShopId )
00312     {
00313         $iSet = $this->getShopFieldSet( $iShopId );
00314         return $this->getShopFieldSetName($sField, $iSet);
00315     }
00316 
00325     public function getSqlSetIncludeSnippet( $iShopId )
00326     {
00327         $iBit     = $this->getShopBit( $iShopId );
00328         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId);
00329         $sSnippet = "{$sField} = {$sField} | $iBit";
00330 
00331         return $sSnippet;
00332     }
00333 
00342     public function getSqlUnsetIncludeSnippet( $iShopId )
00343     {
00344         $iBit     = $this->getShopBit( $iShopId );
00345         $sField   = $this->getShopFieldName( 'oxshopincl', $iShopId );
00346         $sSnippet = "{$sField} = {$sField} & ~{$iBit}";
00347 
00348         return $sSnippet;
00349     }
00350 
00359     public function getSqlSetExcludeSnippet( $iShopId )
00360     {
00361         $iBit     = $this->getShopBit( $iShopId );
00362         $sField   = $this->getShopFieldName( 'oxshopexcl', $iShopId );
00363         $sSnippet = "{$sField} = {$sField} | $iBit";
00364 
00365         return $sSnippet;
00366     }
00367 
00376     public function getSqlUnsetExcludeSnippet( $iShopId )
00377     {
00378         $iBit     = $this->getShopBit( $iShopId );
00379         $iField   = $this->getShopFieldName( 'oxshopexcl', $iShopId);
00380         $sSnippet = "{$iField} = {$iField} & ~{$iBit}";
00381 
00382         return $sSnippet;
00383     }
00384 
00392     protected function _getShopFieldSetId($iShopId)
00393     {
00394         return 1 + (($iShopId - 1) % self::SHOP_FIELD_SET_SIZE);
00395     }
00396 
00402     protected function _getShopFieldCount()
00403     {
00404         return 1 + $this->getShopFieldSet($this->_getMaxShopId());
00405     }
00406 
00412     protected function _getMaxShopId()
00413     {
00414         return oxDb::getDb()->getOne( "select max(oxid) as maxid from oxshops" );
00415     }
00416 
00425     protected function _combineShopSetBits( $aShopSetBits )
00426     {
00427         $aFieldSets = array_fill(0, $this->_getShopFieldCount(), 0);
00428         foreach ( $aShopSetBits as $iShopSet => $aBits ) {
00429             //this works for large numbers when $sShopNr is up to (inclusive) 64
00430             $iRes = oxDb::getDb()->getOne( "select (".implode(" | ", $aBits).") as bitwiseOr" );
00431             $aFieldSets[$iShopSet] = $iRes;
00432         }
00433 
00434         //for more than 64 shop, we return array.
00435         return $aFieldSets;
00436     }
00437 }