BxDolStudioCart.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. class BxDolStudioCart extends BxDol implements iBxDolSingleton
  10. {
  11. public static $sIDiv = ':';
  12. public static $sPDiv = '_';
  13. private $sSessionKey = 'bx-std-str-cart';
  14. private $bAllowAccumulate = false;
  15. public 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 add($sVendor, $iItemId, $iItemCount)
  59. {
  60. $sDiv = BxDolStudioCart::$sPDiv;
  61. $oSession = BxDolSession::getInstance();
  62. $sCartItem = $sVendor . $sDiv . $iItemId . $sDiv . $iItemCount;
  63. $sCartItems = $this->getItems();
  64. if(strpos($sCartItems, $sVendor . $sDiv . $iItemId . $sDiv) !== false) {
  65. if($this->bAllowAccumulate)
  66. $sCartItems = preg_replace("'" . $sVendor . $sDiv . $iItemId . $sDiv . "([0-9])+'e", "'" . $sVendor . $sDiv . $iItemId . $sDiv ."' . (\\1 + " . $iItemCount . ")", $sCartItems);
  67. } else
  68. $sCartItems = empty($sCartItems) ? $sCartItem : $sCartItems . BxDolStudioCart::$sIDiv . $sCartItem;
  69. $this->setItems($sCartItems);
  70. }
  71. public function delete($sVendor, $iItemId = 0)
  72. {
  73. $sPattern = "'" . $sVendor . (!empty($iItemId) ? "_" . $iItemId : "_[0-9]+") . "_[0-9]+:?'";
  74. $sCartItems = $this->getItems();
  75. $sCartItems = trim(preg_replace($sPattern, "", $sCartItems), BxDolStudioCart::$sIDiv);
  76. $this->setItems($sCartItems);
  77. }
  78. public function getCount($sVendor = '')
  79. {
  80. if($sVendor == '') {
  81. $sItems = $this->getItems();
  82. $aItems = $this->items2array($sItems);
  83. return count($aItems);
  84. }
  85. $aVendors = $this->parseByVendor();
  86. if(!isset($aVendors[$sVendor]))
  87. return 0;
  88. return count($aVendors[$sVendor]);
  89. }
  90. public function getByVendor($sVendor)
  91. {
  92. $aVendors = $this->parseByVendor();
  93. if(!isset($aVendors[$sVendor]) || empty($aVendors[$sVendor]))
  94. return array();
  95. return $aVendors[$sVendor];
  96. }
  97. public function parseByVendor()
  98. {
  99. $sItems = $this->getItems();
  100. return $this->parseBy($this->items2array($sItems), 'vendor');
  101. }
  102. protected function getItems()
  103. {
  104. $oSession = BxDolSession::getInstance();
  105. return $oSession->getValue($this->sSessionKey);
  106. }
  107. protected function setItems($sItems)
  108. {
  109. $oSession = BxDolSession::getInstance();
  110. $oSession->setValue($this->sSessionKey, $sItems);
  111. }
  112. protected function parseBy($aItems, $sKey)
  113. {
  114. $aResult = array();
  115. foreach($aItems as $aItem)
  116. if(isset($aItem[$sKey]))
  117. $aResult[$aItem[$sKey]][] = $aItem;
  118. return $aResult;
  119. }
  120. }
  121. /** @} */