BxBaseModPaymentCart.php 11 KB

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