BxMarketModule.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /**
  14. * Market module
  15. */
  16. class BxMarketModule extends BxBaseModTextModule
  17. {
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. }
  22. public function actionCheckName()
  23. {
  24. $CNF = &$this->_oConfig->CNF;
  25. $sTitle = strtolower(bx_process_input(bx_get('title')));
  26. if(empty($sTitle))
  27. return echoJson(array());
  28. echoJson(array(
  29. 'title' => $sTitle,
  30. 'name' => uriGenerate($sTitle, $CNF['TABLE_ENTRIES'], $CNF['FIELD_NAME'])
  31. ));
  32. }
  33. public function serviceEntityCreate ()
  34. {
  35. $this->_oTemplate->addJs(array('entry.js'));
  36. return $this->_oTemplate->getJsCode('entry') . parent::serviceEntityCreate();
  37. }
  38. public function serviceEntityDownload ($iContentId = 0)
  39. {
  40. $CNF = &$this->_oConfig->CNF;
  41. if(!$iContentId)
  42. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  43. if(!$iContentId)
  44. return false;
  45. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  46. if(empty($aContentInfo) || !is_array($aContentInfo))
  47. return MsgBox(_t('_sys_txt_error_occured'));
  48. if($this->checkAllowedDownload($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  49. return MsgBox(_t('_bx_market_err_access_denied'));
  50. return $this->_oTemplate->entryAttachmentsByStorage($CNF['OBJECT_STORAGE_FILES'], $aContentInfo);
  51. }
  52. public function serviceEntityRating($iContentId = 0)
  53. {
  54. return $this->_serviceTemplateFunc ('entryRating', $iContentId);
  55. }
  56. public function serviceEntityAuthorEntities($iContentId = 0)
  57. {
  58. $CNF = &$this->_oConfig->CNF;
  59. $aContentInfo = $this->_getContentInfo($iContentId);
  60. if($aContentInfo === false)
  61. return false;
  62. $oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
  63. if (!$oProfile)
  64. $oProfile = BxDolProfileUndefined::getInstance();
  65. $aBlock = $this->_serviceBrowse ('author', array('author' => $aContentInfo[$CNF['FIELD_AUTHOR']], 'except' => array($iContentId), 'per_page' => 2), BX_DB_PADDING_DEF, true);
  66. $aBlock['title'] = _t('_bx_market_page_block_title_entry_author_entries', $oProfile->getDisplayName());
  67. return $aBlock;
  68. }
  69. /**
  70. * Licenses
  71. */
  72. public function serviceBlockLicenses()
  73. {
  74. $sGrid = $this->_oConfig->getGridObject('licenses');
  75. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  76. if(!$oGrid)
  77. return '';
  78. return array(
  79. 'content' => $this->_oTemplate->getJsCode('licenses', array('sObjNameGrid' => $sGrid)) . $oGrid->getCode(),
  80. );
  81. }
  82. /**
  83. * Get number of unused licenses for some profile
  84. * @param $iProfileId - profile to get unused licenses for, if omitted then currently logged in profile is used
  85. * @return integer
  86. */
  87. public function serviceGetUnusedLicensesNum ($iProfileId = 0)
  88. {
  89. if(!$iProfileId)
  90. $iProfileId = bx_get_logged_profile_id();
  91. $aLicenses = $this->_oDb->getLicense(array('type' => 'unused', 'profile_id' => (int)$iProfileId));
  92. return !empty($aLicenses) && is_array($aLicenses) ? count($aLicenses) : 0;
  93. }
  94. /**
  95. * Integration with Payment based modules.
  96. */
  97. public function serviceGetPaymentData()
  98. {
  99. return $this->_aModule;
  100. }
  101. public function serviceGetCartItem($iItemId)
  102. {
  103. $CNF = &$this->_oConfig->CNF;
  104. if(!$iItemId)
  105. return array();
  106. $aItem = $this->_oDb->getContentInfoById($iItemId);
  107. if(empty($aItem) || !is_array($aItem))
  108. return array();
  109. return array (
  110. 'id' => $aItem[$CNF['FIELD_ID']],
  111. 'author_id' => $aItem[$CNF['FIELD_AUTHOR']],
  112. 'name' => $aItem[$CNF['FIELD_NAME']],
  113. 'title' => $aItem[$CNF['FIELD_TITLE']],
  114. 'description' => $aItem[$CNF['FIELD_TEXT']],
  115. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-product&id=' . $aItem[$CNF['FIELD_ID']]),
  116. 'price_single' => $aItem[$CNF['FIELD_PRICE_SINGLE']],
  117. 'price_recurring' => $aItem[$CNF['FIELD_PRICE_RECURRING']],
  118. );
  119. }
  120. public function serviceGetCartItems($iSellerId)
  121. {
  122. $CNF = &$this->_oConfig->CNF;
  123. $iSellerId = (int)$iSellerId;
  124. if(empty($iSellerId))
  125. return array();
  126. $aItems = $this->_oDb->getEntriesByAuthor($iSellerId);
  127. $aResult = array();
  128. foreach($aItems as $aItem)
  129. $aResult[] = array(
  130. 'id' => $aItem[$CNF['FIELD_ID']],
  131. 'author_id' => $aItem[$CNF['FIELD_AUTHOR']],
  132. 'name' => $aItem[$CNF['FIELD_NAME']],
  133. 'title' => $aItem[$CNF['FIELD_TITLE']],
  134. 'description' => $aItem[$CNF['FIELD_TEXT']],
  135. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-product&id=' . $aItem[$CNF['FIELD_ID']]),
  136. 'price_single' => $aItem[$CNF['FIELD_PRICE_SINGLE']],
  137. 'price_recurring' => $aItem[$CNF['FIELD_PRICE_RECURRING']],
  138. );
  139. return $aResult;
  140. }
  141. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  142. {
  143. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_SINGLE);
  144. }
  145. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  146. {
  147. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_RECURRING);
  148. }
  149. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  150. {
  151. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_SINGLE);
  152. }
  153. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  154. {
  155. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_MARKET_LICENSE_TYPE_RECURRING);
  156. }
  157. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  158. {
  159. //TODO: Do something if it's necessary.
  160. }
  161. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  162. {
  163. $aItem = $this->serviceGetCartItem($iItemId);
  164. if(empty($aItem) || !is_array($aItem))
  165. return array();
  166. $sDuration = '';
  167. if($sType == BX_MARKET_LICENSE_TYPE_RECURRING) {
  168. $aProduct = $this->_oDb->getContentInfoById($iItemId);
  169. $sDuration = $aProduct[$this->_oConfig->CNF['FIELD_DURATION_RECURRING']];
  170. }
  171. if(!$this->_oDb->registerLicense($iClientId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType, $sDuration))
  172. return array();
  173. return $aItem;
  174. }
  175. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  176. {
  177. return $this->_oDb->unregisterLicense($iClientId, $iItemId, $sOrder, $sLicense, $sType);
  178. }
  179. public function getGhostTemplateVars($aFile, $iProfileId, $iContentId, $oStorage, $oImagesTranscoder)
  180. {
  181. $CNF = &$this->_oConfig->CNF;
  182. $sMethod = '';
  183. $sStorage = $oStorage->getObject();
  184. switch($sStorage) {
  185. case $CNF['OBJECT_STORAGE']:
  186. $sMethod = 'getPhoto';
  187. break;
  188. case $CNF['OBJECT_STORAGE_FILES']:
  189. $sMethod = 'getFile';
  190. break;
  191. }
  192. $aFileInfo = $this->_oDb->$sMethod(array('type' => 'file_id', 'file_id' => $aFile['id']));
  193. $bFileInfo = !empty($aFileInfo) && is_array($aFileInfo);
  194. $bFileInfoTitle = $bFileInfo && isset($aFileInfo['title']);
  195. $bFileInfoVersion = $bFileInfo && isset($aFileInfo['version']);
  196. return array(
  197. 'file_title' => $bFileInfoTitle ? $aFileInfo['title'] : '',
  198. 'file_title_attr' => $bFileInfoTitle ? bx_html_attribute($aFileInfo['title']) : '',
  199. 'file_version' => $bFileInfoVersion ? $aFileInfo['version'] : '',
  200. 'file_version_attr' => $bFileInfoVersion ? bx_html_attribute($aFileInfo['version']) : '',
  201. );
  202. }
  203. public function checkAllowedSetCover ()
  204. {
  205. $aCheck = checkActionModule($this->_iProfileId, 'set cover', $this->getName(), false);
  206. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  207. return $aCheck[CHECK_ACTION_MESSAGE];
  208. return CHECK_ACTION_RESULT_ALLOWED;
  209. }
  210. public function checkAllowedDownload($aDataEntry, $isPerformAction = false)
  211. {
  212. $CNF = &$this->_oConfig->CNF;
  213. // moderator and owner always have access
  214. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  215. return CHECK_ACTION_RESULT_ALLOWED;
  216. // check ACL
  217. $aCheck = checkActionModule($this->_iProfileId, 'download entry', $this->getName(), $isPerformAction);
  218. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  219. return $aCheck[CHECK_ACTION_MESSAGE];
  220. if(!$this->_oDb->hasLicense($this->_iProfileId, $aDataEntry[$CNF['FIELD_ID']]))
  221. return true;
  222. return CHECK_ACTION_RESULT_ALLOWED;
  223. }
  224. protected function _getContentInfo($iContentId = 0)
  225. {
  226. if(!$iContentId)
  227. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  228. if(!$iContentId)
  229. return false;
  230. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  231. if(!$aContentInfo)
  232. return false;
  233. return $aContentInfo;
  234. }
  235. }
  236. /** @} */