BxBaseServiceWiki.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 $_bJsCssAdded = false;
  15. /**
  16. * @page service Service Calls
  17. * @section bx_system_general System Services
  18. * @subsection bx_system_general-wiki Wiki
  19. * @subsubsection bx_system_general-wiki_page wiki_page
  20. *
  21. * @code bx_srv('system', 'wiki_page', ["index"], 'TemplServiceWiki'); @endcode
  22. * @code {{~system:wiki_page:TemplServiceWiki["index"]~}} @endcode
  23. *
  24. * Display WIKI page.
  25. * @param $sUri categories object name
  26. *
  27. * @see BxBaseServiceWiki::serviceWikiPage
  28. */
  29. /**
  30. * @ref bx_system_general-wiki_page "wiki_page"
  31. */
  32. public function serviceWikiPage ($sWikiObjectUri, $sUri)
  33. {
  34. $oWiki = BxDolWiki::getObjectInstanceByUri($sWikiObjectUri);
  35. if (!$oWiki) {
  36. $oTemplate = BxDolTemplate::getInstance();
  37. $oTemplate->displayPageNotFound();
  38. return;
  39. }
  40. $oPage = BxDolPage::getObjectInstanceByModuleAndURI($oWiki->getObjectName(), $sUri);
  41. if ($oPage) {
  42. $oPage->displayPage();
  43. } else {
  44. if ($oWiki->isAllowed('add-page')) {
  45. $oPage = BxDolPage::getObjectInstanceByURI($sUri);
  46. $oTemplate = BxDolTemplate::getInstance();
  47. if ($oPage) {
  48. $oTemplate->displayErrorOccured(_t("_sys_wiki_error_page_exists", bx_process_output($sUri)));
  49. } else {
  50. $this->_addCssJs (true);
  51. $s = $oTemplate->parseHtmlByName('wiki_create_page.html', array(
  52. 'page_uri' => bx_process_output($sUri),
  53. 'action_uri' => $oWiki->getWikiUri(),
  54. 'create_page' => _t('_sys_wiki_add_page'),
  55. 'text' => _t('_sys_wiki_add_page_text'),
  56. ));
  57. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  58. $oTemplate->setPageHeader (_t('_sys_wiki_add_page'));
  59. $oTemplate->setPageContent ('page_main_code', $s);
  60. $oTemplate->getPageCode();
  61. }
  62. }
  63. else {
  64. $oTemplate = BxDolTemplate::getInstance();
  65. $oTemplate->displayPageNotFound();
  66. }
  67. }
  68. }
  69. /**
  70. * @page service Service Calls
  71. * @section bx_system_general System Services
  72. * @subsection bx_system_general-wiki Wiki
  73. * @subsubsection bx_system_general-wiki_action wiki_action
  74. *
  75. * @code bx_srv('system', 'wiki_action', [...], 'TemplServiceWiki'); @endcode
  76. *
  77. * Perform WIKI action.
  78. * @param $sWikiObjectUri wiki object URI
  79. *
  80. * @see BxBaseServiceWiki::serviceWikiAction
  81. */
  82. /**
  83. * @ref bx_system_general-wiki_action "wiki_action"
  84. */
  85. public function serviceWikiAction ($sWikiObjectUri, $sAction)
  86. {
  87. $oWiki = BxDolWiki::getObjectInstanceByUri($sWikiObjectUri);
  88. if (!$oWiki) {
  89. echoJson(array('code' => 1, 'actions' => 'ShowMsg', 'msg' => _t('_sys_wiki_error_missing_wiki_object', $sWikiObjectUri)));
  90. return;
  91. }
  92. $sMethod = 'action' . bx_gen_method_name($sAction, array('-'));
  93. if (!method_exists($oWiki, $sMethod) || !$oWiki->isAllowed($sAction)) {
  94. echoJson(array('code' => 2, 'actions' => 'ShowMsg', 'msg' => _t('_sys_wiki_error_action_not_allowed', $sAction, $sWikiObjectUri)));
  95. return;
  96. }
  97. $mixed = $oWiki->$sMethod();
  98. if (is_array($mixed))
  99. echoJson($mixed);
  100. else
  101. echo $mixed;
  102. }
  103. /**
  104. * @page service Service Calls
  105. * @section bx_system_general System Services
  106. * @subsection bx_system_general-wiki Wiki
  107. * @subsubsection bx_system_general-wiki_controls wiki_controls
  108. *
  109. * Get wiki block controls panel
  110. *
  111. * @see BxBaseServiceWiki::serviceWikiControls
  112. */
  113. /**
  114. * @ref bx_system_general-wiki_controls "wiki_controls"
  115. */
  116. public function serviceWikiControls ($oWikiObject, $aWikiVer, $aWikiVerLatest, $sBlockId)
  117. {
  118. $this->_addCssJs ();
  119. $sInfo = '';
  120. if ($aWikiVer && $aWikiVerLatest['revision'] == $aWikiVer['revision']) {
  121. $sInfo = bx_time_js($aWikiVer['added']);
  122. }
  123. elseif ($aWikiVer) {
  124. $oProfile = BxDolProfile::getInstanceMagic($aWikiVer['profile_id']);
  125. $sInfo = _t('_sys_wiki_view_rev', $aWikiVer['revision'], $oProfile->getUrl(), $oProfile->getDisplayName(), bx_time_js($aWikiVer['added']));
  126. }
  127. $o = BxDolTemplate::getInstance();
  128. return $o->parseHtmlByName('wiki_controls.html', array(
  129. 'obj' => $oWikiObject->getObjectName(),
  130. 'block_id' => $sBlockId,
  131. 'info' => $sInfo,
  132. 'options' => json_encode(array(
  133. 'block_id' => $sBlockId,
  134. 'language' => isset($aWikiVer['language']) ? $aWikiVer['language'] : bx_lang_name(),
  135. 'wiki_action_uri' => $oWikiObject->getWikiUri(),
  136. 't_confirm_block_deletion' => _t('_sys_wiki_confirm_block_deletion'),
  137. )),
  138. 'bx_if:menu' => array(
  139. 'condition' => $oWikiObject->isAllowed('history'),
  140. 'content' => array(
  141. 'obj' => $oWikiObject->getObjectName(),
  142. 'block_id' => $sBlockId,
  143. ),
  144. ),
  145. ));
  146. }
  147. /**
  148. * @page service Service Calls
  149. * @section bx_system_general System Services
  150. * @subsection bx_system_general-wiki Wiki
  151. * @subsubsection bx_system_general-wiki_add_block wiki_add_block
  152. *
  153. * Get "add wiki block" panel
  154. *
  155. * @see BxBaseServiceWiki::serviceWikiAddBlock
  156. */
  157. /**
  158. * @ref bx_system_general-wiki_add_block "wiki_add_block"
  159. */
  160. public function serviceWikiAddBlock ($oWikiObject, $sPageObject, $sCellId)
  161. {
  162. $this->_addCssJs ();
  163. if (!preg_match("/cell_(\d+)/", $sCellId, $aMatches))
  164. return '';
  165. $iCellId = $aMatches[1];
  166. $o = BxDolTemplate::getInstance();
  167. return $o->parseHtmlByName('wiki_add_block.html', array(
  168. 'add_block' => _t('_sys_wiki_add_block'),
  169. 'page' => $sPageObject,
  170. 'cell_id' => $iCellId,
  171. 'action_uri' => $oWikiObject->getWikiUri(),
  172. ));
  173. }
  174. /**
  175. * @page service Service Calls
  176. * @section bx_system_general System Services
  177. * @subsection bx_system_general-wiki Wiki
  178. * @subsubsection bx_system_general-get_design_boxes get_design_boxes
  179. *
  180. * Get "design boxes" array
  181. *
  182. * @see BxBaseServiceWiki::serviceGetDesignBoxes
  183. */
  184. /**
  185. * @ref bx_system_general-get_design_boxes "get_design_boxes"
  186. */
  187. public function serviceGetDesignBoxes ()
  188. {
  189. $o = new BxDolStudioBuilderPageQuery();
  190. $aItems = array();
  191. if (!$o->getDesignBoxes(array('type' => 'all'), $aItems, false))
  192. return array();
  193. $a = array();
  194. foreach($aItems as $r)
  195. $a[$r['id']] = _t($r['title']);
  196. return $a;
  197. }
  198. protected function _addCssJs ($bAddPage = false)
  199. {
  200. if ($this->_bJsCssAdded)
  201. return false;
  202. $o = BxDolTemplate::getInstance();
  203. $o->addCss(['wiki.css', 'wiki-github-markdown.css']);
  204. $o->addJs('BxDolWiki.js');
  205. $o->addJsTranslation('_sys_wiki_external_editor_references_comment');
  206. if ($bAddPage)
  207. $o->addJs('studio/js/|forms.js');
  208. $this->_bJsCssAdded = true;
  209. }
  210. }
  211. /** @} */