BxAclModule.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 $mixedItemId level's ID or Unique Name.
  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($mixedItemId)
  152. {
  153. $CNF = &$this->_oConfig->CNF;
  154. if(!$mixedItemId)
  155. return array();
  156. if(is_numeric($mixedItemId))
  157. $aItem = $this->_oDb->getPrices(array('type' => 'by_id_full', 'value' => (int)$mixedItemId));
  158. else
  159. $aItem = $this->_oDb->getPrices(array('type' => 'by_name_full', 'value' => $mixedItemId));
  160. if(empty($aItem) || !is_array($aItem))
  161. return array();
  162. return array (
  163. 'id' => $aItem['id'],
  164. 'author_id' => $this->_oConfig->getOwner(),
  165. 'name' => $aItem['name'],
  166. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  167. 'description' => _t($aItem['level_description']),
  168. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']),
  169. 'price_single' => $aItem['price'],
  170. 'price_recurring' => $aItem['price'],
  171. 'period_recurring' => $aItem['period'],
  172. 'period_unit_recurring' => $aItem['period_unit'],
  173. 'trial_recurring' => $aItem['trial']
  174. );
  175. }
  176. /**
  177. * @page service Service Calls
  178. * @section bx_acl Paid Levels
  179. * @subsection bx_acl-payments Payments
  180. * @subsubsection bx_acl-get_cart_items get_cart_items
  181. *
  182. * @code bx_srv('bx_acl', 'get_cart_items', [...]); @endcode
  183. *
  184. * Get an array with levels' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  185. *
  186. * @param $iSellerId seller ID.
  187. * @return an array with levels' descriptions. Empty array is returned if something is wrong or seller doesn't have any paid level.
  188. *
  189. * @see BxAclModule::serviceGetCartItems
  190. */
  191. /**
  192. * @ref bx_acl-get_cart_items "get_cart_items"
  193. */
  194. public function serviceGetCartItems($iSellerId)
  195. {
  196. $CNF = &$this->_oConfig->CNF;
  197. $iSellerIdSetting = $this->_oConfig->getOwner();
  198. if(empty($iSellerId) || ($iSellerId != $iSellerIdSetting && !isAdmin()))
  199. return array();
  200. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  201. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']);
  202. $aResult = array();
  203. foreach($aItems as $aItem)
  204. $aResult[] = array(
  205. 'id' => $aItem['id'],
  206. 'author_id' => $iSellerIdSetting,
  207. 'name' => $aItem['name'],
  208. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  209. 'description' => _t($aItem['level_description']),
  210. 'url' => $sUrl,
  211. 'price_single' => $aItem['price'],
  212. 'price_recurring' => $aItem['price'],
  213. 'period_recurring' => $aItem['period'],
  214. 'period_unit_recurring' => $aItem['period_unit'],
  215. 'trial_recurring' => $aItem['trial']
  216. );
  217. return $aResult;
  218. }
  219. /**
  220. * @page service Service Calls
  221. * @section bx_acl Paid Levels
  222. * @subsection bx_acl-payments Payments
  223. * @subsubsection bx_acl-register_cart_item register_cart_item
  224. *
  225. * @code bx_srv('bx_acl', 'register_cart_item', [...]); @endcode
  226. *
  227. * Register a processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was registered there.
  228. *
  229. * @param $iClientId client ID.
  230. * @param $iSellerId seller ID
  231. * @param $iItemId item ID.
  232. * @param $iItemCount the number of purchased items.
  233. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  234. * @param $sLicense license number genereted with payment processing module for internal usage
  235. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  236. *
  237. * @see BxAclModule::serviceRegisterCartItem
  238. */
  239. /**
  240. * @ref bx_acl-register_cart_item "register_cart_item"
  241. */
  242. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  243. {
  244. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  245. }
  246. /**
  247. * @page service Service Calls
  248. * @section bx_acl Paid Levels
  249. * @subsection bx_acl-payments Payments
  250. * @subsubsection bx_acl-register_subscription_item register_subscription_item
  251. *
  252. * @code bx_srv('bx_acl', 'register_subscription_item', [...]); @endcode
  253. *
  254. * Register a processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was registered there.
  255. *
  256. * @param $iClientId client ID.
  257. * @param $iSellerId seller ID
  258. * @param $iItemId item ID.
  259. * @param $iItemCount the number of purchased items.
  260. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  261. * @param $sLicense license number genereted with payment processing module for internal usage
  262. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  263. *
  264. * @see BxAclModule::serviceRegisterSubscriptionItem
  265. */
  266. /**
  267. * @ref bx_acl-register_subscription_item "register_subscription_item"
  268. */
  269. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  270. {
  271. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  272. }
  273. /**
  274. * @page service Service Calls
  275. * @section bx_acl Paid Levels
  276. * @subsection bx_acl-payments Payments
  277. * @subsubsection bx_acl-reregister_cart_item reregister_cart_item
  278. *
  279. * @code bx_srv('bx_acl', 'reregister_cart_item', [...]); @endcode
  280. *
  281. * Reregister a single time payment inside the Paid Levels module. Is called with payment processing module after the payment was reregistered there.
  282. *
  283. * @param $iClientId client ID.
  284. * @param $iSellerId seller ID
  285. * @param $iItemIdOld old item ID.
  286. * @param $iItemIdNew new item ID.
  287. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  288. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  289. *
  290. * @see BxAclModule::serviceReregisterCartItem
  291. */
  292. /**
  293. * @ref bx_acl-reregister_cart_item "reregister_cart_item"
  294. */
  295. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  296. {
  297. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_SINGLE);
  298. }
  299. /**
  300. * @page service Service Calls
  301. * @section bx_acl Paid Levels
  302. * @subsection bx_acl-payments Payments
  303. * @subsubsection bx_acl-reregister_subscription_item reregister_subscription_item
  304. *
  305. * @code bx_srv('bx_acl', 'reregister_subscription_item', [...]); @endcode
  306. *
  307. * Reregister a subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was reregistered there.
  308. *
  309. * @param $iClientId client ID.
  310. * @param $iSellerId seller ID
  311. * @param $iItemIdOld old item ID.
  312. * @param $iItemIdNew new item ID.
  313. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  314. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  315. *
  316. * @see BxAclModule::serviceReregisterSubscriptionItem
  317. */
  318. /**
  319. * @ref bx_acl-reregister_subscription_item "reregister_subscription_item"
  320. */
  321. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  322. {
  323. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_RECURRING);
  324. }
  325. /**
  326. * @page service Service Calls
  327. * @section bx_acl Paid Levels
  328. * @subsection bx_acl-payments Payments
  329. * @subsubsection bx_acl-unregister_cart_item unregister_cart_item
  330. *
  331. * @code bx_srv('bx_acl', 'unregister_cart_item', [...]); @endcode
  332. *
  333. * Unregister an earlier processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was unregistered there.
  334. *
  335. * @param $iClientId client ID.
  336. * @param $iSellerId seller ID
  337. * @param $iItemId item ID.
  338. * @param $iItemCount the number of purchased items.
  339. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  340. * @param $sLicense license number genereted with payment processing module for internal usage
  341. * @return boolean value determining where the payment was unregistered or not.
  342. *
  343. * @see BxAclModule::serviceUnregisterCartItem
  344. */
  345. /**
  346. * @ref bx_acl-unregister_cart_item "unregister_cart_item"
  347. */
  348. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  349. {
  350. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  351. }
  352. /**
  353. * @page service Service Calls
  354. * @section bx_acl Paid Levels
  355. * @subsection bx_acl-payments Payments
  356. * @subsubsection bx_acl-unregister_subscription_item unregister_subscription_item
  357. *
  358. * @code bx_srv('bx_acl', 'unregister_subscription_item', [...]); @endcode
  359. *
  360. * Unregister an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was unregistered there.
  361. *
  362. * @param $iClientId client ID.
  363. * @param $iSellerId seller ID
  364. * @param $iItemId item ID.
  365. * @param $iItemCount the number of purchased items.
  366. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  367. * @param $sLicense license number genereted with payment processing module for internal usage
  368. * @return boolean value determining where the subscription was unregistered or not.
  369. *
  370. * @see BxAclModule::serviceUnregisterSubscriptionItem
  371. */
  372. /**
  373. * @ref bx_acl-unregister_subscription_item "unregister_subscription_item"
  374. */
  375. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  376. {
  377. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  378. }
  379. /**
  380. * @page service Service Calls
  381. * @section bx_acl Paid Levels
  382. * @subsection bx_acl-payments Payments
  383. * @subsubsection bx_acl-cancel_subscription_item cancel_subscription_item
  384. *
  385. * @code bx_srv('bx_acl', 'cancel_subscription_item', [...]); @endcode
  386. *
  387. * Cancel an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was canceled there.
  388. *
  389. * @param $iClientId client ID.
  390. * @param $iSellerId seller ID
  391. * @param $iItemId item ID.
  392. * @param $iItemCount the number of purchased items.
  393. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  394. * @return boolean value determining where the subscription was canceled or not.
  395. *
  396. * @see BxAclModule::serviceCancelSubscriptionItem
  397. */
  398. /**
  399. * @ref bx_acl-cancel_subscription_item "cancel_subscription_item"
  400. */
  401. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  402. {
  403. //TODO: Do something if it's necessary.
  404. return true;
  405. }
  406. /**
  407. * @page service Service Calls
  408. * @section bx_acl Paid Levels
  409. * @subsection bx_acl-other Other
  410. * @subsubsection bx_acl-get_prices get_prices
  411. *
  412. * @code bx_srv('bx_acl', 'get_prices', [...]); @endcode
  413. *
  414. * Get array of available price options for membership levels
  415. *
  416. * @param $iLevelId membership level id
  417. * @param $bFreeUnlimitedOnly get unlimited free pricing options only
  418. * @return array of levels, or empty array of there are no any pricing options avaiulable
  419. *
  420. * @see BxAclModule::serviceGetPrices
  421. */
  422. /**
  423. * @ref bx_acl-get_prices "get_prices"
  424. */
  425. public function serviceGetPrices($iLevelId = 0, $bFreeUnlimitedOnly = false)
  426. {
  427. $aParams = array(
  428. 'type' => $iLevelId ? 'by_level_id' : 'all_full',
  429. 'value' => $iLevelId,
  430. );
  431. $aPrices = $this->_oDb->getPrices($aParams, false);
  432. return array_filter($aPrices, function ($r) use ($bFreeUnlimitedOnly) {
  433. return $bFreeUnlimitedOnly ? !$r['price'] && !$r['period'] : true;
  434. });
  435. }
  436. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  437. {
  438. $CNF = &$this->_oConfig->CNF;
  439. $aItem = $this->serviceGetCartItem($iItemId);
  440. if(empty($aItem) || !is_array($aItem))
  441. return array();
  442. $oAcl = BxDolAcl::getInstance();
  443. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  444. $aMembershipInfo = $oAcl->getMemberMembershipInfo($iClientId);
  445. $aPeriod = array('period' => (int)$aItemInfo['period'], 'period_unit' => $aItemInfo['period_unit']);
  446. if($sType == BX_ACL_LICENSE_TYPE_RECURRING && (int)$aItemInfo['trial'] > 0 && (int)$aItemInfo['level_id'] != (int)$aMembershipInfo['id'])
  447. $aPeriod = array('period' => (int)$aItemInfo['trial'], 'period_unit' => 'day');
  448. $iReserve = (int)getParam($CNF['PARAM_RECURRING_RESERVE']);
  449. if(!empty($iReserve))
  450. $aPeriod['period_reserve'] = $iReserve;
  451. if(!$oAcl->setMembership($iClientId, $aItemInfo['level_id'], $aPeriod, true, $sLicense))
  452. return array();
  453. return $aItem;
  454. }
  455. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, $sType)
  456. {
  457. $aItemNew = $this->serviceGetCartItem($iItemIdNew);
  458. if(empty($aItemNew) || !is_array($aItemNew))
  459. return array();
  460. /*
  461. * Note. Membership level cannot be reregistered immediately.
  462. * it will be automatically changed in the end of current period.
  463. */
  464. return $aItemNew;
  465. }
  466. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  467. {
  468. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  469. if(empty($aItemInfo) || !is_array($aItemInfo))
  470. return false;
  471. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  472. }
  473. /**
  474. * COMMON METHODS
  475. */
  476. public function getUserId()
  477. {
  478. return isLogged() ? bx_get_logged_profile_id() : 0;
  479. }
  480. public function getUserInfo($iUserId = 0)
  481. {
  482. $oProfile = BxDolProfile::getInstance($iUserId);
  483. if (!$oProfile)
  484. $oProfile = BxDolProfileUndefined::getInstance();
  485. return array(
  486. $oProfile->getDisplayName(),
  487. $oProfile->getUrl(),
  488. $oProfile->getThumb(),
  489. $oProfile->getUnit()
  490. );
  491. }
  492. }
  493. /** @} */