ソースを参照

Ticket #4971 - Build-in API support: Page content.

AntonLV 1 年間 前
親
コミット
9f28ccb9cc
2 ファイル変更、81 行追加、0 行削除
  1. 13 0
      template/scripts/BxBasePage.php
  2. 68 0
      template/scripts/BxBaseServicePages.php

+ 13 - 0
template/scripts/BxBasePage.php

@@ -717,6 +717,19 @@ class BxBasePage extends BxDolPage
         return $a;
     }
 
+    public function getPageContentAPI($aBlocks = [])
+    {
+        $sModule = $this->getModule();
+        $sName = $this->_getPageTitle();
+        $sMetaTitle = $this->_getPageMetaTitle();
+
+        return [
+            'module' => $sModule,
+            'title' => $sMetaTitle ? $sMetaTitle : $sName,
+            'elements' => $this->getPageBlocksAPI($aBlocks),
+        ];
+    }
+
     public function getPageBlocksAPI($aBlocks = [])
     {
         $bBlocks = !empty($aBlocks) && is_array($aBlocks);

+ 68 - 0
template/scripts/BxBaseServicePages.php

@@ -105,6 +105,74 @@ class BxBaseServicePages extends BxDol
         return $aRes;
     }
 
+    public function serviceGetPageContentByRequest ($sRequest, $sBlocks = '', $sParams = '')
+    {
+        $mixed = null;
+
+        if (substr_count($sRequest, 'page/')> 0){
+            $_GET['i'] = str_replace('page/', '', $sRequest);
+            $aParams = json_decode($sParams, true);
+            if(!empty($aParams) && is_array($aParams))
+                $_GET = array_merge($_GET, $aParams);
+            
+            $mixed = BxDolPage::getObjectInstanceByURI();
+        }
+        else{
+            if(!empty($sParams)) {
+                $aParams = json_decode($sParams, true);
+                if(!empty($aParams) && is_array($aParams))
+                    $_GET = array_merge($_GET, $aParams);
+            }
+            $mixed = BxDolPage::getPageBySeoLink($sRequest);
+        }
+
+        $aExtras = [
+            'request' => $sRequest,
+        ];
+
+        if (($sUrl = $mixed) && is_string($sUrl)) {
+            $aRes = ['redirect' => $sUrl];
+
+            $aExtras = array_merge($aExtras, [
+                'url' => $sUrl
+            ]);
+        }
+        elseif (($oPage = $mixed) && is_object($oPage)) {
+            $aBlocks = [];
+            if(!empty($sBlocks))
+                $aBlocks = explode(',', $sBlocks);
+
+            $aRes = $oPage->getPageContentAPI($aBlocks);
+
+            $aExtras = array_merge($aExtras, [
+                'page' => $oPage,
+                'blocks' => $aBlocks
+            ]);
+        }
+        else {
+            $aRes = ['code' => 404, 'error' => _t("_sys_request_page_not_found_cpt"), 'data' => ['page_status' => 404]];
+        }
+
+        $aExtras['data'] = &$aRes;
+
+        /**
+         * @hooks
+         * @hookdef hook-system-get_page_api 'system', 'get_page_content_api' - hook to override page content, is used in API calls
+         * - $unit_name - equals `system`
+         * - $action - equals `get_page_content_api`
+         * - $object_id - not used
+         * - $sender_id - not used
+         * - $extra_params - array of additional params with the following array keys:
+         *      - `page` - [object] an instance of page class, @see BxDolPage 
+         *      - `blocks` - [array] array with page blocks
+         *      - `data` - [array] by ref, page peremeters array as key&value pairs, can be overridden in hook processing
+         * @hook @ref hook-system-get_page_content_api
+         */
+        bx_alert('system', 'get_page_content_api', 0, 0, $aExtras);
+
+        return $aRes;
+    }
+
     /**
      * @page service Service Calls
      * @section bx_system_pages System Services