BxAclModule.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. public function serviceGetSafeServices()
  31. {
  32. return array (
  33. 'GetViewUrl' => '',
  34. 'GetBlockView' => '',
  35. 'GetMembershipActions' => '',
  36. );
  37. }
  38. /**
  39. * @page service Service Calls
  40. * @section bx_acl Paid Levels
  41. * @subsection bx_acl-other Other
  42. * @subsubsection bx_acl-get_view_url get_view_url
  43. *
  44. * @code bx_srv('bx_acl', 'get_view_url', [...]); @endcode
  45. *
  46. * Get page URL with membership levels list.
  47. *
  48. * @return string with page URL.
  49. *
  50. * @see BxAclModule::serviceGetViewUrl
  51. */
  52. /**
  53. * @ref bx_acl-get_view_url "get_view_url"
  54. */
  55. public function serviceGetViewUrl()
  56. {
  57. $CNF = &$this->_oConfig->CNF;
  58. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']);
  59. }
  60. /**
  61. * @page service Service Calls
  62. * @section bx_acl Paid Levels
  63. * @subsection bx_acl-page_blocks Page Blocks
  64. * @subsubsection bx_acl-get_block_view get_block_view
  65. *
  66. * @code bx_srv('bx_acl', 'get_block_view', [...]); @endcode
  67. *
  68. * Get page block with a list of available ACL levels to purchase or get for Free. List is represented as table.
  69. *
  70. * @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.
  71. *
  72. * @see BxAclModule::serviceGetBlockView
  73. */
  74. /**
  75. * @ref bx_acl-get_block_view "get_block_view"
  76. */
  77. public function serviceGetBlockView()
  78. {
  79. bx_require_authentication(false, false, $this->serviceGetViewUrl());
  80. $sGrid = $this->_oConfig->getGridObject('view');
  81. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  82. if(!$oGrid)
  83. return '';
  84. $this->_oTemplate->addCss(array('view.css'));
  85. return array(
  86. 'content' => $oGrid->getCode()
  87. );
  88. }
  89. /**
  90. * @page service Service Calls
  91. * @section bx_acl Paid Levels
  92. * @subsection bx_acl-page_blocks Page Blocks
  93. * @subsubsection bx_acl-get_membership_actions get_membership_actions
  94. *
  95. * @code bx_srv('bx_acl', 'get_membership_actions', [...]); @endcode
  96. *
  97. * Get page block with a list of current membership level's actions.
  98. *
  99. * @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.
  100. *
  101. * @see BxAclModule::serviceGetMembershipActions
  102. */
  103. /**
  104. * @ref bx_acl-get_membership_actions "get_membership_actions"
  105. */
  106. public function serviceGetMembershipActions($iProfileId)
  107. {
  108. if($iProfileId != $this->getUserId())
  109. return '';
  110. return $this->_oTemplate->displayMembershipActions($iProfileId);
  111. }
  112. /**
  113. * @page service Service Calls
  114. * @section bx_acl Paid Levels
  115. * @subsection bx_acl-payments Payments
  116. * @subsubsection bx_acl-get_payment_data get_payment_data
  117. *
  118. * @code bx_srv('bx_acl', 'get_payment_data', [...]); @endcode
  119. *
  120. * Get an array with module's description. Is needed for payments processing module.
  121. *
  122. * @return an array with module's description.
  123. *
  124. * @see BxAclModule::serviceGetPaymentData
  125. */
  126. /**
  127. * @ref bx_acl-get_payment_data "get_payment_data"
  128. */
  129. public function serviceGetPaymentData()
  130. {
  131. return $this->_aModule;
  132. }
  133. /**
  134. * @page service Service Calls
  135. * @section bx_acl Paid Levels
  136. * @subsection bx_acl-payments Payments
  137. * @subsubsection bx_acl-get_cart_item get_cart_item
  138. *
  139. * @code bx_srv('bx_acl', 'get_cart_item', [...]); @endcode
  140. *
  141. * Get an array with level's description. Is used in Shopping Cart in payments processing module.
  142. *
  143. * @param $iItemId level's ID.
  144. * @return an array with level's description. Empty array is returned if something is wrong.
  145. *
  146. * @see BxAclModule::serviceGetCartItem
  147. */
  148. /**
  149. * @ref bx_acl-get_cart_item "get_cart_item"
  150. */
  151. public function serviceGetCartItem($iItemId)
  152. {
  153. $CNF = &$this->_oConfig->CNF;
  154. if(!$iItemId)
  155. return array();
  156. $aItem = $this->_oDb->getPrices(array('type' => 'by_id_full', 'value' => $iItemId));
  157. if(empty($aItem) || !is_array($aItem))
  158. return array();
  159. return array (
  160. 'id' => $aItem['id'],
  161. 'author_id' => $this->_oConfig->getOwner(),
  162. 'name' => $aItem['name'],
  163. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  164. 'description' => _t($aItem['level_description']),
  165. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']),
  166. 'price_single' => $aItem['price'],
  167. 'price_recurring' => $aItem['price'],
  168. 'period_recurring' => $aItem['period'],
  169. 'period_unit_recurring' => $aItem['period_unit'],
  170. 'trial_recurring' => $aItem['trial']
  171. );
  172. }
  173. /**
  174. * @page service Service Calls
  175. * @section bx_acl Paid Levels
  176. * @subsection bx_acl-payments Payments
  177. * @subsubsection bx_acl-get_cart_items get_cart_items
  178. *
  179. * @code bx_srv('bx_acl', 'get_cart_items', [...]); @endcode
  180. *
  181. * Get an array with levels' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  182. *
  183. * @param $iSellerId seller ID.
  184. * @return an array with levels' descriptions. Empty array is returned if something is wrong or seller doesn't have any paid level.
  185. *
  186. * @see BxAclModule::serviceGetCartItems
  187. */
  188. /**
  189. * @ref bx_acl-get_cart_items "get_cart_items"
  190. */
  191. public function serviceGetCartItems($iSellerId)
  192. {
  193. $CNF = &$this->_oConfig->CNF;
  194. $iSellerIdSetting = $this->_oConfig->getOwner();
  195. if(empty($iSellerId) || ($iSellerId != $iSellerIdSetting && !isAdmin()))
  196. return array();
  197. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  198. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']);
  199. $aResult = array();
  200. foreach($aItems as $aItem)
  201. $aResult[] = array(
  202. 'id' => $aItem['id'],
  203. 'author_id' => $iSellerIdSetting,
  204. 'name' => $aItem['name'],
  205. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  206. 'description' => _t($aItem['level_description']),
  207. 'url' => $sUrl,
  208. 'price_single' => $aItem['price'],
  209. 'price_recurring' => $aItem['price'],
  210. 'period_recurring' => $aItem['period'],
  211. 'period_unit_recurring' => $aItem['period_unit'],
  212. 'trial_recurring' => $aItem['trial']
  213. );
  214. return $aResult;
  215. }
  216. /**
  217. * @page service Service Calls
  218. * @section bx_acl Paid Levels
  219. * @subsection bx_acl-payments Payments
  220. * @subsubsection bx_acl-register_cart_item register_cart_item
  221. *
  222. * @code bx_srv('bx_acl', 'register_cart_item', [...]); @endcode
  223. *
  224. * Register a processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was registered there.
  225. *
  226. * @param $iClientId client ID.
  227. * @param $iSellerId seller ID
  228. * @param $iItemId item ID.
  229. * @param $iItemCount the number of purchased items.
  230. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  231. * @param $sLicense license number genereted with payment processing module for internal usage
  232. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  233. *
  234. * @see BxAclModule::serviceRegisterCartItem
  235. */
  236. /**
  237. * @ref bx_acl-register_cart_item "register_cart_item"
  238. */
  239. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  240. {
  241. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  242. }
  243. /**
  244. * @page service Service Calls
  245. * @section bx_acl Paid Levels
  246. * @subsection bx_acl-payments Payments
  247. * @subsubsection bx_acl-register_subscription_item register_subscription_item
  248. *
  249. * @code bx_srv('bx_acl', 'register_subscription_item', [...]); @endcode
  250. *
  251. * Register a processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was registered there.
  252. *
  253. * @param $iClientId client ID.
  254. * @param $iSellerId seller ID
  255. * @param $iItemId item ID.
  256. * @param $iItemCount the number of purchased items.
  257. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  258. * @param $sLicense license number genereted with payment processing module for internal usage
  259. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  260. *
  261. * @see BxAclModule::serviceRegisterSubscriptionItem
  262. */
  263. /**
  264. * @ref bx_acl-register_subscription_item "register_subscription_item"
  265. */
  266. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  267. {
  268. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  269. }
  270. /**
  271. * @page service Service Calls
  272. * @section bx_acl Paid Levels
  273. * @subsection bx_acl-payments Payments
  274. * @subsubsection bx_acl-reregister_cart_item reregister_cart_item
  275. *
  276. * @code bx_srv('bx_acl', 'reregister_cart_item', [...]); @endcode
  277. *
  278. * Reregister a single time payment inside the Paid Levels module. Is called with payment processing module after the payment was reregistered there.
  279. *
  280. * @param $iClientId client ID.
  281. * @param $iSellerId seller ID
  282. * @param $iItemIdOld old item ID.
  283. * @param $iItemIdNew new item ID.
  284. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  285. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  286. *
  287. * @see BxAclModule::serviceReregisterCartItem
  288. */
  289. /**
  290. * @ref bx_acl-reregister_cart_item "reregister_cart_item"
  291. */
  292. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  293. {
  294. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_SINGLE);
  295. }
  296. /**
  297. * @page service Service Calls
  298. * @section bx_acl Paid Levels
  299. * @subsection bx_acl-payments Payments
  300. * @subsubsection bx_acl-reregister_subscription_item reregister_subscription_item
  301. *
  302. * @code bx_srv('bx_acl', 'reregister_subscription_item', [...]); @endcode
  303. *
  304. * Reregister a subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was reregistered there.
  305. *
  306. * @param $iClientId client ID.
  307. * @param $iSellerId seller ID
  308. * @param $iItemIdOld old item ID.
  309. * @param $iItemIdNew new item ID.
  310. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  311. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  312. *
  313. * @see BxAclModule::serviceReregisterSubscriptionItem
  314. */
  315. /**
  316. * @ref bx_acl-reregister_subscription_item "reregister_subscription_item"
  317. */
  318. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  319. {
  320. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_RECURRING);
  321. }
  322. /**
  323. * @page service Service Calls
  324. * @section bx_acl Paid Levels
  325. * @subsection bx_acl-payments Payments
  326. * @subsubsection bx_acl-unregister_cart_item unregister_cart_item
  327. *
  328. * @code bx_srv('bx_acl', 'unregister_cart_item', [...]); @endcode
  329. *
  330. * Unregister an earlier processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was unregistered there.
  331. *
  332. * @param $iClientId client ID.
  333. * @param $iSellerId seller ID
  334. * @param $iItemId item ID.
  335. * @param $iItemCount the number of purchased items.
  336. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  337. * @param $sLicense license number genereted with payment processing module for internal usage
  338. * @return boolean value determining where the payment was unregistered or not.
  339. *
  340. * @see BxAclModule::serviceUnregisterCartItem
  341. */
  342. /**
  343. * @ref bx_acl-unregister_cart_item "unregister_cart_item"
  344. */
  345. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  346. {
  347. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  348. }
  349. /**
  350. * @page service Service Calls
  351. * @section bx_acl Paid Levels
  352. * @subsection bx_acl-payments Payments
  353. * @subsubsection bx_acl-unregister_subscription_item unregister_subscription_item
  354. *
  355. * @code bx_srv('bx_acl', 'unregister_subscription_item', [...]); @endcode
  356. *
  357. * Unregister an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was unregistered there.
  358. *
  359. * @param $iClientId client ID.
  360. * @param $iSellerId seller ID
  361. * @param $iItemId item ID.
  362. * @param $iItemCount the number of purchased items.
  363. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  364. * @param $sLicense license number genereted with payment processing module for internal usage
  365. * @return boolean value determining where the subscription was unregistered or not.
  366. *
  367. * @see BxAclModule::serviceUnregisterSubscriptionItem
  368. */
  369. /**
  370. * @ref bx_acl-unregister_subscription_item "unregister_subscription_item"
  371. */
  372. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  373. {
  374. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  375. }
  376. /**
  377. * @page service Service Calls
  378. * @section bx_acl Paid Levels
  379. * @subsection bx_acl-payments Payments
  380. * @subsubsection bx_acl-cancel_subscription_item cancel_subscription_item
  381. *
  382. * @code bx_srv('bx_acl', 'cancel_subscription_item', [...]); @endcode
  383. *
  384. * Cancel an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was canceled there.
  385. *
  386. * @param $iClientId client ID.
  387. * @param $iSellerId seller ID
  388. * @param $iItemId item ID.
  389. * @param $iItemCount the number of purchased items.
  390. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  391. * @return boolean value determining where the subscription was canceled or not.
  392. *
  393. * @see BxAclModule::serviceCancelSubscriptionItem
  394. */
  395. /**
  396. * @ref bx_acl-cancel_subscription_item "cancel_subscription_item"
  397. */
  398. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  399. {
  400. //TODO: Do something if it's necessary.
  401. return true;
  402. }
  403. /**
  404. * @page service Service Calls
  405. * @section bx_acl Paid Levels
  406. * @subsection bx_acl-other Other
  407. * @subsubsection bx_acl-get_prices get_prices
  408. *
  409. * @code bx_srv('bx_acl', 'get_prices', [...]); @endcode
  410. *
  411. * Get array of available price options for membership levels
  412. *
  413. * @param $iLevelId membership level id
  414. * @param $bFreeUnlimitedOnly get unlimited free pricing options only
  415. * @return array of levels, or empty array of there are no any pricing options avaiulable
  416. *
  417. * @see BxAclModule::serviceGetPrices
  418. */
  419. /**
  420. * @ref bx_acl-get_prices "get_prices"
  421. */
  422. public function serviceGetPrices($iLevelId = 0, $bFreeUnlimitedOnly = false)
  423. {
  424. $aParams = array(
  425. 'type' => $iLevelId ? 'by_level_id' : 'all_full',
  426. 'value' => $iLevelId,
  427. );
  428. $aPrices = $this->_oDb->getPrices($aParams, false);
  429. return array_filter($aPrices, function ($r) use ($bFreeUnlimitedOnly) {
  430. return $bFreeUnlimitedOnly ? !$r['price'] && !$r['period'] : true;
  431. });
  432. }
  433. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  434. {
  435. $CNF = &$this->_oConfig->CNF;
  436. $aItem = $this->serviceGetCartItem($iItemId);
  437. if(empty($aItem) || !is_array($aItem))
  438. return array();
  439. $oAcl = BxDolAcl::getInstance();
  440. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  441. $aMembershipInfo = $oAcl->getMemberMembershipInfo($iClientId);
  442. $aPeriod = array('period' => (int)$aItemInfo['period'], 'period_unit' => $aItemInfo['period_unit']);
  443. if($sType == BX_ACL_LICENSE_TYPE_RECURRING && (int)$aItemInfo['trial'] > 0 && (int)$aItemInfo['level_id'] != (int)$aMembershipInfo['id'])
  444. $aPeriod = array('period' => (int)$aItemInfo['trial'], 'period_unit' => 'day');
  445. $iReserve = (int)getParam($CNF['PARAM_RECURRING_RESERVE']);
  446. if(!empty($iReserve))
  447. $aPeriod['period_reserve'] = $iReserve;
  448. if(!$oAcl->setMembership($iClientId, $aItemInfo['level_id'], $aPeriod, true, $sLicense))
  449. return array();
  450. return $aItem;
  451. }
  452. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, $sType)
  453. {
  454. $aItemNew = $this->serviceGetCartItem($iItemIdNew);
  455. if(empty($aItemNew) || !is_array($aItemNew))
  456. return array();
  457. /*
  458. * Note. Membership level cannot be reregistered immediately.
  459. * it will be automatically changed in the end of current period.
  460. */
  461. return $aItemNew;
  462. }
  463. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  464. {
  465. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  466. if(empty($aItemInfo) || !is_array($aItemInfo))
  467. return false;
  468. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  469. }
  470. /**
  471. * COMMON METHODS
  472. */
  473. public function getUserId()
  474. {
  475. return isLogged() ? bx_get_logged_profile_id() : 0;
  476. }
  477. public function getUserInfo($iUserId = 0)
  478. {
  479. $oProfile = BxDolProfile::getInstance($iUserId);
  480. if (!$oProfile)
  481. $oProfile = BxDolProfileUndefined::getInstance();
  482. return array(
  483. $oProfile->getDisplayName(),
  484. $oProfile->getUrl(),
  485. $oProfile->getThumb(),
  486. $oProfile->getUnit()
  487. );
  488. }
  489. }
  490. /** @} */