BxAclModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 PaidLevels Paid Levels
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. define('BX_ACL_LICENSE_TYPE_SINGLE', 'single'); //--- one-time payment license
  13. define('BX_ACL_LICENSE_TYPE_RECURRING', 'recurring'); //--- recurring payment license
  14. class BxAclModule extends BxDolModule
  15. {
  16. /**
  17. * Constructor
  18. */
  19. function __construct($aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_oConfig->init($this->_oDb);
  23. }
  24. /**
  25. * ACTION METHODS
  26. */
  27. /**
  28. * SERVICE METHODS
  29. */
  30. /**
  31. * @page service Service Calls
  32. * @section bx_acl Paid Levels
  33. * @subsection bx_acl-page_blocks Page Blocks
  34. * @subsubsection bx_acl-get_block_view get_block_view
  35. *
  36. * @code bx_srv('bx_acl', 'get_block_view', [...]); @endcode
  37. *
  38. * Get page block with a list of available ACL levels to purchase or get for Free. List is represented as table.
  39. *
  40. * @return HTML string with a list of ACL levels to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. On error empty string is returned.
  41. *
  42. * @see BxAclModule::serviceGetBlockView
  43. */
  44. /**
  45. * @ref bx_acl-get_block_view "get_block_view"
  46. */
  47. public function serviceGetBlockView()
  48. {
  49. $sGrid = $this->_oConfig->getGridObject('view');
  50. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  51. if(!$oGrid)
  52. return '';
  53. $this->_oTemplate->addCss(array('view.css'));
  54. return array(
  55. 'content' => $oGrid->getCode()
  56. );
  57. }
  58. /**
  59. * @page service Service Calls
  60. * @section bx_acl Paid Levels
  61. * @subsection bx_acl-page_blocks Page Blocks
  62. * @subsubsection bx_acl-get_membership_actions get_membership_actions
  63. *
  64. * @code bx_srv('bx_acl', 'get_membership_actions', [...]); @endcode
  65. *
  66. * Get page block with a list of current membership level's actions.
  67. *
  68. * @return HTML string with a list of actions to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. On error empty string is returned.
  69. *
  70. * @see BxAclModule::serviceGetMembershipActions
  71. */
  72. /**
  73. * @ref bx_acl-get_membership_actions "get_membership_actions"
  74. */
  75. public function serviceGetMembershipActions($iProfileId)
  76. {
  77. if($iProfileId != $this->getUserId())
  78. return '';
  79. return $this->_oTemplate->displayMembershipActions($iProfileId);
  80. }
  81. /**
  82. * @page service Service Calls
  83. * @section bx_acl Paid Levels
  84. * @subsection bx_acl-payments Payments
  85. * @subsubsection bx_acl-get_payment_data get_payment_data
  86. *
  87. * @code bx_srv('bx_acl', 'get_payment_data', [...]); @endcode
  88. *
  89. * Get an array with module's description. Is needed for payments processing module.
  90. *
  91. * @return an array with module's description.
  92. *
  93. * @see BxAclModule::serviceGetPaymentData
  94. */
  95. /**
  96. * @ref bx_acl-get_payment_data "get_payment_data"
  97. */
  98. public function serviceGetPaymentData()
  99. {
  100. return $this->_aModule;
  101. }
  102. /**
  103. * @page service Service Calls
  104. * @section bx_acl Paid Levels
  105. * @subsection bx_acl-payments Payments
  106. * @subsubsection bx_acl-get_cart_item get_cart_item
  107. *
  108. * @code bx_srv('bx_acl', 'get_cart_item', [...]); @endcode
  109. *
  110. * Get an array with level's description. Is used in Shopping Cart in payments processing module.
  111. *
  112. * @param $iItemId level's ID.
  113. * @return an array with level's description. Empty array is returned if something is wrong.
  114. *
  115. * @see BxAclModule::serviceGetCartItem
  116. */
  117. /**
  118. * @ref bx_acl-get_cart_item "get_cart_item"
  119. */
  120. public function serviceGetCartItem($iItemId)
  121. {
  122. $CNF = &$this->_oConfig->CNF;
  123. if(!$iItemId)
  124. return array();
  125. $aItem = $this->_oDb->getPrices(array('type' => 'by_id_full', 'value' => $iItemId));
  126. if(empty($aItem) || !is_array($aItem))
  127. return array();
  128. return array (
  129. 'id' => $aItem['id'],
  130. 'author_id' => $this->_oConfig->getOwner(),
  131. 'name' => $aItem['name'],
  132. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  133. 'description' => _t($aItem['level_description']),
  134. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']),
  135. 'price_single' => $aItem['price'],
  136. 'price_recurring' => $aItem['price'],
  137. 'period_recurring' => $aItem['period'],
  138. 'period_unit_recurring' => $aItem['period_unit'],
  139. 'trial_recurring' => $aItem['trial']
  140. );
  141. }
  142. /**
  143. * @page service Service Calls
  144. * @section bx_acl Paid Levels
  145. * @subsection bx_acl-payments Payments
  146. * @subsubsection bx_acl-get_cart_items get_cart_items
  147. *
  148. * @code bx_srv('bx_acl', 'get_cart_items', [...]); @endcode
  149. *
  150. * Get an array with levels' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  151. *
  152. * @param $iSellerId seller ID.
  153. * @return an array with levels' descriptions. Empty array is returned if something is wrong or seller doesn't have any paid level.
  154. *
  155. * @see BxAclModule::serviceGetCartItems
  156. */
  157. /**
  158. * @ref bx_acl-get_cart_items "get_cart_items"
  159. */
  160. public function serviceGetCartItems($iSellerId)
  161. {
  162. $CNF = &$this->_oConfig->CNF;
  163. $iSellerIdSetting = $this->_oConfig->getOwner();
  164. if(empty($iSellerId) || ($iSellerId != $iSellerIdSetting && !isAdmin()))
  165. return array();
  166. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  167. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']);
  168. $aResult = array();
  169. foreach($aItems as $aItem)
  170. $aResult[] = array(
  171. 'id' => $aItem['id'],
  172. 'author_id' => $iSellerIdSetting,
  173. 'name' => $aItem['name'],
  174. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  175. 'description' => _t($aItem['level_description']),
  176. 'url' => $sUrl,
  177. 'price_single' => $aItem['price'],
  178. 'price_recurring' => $aItem['price'],
  179. 'period_recurring' => $aItem['period'],
  180. 'period_unit_recurring' => $aItem['period_unit'],
  181. 'trial_recurring' => $aItem['trial']
  182. );
  183. return $aResult;
  184. }
  185. /**
  186. * @page service Service Calls
  187. * @section bx_acl Paid Levels
  188. * @subsection bx_acl-payments Payments
  189. * @subsubsection bx_acl-register_cart_item register_cart_item
  190. *
  191. * @code bx_srv('bx_acl', 'register_cart_item', [...]); @endcode
  192. *
  193. * Register a processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was registered there.
  194. *
  195. * @param $iClientId client ID.
  196. * @param $iSellerId seller ID
  197. * @param $iItemId item ID.
  198. * @param $iItemCount the number of purchased items.
  199. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  200. * @param $sLicense license number genereted with payment processing module for internal usage
  201. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  202. *
  203. * @see BxAclModule::serviceRegisterCartItem
  204. */
  205. /**
  206. * @ref bx_acl-register_cart_item "register_cart_item"
  207. */
  208. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  209. {
  210. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  211. }
  212. /**
  213. * @page service Service Calls
  214. * @section bx_acl Paid Levels
  215. * @subsection bx_acl-payments Payments
  216. * @subsubsection bx_acl-register_subscription_item register_subscription_item
  217. *
  218. * @code bx_srv('bx_acl', 'register_subscription_item', [...]); @endcode
  219. *
  220. * Register a processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was registered there.
  221. *
  222. * @param $iClientId client ID.
  223. * @param $iSellerId seller ID
  224. * @param $iItemId item ID.
  225. * @param $iItemCount the number of purchased items.
  226. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  227. * @param $sLicense license number genereted with payment processing module for internal usage
  228. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  229. *
  230. * @see BxAclModule::serviceRegisterSubscriptionItem
  231. */
  232. /**
  233. * @ref bx_acl-register_subscription_item "register_subscription_item"
  234. */
  235. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  236. {
  237. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  238. }
  239. /**
  240. * @page service Service Calls
  241. * @section bx_acl Paid Levels
  242. * @subsection bx_acl-payments Payments
  243. * @subsubsection bx_acl-unregister_cart_item unregister_cart_item
  244. *
  245. * @code bx_srv('bx_acl', 'unregister_cart_item', [...]); @endcode
  246. *
  247. * Unregister an earlier processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was unregistered there.
  248. *
  249. * @param $iClientId client ID.
  250. * @param $iSellerId seller ID
  251. * @param $iItemId item ID.
  252. * @param $iItemCount the number of purchased items.
  253. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  254. * @param $sLicense license number genereted with payment processing module for internal usage
  255. * @return boolean value determining where the payment was unregistered or not.
  256. *
  257. * @see BxAclModule::serviceUnregisterCartItem
  258. */
  259. /**
  260. * @ref bx_acl-unregister_cart_item "unregister_cart_item"
  261. */
  262. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  263. {
  264. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  265. }
  266. /**
  267. * @page service Service Calls
  268. * @section bx_acl Paid Levels
  269. * @subsection bx_acl-payments Payments
  270. * @subsubsection bx_acl-unregister_subscription_item unregister_subscription_item
  271. *
  272. * @code bx_srv('bx_acl', 'unregister_subscription_item', [...]); @endcode
  273. *
  274. * Unregister an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was unregistered there.
  275. *
  276. * @param $iClientId client ID.
  277. * @param $iSellerId seller ID
  278. * @param $iItemId item ID.
  279. * @param $iItemCount the number of purchased items.
  280. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  281. * @param $sLicense license number genereted with payment processing module for internal usage
  282. * @return boolean value determining where the subscription was unregistered or not.
  283. *
  284. * @see BxAclModule::serviceUnregisterSubscriptionItem
  285. */
  286. /**
  287. * @ref bx_acl-unregister_subscription_item "unregister_subscription_item"
  288. */
  289. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  290. {
  291. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  292. }
  293. /**
  294. * @page service Service Calls
  295. * @section bx_acl Paid Levels
  296. * @subsection bx_acl-payments Payments
  297. * @subsubsection bx_acl-cancel_subscription_item cancel_subscription_item
  298. *
  299. * @code bx_srv('bx_acl', 'cancel_subscription_item', [...]); @endcode
  300. *
  301. * Cancel an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was canceled there.
  302. *
  303. * @param $iClientId client ID.
  304. * @param $iSellerId seller ID
  305. * @param $iItemId item ID.
  306. * @param $iItemCount the number of purchased items.
  307. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  308. * @return boolean value determining where the subscription was canceled or not.
  309. *
  310. * @see BxAclModule::serviceCancelSubscriptionItem
  311. */
  312. /**
  313. * @ref bx_acl-cancel_subscription_item "cancel_subscription_item"
  314. */
  315. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  316. {
  317. //TODO: Do something if it's necessary.
  318. return true;
  319. }
  320. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  321. {
  322. $aItem = $this->serviceGetCartItem($iItemId);
  323. if(empty($aItem) || !is_array($aItem))
  324. return array();
  325. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  326. $iPeriod = (int)$aItemInfo['period'];
  327. $sPeriodUnit = $aItemInfo['period_unit'];
  328. if($sType == BX_ACL_LICENSE_TYPE_RECURRING && (int)$aItemInfo['trial'] > 0) {
  329. $iPeriod = (int)$aItemInfo['trial'];
  330. $sPeriodUnit = 'day';
  331. }
  332. if(!BxDolAcl::getInstance()->setMembership($iClientId, $aItemInfo['level_id'], array('period' => $iPeriod, 'period_unit' => $sPeriodUnit), false, $sLicense))
  333. return array();
  334. return $aItem;
  335. }
  336. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  337. {
  338. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  339. if(empty($aItemInfo) || !is_array($aItemInfo))
  340. return false;
  341. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  342. }
  343. /**
  344. * COMMON METHODS
  345. */
  346. public function getUserId()
  347. {
  348. return isLogged() ? bx_get_logged_profile_id() : 0;
  349. }
  350. public function getUserInfo($iUserId = 0)
  351. {
  352. $oProfile = BxDolProfile::getInstance($iUserId);
  353. if (!$oProfile)
  354. $oProfile = BxDolProfileUndefined::getInstance();
  355. return array(
  356. $oProfile->getDisplayName(),
  357. $oProfile->getUrl(),
  358. $oProfile->getThumb(),
  359. $oProfile->getUnit()
  360. );
  361. }
  362. }
  363. /** @} */