BxMarketModule.php 14 KB

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