BxBaseServiceWiki.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Services for Wiki functionality
  11. */
  12. class BxBaseServiceWiki extends BxDol
  13. {
  14. protected $_bIsApi;
  15. protected $_bJsCssAdded = false;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->_bIsApi = bx_is_api();
  20. }
  21. /**
  22. * @page service Service Calls
  23. * @section bx_system_general System Services
  24. * @subsection bx_system_general-wiki Wiki
  25. * @subsubsection bx_system_general-wiki_page wiki_page
  26. *
  27. * @code bx_srv('system', 'wiki_page', ["index"], 'TemplServiceWiki'); @endcode
  28. * @code {{~system:wiki_page:TemplServiceWiki["index"]~}} @endcode
  29. *
  30. * Display WIKI page.
  31. * @param $sUri categories object name
  32. *
  33. * @see BxBaseServiceWiki::serviceWikiPage
  34. */
  35. /**
  36. * @ref bx_system_general-wiki_page "wiki_page"
  37. */
  38. public function serviceWikiPage ($sWikiObjectUri, $sUri)
  39. {
  40. $oWiki = BxDolWiki::getObjectInstanceByUri($sWikiObjectUri);
  41. if(!$oWiki) {
  42. if($this->_bIsApi)
  43. return null;
  44. BxDolTemplate::getInstance()->displayPageNotFound();
  45. }
  46. $oPage = BxDolPage::getObjectInstanceByModuleAndURI($oWiki->getObjectName(), $sUri);
  47. if($oPage) {
  48. $_GET['i'] = $sUri;
  49. if($this->_bIsApi)
  50. return $oPage;
  51. $oPage->displayPage();
  52. }
  53. else {
  54. if($oWiki->isAllowed('add-page')) {
  55. if(($oPage = BxDolPage::getObjectInstanceByURI($sUri)) !== false) {
  56. if($this->_bIsApi) {
  57. $aPage = $oPage->getObject();
  58. if(($sUrl = $aPage['url'] ?? false))
  59. return bx_api_get_relative_url(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($aPage['url']));
  60. else
  61. return null;
  62. }
  63. BxDolTemplate::getInstance()->displayErrorOccured(_t("_sys_wiki_error_page_exists", bx_process_output($sUri)));
  64. }
  65. else {
  66. $oPage = BxDolPage::getObjectInstance('sys_wiki_add_page');
  67. if(!$oPage)
  68. return $this->_bIsApi ? null : BxDolTemplate::getInstance()->displayPageNotFound();
  69. $oPage->addMarkers([
  70. 'object' => $oWiki->getObjectName(),
  71. 'uri' => $sUri
  72. ]);
  73. if($this->_bIsApi)
  74. return $oPage;
  75. $this->_addCssJs(true);
  76. $oPage->displayPage();
  77. }
  78. }
  79. else {
  80. if($this->_bIsApi)
  81. return null;
  82. BxDolTemplate::getInstance()->displayPageNotFound();
  83. }
  84. }
  85. }
  86. /**
  87. * @page service Service Calls
  88. * @section bx_system_general System Services
  89. * @subsection bx_system_general-wiki Wiki
  90. * @subsubsection bx_system_general-wiki_add_page wiki_add_page
  91. *
  92. * @code bx_srv('system', 'wiki_add_page', [...], 'TemplServiceWiki'); @endcode
  93. *
  94. * Display add WIKI page.
  95. * @param $sObject wiki object name
  96. * @param $sUri new page URI
  97. *
  98. * @see BxBaseServiceWiki::serviceWikiAddPage
  99. */
  100. /**
  101. * @ref bx_system_general-wiki_add_page "wiki_add_page"
  102. */
  103. public function serviceWikiAddPage ($sObject, $sUri)
  104. {
  105. $oWiki = BxDolWiki::getObjectInstance($sObject);
  106. if(!$oWiki)
  107. return $this->_bIsApi ? [] : '';
  108. $sTitle = _t('_sys_wiki_add_page');
  109. $sText = _t('_sys_wiki_add_page_text');
  110. if($this->_bIsApi)
  111. return [bx_api_get_block('wiki_add_page', [
  112. 'title' => $sTitle,
  113. 'text' => $sText,
  114. 'request_url' => 'system/wiki_action/TemplServiceWiki&params[]=' . $oWiki->getUri() . '&params[]=add-page&params[]=' . $sUri
  115. ])];
  116. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_create_page.html', [
  117. 'page_uri' => bx_process_output($sUri),
  118. 'action_uri' => $oWiki->getUri(),
  119. 'create_page' => $sTitle,
  120. 'text' => $sText,
  121. ]);
  122. }
  123. /**
  124. * @page service Service Calls
  125. * @section bx_system_general System Services
  126. * @subsection bx_system_general-wiki Wiki
  127. * @subsubsection bx_system_general-wiki_action wiki_action
  128. *
  129. * @code bx_srv('system', 'wiki_action', [...], 'TemplServiceWiki'); @endcode
  130. *
  131. * Perform WIKI action.
  132. * @param $sWikiObjectUri wiki object URI
  133. *
  134. * @see BxBaseServiceWiki::serviceWikiAction
  135. */
  136. /**
  137. * @ref bx_system_general-wiki_action "wiki_action"
  138. */
  139. public function serviceWikiAction ($sWikiObjectUri, $sAction)
  140. {
  141. $oWiki = BxDolWiki::getObjectInstanceByUri($sWikiObjectUri);
  142. if(!$oWiki)
  143. 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]);
  144. $sMethod = 'action' . bx_gen_method_name($sAction, array('-'));
  145. if(!method_exists($oWiki, $sMethod) || !$oWiki->isAllowed($sAction))
  146. 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]);
  147. $mixed = call_user_func_array([$oWiki, $sMethod], array_slice(func_get_args(), 2));
  148. if($this->_bIsApi)
  149. return $mixed;
  150. if(is_array($mixed))
  151. echoJson($mixed);
  152. else
  153. echo $mixed;
  154. }
  155. /**
  156. * @page service Service Calls
  157. * @section bx_system_general System Services
  158. * @subsection bx_system_general-wiki Wiki
  159. * @subsubsection bx_system_general-wiki_controls wiki_controls
  160. *
  161. * Get wiki block controls panel
  162. *
  163. * @see BxBaseServiceWiki::serviceWikiControls
  164. */
  165. /**
  166. * @ref bx_system_general-wiki_controls "wiki_controls"
  167. */
  168. public function serviceWikiControls ($oWikiObject, $aWikiVer, $aWikiVerLatest, $iBlockId)
  169. {
  170. $this->_addCssJs();
  171. $sObject = $oWikiObject->getObjectName();
  172. $bAllowedManage = $oWikiObject->isAllowed('history');
  173. $mixedInfo = '';
  174. if($aWikiVer && $aWikiVerLatest['revision'] == $aWikiVer['revision']) {
  175. $mixedInfo = $this->_bIsApi ? [
  176. 'added' => $aWikiVer['added']
  177. ] : bx_time_js($aWikiVer['added']);
  178. }
  179. else if($aWikiVer) {
  180. $oProfile = BxDolProfile::getInstanceMagic($aWikiVer['profile_id']);
  181. $mixedInfo = $this->_bIsApi ? [
  182. 'revision' => $aWikiVer['revision'],
  183. 'author_data' => BxDolProfile::getData($aWikiVer['profile_id']),
  184. 'added' => $aWikiVer['added']
  185. ] : _t('_sys_wiki_view_rev', $aWikiVer['revision'], $oProfile->getUrl(), $oProfile->getDisplayName(), bx_time_js($aWikiVer['added']));
  186. }
  187. if($this->_bIsApi) {
  188. $mixedMenu = '';
  189. if($bAllowedManage) {
  190. $oMenu = BxTemplMenu::getObjectInstance('sys_wiki');
  191. $oMenu->setParams($sObject, $iBlockId);
  192. $mixedMenu = $oMenu->getCodeAPI();
  193. }
  194. return [
  195. 'info' => $mixedInfo,
  196. 'menu' => $mixedMenu
  197. ];
  198. }
  199. $o = BxDolTemplate::getInstance();
  200. $o->addJs('stackedit.js/stackedit.min.js');
  201. return $o->parseHtmlByName('wiki_controls.html', [
  202. 'obj' => $sObject,
  203. 'block_id' => $iBlockId,
  204. 'info' => $mixedInfo,
  205. 'options' => json_encode([
  206. 'block_id' => $iBlockId,
  207. 'language' => isset($aWikiVer['language']) ? $aWikiVer['language'] : bx_lang_name(),
  208. 'wiki_action_uri' => $oWikiObject->getUri(),
  209. 't_confirm_block_deletion' => _t('_sys_wiki_confirm_block_deletion'),
  210. ]),
  211. 'bx_if:menu' => [
  212. 'condition' => $bAllowedManage,
  213. 'content' => [
  214. 'obj' => $sObject,
  215. 'block_id' => $iBlockId,
  216. ],
  217. ],
  218. ]);
  219. }
  220. /**
  221. * @page service Service Calls
  222. * @section bx_system_general System Services
  223. * @subsection bx_system_general-wiki Wiki
  224. * @subsubsection bx_system_general-wiki_add_block wiki_add_block
  225. *
  226. * Get "add wiki block" panel
  227. *
  228. * @see BxBaseServiceWiki::serviceWikiAddBlock
  229. */
  230. /**
  231. * @ref bx_system_general-wiki_add_block "wiki_add_block"
  232. */
  233. public function serviceWikiAddBlock ($oWikiObject, $sPageObject, $sCellId)
  234. {
  235. $this->_addCssJs();
  236. $aMatches = [];
  237. if(!preg_match("/cell_(\d+)/", $sCellId, $aMatches))
  238. return '';
  239. $iCellId = $aMatches[1];
  240. $sTxtAddBlock = _t('_sys_wiki_add_block');
  241. if($this->_bIsApi) {
  242. $sUri = $oWikiObject->getUri();
  243. return [
  244. 'id' => $sUri . '_add_block',
  245. 'module' => $oWikiObject->getModule(),
  246. 'title' => '',
  247. 'description' => '',
  248. 'icon' => '',
  249. 'designbox_id' => 0,
  250. 'hidden_on' => false,
  251. 'content' => [bx_api_get_block('wiki_add_block', [
  252. 'title' => $sTxtAddBlock,
  253. 'request_url' => 'system/wiki_action/TemplServiceWiki&params[]=' . $sUri . '&params[]=add&params[]=' . $sPageObject . '&params[]=' . $iCellId
  254. ])],
  255. 'content_empty' => '',
  256. 'config_api' => '',
  257. 'menu' => '',
  258. 'source' => 'system:block_' . $sUri . '_add_block'
  259. ];
  260. }
  261. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_add_block.html', [
  262. 'add_block' => $sTxtAddBlock,
  263. 'page' => $sPageObject,
  264. 'cell_id' => $iCellId,
  265. 'action_uri' => $oWikiObject->getUri(),
  266. ]);
  267. }
  268. /**
  269. * @page service Service Calls
  270. * @section bx_system_general System Services
  271. * @subsection bx_system_general-wiki Wiki
  272. * @subsubsection bx_system_general-get_design_boxes get_design_boxes
  273. *
  274. * Get "design boxes" array
  275. *
  276. * @see BxBaseServiceWiki::serviceGetDesignBoxes
  277. */
  278. /**
  279. * @ref bx_system_general-get_design_boxes "get_design_boxes"
  280. */
  281. public function serviceGetDesignBoxes ()
  282. {
  283. $o = new BxDolStudioBuilderPageQuery();
  284. $aItems = array();
  285. if (!$o->getDesignBoxes(array('type' => 'all'), $aItems, false))
  286. return array();
  287. $a = array();
  288. foreach($aItems as $r)
  289. $a[$r['id']] = _t($r['title']);
  290. return $a;
  291. }
  292. /**
  293. * @page service Service Calls
  294. * @section bx_system_general System Services
  295. * @subsection bx_system_general-wiki Wiki
  296. * @subsubsection bx_system_general-pages_list pages_list
  297. *
  298. * Get list of pages in current module
  299. *
  300. * @see BxBaseServiceWiki::servicePagesList
  301. */
  302. /**
  303. * @ref bx_system_general-pages_list "pages_list"
  304. */
  305. public function servicePagesList ()
  306. {
  307. if (!($sPageObject = BxDolPageQuery::getPageObjectNameByURI(bx_get('i'))))
  308. return '';
  309. if (!($aPage = BxDolPageQuery::getPageObject ($sPageObject)))
  310. return '';
  311. if (!($oWiki = BxDolWiki::getObjectInstance($aPage['module'])))
  312. return '';
  313. BxDolTemplate::getInstance()->addCss(['wiki.css']);
  314. return $oWiki->getContents();
  315. }
  316. /**
  317. * @page service Service Calls
  318. * @section bx_system_general System Services
  319. * @subsection bx_system_general-wiki Wiki
  320. * @subsubsection bx_system_general-page_contents page_contents
  321. *
  322. * Get table of contents for current page, generated automatically from paragraphs headers
  323. *
  324. * @see BxBaseServiceWiki::servicePageContents
  325. */
  326. /**
  327. * @ref bx_system_general-page_contents "page_contents"
  328. */
  329. public function servicePageContents ()
  330. {
  331. $o = BxDolTemplate::getInstance();
  332. $o->addJs('toc.js');
  333. return $o->parseHtmlByName('wiki_page_contents.html', []);
  334. }
  335. protected function _addCssJs ($bAddPage = false)
  336. {
  337. if ($this->_bJsCssAdded)
  338. return false;
  339. $o = BxDolTemplate::getInstance();
  340. $o->addCss(['wiki.css']);
  341. $o->addJs('BxDolWiki.js');
  342. $o->addJsTranslation('_sys_wiki_external_editor_references_comment');
  343. if ($bAddPage)
  344. $o->addJs('studio/js/|forms.js');
  345. $this->_bJsCssAdded = true;
  346. }
  347. }
  348. /** @} */