BxBaseStudioApiConfigs.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioApiConfigs extends BxDolStudioApiConfigs
  11. {
  12. protected $_sConfigFile;
  13. protected $_sInstallerFile;
  14. public function __construct ($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct ($aOptions, $oTemplate);
  17. $this->_sConfigFile = 'install/config.php';
  18. $this->_sInstallerFile = 'install/installer.php';
  19. $this->oDb = BxDolModuleQuery::getInstance();
  20. }
  21. public function performActionApply()
  22. {
  23. $iId = $this->_getIds();
  24. $mixedResult = $this->_performActionApply($iId);
  25. if($mixedResult === true)
  26. $mixedResult = ['code' => 0, 'msg' => _t('_adm_api_msg_applied')];
  27. else
  28. $mixedResult = ['code' => 1, 'msg' => is_string($mixedResult) ? $mixedResult : _t('_adm_err_operation_failed')];
  29. return echoJson($mixedResult);
  30. }
  31. public function performActionRemove()
  32. {
  33. $iId = $this->_getIds();
  34. $aResult = [];
  35. if($this->_performActionRemove($iId))
  36. $aResult = ['code' => 0, 'msg' => _t('_adm_api_msg_removed')];
  37. else
  38. $aResult = ['code' => 1, 'msg' => _t('_adm_err_operation_failed')];
  39. return echoJson($aResult);
  40. }
  41. public function performActionExport()
  42. {
  43. $iId = $this->_getIds();
  44. list($bResult, $sResult) = $this->_performActionExport($iId);
  45. if(!$bResult)
  46. return echoJson(['code' => 1, 'msg' => _t($sResult)]);
  47. return echoJson([
  48. 'code' => 0,
  49. 'redirect' => BX_DOL_URL_STUDIO . bx_append_url_params('api.php', [
  50. 'page' => 'api_config',
  51. 'api_action' => 'download',
  52. 'api_value' => $sResult
  53. ])
  54. ]);
  55. }
  56. public function performActionApplyAll()
  57. {
  58. $iAffected = 0;
  59. $aModules = $this->oDb->getModulesBy(['type' => 'modules']);
  60. foreach($aModules as $aModule) {
  61. if((int)$aModule['enabled'] == 0)
  62. continue;
  63. $iAffected += ($this->_performActionApply($aModule['id']) === true ? 1 : 0);
  64. }
  65. return echoJson(['code' => 0, 'msg' => _t('_adm_api_msg_applied_for', $iAffected)]);
  66. }
  67. public function performActionRemoveAll()
  68. {
  69. $iAffected = 0;
  70. $aModules = $this->oDb->getModulesBy(['type' => 'modules']);
  71. foreach($aModules as $aModule) {
  72. if((int)$aModule['enabled'] == 0)
  73. continue;
  74. $iAffected += ($this->_performActionRemove($aModule['id']) === true ? 1 : 0);
  75. }
  76. return echoJson(['code' => 0, 'msg' => _t('_adm_api_msg_removed_for', $iAffected)]);
  77. }
  78. public function performActionExportAll()
  79. {
  80. list($bResult, $sResult) = $this->_performActionExport();
  81. if(!$bResult)
  82. return echoJson(['code' => 1, 'msg' => _t($sResult)]);
  83. return echoJson([
  84. 'code' => 0,
  85. 'redirect' => BX_DOL_URL_STUDIO . bx_append_url_params('api.php', [
  86. 'page' => 'api_config',
  87. 'api_action' => 'download',
  88. 'api_value' => $sResult
  89. ])
  90. ]);
  91. }
  92. public function performActionImportAll()
  93. {
  94. $sAction = 'import_all';
  95. $sForm = 'adm_api_settings_' . $sAction;
  96. $aForm = [
  97. 'form_attrs' => [
  98. 'id' => $sForm,
  99. 'name' => $sForm,
  100. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_api_configs&a=' . $sAction,
  101. 'method' => 'post',
  102. 'enctype' => 'multipart/form-data'
  103. ],
  104. 'params' => [
  105. 'db' => [
  106. 'submit_name' => 'save'
  107. ],
  108. ],
  109. 'inputs' => [
  110. 'file' => [
  111. 'type' => 'file',
  112. 'name' => 'file',
  113. 'caption' => '',
  114. 'value' => '',
  115. ],
  116. 'controls' => [
  117. 'type' => 'input_set', [
  118. 'type' => 'submit',
  119. 'name' => 'save',
  120. 'value' => _t('_adm_api_btn_import'),
  121. ], [
  122. 'type' => 'button',
  123. 'name' => 'cancel',
  124. 'value' => _t('_Cancel'),
  125. 'attrs' => [
  126. 'class' => 'bx-def-margin-sec-left-auto',
  127. 'onclick' => '$(".bx-popup-applied:visible").dolPopupHide()'
  128. ]
  129. ]
  130. ]
  131. ]
  132. ];
  133. $oForm = new BxTemplStudioFormView($aForm);
  134. $oForm->initChecker();
  135. if($oForm->isSubmittedAndValid()) {
  136. $sError = _t('_adm_api_err_cannot_perform_action');
  137. $sFile = '';
  138. if(!($sFile = $_FILES['file']['tmp_name'] ?? false))
  139. return echoJson(['code' => 1, 'msg' => $sError]);
  140. if($this->oDb->executeSQL($sFile) !== true)
  141. return echoJson(['code' => 2, 'msg' => $sError]);
  142. BxDolCacheUtilities::getInstance()->clear('db');
  143. return echoJson(['code' => 0, 'msg' => _t('_adm_api_msg_imported')]);
  144. }
  145. $sFormId = $oForm->getId();
  146. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_adm_api_popup_import'), $this->_oTemplate->parseHtmlByName('api_settings_form.html', [
  147. 'form_id' => $sFormId,
  148. 'form' => $oForm->getCode(true),
  149. 'object' => $this->_sObject,
  150. 'action' => $sAction
  151. ]));
  152. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  153. }
  154. protected function _performActionApply($iId)
  155. {
  156. $aModule = $this->oDb->getModuleById($iId);
  157. if(!$aModule || !is_array($aModule))
  158. return false;
  159. $aConfig = BxDolStudioInstallerUtils::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $aModule['path'] . $this->_sConfigFile);
  160. if(!$aConfig || !is_array($aConfig))
  161. return false;
  162. $sPathInstaller = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . $this->_sInstallerFile;
  163. if(!file_exists($sPathInstaller))
  164. return false;
  165. require_once($sPathInstaller);
  166. $sClassName = $aModule['class_prefix'] . 'Installer';
  167. $oInstaller = new $sClassName($aConfig);
  168. $mixedResult = $oInstaller->callAction('enable', 'execute_sql_addons');
  169. return $mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS ? true : ($mixedResult['msg'] ?? false);
  170. }
  171. protected function _performActionRemove($iId)
  172. {
  173. $aModule = $this->oDb->getModuleById($iId);
  174. if(!$aModule || !is_array($aModule))
  175. return false;
  176. $this->oDb->query("UPDATE `sys_pages_blocks` SET `active_api`=0 WHERE `module`=:module", [
  177. 'module' => $aModule['name']
  178. ]);
  179. $this->oDb->query("UPDATE `sys_menu_items` SET `active_api`=0 WHERE `module`=:module", [
  180. 'module' => $aModule['name']
  181. ]);
  182. return true;
  183. }
  184. protected function _performActionExport($iId = 0)
  185. {
  186. $sModule = '';
  187. if($iId && ($aModule = BxDolModuleDb::getInstance()->getModuleById($iId)) && is_array($aModule))
  188. $sModule = $aModule['name'];
  189. $sContent = '';
  190. if(($_sContent = $this->_getQueriesPagesConfigApi($sModule)))
  191. $sContent .= "-- \n-- Pages 'Config API':\n-- " . $_sContent;
  192. if(($_sContent = $this->_getQueriesPagesActiveApi($sModule)))
  193. $sContent .= "-- \n-- Pages 'Active API':\n-- " . $_sContent;
  194. if(($_sContent = $this->_getQueriesMenusConfigApi($sModule)))
  195. $sContent .= "-- \n-- Manus 'Config API':\n-- " . $_sContent;
  196. if(($_sContent = $this->_getQueriesMenusActiveApi($sModule)))
  197. $sContent .= "-- \n-- Manus 'Active API':\n-- " . $_sContent;
  198. if(!$sContent)
  199. return [false, '_adm_api_msg_export_empty'];
  200. $iNow = time();
  201. $sFile = "api_settings_" . ($sModule ?: 'all') . "_". date('d_m_Y', $iNow) . ".sql";
  202. $mixedResult = false;
  203. if(($oHandle = fopen(BX_DIRECTORY_PATH_TMP . $sFile, 'w'))) {
  204. $mixedResult = fwrite($oHandle, $sContent);
  205. fclose($oHandle);
  206. }
  207. return $mixedResult ? [true, $sFile] : [false, '_adm_err_operation_failed'];
  208. }
  209. }
  210. /** @} */