BxBaseModPaymentCart.php 10 KB

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