OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxshopmetadata.php
Go to the documentation of this file.
1 <?php
2 
8 {
14  private static $_instance = null;
15 
21  const SHOP_FIELD_SET_SIZE = 64;
22 
30  public static function getInstance()
31  {
32  return oxRegistry::get("oxShopMetadata");
33  }
34 
44  public function getShopBit( $iShopId )
45  {
46  // get shop field set id for shop field set
47  $iShopId = $this->_getShopFieldSetId((int) $iShopId);
48 
49  //this works for large numbers when $sShopNr is up to (inclusive) 128
50  $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
51 
52  //as php ints supports only 32 bits, we return string.
53  return $iRes;
54  }
55 
63  public function getShopBits( $iShopId )
64  {
65  $aShopBits = array();
66  $iFieldCount = $this->_getShopFieldCount();
67  if ($iFieldCount) {
68  // Fill array with 0 values
69  $aShopBits = array_fill(0, $iFieldCount, 0);
70  // Calculate shop bit for current field set
71  $aShopBits[$this->getShopFieldSet($iShopId)] = $this->getShopBit($iShopId);
72  }
73  return $aShopBits;
74  }
75 
81  public function getMultiShopBits()
82  {
83  $aShopBits = array();
84  $iFieldCount = $this->_getShopFieldCount();
85  if ($iFieldCount) {
86  // Fill array with max 64bit int values
87  $aShopBits = array_fill(0, $iFieldCount, MAX_64BIT_INTEGER);
88  }
89  return $aShopBits;
90  }
91 
100  public function getParentShopBits( $sShopId, $blIsInherited = null )
101  {
102  $iCnt = 0;
103  $iMax = $this->_getMaxShopId();
104  $aShopSetBits = array();
105  do {
106  //collects inherited shop set bit array up to uninherited parent.
107  $aInheritedShops = array();
108  $sQ = 'select oxid, oxparentid, oxisinherited from oxshops where oxid = "'.$sShopId.'" ';
109  $rs = oxDb::getDb()->select( $sQ );
110  if ( $rs && $rs->recordCount()> 0 && $rs->fields[0] ) {
111  $iOXID = $rs->fields[0];
112  $sParentID = $rs->fields[1];
113 
114  //the default value is taking from the shop settings
115  if ( is_null($blIsInherited) ) {
116  $blIsInherited = $rs->fields[2];
117  }
118  $aShopSetBits[$this->getShopFieldSet($iOXID)][] = $this->getShopBit($iOXID);
119  }
120  $sShopId = $sParentID;
121 
122  } while ( ( $blIsInherited && $sParentID ) && $iCnt++ < $iMax );
123 
124  $aFieldSets = $this->_combineShopSetBits($aShopSetBits);
125  return $aFieldSets;
126  }
127 
137  public function isIncludedInShop($iShopId, $sTable, $sOXID)
138  {
139  $oDb = oxDb::getDb();
140  $sField = $this->getShopFieldName('oxshopincl', $iShopId);
141  $iShopBit = $this->getShopBit($iShopId);
142  $sSelect = "select ({$iShopBit} & {$sField}) as isIncluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
143 
144  return (bool) $oDb->getOne( $sSelect );
145  }
146 
156  public function isExcludedFromShop($iShopId, $sTable, $sOXID)
157  {
158  $oDb = oxDb::getDb();
159  $sField = $this->getShopFieldName('oxshopexcl', $iShopId);
160  $iShopBit = $this->getShopBit($iShopId);
161  $sSelect = "select ({$iShopBit} & {$sField}) as isExcluded from {$sTable} where oxid = ".$oDb->quote( $sOXID );
162 
163  return (bool) $oDb->getOne( $sSelect );
164  }
165 
173  public function getShopFieldSet($iShopId)
174  {
175  return ceil($iShopId / self::SHOP_FIELD_SET_SIZE)-1;
176  }
177 
183  public function getShopFields()
184  {
185  $iCount = $this->_getShopFieldCount();
186  $aFields = array();
187  for ($iSet=0; $iSet<$iCount; $iSet++) {
188  $aFields[] = $this->getShopFieldSetName('oxshopincl', $iSet);
189  $aFields[] = $this->getShopFieldSetName('oxshopexcl', $iSet);
190  }
191  return $aFields;
192  }
193 
194 
195 
204  public function shopFieldSetExist( $iShopId, $sTable = 'oxarticles')
205  {
206  $blExists = true;
207  $sFieldSet = $this->getShopFieldSet($iShopId);
208  if ( $sFieldSet > 0 ) {
209  $sFieldName = "oxshopincl".$sFieldSet;
210  $oDbMetadata = oxNew('oxDbMetaDataHandler');
211  $blExists = $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sFieldName, $sTable );
212  }
213  return $blExists;
214  }
215 
223  public function addShopFieldSets( $iShopId )
224  {
225  set_time_limit(0);
226  $aSql = array();
227  $aMultiShopTables = $this->getConfig()->getConfigParam( 'aMultiShopTables' );
228  $iFieldSet = $this->getShopFieldSet($iShopId);
229  $oDbMetadata = oxNew('oxDbMetaDataHandler');
230 
231  foreach ( $aMultiShopTables as $sTable ) {
232  foreach ( array( "OXSHOPINCL", "OXSHOPEXCL" ) as $sField ) {
233  $sNewFieldName = $sField . $iFieldSet;
234  if ($iFieldSet > 1) {
235  $iPrevLang = $iFieldSet-1;
236  $sPrevField = $sField.$iPrevLang;
237  } else {
238  $sPrevField = $sField;
239  }
240  if ( !$oDbMetadata->fieldExists( $sNewFieldName, $sTable ) ) {
241 
242  //getting add field sql
243  $aSql[] = $oDbMetadata->getAddFieldSql( $sTable, $sField, $sNewFieldName, $sPrevField );
244 
245 
246  //getting add index sql on added field
247  $aSql = array_merge($aSql, (array) $oDbMetadata->getAddFieldIndexSql($sTable, $sField, $sNewFieldName));
248  }
249  }
250  }
251  $oDbMetadata->executeSql($aSql);
252  }
253 
261  public function getShopFieldSetSuffix( $iSet )
262  {
263  $sSuffix = ( $iSet > 0 ) ? $iSet : '';
264  return $sSuffix;
265  }
266 
274  public function getShopFieldSuffix( $iShopId )
275  {
276  $iSet = $this->getShopFieldSet( $iShopId );
277  return $this->getShopFieldSetSuffix($iSet);
278  }
279 
288  public function getShopFieldSetName( $sField, $iSet )
289  {
290  $sSuffix = $this->getShopFieldSetSuffix($iSet);
291  return $sField.$sSuffix;
292  }
293 
302  public function getShopFieldName( $sField, $iShopId )
303  {
304  $iSet = $this->getShopFieldSet( $iShopId );
305  return $this->getShopFieldSetName($sField, $iSet);
306  }
307 
316  public function getSqlSetIncludeSnippet( $iShopId )
317  {
318  $iBit = $this->getShopBit( $iShopId );
319  $sField = $this->getShopFieldName( 'oxshopincl', $iShopId);
320  $sSnippet = "{$sField} = {$sField} | $iBit";
321 
322  return $sSnippet;
323  }
324 
333  public function getSqlUnsetIncludeSnippet( $iShopId )
334  {
335  $iBit = $this->getShopBit( $iShopId );
336  $sField = $this->getShopFieldName( 'oxshopincl', $iShopId );
337  $sSnippet = "{$sField} = {$sField} & ~{$iBit}";
338 
339  return $sSnippet;
340  }
341 
350  public function getSqlSetExcludeSnippet( $iShopId )
351  {
352  $iBit = $this->getShopBit( $iShopId );
353  $sField = $this->getShopFieldName( 'oxshopexcl', $iShopId );
354  $sSnippet = "{$sField} = {$sField} | $iBit";
355 
356  return $sSnippet;
357  }
358 
367  public function getSqlUnsetExcludeSnippet( $iShopId )
368  {
369  $iBit = $this->getShopBit( $iShopId );
370  $iField = $this->getShopFieldName( 'oxshopexcl', $iShopId);
371  $sSnippet = "{$iField} = {$iField} & ~{$iBit}";
372 
373  return $sSnippet;
374  }
375 
383  protected function _getShopFieldSetId($iShopId)
384  {
385  return 1 + (($iShopId - 1) % self::SHOP_FIELD_SET_SIZE);
386  }
387 
393  protected function _getShopFieldCount()
394  {
395  return 1 + $this->getShopFieldSet($this->_getMaxShopId());
396  }
397 
403  protected function _getMaxShopId()
404  {
405  return oxDb::getDb()->getOne( "select max(oxid) as maxid from oxshops" );
406  }
407 
416  protected function _combineShopSetBits( $aShopSetBits )
417  {
418  $aFieldSets = array_fill(0, $this->_getShopFieldCount(), 0);
419  foreach ( $aShopSetBits as $iShopSet => $aBits ) {
420  //this works for large numbers when $sShopNr is up to (inclusive) 64
421  $iRes = oxDb::getDb()->getOne( "select (".implode(" | ", $aBits).") as bitwiseOr" );
422  $aFieldSets[$iShopSet] = $iRes;
423  }
424 
425  //for more than 64 shop, we return array.
426  return $aFieldSets;
427  }
428 }