BxDolStudioCart.php 4.1 KB

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