BxDolStudioModule.php 8.4 KB

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