BxDolStudioStore.php 13 KB

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