瀏覽代碼

Ticket #4148 - Build-in API support: Site search in a list of sections.

AntonLV 3 年之前
父節點
當前提交
f051fdae03

+ 60 - 8
inc/classes/BxDolSearch.php

@@ -37,6 +37,8 @@ define('BX_DOL_SEARCH_KEYWORD_PAGE', 'site-search-page');
  */
 class BxDolSearch extends BxDol
 {
+    protected $_bIsApi;
+
     protected $aClasses = array(); ///< array of all search classes
     protected $aChoice  = array(); ///< array of current search classes which were choosen in search area
     protected $_bRawProcessing = false; ///< display search results without design box and paginate
@@ -57,6 +59,8 @@ class BxDolSearch extends BxDol
     {
         parent::__construct();
 
+        $this->_bIsApi = bx_is_api();
+
         $this->aClasses = BxDolDb::getInstance()->fromCache('sys_objects_search', 'getAllWithKey',
            'SELECT `ID` as `id`,
                    `Title` as `title`,
@@ -85,7 +89,6 @@ class BxDolSearch extends BxDol
     {
         $sCode = $this->_bDataProcessing ? array() : '';
 
-        $bIsApi = bx_is_api();
         $bSingle = count($this->aChoice) == 1;
         foreach ($this->aChoice as $sKey => $aValue) {
             if (!$this->_sMetaType && !$aValue['GlobalSearch'])
@@ -114,14 +117,33 @@ class BxDolSearch extends BxDol
                 $oEx->setUnitTemplate($this->_sUnitTemplate);
 
             if ($this->_bDataProcessing) {
-                $aSearchData = $oEx->getSearchData();
-
-                $sCode[$sKey] = $bIsApi ? $oEx->decodeData($aSearchData) : $aSearchData;
+                if($this->_bIsApi) {
+                    if($bSingle)
+                        $sCode = $oEx->decodeData($oEx->getSearchData());
+                    else
+                        $sCode[$sKey] = $oEx->getSearchQuery(['for_union' => true]);
+                }
+                else
+                    $sCode[$sKey] = $oEx->getSearchData();
             }
             else
                 $sCode .= $this->_bRawProcessing ? $oEx->processingRaw() : $oEx->processing();
         }
 
+        if($this->_bIsApi && !$bSingle && is_array($sCode)) {
+            $bExtendedUnits = getParam('sys_api_extended_units') == 'on';
+
+            $aQueries = [];
+            foreach($sCode as $aQuery)
+                $aQueries[] = $aQuery['query'];
+
+            $aItems = BxDolDb::getInstance()->getAll('(' . implode(') UNION (', $aQueries) . ') ORDER BY `added` DESC ' . current($sCode)['limit']);
+
+            $sCode = [];
+            foreach($aItems as $aItem)
+                $sCode[] = bx_srv($aItem['module'], 'get_info_api', [$aItem['id'], $bExtendedUnits]);
+        }
+
         return $sCode;
     }
 
@@ -324,6 +346,7 @@ class BxDolSearchResult implements iBxDolReplaceable
     protected $bForceAjaxPaginate = false; ///< force ajax paginate
     protected $iPaginatePerPage = BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT; ///< default 'per page' value for paginate.
 
+    protected $_bIsApi;
     protected $_bSingleSearch = true;
     protected $_bLiveSearch = false;
     protected $_sMetaType = '';
@@ -340,6 +363,8 @@ class BxDolSearchResult implements iBxDolReplaceable
      */
     function __construct ()
     {
+        $this->_bIsApi = bx_is_api();
+
         if (isset($this->aPseud['id']))
             $this->aCurrent['ident'] = $this->aPseud['id'];
     }
@@ -685,6 +710,14 @@ class BxDolSearchResult implements iBxDolReplaceable
         }
         return array();
     }
+    
+    function getSearchQuery($aParams = [])
+    {
+        $this->aPseud = ['id' => 'id', 'added' => 'added'];
+
+        $this->setConditionParams();
+        return $this->getSearchDataByParams($aParams);
+    }
 
     /**
      * Get array with code for sql elements
@@ -726,17 +759,30 @@ class BxDolSearchResult implements iBxDolReplaceable
      */
     function getSearchDataByParams ($aParams = '')
     {
+        $bForUnion = isset($aParams['for_union']) && $aParams['for_union'] === true;
+
         $aSql = array('ownFields'=>'', 'joinFields'=>'', 'order'=>'');
 
         // searchFields
-        foreach ($this->aCurrent['ownFields'] as $sValue)
-            $aSql['ownFields'] .= $this->setFieldUnit($sValue, $this->aCurrent['table']);
+        if($bForUnion) {
+            $sTable = isset($this->aCurrent['tableSearch']) ? $this->aCurrent['tableSearch'] : $this->aCurrent['table'];
+
+            $aSql['ownFields'] .= $this->setFieldUnit('id', $sTable);
+            $aSql['ownFields'] .= $this->setFieldUnit('added', $sTable);
+            $aSql['ownFields'] .= "'" . $this->aCurrent['module_name']  . "' AS `module`";
+        }
+        else
+            foreach ($this->aCurrent['ownFields'] as $sValue)
+                $aSql['ownFields'] .= $this->setFieldUnit($sValue, $this->aCurrent['table']);
 
         // joinFields & join
         $aJoins = $this->getJoins();
         if (!empty($aJoins)) {
-            $aSql['ownFields'] .= $aJoins['ownFields'];
-            $aSql['ownFields'] .= $aJoins['joinFields'];
+            if(!$bForUnion) {
+                $aSql['ownFields'] .= $aJoins['ownFields'];
+                $aSql['ownFields'] .= $aJoins['joinFields'];
+            }
+
             $aSql['join'] = $aJoins['join'];
             $aSql['groupBy'] = $aJoins['groupBy'];
         } 
@@ -778,6 +824,12 @@ class BxDolSearchResult implements iBxDolReplaceable
         if (isset($aSql['groupBy']))
             $sqlQuery .= ' ' . $aSql['groupBy'];
 
+        if($bForUnion)
+            return [
+                'query' => $sqlQuery, 
+                'limit' => $aSql['limit']
+            ];
+
         if (isset($aSql['order']))
             $sqlQuery .= ' ' . $aSql['order'];
 

+ 15 - 3
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -446,7 +446,6 @@ class BxBaseModGeneralModule extends BxDolModule
             'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'embed/' . $iContentId . '/'
         ]);
     }
