BxBaseModPaymentCart.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 BasePayment Base classes for Payment like modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModPaymentCart extends BxDol
  12. {
  13. protected $MODULE;
  14. protected $_oModule;
  15. protected $_bSingleSeller;
  16. protected $_iSingleSeller;
  17. function __construct()
  18. {
  19. parent::__construct();
  20. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  21. $this->_bSingleSeller = $this->_oModule->_oConfig->isSingleSeller();
  22. $this->_iSingleSeller = 0;
  23. if($this->_bSingleSeller)
  24. $this->_iSingleSeller = $this->_oModule->_oConfig->getSiteAdmin();
  25. }
  26. /**
  27. * @page service Service Calls
  28. * @section bx_base_payment Base Payment
  29. * @subsection bx_base_payment-integration Integration
  30. * @subsubsection bx_base_payment-get_cart_url get_cart_url
  31. *
  32. * @code bx_srv('bx_payment', 'get_cart_url', [...], 'Cart'); @endcode
  33. *
  34. * Get shopping cart URL.
  35. *
  36. * @param $iVendor (optional) integer value with vendor ID.
  37. * @return string with shopping cart URL.
  38. *
  39. * @see BxBaseModPaymentCart::serviceGetCartUrl
  40. */
  41. /**
  42. * @ref bx_base_payment-get_cart_url "get_cart_url"
  43. */
  44. public function serviceGetCartUrl($iVendor = 0)
  45. {
  46. if(!$this->_oModule->isLogged())
  47. return '';
  48. if(!$this->_bSingleSeller && $iVendor == 0)
  49. return $this->_oModule->_oConfig->getUrl('URL_CARTS');
  50. return bx_append_url_params($this->_oModule->_oConfig->getUrl('URL_CART'), array(
  51. 'seller_id' => !$this->_bSingleSeller ? $iVendor : $this->_oModule->_oConfig->getSiteAdmin()
  52. ));
  53. }
  54. /**
  55. * @page service Service Calls
  56. * @section bx_base_payment Base Payment
  57. * @subsection bx_base_payment-integration Integration
  58. * @subsubsection bx_base_payment-get_cart_url get_cart_url
  59. *
  60. * @code bx_srv('bx_payment', 'get_cart_url', [...], 'Cart'); @endcode
  61. *
  62. * Get shopping cart URL.
  63. *
  64. * @param $iVendor (optional) integer value with vendor ID.
  65. * @return string with shopping cart URL.
  66. *
  67. * @see BxBaseModPaymentCart::serviceGetCartUrl
  68. */
  69. /**
  70. * @ref bx_base_payment-get_cart_url "get_cart_url"
  71. */
  72. public function serviceGetCartJs($sType = '', $iVendorId = 0)
  73. {
  74. return $this->_oModule->_oTemplate->displayCartJs($sType, $iVendorId);
  75. }
  76. /**
  77. * @page service Service Calls
  78. * @section bx_base_payment Base Payment
  79. * @subsection bx_base_payment-integration Integration
  80. * @subsubsection bx_base_payment-get_add_to_cart_js get_add_to_cart_js
  81. *
  82. * @code bx_srv('bx_payment', 'get_add_to_cart_js', [...], 'Cart'); @endcode
  83. *
  84. * Get JavaScript code to use in OnClick attributes.
  85. *
  86. * @param $iVendor integer value with vendor ID.
  87. * @param $mixedModuleId mixed value (ID, Name or URI) determining a module from which the action was initiated.
  88. * @param $iItemId $iItemId integer value with item ID.
  89. * @param $iItemCount integer value with a number of items for purchasing.
  90. * @param $bNeedRedirect (optional) boolean value determining whether redirect is needed after add action or not.
  91. * @param $aCustom (optional) array with custom data to attach to an item added into a cart.
  92. * @return string with JavaScript code to use in OnClick attributes of HTML elements.
  93. *
  94. * @see BxBaseModPaymentCart::serviceGetAddToCartJs
  95. */
  96. /**
  97. * @ref bx_base_payment-get_add_to_cart_js "get_add_to_cart_js"
  98. */
  99. public function serviceGetAddToCartJs($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect = false, $aCustom = array())
  100. {
  101. $iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
  102. if(empty($iModuleId))
  103. return '';
  104. return $this->_oModule->_oTemplate->displayAddToCartJs($iVendorId, $iModuleId, $iItemId, $iItemCount, $bNeedRedirect, $aCustom);
  105. }
  106. /**
  107. * @page service Service Calls
  108. * @section bx_base_payment Base Payment
  109. * @subsection bx_base_payment-integration Integration
  110. * @subsubsection bx_base_payment-get_add_to_cart_link get_add_to_cart_link
  111. *
  112. * @code bx_srv('bx_payment', 'get_add_to_cart_link', [...], 'Cart'); @endcode
  113. *
  114. * Get HTML code for "Add to Cart" link.
  115. *
  116. * @param $iVendorId integer value with vendor ID.
  117. * @param $mixedModuleId mixed value (ID, Name or URI) determining a module from which the action was initiated.
  118. * @param $iItemId $iItemId integer value with item ID.
  119. * @param $iItemCount integer value with a number of items for purchasing.
  120. * @param $bNeedRedirect (optional) boolean value determining whether redirect is needed after add action or not.
  121. * @param $aCustom (optional) array with custom data to attach to an item added into a cart.
  122. * @return HTML string with link to display on the site.
  123. *
  124. * @see BxBaseModPaymentCart::serviceGetAddToCartLink
  125. */
  126. /**
  127. * @ref bx_base_payment-get_add_to_cart_link "get_add_to_cart_link"
  128. */
  129. public function serviceGetAddToCartLink($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect = false, $aCustom = array())
  130. {
  131. $iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
  132. if(empty($iModuleId))
  133. return '';
  134. return $this->_oModule->_oTemplate->displayAddToCartLink($iVendorId, $iModuleId, $iItemId, $iItemCount, $bNeedRedirect, $aCustom);
  135. }
  136. /**
  137. * @page service Service Calls
  138. * @section bx_base_payment Base Payment
  139. * @subsection bx_base_payment-integration Integration
  140. * @subsubsection bx_base_payment-get_cart_item_descriptor get_cart_item_descriptor
  141. *
  142. * @code bx_srv('bx_payment', 'get_cart_item_descriptor', [...], 'Cart'); @endcode
  143. *
  144. * Get cart item descriptor: 1-2-3-1.
  145. *
  146. * @param $iVendorId integer value with vendor ID.
  147. * @param $iModuleId integer value with module ID determining a module from which the action was initiated.
  148. * @param $iItemId $iItemId integer value with item ID.
  149. * @param $iItemCount integer value with a number of items for purchasing.
  150. * @return string with item descriptor.
  151. *
  152. * @see BxBaseModPaymentCart::serviceGetCartItemDescriptor
  153. */
  154. /**
  155. * @ref bx_base_payment-get_cart_item_descriptor "get_cart_item_descriptor"
  156. */
  157. public function serviceGetCartItemDescriptor($iVendorId, $iModuleId, $iItemId, $iItemCount = false)
  158. {
  159. $aDescriptor = [$iVendorId, $iModuleId, $iItemId];
  160. if($iItemCount !== false)
  161. $aDescriptor[] = $iItemCount;
  162. return $this->_oModule->_oConfig->descriptorA2S($aDescriptor);
  163. }
  164. /**
  165. * @page service Service Calls
  166. * @section bx_base_payment Base Payment
  167. * @subsection bx_base_payment-integration Integration
  168. * @subsubsection bx_base_payment-get_cart_items_count get_cart_items_count
  169. *
  170. * @code bx_srv('bx_payment', 'get_cart_items_count', [...], 'Cart'); @endcode
  171. *
  172. * Get items count in member's shopping cart.
  173. *
  174. * @param $iUserId (optional) integer value with user ID. If empty value is provided then currently logged in user will be used.
  175. * @return integer value with items count.
  176. *
  177. * @see BxBaseModPaymentCart::serviceGetCartItemsCount
  178. */
  179. /**
  180. * @ref bx_base_payment-get_cart_items_count "get_cart_items_count"
  181. */
  182. public function serviceGetCartItemsCount($iUserId = 0)
  183. {
  184. $iUserId = !empty($iUserId) ? $iUserId : $this->_oModule->getProfileId();
  185. if(empty($iUserId))
  186. return 0;
  187. $aInfo = $this->getInfo(BX_PAYMENT_TYPE_SINGLE, $iUserId);
  188. $iCount = 0;
  189. foreach($aInfo as $iVendorId => $aVendorCart)
  190. $iCount += $aVendorCart['items_count'];
  191. return $iCount;
  192. }
  193. /**
  194. * @page service Service Calls
  195. * @section bx_base_payment Base Payment
  196. * @subsection bx_base_payment-integration Integration
  197. * @subsubsection bx_base_payment-get_cart_items get_cart_items
  198. *
  199. * @code bx_srv('bx_payment', 'get_cart_items', [...], 'Cart'); @endcode
  200. *
  201. * Get array with items shopping cart.
  202. *
  203. * @param $iSellerId integer value with seller ID.
  204. * @param $iModuleId integer value with module ID determining a module from which the action was initiated.
  205. * @param $iUserId (optional) integer value with user ID. If empty value is provided then currently logged in user will be used.
  206. * @return integer value with items count.
  207. *
  208. * @see BxBaseModPaymentCart::serviceGetCartItems
  209. */
  210. /**
  211. * @ref bx_base_payment-get_cart_items "get_cart_items"
  212. */
  213. public function serviceGetCartItems($iSellerId, $iModuleId, $iUserId = 0)
  214. {
  215. $iUserId = !empty($iUserId) ? $iUserId : $this->_oModule->getProfileId();
  216. if(empty($iUserId))
  217. return 0;
  218. $aContent = $this->_parseByVendor($iUserId);
  219. return isset($aContent[$iSellerId]) && is_array($aContent[$iSellerId]) ? $aContent[$iSellerId] : array();
  220. }
  221. public function getCartItems($iUserId, $iSellerId = 0)
  222. {
  223. $aContent = $this->_parseByVendor($iUserId);
  224. return $iSellerId != 0 && isset($aContent[$iSellerId]) && is_array($aContent[$iSellerId]) ? $aContent[$iSellerId] : [];
  225. }
  226. protected function _parseByVendor($iUserId)
  227. {
  228. $aCart = $this->_oModule->_oDb->getCartContent($iUserId);
  229. if(empty($aCart['items']))
  230. return array();
  231. $aCartItems = $this->_oModule->_oConfig->descriptorsM2A($aCart['items']);
  232. if($this->_bSingleSeller)
  233. $aResult = array($this->_iSingleSeller => $aCartItems);
  234. else
  235. $aResult = $this->_reparseBy($aCartItems, 'vendor_id');
  236. if(empty($aCart['customs']))
  237. return $aResult;
  238. $aCartCustoms = unserialize($aCart['customs']);
  239. foreach($aResult as $iVendor => $aItems)
  240. foreach($aItems as $iIndex => $aItem)
  241. $aResult[$iVendor][$iIndex]['custom'] = $this->_oModule->_oConfig->pullCustom($aItem, $aCartCustoms);
  242. return $aResult;
  243. }
  244. protected function _parseByModule($iUserId)
  245. {
  246. $aCart = $this->_oModule->_oDb->getCartContent($iUserId);
  247. if(empty($aCart['items']))
  248. return array();
  249. $aResult = $this->_reparseBy($this->_oModule->_oConfig->descriptorsM2A($aCart['items']), 'module_id');
  250. if(empty($aCart['customs']))
  251. return $aResult;
  252. $aCartCustoms = unserialize($aCart['customs']);
  253. foreach($aResult as $iModule => $aItems)
  254. foreach($aItems as $iIndex => $aItem)
  255. $aResult[$iModule][$iIndex]['custom'] = $this->_oModule->_oConfig->pullCustom($aItem, $aCartCustoms);
  256. return $aResult;
  257. }
  258. protected function _reparseBy($aItems, $sKey)
  259. {
  260. $aResult = array();
  261. foreach($aItems as $aItem)
  262. if(isset($aItem[$sKey]))
  263. $aResult[$aItem[$sKey]][] = $aItem;
  264. return $aResult;
  265. }
  266. }
  267. /** @} */