1
0

BxDolStudioModule.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. bx_import('BxDolStudioUtils');
  10. define('BX_DOL_STUDIO_MOD_TYPE_SETTINGS', 'settings');
  11. define('BX_DOL_STUDIO_MOD_TYPE_DEFAULT', BX_DOL_STUDIO_MOD_TYPE_SETTINGS);
  12. class BxDolStudioModule extends BxTemplStudioWidget
  13. {
  14. protected $sLangPrefix;
  15. protected $sParamPrefix;
  16. protected $sModule;
  17. protected $aModule;
  18. protected $sPage;
  19. protected $sPageDefault = BX_DOL_STUDIO_MOD_TYPE_DEFAULT;
  20. protected $sManageUrl;
  21. function __construct($sModule, $mixedPageName, $sPage = "")
  22. {
  23. parent::__construct($mixedPageName);
  24. $this->sLangPrefix = 'mod';
  25. $this->sParamPrefix = 'mod';
  26. $this->sPageRssHelpObject = 'sys_studio_module_help';
  27. $this->sModule = '';
  28. if(is_string($sModule) && !empty($sModule))
  29. $this->sModule = $sModule;
  30. $this->sPage = $this->sPageDefault;
  31. if(is_string($sPage) && !empty($sPage))
  32. $this->sPage = $sPage;
  33. $this->sManageUrl = BX_DOL_URL_STUDIO . 'module.php?name=' . $this->aPage['name'];
  34. }
  35. public static function getObjectInstance($mixedPageName, $sPage = "", $bInit = true)
  36. {
  37. $sModule = BX_DOL_STUDIO_MODULE_SYSTEM;
  38. $sClass = get_called_class();
  39. $aPage = BxDolStudioPageQuery::getInstance()->getPages(array('type' => 'by_page_name_full', 'value' => $mixedPageName));
  40. if(!empty($aPage) && is_array($aPage)) {
  41. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
  42. if(!empty($aModule) && is_array($aModule)) {
  43. $sModule = $aModule['name'];
  44. if(file_exists(BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'classes/' . $aModule['class_prefix'] . 'StudioPage.php')) {
  45. bx_import('StudioPage', $aModule);
  46. $sClass = $aModule['class_prefix'] . 'StudioPage';
  47. }
  48. }
  49. }
  50. $oObject = new $sClass($sModule, $mixedPageName, $sPage);
  51. if($bInit)
  52. $oObject->init();
  53. return $oObject;
  54. }
  55. public function init()
  56. {
  57. $this->aModule = BxDolModuleQuery::getInstance()->getModuleByName($this->sModule);
  58. if(empty($this->aModule) || !is_array($this->aModule))
  59. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  60. $this->sPageRssHelpUrl = $this->aModule['help_url'];
  61. $this->sPageRssHelpId = $this->aModule['name'];
  62. $this->addMarkers(array(
  63. 'module_name' => $this->aModule['name'],
  64. 'module_uri' => $this->aModule['uri'],
  65. 'module_title' => $this->aModule['title'],
  66. ));
  67. if($this->sModule != BX_DOL_STUDIO_MODULE_SYSTEM)
  68. $this->addAction(array(
  69. 'type' => 'switcher',
  70. 'name' => 'activate',
  71. 'caption' => '_adm_txt_pca_active',
  72. 'checked' => (int)$this->aModule['enabled'] == 1,
  73. 'onchange' => "javascript:" . $this->getPageJsObject() . ".activate(this, '" . $this->aPage['name'] . "', {widget_id})"
  74. ), false);
  75. }
  76. public function checkAction()
  77. {
  78. $sAction = bx_get($this->sParamPrefix . '_action');
  79. if($sAction === false)
  80. return false;
  81. if(empty($this->aModule) || !is_array($this->aModule))
  82. return array('code' => 1, 'message' => _t('_sys_request_page_not_found_cpt'));
  83. $sAction = bx_process_input($sAction);
  84. $aResult = array('code' => 2, 'message' => _t('_adm_mod_err_cannot_process_action'));
  85. switch($sAction) {
  86. case 'settings':
  87. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  88. $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
  89. if(empty($sValue) || empty($iWidgetId))
  90. break;
  91. $aResult = $this->settings($sValue, $iWidgetId);
  92. break;
  93. case 'activate':
  94. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  95. if(empty($sValue))
  96. break;
  97. $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
  98. $aResult = $this->activate($sValue, $iWidgetId);
  99. break;
  100. case 'uninstall':
  101. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  102. if(empty($sValue))
  103. break;
  104. $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
  105. $aResult = $this->uninstall($sValue, $iWidgetId);
  106. break;
  107. }
  108. return $aResult;
  109. }
  110. public function settings($sPage, $iWidgetId)
  111. {
  112. $aResultError = array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
  113. $aPage = $this->aPage['wid_module'];
  114. if(empty($aPage) || !is_array($aPage))
  115. $aPage = $this->oDb->getPages(array('type' => 'by_page_name_full', 'value' => $sPage));
  116. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
  117. if(empty($aModule) || !is_array($aModule))
  118. return $aResultError;
  119. $sPopupSettings = $this->getPopupSettings($sPage, $iWidgetId);
  120. if(empty($sPopupSettings))
  121. return $aResultError;
  122. return array('code' => 0, 'popup' => array(
  123. 'html' => $sPopupSettings,
  124. 'options' => array(
  125. 'closeOnOuterClick' => true,
  126. 'removeOnClose' => true,
  127. 'pointer' => array(
  128. 'el' => '#bx-std-widget-' . $iWidgetId . ' .bx-std-wa-settings',
  129. )
  130. )
  131. ));
  132. }
  133. public function activate($sPage, $iWidgetId = 0)
  134. {
  135. $aPage = $this->aPage['wid_module'];
  136. if(empty($aPage) || !is_array($aPage))
  137. $aPage = $this->oDb->getPages(array('type' => 'by_page_name_full', 'value' => $sPage));
  138. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
  139. if(empty($aModule) || !is_array($aModule))
  140. return array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
  141. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], ((int)$aModule['enabled'] == 0 ? 'enable' : 'disable'), array('html_response' => true));
  142. if($aResult['code'] != 0)
  143. return $aResult;
  144. $this->aModule = BxDolModuleQuery::getInstance()->getModuleByName($this->sModule);
  145. $aResult = array('code' => 0, 'message' => _t('_adm_scs_operation_done'));
  146. if($iWidgetId == 0) {
  147. $aResult['content'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('page_content_2_col.html', [
  148. 'page_menu_code' => $this->getPageMenu(),
  149. 'page_main_code' => $this->getPageCode()
  150. ]);
  151. }
  152. else
  153. $aResult['widget'] = BxTemplStudioFunctions::getInstance()->getWidget($iWidgetId);
  154. return $aResult;
  155. }
  156. public function uninstall($sPage, $iWidgetId = 0)
  157. {
  158. $aPage = $this->aPage;
  159. if(empty($aPage) || !is_array($aPage))
  160. $aPage = $this->oDb->getPages(array('type' => 'by_page_name_full', 'value' => $sPage));
  161. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
  162. if(empty($aModule) || !is_array($aModule))
  163. return array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
  164. if($iWidgetId != 0 && (int)bx_get($this->sParamPrefix . '_confirmed') != 1)
  165. return array('code' => 2, 'popup' => $this->getPopupConfirmUninstall($iWidgetId, $aModule));
  166. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], 'uninstall', array('html_response' => true));
  167. if($aResult['code'] == BX_DOL_STUDIO_IU_RC_SUCCESS) {
  168. $aResult = array_merge($aResult, array(
  169. 'page' => $aPage['name'],
  170. 'widget_id' => $iWidgetId,
  171. 'eval' => $this->getPageJsObject() . ".onUninstall(oData);"
  172. ));
  173. BxTemplStudioMenuTop::historyDelete($aPage['name']);
  174. }
  175. return $aResult;
  176. }
  177. }
  178. /** @} */