-    
 
     public function serviceGetInfo ($iContentId, $bSearchableFieldsOnly = true)
     {
@@ -474,6 +473,15 @@ class BxBaseModGeneralModule extends BxDolModule
         return $aResult;
     }
 
+    public function serviceGetInfoApi ($iContentId, $bExtendedUnits = false)
+    {
+        $aData = $this->serviceGetInfo($iContentId, false);
+        if($aData)
+            $aData = $this->decodeDataApi($aData, $bExtendedUnits);
+
+        return $aData;
+    }
+
     /**
      * @page service Service Calls
      * @section bx_base_general Base General
@@ -3266,8 +3274,12 @@ class BxBaseModGeneralModule extends BxDolModule
         $CNF = &$this->_oConfig->CNF;
         return bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_FAVORITES_LIST'] . '&profile_id=' . $iProfileId . '&list_id=' . $iListId));
     }
-    
-    
+
+    public function decodeDataApi ($aData, $bExtended = false)
+    {
+        return $aData;
+    }
+
     // ====== PROTECTED METHODS
 
     protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)

+ 10 - 0
modules/base/general/classes/BxBaseModGeneralSearchResult.php

@@ -212,6 +212,16 @@ class BxBaseModGeneralSearchResult extends BxTemplSearchResult
 
         return parent::displayResultBlock();
     }
+
+    function decodeData ($a)
+    {
+        $bExtendedUnits = getParam('sys_api_extended_units') == 'on';
+
+        foreach ($a as $i => $r)
+            $a[$i] = $this->oModule->decodeDataApi($r, $bExtendedUnits);
+
+        return $a;
+    }
 }
 
 /** @} */

+ 33 - 0
modules/base/profile/classes/BxBaseModProfileModule.php

@@ -2058,6 +2058,39 @@ class BxBaseModProfileModule extends BxBaseModGeneralModule implements iBxDolCon
 
         return !empty($sAction) && !empty($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
     }
