1
0

BxDolStudioModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. 'icon' => 'power',
  75. 'checked' => (int)$this->aModule['enabled'] == 1,
  76. 'onchange' => "javascript:" . $this->getPageJsObject() . ".activate(this, '" . $this->aPage['name'] . "', {widget_id})"
  77. ), false);
  78. // Uninstall, for app managers, in the context menu only (the launcher tile's settings form has no place for it). It goes
  79. // with the app's launcher tile id, which is what makes the server ask for confirmation first.
  80. if($this->sModule != BX_DOL_STUDIO_MODULE_SYSTEM && !empty($this->aPage['wid_id']) && BxDolStudioRolesUtils::getInstance()->isActionAllowed(BX_SRA_MANAGE_APPS))
  81. $this->addAction(array(
  82. 'type' => 'button',
  83. 'name' => 'uninstall',
  84. 'caption' => '_adm_txt_uninstall',
  85. 'icon' => 'trash-2',
  86. 'danger' => true,
  87. 'context_only' => true,
  88. 'onclick' => "javascript:" . $this->getPageJsObject() . ".uninstall('" . $this->aPage['name'] . "', " . (int)$this->aPage['wid_id'] . ", 0)"
  89. ));
  90. }
  91. public function checkAction()
  92. {
  93. $sAction = bx_get($this->sParamPrefix . '_action');
  94. if($sAction === false)
  95. return false;
  96. if(empty($this->aModule) || !is_array($this->aModule))
  97. return array('code' => 1, 'message' => _t('_sys_request_page_not_found_cpt'));
  98. $sAction = bx_process_input($sAction);
  99. $aResult = array('code' => 2, 'message' => _t('_adm_mod_err_cannot_process_action'));
  100. switch($sAction) {
  101. case 'settings':
  102. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  103. $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
  104. if(empty($sValue) || empty($iWidgetId))
  105. break;
  106. $aResult = $this->settings($sValue, $iWidgetId);
  107. break;
  108. case 'activate':
  109. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  110. if(empty($sValue))
  111. break;
  112. $iWidgetId = (int)bx_get($this->sParamPrefix . '_widget_id');
  113. $aResult = $this->activate($sValue, $iWidgetId);
  114. break;
  115. case 'uninstall':
  116. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  117. if(empty($sValue))
  118. break;
  119. $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
  120. $aResult = $this->uninstall($sValue, $iWidgetId);
  121. break;
  122. case 'context':
  123. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  124. if(empty($sValue))
  125. break;
  126. $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
  127. $aResult = $this->context($sValue, $iWidgetId);
  128. break;
  129. }
  130. return $aResult;
  131. }
  132. public function settings($sPage, $iWidgetId)
  133. {
  134. $aResultError = array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
  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 $aResultError;
  141. $sPopupSettings = $this->getPopupSettings($sPage, $iWidgetId);
  142. if(empty($sPopupSettings))
  143. return $aResultError;
  144. return array('code' => 0, 'popup' => array(
  145. 'html' => $sPopupSettings,
  146. 'options' => array(
  147. 'closeOnOuterClick' => true,
  148. 'removeOnClose' => true,
  149. 'pointer' => array(
  150. 'el' => '#bx-std-widget-' . $iWidgetId . ' .bx-std-wa-settings',
  151. )
  152. )
  153. ));
  154. }
  155. /**
  156. * The page's context menu for its launcher tile or dock item (studio/js/context_menu.js fetches it on the first right-click).
  157. */
  158. public function context($sPage, $iWidgetId = 0)
  159. {
  160. if(empty($iWidgetId) && !empty($this->aPage['wid_id']))
  161. $iWidgetId = (int)$this->aPage['wid_id'];
  162. $sPopup = $this->getPopupContextMenu($sPage, $iWidgetId);
  163. if(empty($sPopup))
  164. return array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
  165. return array('code' => 0, 'content' => $sPopup);
  166. }
  167. public function activate($sPage, $iWidgetId = 0)
  168. {
  169. $oDbModule = BxDolModuleQuery::getInstance();
  170. $aPage = $this->aPage;
  171. if(empty($aPage) || !is_array($aPage) || $aPage['name'] != $sPage)
  172. $aPage = $this->oDb->getPages(['type' => 'by_page_name_full', 'value' => $sPage]);
  173. $aModule = $oDbModule->getModuleByName($aPage['wid_module']);
  174. if(empty($aModule) || !is_array($aModule))
  175. return ['code' => 1, 'message' => _t('_adm_err_operation_failed')];
  176. $iEnabled = (int)$aModule['enabled'];
  177. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], ($iEnabled == 0 ? 'enable' : 'disable'), array('html_response' => true));
  178. if($aResult['code'] != 0)
  179. return $aResult;
  180. $this->aModule = $oDbModule->getModuleByName($this->sModule);
  181. $iEnabled = 1 - $iEnabled;
  182. $aResult = ['code' => 0, 'message' => _t('_adm_scs_operation_done'), 'page' => $aPage['name'], 'enabled' => $iEnabled];
  183. if($iWidgetId == 0) {
  184. // the app's own page redraws itself; from the dock on another page there is nothing to redraw
  185. $aResult['content'] = '';
  186. if($this->sModule == $aPage['wid_module'] && (!$this->_bShowHeaderBreadcrumb || $iEnabled != 0))
  187. $aResult['content'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('page_content_2_col.html', [
  188. 'page_menu_code' => $this->getPageMenu(),
  189. 'page_main_code' => $this->getPageCode()
  190. ]);
  191. }
  192. else
  193. $aResult['widget'] = BxTemplStudioFunctions::getInstance()->getWidget($iWidgetId);
  194. return $aResult;
  195. }
  196. public function uninstall($sPage, $iWidgetId = 0)
  197. {
  198. $aPage = $this->aPage;
  199. if(empty($aPage) || !is_array($aPage))
  200. $aPage = $this->oDb->getPages(array('type' => 'by_page_name_full', 'value' => $sPage));
  201. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
  202. if(empty($aModule) || !is_array($aModule))
  203. return array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
  204. if($iWidgetId != 0 && (int)bx_get($this->sParamPrefix . '_confirmed') != 1)
  205. return array('code' => 2, 'popup' => $this->getPopupConfirmUninstall($iWidgetId, $aModule));
  206. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], 'uninstall', array('html_response' => true));
  207. if($aResult['code'] == BX_DOL_STUDIO_IU_RC_SUCCESS) {
  208. $aResult = array_merge($aResult, array(
  209. 'page' => $aPage['name'],
  210. 'widget_id' => $iWidgetId,
  211. 'eval' => $this->getPageJsObject() . ".onUninstall(oData);"
  212. ));
  213. BxTemplStudioMenuTop::historyDelete($aPage['name']);
  214. }
  215. return $aResult;
  216. }
  217. }
  218. /** @} */