BxBaseServicePages.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * System service for pages functionality.
  11. */
  12. class BxBaseServicePages extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. /**
  19. * @page service Service Calls
  20. * @section bx_system_pages System Services
  21. * @subsection bx_system_pages-pages Pages
  22. * @subsubsection bx_system_pages-get_page_by_request Get page by page URI
  23. *
  24. * @code bx_srv('system', 'get_page_by_request', ['about'], 'TemplServicePages'); @endcode
  25. * @code {{~system:get_page_by_request:TemplServicePages['about']~}} @endcode
  26. *
  27. * Test method which page data
  28. * @param $sURI page URI
  29. *
  30. * @see BxBaseServicePages::serviceGetPage
  31. */
  32. /**
  33. * @ref bx_system_general-get_page_by_request "get_page_by_request"
  34. */
  35. public function serviceGetPageByRequest ($sRequest, $sBlocks = '', $sParams = '')
  36. {
  37. $bLogged = isLogged();
  38. if($sRequest == 'home' && ($sReplacement = getParam('sys_api_root_page_' . ($bLogged ? 'member' : 'guest'))))
  39. switch($sReplacement) {
  40. case 'dashboard':
  41. $sRequest = 'dashboard';
  42. break;
  43. case 'profile':
  44. $sRequest = ltrim(bx_api_get_relative_url(BxDolProfile::getInstance()->getUrl()), '/');
  45. break;
  46. }
  47. list($aRes, $aExtras) = $this->_getByRequest('page', $sRequest, $sBlocks, $sParams);
  48. if(isset($aRes['code'], $aRes['data']) && (int)$aRes['code'] == 404 && $bLogged)
  49. $aRes['data']['user'] = BxDolProfile::getDataForPage();
  50. $aExtras['data'] = &$aRes;
  51. /**
  52. * @hooks
  53. * @hookdef hook-system-get_page_api 'system', 'get_page_api' - hook to override page peremeters, is used in API calls
  54. * - $unit_name - equals `system`
  55. * - $action - equals `get_page_api`
  56. * - $object_id - not used
  57. * - $sender_id - not used
  58. * - $extra_params - array of additional params with the following array keys:
  59. * - `page` - [object] an instance of page class, @see BxDolPage
  60. * - `blocks` - [array] array with page blocks
  61. * - `data` - [array] by ref, page peremeters array as key&value pairs, can be overridden in hook processing
  62. * @hook @ref hook-system-get_page_api
  63. */
  64. bx_alert('system', 'get_page_api', 0, 0, $aExtras);
  65. return $aRes;
  66. }
  67. public function serviceGetPageContentByRequest ($sRequest, $sBlocks = '', $sParams = '')
  68. {
  69. list($aRes, $aExtras) = $this->_getByRequest('page_content', $sRequest, $sBlocks, $sParams);
  70. $aExtras['data'] = &$aRes;
  71. /**
  72. * @hooks
  73. * @hookdef hook-system-get_page_api 'system', 'get_page_content_api' - hook to override page content, is used in API calls
  74. * - $unit_name - equals `system`
  75. * - $action - equals `get_page_content_api`
  76. * - $object_id - not used
  77. * - $sender_id - not used
  78. * - $extra_params - array of additional params with the following array keys:
  79. * - `page` - [object] an instance of page class, @see BxDolPage
  80. * - `blocks` - [array] array with page blocks
  81. * - `data` - [array] by ref, page peremeters array as key&value pairs, can be overridden in hook processing
  82. * @hook @ref hook-system-get_page_content_api
  83. */
  84. bx_alert('system', 'get_page_content_api', 0, 0, $aExtras);
  85. return $aRes;
  86. }
  87. /**
  88. * @page service Service Calls
  89. * @section bx_system_pages System Services
  90. * @subsection bx_system_pages-pages Pages
  91. * @subsubsection bx_system_pages-get_page_by_uri Get page by page URI
  92. *
  93. * @code bx_srv('system', 'get_page_by_uri', ['about'], 'TemplServicePages'); @endcode
  94. * @code {{~system:get_page_by_uri:TemplServicePages['about']~}} @endcode
  95. *
  96. * Test method which page data
  97. * @param $sURI page URI
  98. *
  99. * @see BxBaseServicePages::serviceGetPage
  100. */
  101. /**
  102. * @ref bx_system_general-get_page_by_uri "get_page_by_uri"
  103. */
  104. public function serviceGetPageByUri ($sURI)
  105. {
  106. $oPage = BxDolPage::getObjectInstanceByURI($sURI, false, true);
  107. if (!$oPage) {
  108. $aRes = ['error' => _t("_sys_request_page_not_found_cpt")];
  109. }
  110. else {
  111. $aRes = $oPage->getPage();
  112. }
  113. return $aRes;
  114. }
  115. public function serviceGetPageBlockData($iBlockId, $iContentId = 0, $sContentModule = '')
  116. {
  117. return BxDolPage::getPageBlockData($iBlockId, $iContentId, $sContentModule);
  118. }
  119. public function serviceSetPageBlockData($iBlockId, $iContentId = 0, $sContentModule = '')
  120. {
  121. if (!isAdmin())
  122. return false;
  123. $sData = @file_get_contents("php://input");
  124. $aData = json_decode($sData, true);
  125. if($aData === null)
  126. return false;
  127. return BxDolPage::setPageBlockData($iBlockId, $iContentId, $sContentModule, $sData);
  128. }
  129. public function serviceGetPageBlockImageData($iImageId)
  130. {
  131. return bx_api_get_image(['sys_images_editor', 'sys_images_editor'], (int)$iImageId);
  132. }
  133. public function serviceGetUrlInfo($sUrl)
  134. {
  135. $oEmbed = BxDolEmbed::getObjectInstance('sys_system');
  136. return $oEmbed->getData($sUrl, '');
  137. }
  138. protected function _getByRequest($sType, $sRequest, $sBlocks = '', $sParams = '')
  139. {
  140. $mixed = null;
  141. if(($sK = 'page') && substr_count($sRequest, $sK . '/') > 0) {
  142. $_GET['i'] = str_replace($sK . '/', '', $sRequest);
  143. $aParams = json_decode($sParams, true);
  144. if(!empty($aParams) && is_array($aParams))
  145. $_GET = array_merge($_GET, $aParams);
  146. $mixed = BxDolPage::getObjectInstanceByURI();
  147. }
  148. else if(($sK = 'wiki') && substr_count($sRequest, $sK . '/') > 0) {
  149. $sUri = str_replace($sK . '/', '', $sRequest);
  150. $mixed = bx_srv('system', 'wiki_page', [$sK, $sUri], 'TemplServiceWiki');
  151. }
  152. else {
  153. if(!empty($sParams)) {
  154. $aParams = json_decode($sParams, true);
  155. if(!empty($aParams) && is_array($aParams))
  156. $_GET = array_merge($_GET, $aParams);
  157. }
  158. $mixed = BxDolPage::getPageBySeoLink($sRequest);
  159. }
  160. $aExtras = [
  161. 'request' => $sRequest,
  162. ];
  163. if(($sUrl = $mixed) && is_string($sUrl)) {
  164. $aResult = ['redirect' => $sUrl];
  165. $aExtras = array_merge($aExtras, [
  166. 'url' => $sUrl
  167. ]);
  168. }
  169. else if(($oPage = $mixed) && is_object($oPage)) {
  170. $aBlocks = [];
  171. if(!empty($sBlocks))
  172. $aBlocks = explode(',', $sBlocks);
  173. $aResult = $oPage->{'get' . bx_gen_method_name($sType) . 'API'}($aBlocks);
  174. $aExtras = array_merge($aExtras, [
  175. 'page' => $oPage,
  176. 'blocks' => $aBlocks
  177. ]);
  178. }
  179. else
  180. $aResult = [
  181. 'code' => 404,
  182. 'error' => _t("_sys_request_page_not_found_cpt"),
  183. 'data' => [
  184. 'page_status' => 404
  185. ]
  186. ];
  187. return [$aResult, $aExtras];
  188. }
  189. }
  190. /** @} */