+    
+    public function decodeDataApi ($aData, $bExtended = false)
+    {
+        $CNF = $this->_oConfig->CNF;
+
+        $iId = (int)$aData[$CNF['FIELD_ID']];
+
+        $aResult = [
+            'id' => $iId, 
+            'module' => $this->getName(),
+            'added' => $aData[$CNF['FIELD_ADDED']],
+            'author' => $aData[$CNF['FIELD_AUTHOR']],
+            'title' => $aData[$CNF['FIELD_TITLE']],
+            $CNF['FIELD_TITLE'] => $aData[$CNF['FIELD_TITLE']],
+            'url' => bx_api_get_relative_url($this->serviceGetLink($iId)),
+            'image' => $this->serviceGetThumb($iId),
+            'cover' => $this->serviceGetCover($iId),
+        ];
+
+        if($bExtended)
+            $aResult['text'] = $aData[$CNF['FIELD_TEXT']];
+
+        if(!empty($CNF['OBJECT_MENU_SNIPPET_META']) && ($oMetaMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this->_oTemplate)) !== false) {
+            $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
+            $bPublic = !$oPrivacy || $oPrivacy->check($iId) || $oPrivacy->isPartiallyVisible($aData[$CNF['FIELD_ALLOW_VIEW_TO']]);
+
+            $oMetaMenu->setContentId($iId);
+            $oMetaMenu->setContentPublic($bPublic);
+            $aResult['meta'] = $oMetaMenu->getCodeAPI();
+        }
+
+        return $aResult;
+    }
 
 
     // ====== PROTECTED METHODS

+ 0 - 41
modules/base/profile/classes/BxBaseModProfileSearchResult.php

@@ -233,47 +233,6 @@ class BxBaseModProfileSearchResult extends BxBaseModGeneralSearchResult
 
         return true;
     }
-    
-    function decodeData ($a)
-    {
-        $CNF = $this->oModule->_oConfig->CNF;
-
-        $aExclude = array_flip([$CNF['FIELD_TEXT'], $CNF['FIELD_PICTURE'], 'cover_data']);
-        $bExclude = getParam('sys_api_extended_units') != 'on';
-
-        $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
-        $bPrivacy = $oPrivacy !== false;
-
-        $oMetaMenu = !empty($CNF['OBJECT_MENU_SNIPPET_META']) ? BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this->oModule->_oTemplate) : false;
-        $bMetaMenu = $oMetaMenu !== false;
-
-        $oContentInfo = $this->getContentInfoObject();
-
-        foreach ($a as $i => $r) {
-            $aAddon = [
-                'module' => $this->oModule->getName(),
-                'title' => $r[$CNF['FIELD_TITLE']],
-                'url' => bx_api_get_relative_url($oContentInfo->getContentLink($r['id'])),
-                'image' => $oContentInfo->getContentThumb($r['id']),
-                'cover' => $oContentInfo->getContentCover($r['id']),
-            ];
-
-            if($bMetaMenu) {
-                $bPublic = !$bPrivacy || $oPrivacy->check($r[$CNF['FIELD_ID']]) || $oPrivacy->isPartiallyVisible($r[$CNF['FIELD_ALLOW_VIEW_TO']]);
-
-                $oMetaMenu->setContentId($r['id']);
-                $oMetaMenu->setContentPublic($bPublic);
-                $aAddon['meta'] = $oMetaMenu->getCodeAPI();
-            }
-
-            $a[$i] = array_merge($r, $aAddon);
-
-            if($bExclude)
-                $a[$i] = array_diff_key($a[$i], $aExclude);
-        }
-
-        return $a;
-    }
 	
     function getItemPerPageInShowCase ()
     {

+ 38 - 0
modules/base/text/classes/BxBaseModTextModule.php

@@ -873,6 +873,44 @@ class BxBaseModTextModule extends BxBaseModGeneralModule implements iBxDolConten
         
         return $mResult;
     }
