BxAclModule.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 BxBaseModGeneralModule
  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. public function actionCheckName()
  28. {
  29. $CNF = &$this->_oConfig->CNF;
  30. $sName = bx_process_input(bx_get('name'));
  31. if(empty($sName))
  32. return echoJson(array());
  33. $sResult = '';
  34. $iId = (int)bx_get('id');
  35. if(!empty($iId)) {
  36. $aPrice = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  37. if(strcmp($sName, $aPrice[$CNF['FIELD_NAME']]) == 0)
  38. $sResult = $sName;
  39. }
  40. echoJson(array(
  41. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getPriceName($sName)
  42. ));
  43. }
  44. public function actionGetActionsInPopup($iLevelId)
  45. {
  46. return echoJson([
  47. 'code' => 0,
  48. 'popup' => [
  49. 'html' => $this->_oTemplate->displayActionsPopup($iLevelId),
  50. 'options' => ['closeOnOuterClick' => true, 'removeOnClose' => true]
  51. ],
  52. ]);
  53. }
  54. public function actionGetActions($iLevelId)
  55. {
  56. if(($iGetStart = bx_get('start')) !== false)
  57. $iStart = (int)$iGetStart;
  58. if(($iGetPerPage = bx_get('per_page')) !== false)
  59. $iLimit = (int)$iGetPerPage;
  60. return echoJson([
  61. 'code' => 0,
  62. 'level_id' => $iLevelId,
  63. 'content' => $this->_oTemplate->displayActions($iLevelId, $iStart, $iLimit),
  64. 'eval' => $this->_oConfig->getJsObject('main').'.onGetActions(oData)'
  65. ]);
  66. }
  67. /**
  68. * SERVICE METHODS
  69. */
  70. public function serviceGetSafeServices()
  71. {
  72. return array (
  73. 'GetViewUrl' => '',
  74. 'GetBlockView' => '',
  75. 'GetMembershipActions' => '',
  76. );
  77. }
  78. /**
  79. * @page service Service Calls
  80. * @section bx_acl Paid Levels
  81. * @subsection bx_acl-other Other
  82. * @subsubsection bx_acl-get_view_url get_view_url
  83. *
  84. * @code bx_srv('bx_acl', 'get_view_url', [...]); @endcode
  85. *
  86. * Get page URL with membership levels list.
  87. *
  88. * @return string with page URL.
  89. *
  90. * @see BxAclModule::serviceGetViewUrl
  91. */
  92. /**
  93. * @ref bx_acl-get_view_url "get_view_url"
  94. */
  95. public function serviceGetViewUrl()
  96. {
  97. $CNF = &$this->_oConfig->CNF;
  98. return bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']));
  99. }
  100. /**
  101. * @page service Service Calls
  102. * @section bx_acl Paid Levels
  103. * @subsection bx_acl-page_blocks Page Blocks
  104. * @subsubsection bx_acl-get_block_view get_block_view
  105. *
  106. * @code bx_srv('bx_acl', 'get_block_view', [...]); @endcode
  107. *
  108. * Get page block with a list of available ACL levels to purchase or get for Free. List is represented as table.
  109. *
  110. * @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.
  111. *
  112. * @see BxAclModule::serviceGetBlockView
  113. */
  114. /**
  115. * @ref bx_acl-get_block_view "get_block_view"
  116. */
  117. public function serviceGetBlockView()
  118. {
  119. if(!$this->_oConfig->isShowToUnauthenticated())
  120. bx_require_authentication(false, false, $this->serviceGetViewUrl());
  121. $sGrid = $this->_oConfig->getGridObject('view');
  122. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  123. if(!$oGrid)
  124. return $this->_bIsApi ? [] : '';
  125. if($this->_bIsApi)
  126. return [
  127. bx_api_get_block('pricing', array_merge_recursive($oGrid->getCodeAPI(true), ['settings' => ['unit' => 'cards']]))
  128. ];
  129. $this->_oTemplate->addCss(['view.css']);
  130. $this->_oTemplate->addJs(['main.js']);
  131. return [
  132. 'content' => $this->_oTemplate->getJsCode('main') . $oGrid->getCode()
  133. ];
  134. }
  135. public function serviceGetBlockActions($iLevelId, $iStart = 0, $iLimit = 0)
  136. {
  137. if(($iGetStart = bx_get('start')) !== false)
  138. $iStart = (int)$iGetStart;
  139. if(($iGetPerPage = bx_get('per_page')) !== false)
  140. $iLimit = (int)$iGetPerPage;
  141. $mixedResult = $this->_oTemplate->displayActionsBlock($iLevelId, $iStart, $iLimit);
  142. return !$this->_bIsApi ? $mixedResult : [
  143. bx_api_get_block('acl_level_actions', $mixedResult)
  144. ];
  145. }
  146. /**
  147. * @page service Service Calls
  148. * @section bx_acl Paid Levels
  149. * @subsection bx_acl-page_blocks Page Blocks
  150. * @subsubsection bx_acl-get_membership_actions get_membership_actions
  151. *
  152. * @code bx_srv('bx_acl', 'get_membership_actions', [...]); @endcode
  153. *
  154. * Get page block with a list of current membership level's actions.
  155. *
  156. * @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.
  157. *
  158. * @see BxAclModule::serviceGetMembershipActions
  159. */
  160. /**
  161. * @ref bx_acl-get_membership_actions "get_membership_actions"
  162. */
  163. public function serviceGetMembershipActions($iProfileId)
  164. {
  165. if($iProfileId != $this->getUserId())
  166. return '';
  167. return $this->_oTemplate->displayMembershipActions($iProfileId);
  168. }
  169. /**
  170. * @page service Service Calls
  171. * @section bx_acl Paid Levels
  172. * @subsection bx_acl-payments Payments
  173. * @subsubsection bx_acl-get_payment_data get_payment_data
  174. *
  175. * @code bx_srv('bx_acl', 'get_payment_data', [...]); @endcode
  176. *
  177. * Get an array with module's description. Is needed for payments processing module.
  178. *
  179. * @return an array with module's description.
  180. *
  181. * @see BxAclModule::serviceGetPaymentData
  182. */
  183. /**
  184. * @ref bx_acl-get_payment_data "get_payment_data"
  185. */
  186. public function serviceGetPaymentData()
  187. {
  188. return $this->_aModule;
  189. }
  190. /**
  191. * @page service Service Calls
  192. * @section bx_acl Paid Levels
  193. * @subsection bx_acl-payments Payments
  194. * @subsubsection bx_acl-get_cart_item get_cart_item
  195. *
  196. * @code bx_srv('bx_acl', 'get_cart_item', [...]); @endcode
  197. *
  198. * Get an array with level's description. Is used in Shopping Cart in payments processing module.
  199. *
  200. * @param $mixedItemId level's ID or Unique Name.
  201. * @return an array with level's description. Empty array is returned if something is wrong.
  202. *
  203. * @see BxAclModule::serviceGetCartItem
  204. */
  205. /**
  206. * @ref bx_acl-get_cart_item "get_cart_item"
  207. */
  208. public function serviceGetCartItem($mixedItemId)
  209. {
  210. $CNF = &$this->_oConfig->CNF;
  211. if(!$mixedItemId)
  212. return array();
  213. if(is_numeric($mixedItemId))
  214. $aItem = $this->_oDb->getPrices(array('type' => 'by_id_full', 'value' => (int)$mixedItemId));
  215. else
  216. $aItem = $this->_oDb->getPrices(array('type' => 'by_name_full', 'value' => $mixedItemId));
  217. if(empty($aItem) || !is_array($aItem))
  218. return array();
  219. $sTitle = '';
  220. if((int)$aItem['period'] == 0)
  221. $sTitle = _t('_bx_acl_txt_cart_item_title_lifetime', _t($aItem['level_name']));
  222. else
  223. $sTitle = _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']);
  224. $sDescription = _t($aItem['level_description']);
  225. if(empty($sDescription))
  226. $sDescription = _t('_bx_acl_txt_cart_item_description', getParam('site_title'));
  227. return array (
  228. 'id' => $aItem['id'],
  229. 'author_id' => $this->_oConfig->getOwner(),
  230. 'name' => $aItem['name'],
  231. 'title' => $sTitle,
  232. 'description' => $sDescription,
  233. 'url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW'])),
  234. 'price_single' => $aItem['price'],
  235. 'price_recurring' => $aItem['price'],
  236. 'period_recurring' => $aItem['period'],
  237. 'period_unit_recurring' => $aItem['period_unit'],
  238. 'trial_recurring' => $aItem['trial'],
  239. 'added' => $aItem['added'],
  240. );
  241. }
  242. /**
  243. * @page service Service Calls
  244. * @section bx_acl Paid Levels
  245. * @subsection bx_acl-payments Payments
  246. * @subsubsection bx_acl-get_cart_items get_cart_items
  247. *
  248. * @code bx_srv('bx_acl', 'get_cart_items', [...]); @endcode
  249. *
  250. * Get an array with levels' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  251. *
  252. * @param $iSellerId seller ID.
  253. * @return an array with levels' descriptions. Empty array is returned if something is wrong or seller doesn't have any paid level.
  254. *
  255. * @see BxAclModule::serviceGetCartItems
  256. */
  257. /**
  258. * @ref bx_acl-get_cart_items "get_cart_items"
  259. */
  260. public function serviceGetCartItems($iSellerId)
  261. {
  262. $CNF = &$this->_oConfig->CNF;
  263. $iSellerIdSetting = $this->_oConfig->getOwner();
  264. if(empty($iSellerId) || ($iSellerId != $iSellerIdSetting && !isAdmin()))
  265. return array();
  266. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  267. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']));
  268. $aResult = array();
  269. foreach($aItems as $aItem) {
  270. $sTitle = '';
  271. if((int)$aItem['period'] == 0)
  272. $sTitle = _t('_bx_acl_txt_cart_item_title_lifetime', _t($aItem['level_name']));
  273. else
  274. $sTitle = _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']);
  275. $sDescription = _t($aItem['level_description']);
  276. if(empty($sDescription))
  277. $sDescription = _t('_bx_acl_txt_cart_item_description', getParam('site_title'));
  278. $aResult[] = array(
  279. 'id' => $aItem['id'],
  280. 'author_id' => $iSellerIdSetting,
  281. 'name' => $aItem['name'],
  282. 'title' => $sTitle,
  283. 'description' => $sDescription,
  284. 'url' => $sUrl,
  285. 'price_single' => $aItem['price'],
  286. 'price_recurring' => $aItem['price'],
  287. 'period_recurring' => $aItem['period'],
  288. 'period_unit_recurring' => $aItem['period_unit'],
  289. 'trial_recurring' => $aItem['trial']
  290. );
  291. }
  292. return $aResult;
  293. }
  294. /**
  295. * @page service Service Calls
  296. * @section bx_acl Paid Levels
  297. * @subsection bx_acl-payments Payments
  298. * @subsubsection bx_acl-register_cart_item register_cart_item
  299. *
  300. * @code bx_srv('bx_acl', 'register_cart_item', [...]); @endcode
  301. *
  302. * Register a processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was registered there.
  303. *
  304. * @param $iClientId client ID.
  305. * @param $iSellerId seller ID
  306. * @param $iItemId item ID.
  307. * @param $iItemCount the number of purchased items.
  308. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  309. * @param $sLicense license number genereted with payment processing module for internal usage
  310. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  311. *
  312. * @see BxAclModule::serviceRegisterCartItem
  313. */
  314. /**
  315. * @ref bx_acl-register_cart_item "register_cart_item"
  316. */
  317. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  318. {
  319. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  320. }
  321. /**
  322. * @page service Service Calls
  323. * @section bx_acl Paid Levels
  324. * @subsection bx_acl-payments Payments
  325. * @subsubsection bx_acl-register_subscription_item register_subscription_item
  326. *
  327. * @code bx_srv('bx_acl', 'register_subscription_item', [...]); @endcode
  328. *
  329. * Register a processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was registered there.
  330. *
  331. * @param $iClientId client ID.
  332. * @param $iSellerId seller ID
  333. * @param $iItemId item ID.
  334. * @param $iItemCount the number of purchased items.
  335. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  336. * @param $sLicense license number genereted with payment processing module for internal usage
  337. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  338. *
  339. * @see BxAclModule::serviceRegisterSubscriptionItem
  340. */
  341. /**
  342. * @ref bx_acl-register_subscription_item "register_subscription_item"
  343. */
  344. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  345. {
  346. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  347. }
  348. /**
  349. * @page service Service Calls
  350. * @section bx_acl Paid Levels
  351. * @subsection bx_acl-payments Payments
  352. * @subsubsection bx_acl-reregister_cart_item reregister_cart_item
  353. *
  354. * @code bx_srv('bx_acl', 'reregister_cart_item', [...]); @endcode
  355. *
  356. * Reregister a single time payment inside the Paid Levels module. Is called with payment processing module after the payment was reregistered there.
  357. *
  358. * @param $iClientId client ID.
  359. * @param $iSellerId seller ID
  360. * @param $iItemIdOld old item ID.
  361. * @param $iItemIdNew new item ID.
  362. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  363. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  364. *
  365. * @see BxAclModule::serviceReregisterCartItem
  366. */
  367. /**
  368. * @ref bx_acl-reregister_cart_item "reregister_cart_item"
  369. */
  370. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  371. {
  372. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_SINGLE);
  373. }
  374. /**
  375. * @page service Service Calls
  376. * @section bx_acl Paid Levels
  377. * @subsection bx_acl-payments Payments
  378. * @subsubsection bx_acl-reregister_subscription_item reregister_subscription_item
  379. *
  380. * @code bx_srv('bx_acl', 'reregister_subscription_item', [...]); @endcode
  381. *
  382. * Reregister a subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was reregistered there.
  383. *
  384. * @param $iClientId client ID.
  385. * @param $iSellerId seller ID
  386. * @param $iItemIdOld old item ID.
  387. * @param $iItemIdNew new item ID.
  388. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  389. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  390. *
  391. * @see BxAclModule::serviceReregisterSubscriptionItem
  392. */
  393. /**
  394. * @ref bx_acl-reregister_subscription_item "reregister_subscription_item"
  395. */
  396. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  397. {
  398. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, BX_ACL_LICENSE_TYPE_RECURRING);
  399. }
  400. /**
  401. * @page service Service Calls
  402. * @section bx_acl Paid Levels
  403. * @subsection bx_acl-payments Payments
  404. * @subsubsection bx_acl-unregister_cart_item unregister_cart_item
  405. *
  406. * @code bx_srv('bx_acl', 'unregister_cart_item', [...]); @endcode
  407. *
  408. * Unregister an earlier processed single time payment inside the Paid Levels module. Is called with payment processing module after the payment was unregistered there.
  409. *
  410. * @param $iClientId client ID.
  411. * @param $iSellerId seller ID
  412. * @param $iItemId item ID.
  413. * @param $iItemCount the number of purchased items.
  414. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  415. * @param $sLicense license number genereted with payment processing module for internal usage
  416. * @return boolean value determining where the payment was unregistered or not.
  417. *
  418. * @see BxAclModule::serviceUnregisterCartItem
  419. */
  420. /**
  421. * @ref bx_acl-unregister_cart_item "unregister_cart_item"
  422. */
  423. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  424. {
  425. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  426. }
  427. /**
  428. * @page service Service Calls
  429. * @section bx_acl Paid Levels
  430. * @subsection bx_acl-payments Payments
  431. * @subsubsection bx_acl-unregister_subscription_item unregister_subscription_item
  432. *
  433. * @code bx_srv('bx_acl', 'unregister_subscription_item', [...]); @endcode
  434. *
  435. * Unregister an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was unregistered there.
  436. *
  437. * @param $iClientId client ID.
  438. * @param $iSellerId seller ID
  439. * @param $iItemId item ID.
  440. * @param $iItemCount the number of purchased items.
  441. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  442. * @param $sLicense license number genereted with payment processing module for internal usage
  443. * @return boolean value determining where the subscription was unregistered or not.
  444. *
  445. * @see BxAclModule::serviceUnregisterSubscriptionItem
  446. */
  447. /**
  448. * @ref bx_acl-unregister_subscription_item "unregister_subscription_item"
  449. */
  450. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  451. {
  452. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  453. }
  454. /**
  455. * @page service Service Calls
  456. * @section bx_acl Paid Levels
  457. * @subsection bx_acl-payments Payments
  458. * @subsubsection bx_acl-cancel_subscription_item cancel_subscription_item
  459. *
  460. * @code bx_srv('bx_acl', 'cancel_subscription_item', [...]); @endcode
  461. *
  462. * Cancel an earlier processed subscription (recurring payment) inside the Paid Levels module. Is called with payment processing module after the subscription was canceled there.
  463. *
  464. * @param $iClientId client ID.
  465. * @param $iSellerId seller ID
  466. * @param $iItemId item ID.
  467. * @param $iItemCount the number of purchased items.
  468. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  469. * @return boolean value determining where the subscription was canceled or not.
  470. *
  471. * @see BxAclModule::serviceCancelSubscriptionItem
  472. */
  473. /**
  474. * @ref bx_acl-cancel_subscription_item "cancel_subscription_item"
  475. */
  476. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  477. {
  478. //TODO: Do something if it's necessary.
  479. return true;
  480. }
  481. /**
  482. * @page service Service Calls
  483. * @section bx_acl Paid Levels
  484. * @subsection bx_acl-other Other
  485. * @subsubsection bx_acl-get_prices get_prices
  486. *
  487. * @code bx_srv('bx_acl', 'get_prices', [...]); @endcode
  488. *
  489. * Get array of available price options for membership levels
  490. *
  491. * @param $iLevelId membership level id
  492. * @param $bFreeUnlimitedOnly get unlimited free pricing options only
  493. * @return array of levels, or empty array of there are no any pricing options avaiulable
  494. *
  495. * @see BxAclModule::serviceGetPrices
  496. */
  497. /**
  498. * @ref bx_acl-get_prices "get_prices"
  499. */
  500. public function serviceGetPrices($iLevelId = 0, $bFreeUnlimitedOnly = false)
  501. {
  502. $aParams = array(
  503. 'type' => $iLevelId ? 'by_level_id' : 'all_full',
  504. 'value' => $iLevelId,
  505. );
  506. $aPrices = $this->_oDb->getPrices($aParams, false);
  507. return array_filter($aPrices, function ($r) use ($bFreeUnlimitedOnly) {
  508. return $bFreeUnlimitedOnly ? !$r['price'] && !$r['period'] : true;
  509. });
  510. }
  511. /**
  512. * @page service Service Calls
  513. * @section bx_acl Paid Levels
  514. * @subsection bx_acl-other Other
  515. * @subsubsection bx_acl-get_products_names get_products_names
  516. *
  517. * @code bx_srv('bx_acl', 'get_products_names', [...]); @endcode
  518. *
  519. * Get an array of products names.
  520. *
  521. * @param $iVendorId return products of 1 vendor only
  522. * @param $iLimit limit the result artay
  523. * @return an array of products names
  524. *
  525. * @see BxAclModule::serviceGetProductsNames
  526. */
  527. /**
  528. * @ref bx_acl-get_products_names "get_products_names"
  529. */
  530. public function serviceGetProductsNames($iVendorId = 0, $iLimit = 1000)
  531. {
  532. return $this->_oDb->getProductsNames($iVendorId, $iLimit);
  533. }
  534. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  535. {
  536. $CNF = &$this->_oConfig->CNF;
  537. $aItem = $this->serviceGetCartItem($iItemId);
  538. if(empty($aItem) || !is_array($aItem))
  539. return [];
  540. $oAcl = BxDolAcl::getInstance();
  541. $aItemInfo = $this->_oDb->getPrices(['type' => 'by_id', 'value' => $iItemId]);
  542. $aMembershipInfo = $oAcl->getMemberMembershipInfo($iClientId);
  543. $aPeriod = ['period' => (int)$aItemInfo['period'], 'period_unit' => $aItemInfo['period_unit']];
  544. if($sType == BX_ACL_LICENSE_TYPE_RECURRING && (int)$aItemInfo['trial'] > 0 && (int)$aItemInfo['level_id'] != (int)$aMembershipInfo['id'])
  545. $aPeriod = ['period' => (int)$aItemInfo['trial'], 'period_unit' => 'day', 'period_trial' => true];
  546. $iReserve = (int)getParam($CNF['PARAM_RECURRING_RESERVE']);
  547. if(!empty($iReserve))
  548. $aPeriod['period_reserve'] = $iReserve;
  549. $bImmediate = (int)$aItemInfo['immediate'] != 0;
  550. if(!$oAcl->setMembership($iClientId, $aItemInfo['level_id'], $aPeriod, $bImmediate, $sLicense))
  551. return [];
  552. $this->_oDb->insertLicense([
  553. 'profile_id' => $iClientId,
  554. 'price_id' => $iItemId,
  555. 'type' => $sType,
  556. 'order' => $sOrder,
  557. 'license' => $sLicense,
  558. 'added' => time()
  559. ]);
  560. if($bImmediate && !empty($aMembershipInfo['transaction_id'])) {
  561. $oPayments = BxDolPayments::getInstance();
  562. $sLicenseOld = $aMembershipInfo['transaction_id'];
  563. $aLicenseOld = $this->_oDb->getLicenses(['type' => 'by_license', 'license' => $sLicenseOld]);
  564. $sOrderOld = '';
  565. if(empty($aLicenseOld) || !is_array($aLicenseOld)) {
  566. $aOrders = $oPayments->getOrdersInfo(['license' => $sLicenseOld]);
  567. if(!empty($aOrders) && is_array($aOrders)) {
  568. $aOrder = reset($aOrders);
  569. $aPendings = $oPayments->getPendingOrdersInfo(['id' => $aOrder['pending_id']]);
  570. if(!empty($aPendings) && is_array($aPendings)) {
  571. $aPending = reset($aPendings);
  572. if($aPending['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
  573. $sOrderOld = $aPending['order'];
  574. }
  575. }
  576. }
  577. else if($aLicenseOld['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
  578. $sOrderOld = $aLicenseOld['order'];
  579. if(!empty($sOrderOld) && $sOrderOld != $sOrder && (($aResult = $oPayments->cancelSubscription($sOrderOld)) === false || (int)$aResult['code'] != 0)) {
  580. sendMailTemplate($CNF['ETEMPLATE_SBS_CANCEL_REQUIRED'], 0, $iClientId, [
  581. 'level' => _t($aMembershipInfo['name']),
  582. 'order' => $sOrderOld,
  583. 'date_starts' => $this->_oConfig->formatDate($aMembershipInfo['date_starts']),
  584. 'date_expires' => $this->_oConfig->formatDate($aMembershipInfo['date_expires']),
  585. ], BX_EMAIL_NOTIFY, true);
  586. }
  587. }
  588. return $aItem;
  589. }
  590. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder, $sType)
  591. {
  592. $aItemNew = $this->serviceGetCartItem($iItemIdNew);
  593. if(empty($aItemNew) || !is_array($aItemNew))
  594. return array();
  595. /*
  596. * Note. Membership level cannot be reregistered immediately.
  597. * it will be automatically changed in the end of current period.
  598. */
  599. return $aItemNew;
  600. }
  601. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  602. {
  603. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  604. if(empty($aItemInfo) || !is_array($aItemInfo))
  605. return false;
  606. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  607. }
  608. /**
  609. * COMMON METHODS
  610. */
  611. public function getUserId()
  612. {
  613. return isLogged() ? bx_get_logged_profile_id() : 0;
  614. }
  615. public function getUserInfo($iUserId = 0)
  616. {
  617. $oProfile = BxDolProfile::getInstance($iUserId);
  618. if (!$oProfile)
  619. $oProfile = BxDolProfileUndefined::getInstance();
  620. return array(
  621. $oProfile->getDisplayName(),
  622. $oProfile->getUrl(),
  623. $oProfile->getThumb(),
  624. $oProfile->getUnit()
  625. );
  626. }
  627. }
  628. /** @} */