1
0

BxBaseServicePages.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. list($aRes, $aExtras) = $this->_getByRequest('page', $sRequest, $sBlocks, $sParams);
  38. if(isset($aRes['code'], $aRes['data']) && (int)$aRes['code'] == 404 && isLogged())
  39. $aRes['data']['user'] = BxDolProfile::getDataForPage();
  40. $aExtras['data'] = &$aRes;
  41. /**
  42. * @hooks
  43. * @hookdef hook-system-get_page_api 'system', 'get_page_api' - hook to override page peremeters, is used in API calls
  44. * - $unit_name - equals `system`
  45. * - $action - equals `get_page_api`
  46. * - $object_id - not used
  47. * - $sender_id - not used
  48. * - $extra_params - array of additional params with the following array keys:
  49. * - `page` - [object] an instance of page class, @see BxDolPage
  50. * - `blocks` - [array] array with page blocks
  51. * - `data` - [array] by ref, page peremeters array as key&value pairs, can be overridden in hook processing
  52. * @hook @ref hook-system-get_page_api
  53. */
  54. bx_alert('system', 'get_page_api', 0, 0, $aExtras);
  55. return $aRes;
  56. }
  57. public function serviceGetPageContentByRequest ($sRequest, $sBlocks = '', $sParams = '')
  58. {
  59. list($aRes, $aExtras) = $this->_getByRequest('page_content', $sRequest, $sBlocks, $sParams);
  60. $aExtras['data'] = &$aRes;
  61. /**
  62. * @hooks
  63. * @hookdef hook-system-get_page_api 'system', 'get_page_content_api' - hook to override page content, is used in API calls
  64. * - $unit_name - equals `system`
  65. * - $action - equals `get_page_content_api`
  66. * - $object_id - not used
  67. * - $sender_id - not used
  68. * - $extra_params - array of additional params with the following array keys:
  69. * - `page` - [object] an instance of page class, @see BxDolPage
  70. * - `blocks` - [array] array with page blocks
  71. * - `data` - [array] by ref, page peremeters array as key&value pairs, can be overridden in hook processing
  72. * @hook @ref hook-system-get_page_content_api
  73. */
  74. bx_alert('system', 'get_page_content_api', 0, 0, $aExtras);
  75. return $aRes;
  76. }
  77. /**
  78. * @page service Service Calls
  79. * @section bx_system_pages System Services
  80. * @subsection bx_system_pages-pages Pages
  81. * @subsubsection bx_system_pages-get_page_by_uri Get page by page URI
  82. *
  83. * @code bx_srv('system', 'get_page_by_uri', ['about'], 'TemplServicePages'); @endcode
  84. * @code {{~system:get_page_by_uri:TemplServicePages['about']~}} @endcode
  85. *
  86. * Test method which page data
  87. * @param $sURI page URI
  88. *
  89. * @see BxBaseServicePages::serviceGetPage
  90. */
  91. /**
  92. * @ref bx_system_general-get_page_by_uri "get_page_by_uri"
  93. */
  94. public function serviceGetPageByUri ($sURI)
  95. {
  96. $oPage = BxDolPage::getObjectInstanceByURI($sURI, false, true);
  97. if (!$oPage) {
  98. $aRes = ['error' => _t("_sys_request_page_not_found_cpt")];
  99. }
  100. else {
  101. $aRes = $oPage->getPage();
  102. }
  103. return $aRes;
  104. }
  105. public function serviceGetPageBlockData($iBlockId, $iContentId = 0, $sContentModule = '')
  106. {
  107. return BxDolPage::getPageBlockData($iBlockId, $iContentId, $sContentModule);
  108. }
  109. public function serviceSetPageBlockData($iBlockId, $iContentId = 0, $sContentModule = '')
  110. {
  111. $sData = @file_get_contents("php://input");
  112. $aData = json_decode($sData, true);
  113. if($aData === null)
  114. return false;
  115. return BxDolPage::setPageBlockData($iBlockId, $iContentId, $sContentModule, $sData);
  116. }
  117. public function serviceGetUrlInfo($sUrl)
  118. {
  119. $oEmbed = BxDolEmbed::getObjectInstance('sys_system');
  120. return $oEmbed->getData($sUrl, '');
  121. }
  122. protected function _getByRequest($sType, $sRequest, $sBlocks = '', $sParams = '')
  123. {
  124. $mixed = null;
  125. if (substr_count($sRequest, 'page/')> 0){
  126. $_GET['i'] = str_replace('page/', '', $sRequest);
  127. $aParams = json_decode($sParams, true);
  128. if(!empty($aParams) && is_array($aParams))
  129. $_GET = array_merge($_GET, $aParams);
  130. $mixed = BxDolPage::getObjectInstanceByURI();
  131. }
  132. else{
  133. if(!empty($sParams)) {
  134. $aParams = json_decode($sParams, true);
  135. if(!empty($aParams) && is_array($aParams))
  136. $_GET = array_merge($_GET, $aParams);
  137. }
  138. $mixed = BxDolPage::getPageBySeoLink($sRequest);
  139. }
  140. $aExtras = [
  141. 'request' => $sRequest,
  142. ];
  143. if(($sUrl = $mixed) && is_string($sUrl)) {
  144. $aResult = ['redirect' => $sUrl];
  145. $aExtras = array_merge($aExtras, [
  146. 'url' => $sUrl
  147. ]);
  148. }
  149. else if(($oPage = $mixed) && is_object($oPage)) {
  150. $aBlocks = [];
  151. if(!empty($sBlocks))
  152. $aBlocks = explode(',', $sBlocks);
  153. $aResult = $oPage->{'get' . bx_gen_method_name($sType) . 'API'}($aBlocks);
  154. $aExtras = array_merge($aExtras, [
  155. 'page' => $oPage,
  156. 'blocks' => $aBlocks
  157. ]);
  158. }
  159. else
  160. $aResult = [
  161. 'code' => 404,
  162. 'error' => _t("_sys_request_page_not_found_cpt"),
  163. 'data' => [
  164. 'page_status' => 404
  165. ]
  166. ];
  167. return [$aResult, $aExtras];
  168. }
  169. }
  170. /** @} */