1
0

BxBaseModPaymentCart.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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)
  158. {
  159. return $this->_oModule->_oConfig->descriptorA2S(array($iVendorId, $iModuleId, $iItemId, $iItemCount));
  160. }
  161. /**
  162. * @page service Service Calls
  163. * @section bx_base_payment Base Payment
  164. * @subsection bx_base_payment-integration Integration
  165. * @subsubsection bx_base_payment-get_cart_items_count get_cart_items_count
  166. *
  167. * @code bx_srv('bx_payment', 'get_cart_items_count', [...], 'Cart'); @endcode
  168. *
  169. * Get items count in member's shopping cart.
  170. *
  171. * @param $iUserId (optional) integer value with user ID. If empty value is provided then currently logged in user will be used.
  172. * @return integer value with items count.
  173. *
  174. * @see BxBaseModPaymentCart::serviceGetCartItemsCount
  175. */
  176. /**
  177. * @ref bx_base_payment-get_cart_items_count "get_cart_items_count"
  178. */
  179. public function serviceGetCartItemsCount($iUserId = 0)
  180. {
  181. $iUserId = !empty($iUserId) ? $iUserId : $this->_oModule->getProfileId();
  182. if(empty($iUserId))
  183. return 0;
  184. $aInfo = $this->getInfo(BX_PAYMENT_TYPE_SINGLE, $iUserId);
  185. $iCount = 0;
  186. foreach($aInfo as $iVendorId => $aVendorCart)
  187. $iCount += $aVendorCart['items_count'];
  188. return $iCount;
  189. }
  190. /**
  191. * @page service Service Calls
  192. * @section bx_base_payment Base Payment
  193. * @subsection bx_base_payment-integration Integration
  194. * @subsubsection bx_base_payment-get_cart_items get_cart_items
  195. *
  196. * @code bx_srv('bx_payment', 'get_cart_items', [...], 'Cart'); @endcode
  197. *
  198. * Get array with items shopping cart.
  199. *
  200. * @param $iSellerId integer value with seller ID.
  201. * @param $iModuleId integer value with module ID determining a module from which the action was initiated.
  202. * @param $iUserId (optional) integer value with user ID. If empty value is provided then currently logged in user will be used.
  203. * @return integer value with items count.
  204. *
  205. * @see BxBaseModPaymentCart::serviceGetCartItems
  206. */
  207. /**
  208. * @ref bx_base_payment-get_cart_items "get_cart_items"
  209. */
  210. public function serviceGetCartItems($iSellerId, $iModuleId, $iUserId = 0)
  211. {
  212. $iUserId = !empty($iUserId) ? $iUserId : $this->_oModule->getProfileId();
  213. if(empty($iUserId))
  214. return 0;
  215. $aContent = $this->_parseByVendor($iUserId);
  216. return $aContent[$iSellerId];
  217. }
  218. protected function _parseByVendor($iUserId)
  219. {
  220. $aCart = $this->_oModule->_oDb->getCartContent($iUserId);
  221. if(empty($aCart['items']))
  222. return array();
  223. $aCartItems = $this->_oModule->_oConfig->descriptorsM2A($aCart['items']);
  224. if($this->_bSingleSeller)
  225. $aResult = array($this->_iSingleSeller => $aCartItems);
  226. else
  227. $aResult = $this->_reparseBy($aCartItems, 'vendor_id');
  228. if(empty($aCart['customs']))
  229. return $aResult;
  230. $aCartCustoms = unserialize($aCart['customs']);
  231. foreach($aResult as $iVendor => $aItems)
  232. foreach($aItems as $iIndex => $aItem)
  233. $aResult[$iVendor][$iIndex]['custom'] = $this->_oModule->_oConfig->pullCustom($aItem, $aCartCustoms);
  234. return $aResult;
  235. }
  236. protected function _parseByModule($iUserId)
  237. {
  238. $aCart = $this->_oModule->_oDb->getCartContent($iUserId);
  239. if(empty($aCart['items']))
  240. return array();
  241. $aResult = $this->_reparseBy($this->_oModule->_oConfig->descriptorsM2A($aCart['items']), 'module_id');
  242. if(empty($aCart['customs']))
  243. return $aResult;
  244. $aCartCustoms = unserialize($aCart['customs']);
  245. foreach($aResult as $iModule => $aItems)
  246. foreach($aItems as $iIndex => $aItem)
  247. $aResult[$iModule][$iIndex]['custom'] = $this->_oModule->_oConfig->pullCustom($aItem, $aCartCustoms);
  248. return $aResult;
  249. }
  250. protected function _reparseBy($aItems, $sKey)
  251. {
  252. $aResult = array();
  253. foreach($aItems as $aItem)
  254. if(isset($aItem[$sKey]))
  255. $aResult[$aItem[$sKey]][] = $aItem;
  256. return $aResult;
  257. }
  258. }
  259. /** @} */