BxDolStudioStore.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 TridentStudio Trident Studio
  7. * @{
  8. */
  9. bx_import('BxTemplStudioPage');
  10. bx_import('BxDolStudioStoreQuery');
  11. bx_import('BxDolStudioInstallerUtils');
  12. define('BX_DOL_STUDIO_STR_TYPE_DEFAULT', 'downloaded');
  13. class BxDolStudioStore extends BxTemplStudioPage
  14. {
  15. protected $sPage;
  16. protected $aContent;
  17. protected $iClient;
  18. protected $aAlias;
  19. protected $bAuthAccessUpdates;
  20. function __construct($sPage = "")
  21. {
  22. parent::__construct('store');
  23. $this->oDb = new BxDolStudioStoreQuery();
  24. $this->aAlias = array(
  25. 'tag' => array(
  26. 'modules' => 'extensions',
  27. 'languages' => 'translations'
  28. ),
  29. 'category' => array()
  30. );
  31. $this->bAuthAccessUpdates = false;
  32. $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
  33. if(is_string($sPage) && !empty($sPage))
  34. $this->sPage = $sPage;
  35. bx_import('BxDolStudioOAuth');
  36. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  37. //--- Check actions ---//
  38. if(($sAction = bx_get('str_action')) !== false) {
  39. $sAction = bx_process_input($sAction);
  40. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_mod_err_cannot_process_action'));
  41. switch($sAction) {
  42. case 'get-file':
  43. $iFileId = (int)bx_get('str_id');
  44. $aResult = $this->getFile($iFileId);
  45. break;
  46. case 'get-product':
  47. $sModuleName = bx_process_input(bx_get('str_id'));
  48. $aResult = $this->getProduct($sModuleName);
  49. break;
  50. case 'get-update':
  51. $sModuleName = bx_process_input(bx_get('str_id'));
  52. $aResult = $this->getUpdate($sModuleName);
  53. break;
  54. case 'get-update-and-install':
  55. $sModuleName = bx_process_input(bx_get('str_id'));
  56. $aResult = $this->getUpdate($sModuleName, true);
  57. break;
  58. case 'get-products-by-type':
  59. $this->sPage = bx_process_input(bx_get('str_value'));
  60. $sContent = $this->getPageCode();
  61. if(!empty($sContent))
  62. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  63. else
  64. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  65. break;
  66. case 'get-products-by-page':
  67. $this->sPage = bx_process_input(bx_get('str_type'));
  68. $sContent = $this->getPageContent();
  69. if(!empty($sContent))
  70. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  71. else
  72. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  73. break;
  74. case 'add-to-cart':
  75. $sVendor = bx_process_input(bx_get('str_vendor'));
  76. $iItem = (int)bx_get('str_item');
  77. $iItemCount = 1;
  78. if(empty($sVendor) || empty($iItem)) {
  79. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
  80. break;
  81. }
  82. bx_import('BxDolStudioCart');
  83. BxDolStudioCart::getInstance()->add($sVendor, $iItem, $iItemCount);
  84. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
  85. break;
  86. case 'delete-from-cart':
  87. $sVendor = bx_process_input(bx_get('str_vendor'));
  88. $iItem = (int)bx_get('str_item');
  89. if(empty($sVendor)) {
  90. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
  91. break;
  92. }
  93. bx_import('BxDolStudioCart');
  94. BxDolStudioCart::getInstance()->delete($sVendor, $iItem);
  95. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '');
  96. break;
  97. case 'checkout-cart':
  98. $sVendor = bx_process_input(bx_get('str_vendor'));
  99. if(empty($sVendor)) {
  100. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
  101. break;
  102. }
  103. $sLocation = $this->checkoutCart($sVendor);
  104. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
  105. break;
  106. case 'install':
  107. $sValue = bx_process_input(bx_get('str_value'));
  108. if(empty($sValue))
  109. break;
  110. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true, 'html_response' => true));
  111. break;
  112. case 'update':
  113. $sValue = bx_process_input(bx_get('str_value'));
  114. if(empty($sValue))
  115. break;
  116. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update', array('html_response' => true));
  117. break;
  118. case 'delete':
  119. $sValue = bx_process_input(bx_get('str_value'));
  120. if(empty($sValue))
  121. break;
  122. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete', array('html_response' => true));
  123. break;
  124. }
  125. if(!empty($aResult['message'])) {
  126. bx_import('BxDolStudioTemplate');
  127. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result.html', array('content' => $aResult['message']));
  128. bx_import('BxTemplStudioFunctions');
  129. $aResult['message'] = BxTemplStudioFunctions::getInstance()->transBox('', $aResult['message']);
  130. }
  131. echo json_encode($aResult);
  132. exit;
  133. }
  134. }
  135. protected function loadGoodies()
  136. {
  137. $iPerPage = 5;
  138. $aProducts = array();
  139. $sJsObject = $this->getPageJsObject();
  140. bx_import('BxDolStudioJson');
  141. $oJson = BxDolStudioJson::getInstance();
  142. // Load featured
  143. $aProducts[] = array(
  144. 'caption' => '_adm_block_cpt_last_featured',
  145. 'actions' => array(
  146. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  147. ),
  148. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  149. );
  150. // Load modules
  151. $aProducts[] = array(
  152. 'caption' => '_adm_block_cpt_last_modules',
  153. 'actions' => array(
  154. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_modules', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('modules', this)")
  155. ),
  156. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  157. );
  158. // Load templates
  159. $aProducts[] = array(
  160. 'caption' => '_adm_block_cpt_last_templates',
  161. 'actions' => array(
  162. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  163. ),
  164. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  165. );
  166. // Load languages
  167. $aProducts[] = array(
  168. 'caption' => '_adm_block_cpt_last_languages',
  169. 'actions' => array(
  170. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_languages', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('languages', this)")
  171. ),
  172. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  173. );
  174. return $aProducts;
  175. }
  176. protected function loadFeatured($iStart, $iPerPage)
  177. {
  178. bx_import('BxDolStudioJson');
  179. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient));
  180. }
  181. protected function loadTag($sTag, $iStart, $iPerPage)
  182. {
  183. bx_import('BxDolStudioJson');
  184. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => $this->aliasToNameTag($sTag), 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient));
  185. }
  186. protected function loadPurchases()
  187. {
  188. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  189. }
  190. protected function loadUpdates()
  191. {
  192. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  193. }
  194. protected function loadCheckout()
  195. {
  196. bx_import('BxDolStudioJson');
  197. $oJson = BxDolStudioJson::getInstance();
  198. bx_import('BxDolStudioCart');
  199. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  200. $aResult = array();
  201. foreach($aVendors as $sVendor => $aItems) {
  202. $aIds = $aCounts = array();
  203. foreach($aItems as $aItem) {
  204. $aIds[] = $aItem['item_id'];
  205. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  206. }
  207. $aProducts = $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  208. if(!empty($aProducts))
  209. $aResult[$sVendor] = array(
  210. 'ids' => $aIds,
  211. 'counts' => $aCounts,
  212. 'products' => $aProducts
  213. );
  214. }
  215. return $aResult;
  216. }
  217. protected function loadDownloaded()
  218. {
  219. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  220. return array(
  221. 'modules' => $oInstallerUtils->getModules(),
  222. 'updates' => $oInstallerUtils->getUpdates()
  223. );
  224. }
  225. protected function loadProduct($sModuleName)
  226. {
  227. bx_import('BxDolStudioJson');
  228. $oJson = BxDolStudioJson::getInstance();
  229. return $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient));
  230. }
  231. /*
  232. * Load package (module, update) using OAuth authorization.
  233. */
  234. protected function loadFile($iFileId)
  235. {
  236. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  237. }
  238. /*
  239. * Load update's package publicly.
  240. */
  241. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  242. {
  243. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  244. }
  245. private function checkoutCart($sVendor)
  246. {
  247. bx_import('BxDolStudioCart');
  248. $oCart = BxDolStudioCart::getInstance();
  249. $aItems = $oCart->getByVendor($sVendor);
  250. if(empty($aItems) || !is_array($aItems))
  251. return false;
  252. $aIds = array();
  253. foreach($aItems as $aItem)
  254. $aIds[] = $aItem['item_id'];
  255. $sSid = bx_site_hash();
  256. return BX_DOL_UNITY_URL_MARKET . 'purchase/' . $sVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  257. }
  258. private function aliasToNameTag($sAlias)
  259. {
  260. return $this->aliasToName('tag', $sAlias);
  261. }
  262. private function aliasToNameCategory($sAlias)
  263. {
  264. return $this->aliasToName('category', $sAlias);
  265. }
  266. private function aliasToName($sType, $sAlias)
  267. {
  268. return isset($this->aAlias[$sType][$sAlias]) ? $this->aAlias[$sType][$sAlias] : $sAlias;
  269. }
  270. }
  271. /** @} */