| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaStudio UNA Studio
- * @{
- */
- bx_import('BxDolStudioUtils');
- define('BX_DOL_STUDIO_MOD_TYPE_SETTINGS', 'settings');
- define('BX_DOL_STUDIO_MOD_TYPE_DEFAULT', BX_DOL_STUDIO_MOD_TYPE_SETTINGS);
- class BxDolStudioModule extends BxTemplStudioWidget
- {
- protected $sLangPrefix;
- protected $sParamPrefix;
- protected $sModule;
- protected $aModule;
- protected $sPage;
- protected $sPageDefault = BX_DOL_STUDIO_MOD_TYPE_DEFAULT;
- protected $sManageUrl;
- function __construct($sModule, $mixedPageName, $sPage = "")
- {
- parent::__construct($mixedPageName);
- $this->sLangPrefix = 'mod';
- $this->sParamPrefix = 'mod';
- $this->sPageRssHelpObject = 'sys_studio_module_help';
- $this->sModule = '';
- if(is_string($sModule) && !empty($sModule))
- $this->sModule = $sModule;
- $this->sPage = $this->sPageDefault;
- if(is_string($sPage) && !empty($sPage))
- $this->sPage = $sPage;
- $this->sManageUrl = '';
- if($this->bPage)
- $this->sManageUrl = BX_DOL_URL_STUDIO . 'module.php?name=' . $this->aPage['name'];
- }
- public static function getObjectInstance($mixedPageName, $sPage = "", $bInit = true)
- {
- $sModule = BX_DOL_STUDIO_MODULE_SYSTEM;
- $sClass = get_called_class();
- $aPage = BxDolStudioPageQuery::getInstance()->getPages(array('type' => 'by_page_name_full', 'value' => $mixedPageName));
- if(!empty($aPage) && is_array($aPage)) {
- $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
- if(!empty($aModule) && is_array($aModule)) {
- $sModule = $aModule['name'];
- if(file_exists(BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'classes/' . $aModule['class_prefix'] . 'StudioPage.php')) {
- bx_import('StudioPage', $aModule);
- $sClass = $aModule['class_prefix'] . 'StudioPage';
- }
- }
- }
- $oObject = new $sClass($sModule, $mixedPageName, $sPage);
- if($bInit)
- $oObject->init();
- return $oObject;
- }
- public function init()
- {
- $this->aModule = BxDolModuleQuery::getInstance()->getModuleByName($this->sModule);
- if(empty($this->aModule) || !is_array($this->aModule))
- BxDolStudioTemplate::getInstance()->displayPageNotFound();
- $this->sPageRssHelpUrl = $this->aModule['help_url'];
- $this->sPageRssHelpId = $this->aModule['name'];
- $this->addMarkers(array(
- 'module_name' => $this->aModule['name'],
- 'module_uri' => $this->aModule['uri'],
- 'module_title' => $this->aModule['title'],
- ));
- if($this->sModule != BX_DOL_STUDIO_MODULE_SYSTEM)
- $this->addAction(array(
- 'type' => 'switcher',
- 'name' => 'activate',
- 'caption' => '_adm_txt_pca_active',
- 'icon' => 'power',
- 'checked' => (int)$this->aModule['enabled'] == 1,
- 'onchange' => "javascript:" . $this->getPageJsObject() . ".activate(this, '" . $this->aPage['name'] . "', {widget_id})"
- ), false);
- // Uninstall, for app managers, in the context menu only (the launcher tile's settings form has no place for it). It goes
- // with the app's launcher tile id, which is what makes the server ask for confirmation first.
- if($this->sModule != BX_DOL_STUDIO_MODULE_SYSTEM && !empty($this->aPage['wid_id']) && BxDolStudioRolesUtils::getInstance()->isActionAllowed(BX_SRA_MANAGE_APPS))
- $this->addAction(array(
- 'type' => 'button',
- 'name' => 'uninstall',
- 'caption' => '_adm_txt_uninstall',
- 'icon' => 'trash-2',
- 'danger' => true,
- 'context_only' => true,
- 'onclick' => "javascript:" . $this->getPageJsObject() . ".uninstall('" . $this->aPage['name'] . "', " . (int)$this->aPage['wid_id'] . ", 0)"
- ));
- }
- public function checkAction()
- {
- $sAction = bx_get($this->sParamPrefix . '_action');
- if($sAction === false)
- return false;
- if(empty($this->aModule) || !is_array($this->aModule))
- return array('code' => 1, 'message' => _t('_sys_request_page_not_found_cpt'));
- $sAction = bx_process_input($sAction);
- $aResult = array('code' => 2, 'message' => _t('_adm_mod_err_cannot_process_action'));
- switch($sAction) {
- case 'settings':
- $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
- $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
- if(empty($sValue) || empty($iWidgetId))
- break;
- $aResult = $this->settings($sValue, $iWidgetId);
- break;
- case 'activate':
- $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
- if(empty($sValue))
- break;
- $iWidgetId = (int)bx_get($this->sParamPrefix . '_widget_id');
- $aResult = $this->activate($sValue, $iWidgetId);
- break;
- case 'uninstall':
- $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
- if(empty($sValue))
- break;
- $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
- $aResult = $this->uninstall($sValue, $iWidgetId);
- break;
- case 'context':
- $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
- if(empty($sValue))
- break;
- $iWidgetId = bx_process_input(bx_get($this->sParamPrefix . '_widget_id'), BX_DATA_INT);
- $aResult = $this->context($sValue, $iWidgetId);
- break;
- }
- return $aResult;
- }
-
- public function settings($sPage, $iWidgetId)
- {
- $aResultError = array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
- $aPage = $this->aPage['wid_module'];
- if(empty($aPage) || !is_array($aPage))
- $aPage = $this->oDb->getPages(array('type' => 'by_page_name_full', 'value' => $sPage));
- $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
- if(empty($aModule) || !is_array($aModule))
- return $aResultError;
- $sPopupSettings = $this->getPopupSettings($sPage, $iWidgetId);
- if(empty($sPopupSettings))
- return $aResultError;
- return array('code' => 0, 'popup' => array(
- 'html' => $sPopupSettings,
- 'options' => array(
- 'closeOnOuterClick' => true,
- 'removeOnClose' => true,
- 'pointer' => array(
- 'el' => '#bx-std-widget-' . $iWidgetId . ' .bx-std-wa-settings',
- )
- )
- ));
- }
- /**
- * The page's context menu for its launcher tile or dock item (studio/js/context_menu.js fetches it on the first right-click).
- */
- public function context($sPage, $iWidgetId = 0)
- {
- if(empty($iWidgetId) && !empty($this->aPage['wid_id']))
- $iWidgetId = (int)$this->aPage['wid_id'];
- $sPopup = $this->getPopupContextMenu($sPage, $iWidgetId);
- if(empty($sPopup))
- return array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
- return array('code' => 0, 'content' => $sPopup);
- }
- public function activate($sPage, $iWidgetId = 0)
- {
- $oDbModule = BxDolModuleQuery::getInstance();
- $aPage = $this->aPage;
- if(empty($aPage) || !is_array($aPage) || $aPage['name'] != $sPage)
- $aPage = $this->oDb->getPages(['type' => 'by_page_name_full', 'value' => $sPage]);
- $aModule = $oDbModule->getModuleByName($aPage['wid_module']);
- if(empty($aModule) || !is_array($aModule))
- return ['code' => 1, 'message' => _t('_adm_err_operation_failed')];
- $iEnabled = (int)$aModule['enabled'];
- $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], ($iEnabled == 0 ? 'enable' : 'disable'), array('html_response' => true));
- if($aResult['code'] != 0)
- return $aResult;
- $this->aModule = $oDbModule->getModuleByName($this->sModule);
- $iEnabled = 1 - $iEnabled;
- $aResult = ['code' => 0, 'message' => _t('_adm_scs_operation_done'), 'page' => $aPage['name'], 'enabled' => $iEnabled];
- if($iWidgetId == 0) {
- // the app's own page redraws itself; from the dock on another page there is nothing to redraw
- $aResult['content'] = '';
- if($this->sModule == $aPage['wid_module'] && (!$this->_bShowHeaderBreadcrumb || $iEnabled != 0))
- $aResult['content'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('page_content_2_col.html', [
- 'page_menu_code' => $this->getPageMenu(),
- 'page_main_code' => $this->getPageCode()
- ]);
- }
- else
- $aResult['widget'] = BxTemplStudioFunctions::getInstance()->getWidget($iWidgetId);
- return $aResult;
- }
- public function uninstall($sPage, $iWidgetId = 0)
- {
- $aPage = $this->aPage;
- if(empty($aPage) || !is_array($aPage))
- $aPage = $this->oDb->getPages(array('type' => 'by_page_name_full', 'value' => $sPage));
- $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aPage['wid_module']);
- if(empty($aModule) || !is_array($aModule))
- return array('code' => 1, 'message' => _t('_adm_err_operation_failed'));
- if($iWidgetId != 0 && (int)bx_get($this->sParamPrefix . '_confirmed') != 1)
- return array('code' => 2, 'popup' => $this->getPopupConfirmUninstall($iWidgetId, $aModule));
- $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], 'uninstall', array('html_response' => true));
- if($aResult['code'] == BX_DOL_STUDIO_IU_RC_SUCCESS) {
- $aResult = array_merge($aResult, array(
- 'page' => $aPage['name'],
- 'widget_id' => $iWidgetId,
- 'eval' => $this->getPageJsObject() . ".onUninstall(oData);"
- ));
- BxTemplStudioMenuTop::historyDelete($aPage['name']);
- }
- return $aResult;
- }
- }
- /** @} */
|