BxDolStudioCart.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioCart extends BxDolFactory implements iBxDolSingleton
  10. {
  11. public static $sIDiv = ':';
  12. public static $sPDiv = '_';
  13. private $sSessionKey = 'bx-std-str-cart';
  14. private $bAllowAccumulate = false;
  15. protected function __construct()
  16. {
  17. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  18. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  19. parent::__construct ();
  20. }
  21. public function __clone()
  22. {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. }
  26. public static function getInstance()
  27. {
  28. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  29. $sClass = __CLASS__;
  30. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  31. }
  32. return $GLOBALS['bxDolClasses'][__CLASS__];
  33. }
  34. /**
  35. * Conver items to array with necessary structure.
  36. *
  37. * @param string/array $mixed - string with cart items divided with (:) or an array of cart items.
  38. * @return array with items.
  39. */
  40. public static function items2array($mixed)
  41. {
  42. $aResult = array();
  43. if(empty($mixed))
  44. return $aResult;
  45. $sClass = __CLASS__;
  46. if(is_string($mixed))
  47. $aItems = explode($sClass::$sIDiv, $mixed);
  48. else if(is_array($mixed))
  49. $aItems = $mixed;
  50. else
  51. $aItems = array();
  52. foreach($aItems as $sItem) {
  53. $aItem = explode($sClass::$sPDiv, $sItem);
  54. $aResult[] = array('vendor' => $aItem[0], 'item_id' => $aItem[1], 'item_count' => $aItem[2]);
  55. }
  56. return $aResult;
  57. }
  58. public function exists($iVendor, $iItemId)
  59. {
  60. $sDiv = BxDolStudioCart::$sPDiv;
  61. $sCartItems = $this->getItems();
  62. return strpos($sCartItems, $iVendor . $sDiv . $iItemId . $sDiv) !== false;
  63. }
  64. public function add($iVendor, $iItemId, $iItemCount)
  65. {
  66. $sDiv = BxDolStudioCart::$sPDiv;
  67. $sCartItem = $iVendor . $sDiv . $iItemId . $sDiv . $iItemCount;
  68. $sCartItems = $this->getItems();
  69. if(strpos($sCartItems, $iVendor . $sDiv . $iItemId . $sDiv) !== false) {
  70. if($this->bAllowAccumulate)
  71. $sCartItems = preg_replace("'" . $iVendor . $sDiv . $iItemId . $sDiv . "([0-9])+'e", "'" . $iVendor . $sDiv . $iItemId . $sDiv ."' . (\\1 + " . $iItemCount . ")", $sCartItems);
  72. } else
  73. $sCartItems = empty($sCartItems) ? $sCartItem : $sCartItems . BxDolStudioCart::$sIDiv . $sCartItem;
  74. $this->setItems($sCartItems);
  75. }
  76. public function delete($iVendor, $iItemId = 0)
  77. {
  78. $sPattern = "'" . $iVendor . (!empty($iItemId) ? "_" . $iItemId : "_[0-9]+") . "_[0-9]+:?'";
  79. $sCartItems = $this->getItems();
  80. $sCartItems = trim(preg_replace($sPattern, "", $sCartItems), BxDolStudioCart::$sIDiv);
  81. $this->setItems($sCartItems);
  82. }
  83. public function getCount($iVendor = '')
  84. {
  85. if($iVendor == '') {
  86. $sItems = $this->getItems();
  87. $aItems = $this->items2array($sItems);
  88. return count($aItems);
  89. }
  90. $aVendors = $this->parseByVendor();
  91. if(!isset($aVendors[$iVendor]))
  92. return 0;
  93. return count($aVendors[$iVendor]);
  94. }
  95. public function getByVendor($iVendor)
  96. {
  97. $aVendors = $this->parseByVendor();
  98. if(!isset($aVendors[$iVendor]) || empty($aVendors[$iVendor]))
  99. return array();
  100. return $aVendors[$iVendor];
  101. }
  102. public function parseByVendor()
  103. {
  104. $sItems = $this->getItems();
  105. return $this->parseBy($this->items2array($sItems), 'vendor');
  106. }
  107. protected function getItems()
  108. {
  109. return BxDolSession::getInstance()->getValue($this->sSessionKey);
  110. }
  111. protected function setItems($sItems)
  112. {
  113. BxDolSession::getInstance()->setValue($this->sSessionKey, $sItems);
  114. }
  115. protected function parseBy($aItems, $sKey)
  116. {
  117. $aResult = array();
  118. foreach($aItems as $aItem)
  119. if(isset($aItem[$sKey]))
  120. $aResult[$aItem[$sKey]][] = $aItem;
  121. return $aResult;
  122. }
  123. }
  124. /** @} */