BxMarketModule.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Market Market
  7. * @ingroup TridentModules
  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 serviceEntityCreate ()
  36. {
  37. $oPayments = BxDolPayments::getInstance();
  38. if(!$oPayments->isAcceptingPayments($this->_iProfileId))
  39. return MsgBox(_t('_bx_market_err_not_accept_payments', $oPayments->getDetailsUrl()));
  40. $this->_oTemplate->addJs(array('entry.js'));
  41. return $this->_oTemplate->getJsCode('entry') . parent::serviceEntityCreate();
  42. }
  43. public function serviceEntityDownload ($iContentId = 0)
  44. {
  45. $CNF = &$this->_oConfig->CNF;
  46. if(!$iContentId)
  47. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  48. if(!$iContentId)
  49. return false;
  50. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  51. if(empty($aContentInfo) || !is_array($aContentInfo))
  52. return MsgBox(_t('_sys_txt_error_occured'));
  53. if($this->checkAllowedDownload($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  54. return MsgBox(_t('_bx_market_err_access_denied'));
  55. return $this->_oTemplate->entryAttachmentsByStorage($CNF['OBJECT_STORAGE_FILES'], $aContentInfo);
  56. }
  57. public function serviceEntityRating($iContentId = 0)
  58. {
  59. return $this->_serviceTemplateFunc ('entryRating', $iContentId);
  60. }
  61. public function serviceEntityAuthorEntities($iContentId = 0)
  62. {
  63. $CNF = &$this->_oConfig->CNF;
  64. $aContentInfo = $this->_getContentInfo($iContentId);
  65. if($aContentInfo === false)
  66. return false;
  67. $oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
  68. if (!$oProfile)
  69. $oProfile = BxDolProfileUndefined::getInstance();
  70. $aBlock = $this->_serviceBrowse ('author', array('author' => $aContentInfo[$CNF['FIELD_AUTHOR']], 'except' => array($iContentId), 'per_page' => 2), BX_DB_PADDING_DEF, true);
  71. $aBlock['title'] = _t('_bx_market_page_block_title_entry_author_entries', $oProfile->getDisplayName());
  72. return $aBlock;
  73. }
  74. /**
  75. * Licenses
  76. */
  77. public function serviceBlockLicenses()
  78. {
  79. $sGrid = $this->_oConfig->getGridObject('licenses');
  80. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  81. if(!$oGrid)
  82. return '';
  83. $this->_oTemplate->addJs(array('licenses.js'));
  84. return array(
  85. 'content' => $this->_oTemplate->getJsCode('licenses', array('sObjNameGrid' => $sGrid)) . $oGrid->getCode(),
  86. );
  87. }
  88. /**
  89. * Get number of unused licenses for some profile
  90. * @param $iProfileId - profile to get unused licenses for, if omitted then currently logged in profile is used
  91. * @return integer
  92. */
  93. public function serviceGetUnusedLicensesNum ($iProfileId = 0)
  94. {
  95. if(!$iProfileId)
  96. $iProfileId = bx_get_logged_profile_id();
  97. $aLicenses = $this->_oDb->getLicense(array('type' => 'unused', 'profile_id' => (int)$iProfileId));
  98. return !empty($aLicenses) && is_array($aLicenses) ? count($aLicenses) : 0;
  99. }
  100. public function serviceHasLicense ($iProfileId, $iProductId, $sDomain = '')
  101. {
  102. return $this->_oDb->hasLicense($iProfileId, $iProductId, $sDomain);
  103. }
  104. public function serviceGetLicense ($aParams)
  105. {
  106. return $this->_oDb->getLicense($aParams);
  107. }
  108. public function serviceUpdateLicense ($aSet, $aWhere)
  109. {
  110. return $this->_oDb->updateLicense($aSet, $aWhere);
  111. }
  112. public function serviceGetEntryBy($sType, $mixedValue)
  113. {
  114. return $this->_oDb->getContentInfoBy(array('type' => $sType, 'value' => $mixedValue));
  115. }
  116. public function serviceGetEntriesBy($aParams)
  117. {
  118. return $this->_oDb->getContentInfoBy($aParams);
  119. }
  120. public function serviceGetThumbnail($iPhotoId)
  121. {
  122. $CNF = &$this->_oConfig->CNF;
  123. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  124. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  125. return array(
  126. 'small' => $oImagesTranscoder ? $oImagesTranscoder->getFileUrl($iPhotoId) : '',
  127. 'big' => $oStorage ? $oStorage->getFileUrlById($iPhotoId) : ''
  128. );
  129. }
  130. public function serviceGetFile($iFileId)
  131. {
  132. $CNF = &$this->_oConfig->CNF;
  133. $aFile = $this->_oDb->getFile(array(
  134. 'type' => 'file_id_ext',
  135. 'file_id' => $iFileId
  136. ));
  137. if(!empty($aFile) && is_array($aFile)) {
  138. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  139. $aFile['file_url'] = $oStorage ? $oStorage->getFileUrlById($iFileId) : '';
  140. }
  141. return $aFile;
  142. }
  143. public function serviceGetUpdates($iContentId, $sVersion = '')
  144. {
  145. $CNF = &$this->_oConfig->CNF;
  146. $aFile = $this->_oDb->getFile(array(
  147. 'type' => 'content_id_and_type',
  148. 'content_id' => $iContentId,
  149. 'file_type' => 'update',
  150. 'version' => $sVersion
  151. ));
  152. if(!empty($aFile) && is_array($aFile)) {
  153. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  154. $aFile['file_url'] = $oStorage ? $oStorage->getFileUrlById($aFile['file_id']) : '';
  155. }
  156. return $aFile;
  157. }
  158. public function serviceGetScreenshots($iItemId)
  159. {
  160. $CNF = &$this->_oConfig->CNF;
  161. if(!$iItemId)
  162. return array();
  163. $aData = $this->_oDb->getContentInfoById($iItemId);
  164. $aPhotos = $this->_oDb->getPhoto(array('type' => 'content_id', 'content_id' => $aData[$CNF['FIELD_ID']], 'except' => array($aData[$CNF['FIELD_THUMB']], $aData[$CNF['FIELD_COVER']])));
  165. if(empty($aPhotos) || !is_array($aPhotos))
  166. return array();
  167. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  168. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_SCREENSHOT']);
  169. $aResult = array();
  170. foreach($aPhotos as $aPhoto)
  171. $aResult[] = array(
  172. 'id' => $aPhoto['file_id'],
  173. 'url_sm' => $oImagesTranscoder ? $oImagesTranscoder->getFileUrl($aPhoto['file_id']) : '',
  174. 'url_bg' => $oStorage ? $oStorage->getFileUrlById($aPhoto['file_id']) : ''
  175. );
  176. return $aResult;
  177. }
  178. /**
  179. * Integration with Payment based modules.
  180. */
  181. public function serviceGetPaymentData()
  182. {
  183. return $this->_aModule;
  184. }
  185. public function serviceGetCartItem($iItemId)
  186. {
  187. $CNF = &$this->_oConfig->CNF;
  188. if(!$iItemId)
  189. return array();
  190. $aItem = $this->_oDb->getContentInfoById($iItemId);
  191. if(empty($aItem) || !is_array($aItem))
  192. return array();
  193. return array (
  194. 'id' => $aItem[$CNF['FIELD_ID']],
  195. 'author_id' => $aItem[$CNF['FIELD_AUTHOR']],
  196. 'name' => $aItem[$CNF['FIELD_NAME']],
  197. 'title' => $aItem[$CNF['FIELD_TITLE']],
  198. 'description' => $aItem[$CNF['FIELD_TEXT']],
  199. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-product&id=' . $aItem[$CNF['FIELD_ID']]),
  200. 'price_single' => $aItem[$CNF['FIELD_PRICE_SINGLE']],
  201. 'price_recurring' => $aItem[$CNF['FIELD_PRICE_RECURRING']],
  202. );
  203. }
  204. public function serviceGetCartItems($iSellerId)
  205. {
  206. $CNF = &$this->_oConfig->CNF;
  207. $iSellerId = (int)$iSellerId;
  208. if(empty($iSellerId))
  209. return array();
  210. $aItems = $this->_oDb->getEntriesByAuthor($iSellerId);
  211. $aResult = array();
  212. foreach($aItems as $aItem)
  213. $aResult[] = array(
  214. 'id' => $aItem[$CNF['FIELD_ID']],
  215. 'author_id' => $aItem[$CNF['FIELD_AUTHOR']],
  216. 'name' => $aItem[$CNF['FIELD_NAME']],
  217. 'title' => $aItem[$CNF['FIELD_TITLE']],
  218. 'description' => $aItem[$CNF['FIELD_TEXT']],
  219. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-product&id=' . $aItem[$CNF['FIELD_ID']]),
  220. 'price_single' => $aItem[$CNF['FIELD_PRICE_SINGLE']],
  221. 'price_recurring' => $aItem[$CNF['FIELD_PRICE_RECURRING']],
  222. );
  223. return $aResult;
  224. }
  225. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  226. {
  227. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_SINGLE);
  228. }
  229. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  230. {
  231. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_RECURRING);
  232. }
  233. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  234. {
  235. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_SINGLE);
  236. }
  237. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  238. {
  239. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_RECURRING);
  240. }
  241. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  242. {
  243. //TODO: Do something if it's necessary.
  244. }
  245. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  246. {
  247. $aItem = $this->serviceGetCartItem($iItemId);
  248. if(empty($aItem) || !is_array($aItem))
  249. return array();
  250. $sDuration = '';
  251. if($sType == BX_MARKET_LICENSE_TYPE_RECURRING) {
  252. $aProduct = $this->_oDb->getContentInfoById($iItemId);
  253. $sDuration = $aProduct[$this->_oConfig->CNF['FIELD_DURATION_RECURRING']];
  254. }
  255. if(!$this->_oDb->registerLicense($iClientId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType, $sDuration))
  256. return array();
  257. return $aItem;
  258. }
  259. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  260. {
  261. return $this->_oDb->unregisterLicense($iClientId, $iItemId, $sOrder, $sLicense, $sType);
  262. }
  263. public function getGhostTemplateVars($aFile, $iProfileId, $iContentId, $oStorage, $oImagesTranscoder)
  264. {
  265. $CNF = &$this->_oConfig->CNF;
  266. $sMethod = '';
  267. $sStorage = $oStorage->getObject();
  268. switch($sStorage) {
  269. case $CNF['OBJECT_STORAGE']:
  270. $sMethod = 'getPhoto';
  271. break;
  272. case $CNF['OBJECT_STORAGE_FILES']:
  273. $sMethod = 'getFile';
  274. break;
  275. }
  276. $aFileInfo = $this->_oDb->$sMethod(array('type' => 'file_id', 'file_id' => $aFile['id']));
  277. $bFileInfo = !empty($aFileInfo) && is_array($aFileInfo);
  278. $bFileInfoTitle = $bFileInfo && isset($aFileInfo['title']);
  279. $aTmplVars = array(
  280. 'file_title' => $bFileInfoTitle ? $aFileInfo['title'] : '',
  281. 'file_title_attr' => $bFileInfoTitle ? bx_html_attribute($aFileInfo['title']) : '',
  282. );
  283. if($sStorage == $CNF['OBJECT_STORAGE'])
  284. return $aTmplVars;
  285. $bFileInfoVersion = $bFileInfo && isset($aFileInfo['version']);
  286. $bFileInfoTypeVersion = !$bFileInfo || ($bFileInfo && isset($aFileInfo['type']) && $aFileInfo['type'] == BX_MARKET_FILE_TYPE_VERSION);
  287. $bFileInfoTypeUpdate = $bFileInfo && isset($aFileInfo['type']) && $aFileInfo['type'] == BX_MARKET_FILE_TYPE_UPDATE;
  288. $aVersions = $this->_oDb->$sMethod(array('type' => 'content_id', 'content_id' => $iContentId));
  289. $aTmplVars = array_merge($aTmplVars, array(
  290. 'file_version' => $bFileInfoVersion ? $aFileInfo['version'] : '',
  291. 'file_version_attr' => $bFileInfoVersion ? bx_html_attribute($aFileInfo['version']) : '',
  292. 'file_type_version_selected' => $bFileInfoTypeVersion ? ' selected="selected"' : '',
  293. 'file_type_update_selected' => $bFileInfoTypeUpdate ? ' selected="selected"' : '',
  294. 'file_type_version_elements' => !$bFileInfoTypeVersion ? ' style="display:none;"' : '',
  295. 'file_type_update_elements' => !$bFileInfoTypeUpdate ? ' style="display:none;"' : '',
  296. 'file_version_from_options' => $this->_oTemplate->getGhostTemplateFileOptions('version', $aFileInfo, $aVersions),
  297. 'file_version_to_options' => $this->_oTemplate->getGhostTemplateFileOptions('version_to', $aFileInfo, $aVersions)
  298. ));
  299. return $aTmplVars;
  300. }
  301. public function checkAllowedSetCover ()
  302. {
  303. $aCheck = checkActionModule($this->_iProfileId, 'set cover', $this->getName(), false);
  304. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  305. return $aCheck[CHECK_ACTION_MESSAGE];
  306. return CHECK_ACTION_RESULT_ALLOWED;
  307. }
  308. public function checkAllowedDownload($aDataEntry, $isPerformAction = false)
  309. {
  310. $CNF = &$this->_oConfig->CNF;
  311. // moderator and owner always have access
  312. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  313. return CHECK_ACTION_RESULT_ALLOWED;
  314. // check ACL
  315. $aCheck = checkActionModule($this->_iProfileId, 'download entry', $this->getName(), $isPerformAction);
  316. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  317. return $aCheck[CHECK_ACTION_MESSAGE];
  318. if(!$this->_oDb->hasLicense($this->_iProfileId, $aDataEntry[$CNF['FIELD_ID']]))
  319. return true;
  320. return CHECK_ACTION_RESULT_ALLOWED;
  321. }
  322. protected function _getContentInfo($iContentId = 0)
  323. {
  324. if(!$iContentId)
  325. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  326. if(!$iContentId)
  327. return false;
  328. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  329. if(!$aContentInfo)
  330. return false;
  331. return $aContentInfo;
  332. }
  333. }
  334. /** @} */