BxDolStudioStore.php 13 KB

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