| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaBaseView UNA Base Representation Classes
- * @{
- */
- /**
- * Services for Wiki functionality
- */
- class BxBaseServiceWiki extends BxDol
- {
- protected $_bIsApi;
- protected $_bJsCssAdded = false;
- public function __construct()
- {
- parent::__construct();
- $this->_bIsApi = bx_is_api();
- }
-
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-wiki_page wiki_page
- *
- * @code bx_srv('system', 'wiki_page', ["index"], 'TemplServiceWiki'); @endcode
- * @code {{~system:wiki_page:TemplServiceWiki["index"]~}} @endcode
- *
- * Display WIKI page.
- * @param $sUri categories object name
- *
- * @see BxBaseServiceWiki::serviceWikiPage
- */
- /**
- * @ref bx_system_general-wiki_page "wiki_page"
- */
- public function serviceWikiPage ($sWikiObjectUri, $sUri)
- {
- $oWiki = BxDolWiki::getObjectInstanceByUri($sWikiObjectUri);
- if(!$oWiki) {
- if($this->_bIsApi)
- return null;
- BxDolTemplate::getInstance()->displayPageNotFound();
- }
- $oPage = BxDolPage::getObjectInstanceByModuleAndURI($oWiki->getObjectName(), $sUri);
- if($oPage) {
- $_GET['i'] = $sUri;
- if($this->_bIsApi)
- return $oPage;
- $oPage->displayPage();
- }
- else {
- if($oWiki->isAllowed('add-page')) {
- if(($oPage = BxDolPage::getObjectInstanceByURI($sUri)) !== false) {
- if($this->_bIsApi) {
- $aPage = $oPage->getObject();
- if(($sUrl = $aPage['url'] ?? false))
- return bx_api_get_relative_url(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($aPage['url']));
- else
- return null;
- }
- BxDolTemplate::getInstance()->displayErrorOccured(_t("_sys_wiki_error_page_exists", bx_process_output($sUri)));
- }
- else {
- $oPage = BxDolPage::getObjectInstance('sys_wiki_add_page');
- if(!$oPage)
- return $this->_bIsApi ? null : BxDolTemplate::getInstance()->displayPageNotFound();
- $oPage->addMarkers([
- 'object' => $oWiki->getObjectName(),
- 'uri' => $sUri
- ]);
- if($this->_bIsApi)
- return $oPage;
- $this->_addCssJs(true);
- $oPage->displayPage();
- }
- }
- else {
- if($this->_bIsApi)
- return null;
- BxDolTemplate::getInstance()->displayPageNotFound();
- }
- }
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-wiki_add_page wiki_add_page
- *
- * @code bx_srv('system', 'wiki_add_page', [...], 'TemplServiceWiki'); @endcode
- *
- * Display add WIKI page.
- * @param $sObject wiki object name
- * @param $sUri new page URI
- *
- * @see BxBaseServiceWiki::serviceWikiAddPage
- */
- /**
- * @ref bx_system_general-wiki_add_page "wiki_add_page"
- */
- public function serviceWikiAddPage ($sObject, $sUri)
- {
- $oWiki = BxDolWiki::getObjectInstance($sObject);
- if(!$oWiki)
- return $this->_bIsApi ? [] : '';
- $sTitle = _t('_sys_wiki_add_page');
- $sText = _t('_sys_wiki_add_page_text');
-
- if($this->_bIsApi)
- return [bx_api_get_block('wiki_add_page', [
- 'title' => $sTitle,
- 'text' => $sText,
- 'request_url' => 'system/wiki_action/TemplServiceWiki¶ms[]=' . $oWiki->getUri() . '¶ms[]=add-page¶ms[]=' . $sUri
- ])];
- return BxDolTemplate::getInstance()->parseHtmlByName('wiki_create_page.html', [
- 'page_uri' => bx_process_output($sUri),
- 'action_uri' => $oWiki->getUri(),
- 'create_page' => $sTitle,
- 'text' => $sText,
- ]);
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-wiki_action wiki_action
- *
- * @code bx_srv('system', 'wiki_action', [...], 'TemplServiceWiki'); @endcode
- *
- * Perform WIKI action.
- * @param $sWikiObjectUri wiki object URI
- *
- * @see BxBaseServiceWiki::serviceWikiAction
- */
- /**
- * @ref bx_system_general-wiki_action "wiki_action"
- */
- public function serviceWikiAction ($sWikiObjectUri, $sAction)
- {
- $oWiki = BxDolWiki::getObjectInstanceByUri($sWikiObjectUri);
- if(!$oWiki)
- return ($sMsg = _t('_sys_wiki_error_missing_wiki_object', $sWikiObjectUri)) && $this->_bIsApi ? [bx_api_get_msg($sMsg, ['ext' => ['code' => 1]])] : echoJson(['code' => 1, 'actions' => 'ShowMsg', 'msg' => $sMsg]);
- $sMethod = 'action' . bx_gen_method_name($sAction, array('-'));
- if(!method_exists($oWiki, $sMethod) || !$oWiki->isAllowed($sAction))
- return ($sMsg = _t('_sys_wiki_error_action_not_allowed', $sAction, $sWikiObjectUri)) && $this->_bIsApi ? [bx_api_get_msg($sMsg, ['ext' => ['code' => 2]])] : echoJson(['code' => 2, 'actions' => 'ShowMsg', 'msg' => $sMsg]);
- $mixed = call_user_func_array([$oWiki, $sMethod], array_slice(func_get_args(), 2));
- if($this->_bIsApi)
- return $mixed;
- if(is_array($mixed))
- echoJson($mixed);
- else
- echo $mixed;
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-wiki_controls wiki_controls
- *
- * Get wiki block controls panel
- *
- * @see BxBaseServiceWiki::serviceWikiControls
- */
- /**
- * @ref bx_system_general-wiki_controls "wiki_controls"
- */
- public function serviceWikiControls ($oWikiObject, $aWikiVer, $aWikiVerLatest, $iBlockId)
- {
- $this->_addCssJs();
- $sObject = $oWikiObject->getObjectName();
- $bAllowedManage = $oWikiObject->isAllowed('history');
- $mixedInfo = '';
- if($aWikiVer && $aWikiVerLatest['revision'] == $aWikiVer['revision']) {
- $mixedInfo = $this->_bIsApi ? [
- 'added' => $aWikiVer['added']
- ] : bx_time_js($aWikiVer['added']);
- }
- else if($aWikiVer) {
- $oProfile = BxDolProfile::getInstanceMagic($aWikiVer['profile_id']);
- $mixedInfo = $this->_bIsApi ? [
- 'revision' => $aWikiVer['revision'],
- 'author_data' => BxDolProfile::getData($aWikiVer['profile_id']),
- 'added' => $aWikiVer['added']
- ] : _t('_sys_wiki_view_rev', $aWikiVer['revision'], $oProfile->getUrl(), $oProfile->getDisplayName(), bx_time_js($aWikiVer['added']));
- }
- if($this->_bIsApi) {
- $mixedMenu = '';
- if($bAllowedManage) {
- $oMenu = BxTemplMenu::getObjectInstance('sys_wiki');
- $oMenu->setParams($sObject, $iBlockId);
- $mixedMenu = $oMenu->getCodeAPI();
- }
- return [
- 'info' => $mixedInfo,
- 'menu' => $mixedMenu
- ];
- }
- $o = BxDolTemplate::getInstance();
- $o->addJs('stackedit.js/stackedit.min.js');
- return $o->parseHtmlByName('wiki_controls.html', [
- 'obj' => $sObject,
- 'block_id' => $iBlockId,
- 'info' => $mixedInfo,
- 'options' => json_encode([
- 'block_id' => $iBlockId,
- 'language' => isset($aWikiVer['language']) ? $aWikiVer['language'] : bx_lang_name(),
- 'wiki_action_uri' => $oWikiObject->getUri(),
- 't_confirm_block_deletion' => _t('_sys_wiki_confirm_block_deletion'),
- ]),
- 'bx_if:menu' => [
- 'condition' => $bAllowedManage,
- 'content' => [
- 'obj' => $sObject,
- 'block_id' => $iBlockId,
- ],
- ],
- ]);
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-wiki_add_block wiki_add_block
- *
- * Get "add wiki block" panel
- *
- * @see BxBaseServiceWiki::serviceWikiAddBlock
- */
- /**
- * @ref bx_system_general-wiki_add_block "wiki_add_block"
- */
- public function serviceWikiAddBlock ($oWikiObject, $sPageObject, $sCellId)
- {
- $this->_addCssJs();
- $aMatches = [];
- if(!preg_match("/cell_(\d+)/", $sCellId, $aMatches))
- return '';
- $iCellId = $aMatches[1];
- $sTxtAddBlock = _t('_sys_wiki_add_block');
- if($this->_bIsApi) {
- $sUri = $oWikiObject->getUri();
- return [
- 'id' => $sUri . '_add_block',
- 'module' => $oWikiObject->getModule(),
- 'title' => '',
- 'description' => '',
- 'icon' => '',
- 'designbox_id' => 0,
- 'hidden_on' => false,
- 'content' => [bx_api_get_block('wiki_add_block', [
- 'title' => $sTxtAddBlock,
- 'request_url' => 'system/wiki_action/TemplServiceWiki¶ms[]=' . $sUri . '¶ms[]=add¶ms[]=' . $sPageObject . '¶ms[]=' . $iCellId
- ])],
- 'content_empty' => '',
- 'config_api' => '',
- 'menu' => '',
- 'source' => 'system:block_' . $sUri . '_add_block'
- ];
- }
- return BxDolTemplate::getInstance()->parseHtmlByName('wiki_add_block.html', [
- 'add_block' => $sTxtAddBlock,
- 'page' => $sPageObject,
- 'cell_id' => $iCellId,
- 'action_uri' => $oWikiObject->getUri(),
- ]);
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-get_design_boxes get_design_boxes
- *
- * Get "design boxes" array
- *
- * @see BxBaseServiceWiki::serviceGetDesignBoxes
- */
- /**
- * @ref bx_system_general-get_design_boxes "get_design_boxes"
- */
- public function serviceGetDesignBoxes ()
- {
- $o = new BxDolStudioBuilderPageQuery();
- $aItems = array();
- if (!$o->getDesignBoxes(array('type' => 'all'), $aItems, false))
- return array();
- $a = array();
- foreach($aItems as $r)
- $a[$r['id']] = _t($r['title']);
- return $a;
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-pages_list pages_list
- *
- * Get list of pages in current module
- *
- * @see BxBaseServiceWiki::servicePagesList
- */
- /**
- * @ref bx_system_general-pages_list "pages_list"
- */
- public function servicePagesList ()
- {
- if (!($sPageObject = BxDolPageQuery::getPageObjectNameByURI(bx_get('i'))))
- return '';
- if (!($aPage = BxDolPageQuery::getPageObject ($sPageObject)))
- return '';
- if (!($oWiki = BxDolWiki::getObjectInstance($aPage['module'])))
- return '';
- BxDolTemplate::getInstance()->addCss(['wiki.css']);
- return $oWiki->getContents();
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-wiki Wiki
- * @subsubsection bx_system_general-page_contents page_contents
- *
- * Get table of contents for current page, generated automatically from paragraphs headers
- *
- * @see BxBaseServiceWiki::servicePageContents
- */
- /**
- * @ref bx_system_general-page_contents "page_contents"
- */
- public function servicePageContents ()
- {
- $o = BxDolTemplate::getInstance();
- $o->addJs('toc.js');
- return $o->parseHtmlByName('wiki_page_contents.html', []);
- }
- protected function _addCssJs ($bAddPage = false)
- {
- if ($this->_bJsCssAdded)
- return false;
- $o = BxDolTemplate::getInstance();
- $o->addCss(['wiki.css']);
- $o->addJs('BxDolWiki.js');
- $o->addJsTranslation('_sys_wiki_external_editor_references_comment');
- if ($bAddPage)
- $o->addJs('studio/js/|forms.js');
- $this->_bJsCssAdded = true;
- }
- }
- /** @} */
|