+    
+    public function decodeDataApi ($aData, $bExtended = false)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $iId = (int)$aData[$CNF['FIELD_ID']];
+
+        $sSummary = '';
+        if(!empty($CNF['PARAM_CHARS_SUMMARY_PLAIN'])) {
+            $sSummary = $this->_oTemplate->getText($aData);
+            $sSummary = BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($sSummary), (int)getParam($CNF['PARAM_CHARS_SUMMARY_PLAIN']));
+        }
+
+        $aResult = [
+            'id' => $iId,
+            'added' => $aData[$CNF['FIELD_ADDED']],
+            'author' => $aData[$CNF['FIELD_AUTHOR']],
+            'author_data' => !empty($aData[$CNF['FIELD_AUTHOR']]) ? BxDolProfile::getData($aData[$CNF['FIELD_AUTHOR']]) : '',
+            'title' => $aData[$CNF['FIELD_TITLE']],
+            'url' => bx_api_get_relative_url($this->serviceGetLink($iId)),
+            'image' => $this->serviceGetThumb($iId),
+            'summary_plain' => $sSummary
+        ];
+
+        if($bExtended)
+            $aResult['text'] = $aData[$CNF['FIELD_TEXT']];
+
+        /**
+         * Disabled for now, because Author is used only.
+        if(!empty($CNF['OBJECT_MENU_SNIPPET_META']) && ($oMetaMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this->_oTemplate)) !== false) {
+            $oMetaMenu->setContentId($iId);
+
+            $aResult['meta'] = $oMetaMenu->getCodeAPI();
+        }
+         */
+
+        return $aResult;
+    }
 
 
     // ====== PROTECTED METHODS

+ 0 - 48
modules/base/text/classes/BxBaseModTextSearchResult.php

@@ -236,54 +236,6 @@ class BxBaseModTextSearchResult extends BxBaseModGeneralSearchResult
 
         return true;
     }
-    
-    function decodeData ($a)
-    {
-        $CNF = &$this->oModule->_oConfig->CNF;
-
-        $aExclude = array_flip([$CNF['FIELD_TEXT'], $CNF['FIELD_THUMB'], 'thumb_data']);
-        $bExclude = getParam('sys_api_extended_units') != 'on';
-
-        $oMetaMenu = null;
-        $bMetaMenu = false;
-
-        /**
-         * Disabled for now, because Author is used only.
-        $oMetaMenu = !empty($CNF['OBJECT_MENU_SNIPPET_META']) ? BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this->oModule->_oTemplate) : false;
-        $bMetaMenu = $oMetaMenu !== false;
-         */
-
-        $oFunctions = BxTemplFunctions::getInstance();
-        $oContentInfo = $this->getContentInfoObject();
-        
-        $bSummaryPlain = isset($CNF['PARAM_CHARS_SUMMARY_PLAIN']) && $CNF['PARAM_CHARS_SUMMARY_PLAIN'];
-        $iSummaryPlain = $bSummaryPlain ? (int)getParam($CNF['PARAM_CHARS_SUMMARY_PLAIN']) : 0;
-
-        foreach ($a as $i => $r) {
-            $sSummary = '';
-            if($bSummaryPlain) {
-                $sSummary = $this->oModule->_oTemplate->getText($r);
-                $sSummary = $oFunctions->getStringWithLimitedLength(strip_tags($sSummary), $iSummaryPlain);
-            }
-
-            $a[$i] = array_merge($a[$i], [
-                'author_data' => isset($r['author']) ? BxDolProfile::getData($r[$CNF['FIELD_AUTHOR']]) : '',
-                'url' => bx_api_get_relative_url($oContentInfo->getContentLink($r['id'])),
-                'image' => $oContentInfo->getContentThumb($r['id']),
-                'summary_plain' => $sSummary
-            ]);
-
-            if($bMetaMenu) {
-                $oMetaMenu->setContentId($r['id']);
-                $a[$i]['meta'] = $oMetaMenu->getCodeAPI();
-            }
-
-            if($bExclude)
-                $a[$i] = array_diff_key($a[$i], $aExclude);
-        }
-
-        return $a;
-    }
 
     function _getPseud ()
     {

+ 1 - 9
template/scripts/BxBaseServices.php

@@ -1011,15 +1011,7 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
             ]
         ]);
 
-        $aData = [];
-        $aResponse = $oSearch->response();
-        foreach($aResponse as $sSection => $aItems) {
-            if(empty($aItems))
-                continue;
-
-            $aData = array_merge($aData, $aItems);
-        }
-
+        $aData = $oSearch->response();
         if(count($aData) > $aParamsBrowse['per_page'])
             $aData = array_slice($aData, $aParamsBrowse['start'], $aParamsBrowse['per_page']);