BxAclModule.php 20 KB

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