BxMarketModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 Market Market
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_MARKET_LICENSE_TYPE_SINGLE', 'single'); //--- one-time payment license
  12. define('BX_MARKET_LICENSE_TYPE_RECURRING', 'recurring'); //--- recurring payment license
  13. define('BX_MARKET_FILE_TYPE_VERSION', 'version');
  14. define('BX_MARKET_FILE_TYPE_UPDATE', 'update');
  15. /**
  16. * Market module
  17. */
  18. class BxMarketModule extends BxBaseModTextModule
  19. {
  20. function __construct(&$aModule)
  21. {
  22. parent::__construct($aModule);
  23. }
  24. public function actionCheckName()
  25. {
  26. $CNF = &$this->_oConfig->CNF;
  27. $sTitle = strtolower(bx_process_input(bx_get('title')));
  28. if(empty($sTitle))
  29. return echoJson(array());
  30. echoJson(array(
  31. 'title' => $sTitle,
  32. 'name' => uriGenerate($sTitle, $CNF['TABLE_ENTRIES'], $CNF['FIELD_NAME'])
  33. ));
  34. }
  35. public function serviceGetSearchableFields ()
  36. {
  37. $CNF = &$this->_oConfig->CNF;
  38. $aResult = parent::serviceGetSearchableFields();
  39. unset($aResult[$CNF['FIELD_PRICE_SINGLE']]);
  40. return $aResult;
  41. }
  42. public function serviceEntityCreate ()
  43. {
  44. $oPayments = BxDolPayments::getInstance();
  45. if(!$oPayments->isActive())
  46. return MsgBox(_t('_bx_market_err_no_payments'));
  47. if(!$oPayments->isAcceptingPayments($this->_iProfileId))
  48. return MsgBox(_t('_bx_market_err_not_accept_payments', $oPayments->getDetailsUrl()));
  49. $this->_oTemplate->addJs(array('entry.js'));
  50. return $this->_oTemplate->getJsCode('entry') . parent::serviceEntityCreate();
  51. }
  52. public function serviceEntityDownload ($iContentId = 0)
  53. {
  54. $CNF = &$this->_oConfig->CNF;
  55. if(!$iContentId)
  56. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  57. if(!$iContentId)
  58. return false;
  59. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  60. if(empty($aContentInfo) || !is_array($aContentInfo))
  61. return MsgBox(_t('_sys_txt_error_occured'));
  62. if($this->checkAllowedDownload($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  63. return MsgBox(_t('_bx_market_err_access_denied'));
  64. return $this->_oTemplate->entryAttachmentsByStorage($CNF['OBJECT_STORAGE_FILES'], $aContentInfo);
  65. }
  66. public function serviceEntityRating($iContentId = 0)
  67. {
  68. return $this->_serviceTemplateFunc ('entryRating', $iContentId);
  69. }
  70. public function serviceEntityAuthorEntities($iContentId = 0)
  71. {
  72. $CNF = &$this->_oConfig->CNF;
  73. $aContentInfo = $this->_getContentInfo($iContentId);
  74. if($aContentInfo === false)
  75. return false;
  76. $oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
  77. if (!$oProfile)
  78. $oProfile = BxDolProfileUndefined::getInstance();
  79. $aBlock = $this->_serviceBrowse ('author', array('author' => $aContentInfo[$CNF['FIELD_AUTHOR']], 'except' => array($iContentId), 'per_page' => 2), BX_DB_PADDING_DEF, true);
  80. $aBlock['title'] = _t('_bx_market_page_block_title_entry_author_entries', $oProfile->getDisplayName());
  81. return $aBlock;
  82. }
  83. /**
  84. * Licenses
  85. */
  86. public function serviceBlockLicenses()
  87. {
  88. $sGrid = $this->_oConfig->getGridObject('licenses');
  89. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  90. if(!$oGrid)
  91. return '';
  92. $this->_oTemplate->addJs(array('licenses.js'));
  93. return array(
  94. 'content' => $this->_oTemplate->getJsCode('licenses', array('sObjNameGrid' => $sGrid)) . $oGrid->getCode(),
  95. );
  96. }
  97. /**
  98. * Get number of unused licenses for some profile
  99. * @param $iProfileId - profile to get unused licenses for, if omitted then currently logged in profile is used
  100. * @return integer
  101. */
  102. public function serviceGetUnusedLicensesNum ($iProfileId = 0)
  103. {
  104. if(!$iProfileId)
  105. $iProfileId = bx_get_logged_profile_id();
  106. $aLicenses = $this->_oDb->getLicense(array('type' => 'unused', 'profile_id' => (int)$iProfileId));
  107. return !empty($aLicenses) && is_array($aLicenses) ? count($aLicenses) : 0;
  108. }
  109. public function serviceHasLicense ($iProfileId, $iProductId, $sDomain = '')
  110. {
  111. return $this->_oDb->hasLicense($iProfileId, $iProductId, $sDomain);
  112. }
  113. public function serviceGetLicense ($aParams)
  114. {
  115. return $this->_oDb->getLicense($aParams);
  116. }
  117. public function serviceUpdateLicense ($aSet, $aWhere)
  118. {
  119. return $this->_oDb->updateLicense($aSet, $aWhere);
  120. }
  121. public function serviceGetEntryBy($sType, $mixedValue)
  122. {
  123. return $this->_oDb->getContentInfoBy(array('type' => $sType, 'value' => $mixedValue));
  124. }
  125. public function serviceGetEntriesBy($aParams)
  126. {
  127. return $this->_oDb->getContentInfoBy($aParams);
  128. }
  129. public function serviceGetThumbnail($iPhotoId)
  130. {
  131. $CNF = &$this->_oConfig->CNF;
  132. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  133. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  134. return array(
  135. 'small' => $oImagesTranscoder ? $oImagesTranscoder->getFileUrl($iPhotoId) : '',
  136. 'big' => $oStorage ? $oStorage->getFileUrlById($iPhotoId) : ''
  137. );
  138. }
  139. public function serviceGetCover($iPhotoId)
  140. {
  141. $CNF = &$this->_oConfig->CNF;
  142. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  143. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_COVER']);
  144. return array(
  145. 'small' => $oImagesTranscoder ? $oImagesTranscoder->getFileUrl($iPhotoId) : '',
  146. 'big' => $oStorage ? $oStorage->getFileUrlById($iPhotoId) : ''
  147. );
  148. }
  149. public function serviceGetFile($iFileId)
  150. {
  151. $CNF = &$this->_oConfig->CNF;
  152. $aFile = $this->_oDb->getFile(array(
  153. 'type' => 'file_id_ext',
  154. 'file_id' => $iFileId
  155. ));
  156. if(!empty($aFile) && is_array($aFile)) {
  157. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  158. $aFile['file_url'] = $oStorage ? $oStorage->getFileUrlById($iFileId) : '';
  159. }
  160. return $aFile;
  161. }
  162. public function serviceGetUpdates($iContentId, $sVersion = '')
  163. {
  164. $CNF = &$this->_oConfig->CNF;
  165. $aFile = $this->_oDb->getFile(array(
  166. 'type' => 'content_id_and_type',
  167. 'content_id' => $iContentId,
  168. 'file_type' => 'update',
  169. 'version' => $sVersion
  170. ));
  171. if(!empty($aFile) && is_array($aFile)) {
  172. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  173. $aFile['file_url'] = $oStorage ? $oStorage->getFileUrlById($aFile['file_id']) : '';
  174. }
  175. return $aFile;
  176. }
  177. public function serviceGetScreenshots($iItemId)
  178. {
  179. $CNF = &$this->_oConfig->CNF;
  180. if(!$iItemId)
  181. return array();
  182. $aData = $this->_oDb->getContentInfoById($iItemId);
  183. $aPhotos = $this->_oDb->getPhoto(array('type' => 'content_id', 'content_id' => $aData[$CNF['FIELD_ID']], 'except' => array($aData[$CNF['FIELD_THUMB']], $aData[$CNF['FIELD_COVER']])));
  184. if(empty($aPhotos) || !is_array($aPhotos))
  185. return array();
  186. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  187. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_SCREENSHOT']);
  188. $aResult = array();
  189. foreach($aPhotos as $aPhoto)
  190. $aResult[] = array(
  191. 'id' => $aPhoto['file_id'],
  192. 'url_sm' => $oImagesTranscoder ? $oImagesTranscoder->getFileUrl($aPhoto['file_id']) : '',
  193. 'url_bg' => $oStorage ? $oStorage->getFileUrlById($aPhoto['file_id']) : ''
  194. );
  195. return $aResult;
  196. }
  197. /**
  198. * Integration with Payment based modules.
  199. */
  200. public function serviceGetPaymentData()
  201. {
  202. return $this->_aModule;
  203. }
  204. public function serviceGetCartItem($iItemId)
  205. {
  206. $CNF = &$this->_oConfig->CNF;
  207. if(!$iItemId)
  208. return array();
  209. $aItem = $this->_oDb->getContentInfoById($iItemId);
  210. if(empty($aItem) || !is_array($aItem))
  211. return array();
  212. return array (
  213. 'id' => $aItem[$CNF['FIELD_ID']],
  214. 'author_id' => $aItem[$CNF['FIELD_AUTHOR']],
  215. 'name' => $aItem[$CNF['FIELD_NAME']],
  216. 'title' => $aItem[$CNF['FIELD_TITLE']],
  217. 'description' => $aItem[$CNF['FIELD_TEXT']],
  218. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-product&id=' . $aItem[$CNF['FIELD_ID']]),
  219. 'price_single' => $aItem[$CNF['FIELD_PRICE_SINGLE']],
  220. 'price_recurring' => $aItem[$CNF['FIELD_PRICE_RECURRING']],
  221. 'trial_recurring' => $aItem[$CNF['FIELD_TRIAL_RECURRING']]
  222. );
  223. }
  224. public function serviceGetCartItems($iSellerId)
  225. {
  226. $CNF = &$this->_oConfig->CNF;
  227. $iSellerId = (int)$iSellerId;
  228. if(empty($iSellerId))
  229. return array();
  230. $aItems = $this->_oDb->getEntriesByAuthor($iSellerId);
  231. $aResult = array();
  232. foreach($aItems as $aItem)
  233. $aResult[] = array(
  234. 'id' => $aItem[$CNF['FIELD_ID']],
  235. 'author_id' => $aItem[$CNF['FIELD_AUTHOR']],
  236. 'name' => $aItem[$CNF['FIELD_NAME']],
  237. 'title' => $aItem[$CNF['FIELD_TITLE']],
  238. 'description' => $aItem[$CNF['FIELD_TEXT']],
  239. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-product&id=' . $aItem[$CNF['FIELD_ID']]),
  240. 'price_single' => $aItem[$CNF['FIELD_PRICE_SINGLE']],
  241. 'price_recurring' => $aItem[$CNF['FIELD_PRICE_RECURRING']],
  242. 'trial_recurring' => $aItem[$CNF['FIELD_TRIAL_RECURRING']]
  243. );
  244. return $aResult;
  245. }
  246. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  247. {
  248. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_SINGLE);
  249. }
  250. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  251. {
  252. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_RECURRING);
  253. }
  254. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  255. {
  256. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_SINGLE);
  257. }
  258. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  259. {
  260. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_RECURRING);
  261. }
  262. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  263. {
  264. //TODO: Do something if it's necessary.
  265. return true;
  266. }
  267. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  268. {
  269. $aItem = $this->serviceGetCartItem($iItemId);
  270. if(empty($aItem) || !is_array($aItem))
  271. return array();
  272. $iTrial = 0;
  273. $sDuration = '';
  274. if($sType == BX_MARKET_LICENSE_TYPE_RECURRING) {
  275. $aProduct = $this->_oDb->getContentInfoById($iItemId);
  276. $iTrial = $aProduct[$this->_oConfig->CNF['FIELD_TRIAL_RECURRING']];
  277. $sDuration = $aProduct[$this->_oConfig->CNF['FIELD_DURATION_RECURRING']];
  278. }
  279. $sAction = $this->_oDb->hasLicenseByOrder($iClientId, $iItemId, $sOrder) ? 'prolong' : 'register';
  280. if(!$this->_oDb->{$sAction . 'License'}($iClientId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType, $sDuration, $iTrial))
  281. return array();
  282. bx_alert($this->getName(), 'license_' . $sAction, 0, false, array(
  283. 'product_id' => $iItemId,
  284. 'profile_id' => $iClientId,
  285. 'order' => $sOrder,
  286. 'license' => $sLicense,
  287. 'type' => $sType,
  288. 'count' => $iItemCount,
  289. 'duration' => $sDuration,
  290. 'trial' => $iTrial
  291. ));
  292. return $aItem;
  293. }
  294. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  295. {
  296. if(!$this->_oDb->unregisterLicense($iClientId, $iItemId, $sOrder, $sLicense, $sType))
  297. return false;
  298. bx_alert($this->getName(), 'license_unregister', 0, false, array(
  299. 'product_id' => $iItemId,
  300. 'profile_id' => $iClientId,
  301. 'order' => $sOrder,
  302. 'license' => $sLicense,
  303. 'type' => $sType,
  304. 'count' => $iItemCount
  305. ));
  306. return true;
  307. }
  308. public function getGhostTemplateVars($aFile, $iProfileId, $iContentId, $oStorage, $oImagesTranscoder)
  309. {
  310. $CNF = &$this->_oConfig->CNF;
  311. $sMethod = '';
  312. $sStorage = $oStorage->getObject();
  313. switch($sStorage) {
  314. case $CNF['OBJECT_STORAGE']:
  315. $sMethod = 'getPhoto';
  316. break;
  317. case $CNF['OBJECT_STORAGE_FILES']:
  318. $sMethod = 'getFile';
  319. break;
  320. }
  321. $aFileInfo = $this->_oDb->$sMethod(array('type' => 'file_id', 'file_id' => $aFile['id']));
  322. $bFileInfo = !empty($aFileInfo) && is_array($aFileInfo);
  323. $bFileInfoTitle = $bFileInfo && isset($aFileInfo['title']);
  324. $aTmplVars = array(
  325. 'file_title' => $bFileInfoTitle ? $aFileInfo['title'] : '',
  326. 'file_title_attr' => $bFileInfoTitle ? bx_html_attribute($aFileInfo['title']) : '',
  327. );
  328. if($sStorage == $CNF['OBJECT_STORAGE'])
  329. return $aTmplVars;
  330. $bFileInfoVersion = $bFileInfo && isset($aFileInfo['version']);
  331. $bFileInfoTypeVersion = !$bFileInfo || ($bFileInfo && isset($aFileInfo['type']) && $aFileInfo['type'] == BX_MARKET_FILE_TYPE_VERSION);
  332. $bFileInfoTypeUpdate = $bFileInfo && isset($aFileInfo['type']) && $aFileInfo['type'] == BX_MARKET_FILE_TYPE_UPDATE;
  333. $aVersions = $this->_oDb->$sMethod(array('type' => 'content_id', 'content_id' => $iContentId));
  334. $aTmplVars = array_merge($aTmplVars, array(
  335. 'file_version' => $bFileInfoVersion ? $aFileInfo['version'] : '',
  336. 'file_version_attr' => $bFileInfoVersion ? bx_html_attribute($aFileInfo['version']) : '',
  337. 'file_type_version_selected' => $bFileInfoTypeVersion ? ' selected="selected"' : '',
  338. 'file_type_update_selected' => $bFileInfoTypeUpdate ? ' selected="selected"' : '',
  339. 'file_type_version_elements' => !$bFileInfoTypeVersion ? ' style="display:none;"' : '',
  340. 'file_type_update_elements' => !$bFileInfoTypeUpdate ? ' style="display:none;"' : '',
  341. 'file_version_from_options' => $this->_oTemplate->getGhostTemplateFileOptions('version', $aFileInfo, $aVersions),
  342. 'file_version_to_options' => $this->_oTemplate->getGhostTemplateFileOptions('version_to', $aFileInfo, $aVersions)
  343. ));
  344. return $aTmplVars;
  345. }
  346. public function checkAllowedSetCover ()
  347. {
  348. $aCheck = checkActionModule($this->_iProfileId, 'set cover', $this->getName(), false);
  349. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  350. return $aCheck[CHECK_ACTION_MESSAGE];
  351. return CHECK_ACTION_RESULT_ALLOWED;
  352. }
  353. public function checkAllowedDownload($aDataEntry, $isPerformAction = false)
  354. {
  355. $CNF = &$this->_oConfig->CNF;
  356. // moderator and owner always have access
  357. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  358. return CHECK_ACTION_RESULT_ALLOWED;
  359. // check ACL
  360. $aCheck = checkActionModule($this->_iProfileId, 'download entry', $this->getName(), $isPerformAction);
  361. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  362. return $aCheck[CHECK_ACTION_MESSAGE];
  363. if(!$this->_oDb->hasLicense($this->_iProfileId, $aDataEntry[$CNF['FIELD_ID']]))
  364. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  365. return CHECK_ACTION_RESULT_ALLOWED;
  366. }
  367. protected function _getContentInfo($iContentId = 0)
  368. {
  369. if(!$iContentId)
  370. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  371. if(!$iContentId)
  372. return false;
  373. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  374. if(!$aContentInfo)
  375. return false;
  376. return $aContentInfo;
  377. }
  378. protected function _entitySocialSharing ($iId, $aParams = array())
  379. {
  380. $CNF = &$this->_oConfig->CNF;
  381. return parent::_entitySocialSharing($iId, array_merge($aParams, array(
  382. 'object_vote' => ''
  383. )));
  384. }
  385. }
  386. /** @} */