Просмотр исходного кода

Ticket #652 - Custom search in Forum module.

AntonLV 9 лет назад
Родитель
Сommit
014ed1dfa9

+ 17 - 5
inc/classes/BxDolGrid.php

@@ -251,6 +251,8 @@ class BxDolGrid extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
 
     protected $_sObject;
     protected $_aOptions;
+
+    protected $_aBrowseParams;
     protected $_sDefaultSortingOrder = 'ASC';
 
     /**
@@ -263,6 +265,13 @@ class BxDolGrid extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
 
         $this->_sObject = $aOptions['object'];
         $this->_aOptions = $aOptions;
+
+        $sBrowseParams = bx_get('bp');
+        if(!empty($sBrowseParams)) {
+        	$aBrowseParams = bx_process_input(unserialize(urldecode($sBrowseParams)));
+        	if(!empty($aBrowseParams) && is_array($aBrowseParams))
+            	$this->setBrowseParams($aBrowseParams);
+        }
     }
 
     /**
@@ -312,6 +321,12 @@ class BxDolGrid extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
         return true;
     }
 
+    public function setBrowseParams($aBrowseParams)
+    {
+    	$this->_aBrowseParams = $aBrowseParams;
+    	$this->_aQueryAppend['bp'] = urlencode(serialize($this->_aBrowseParams));
+    }
+
     /**
      * Replace provided markers in form array
      * @param $a form description array
@@ -393,9 +408,7 @@ class BxDolGrid extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
             $sQuery .= " WHERE 1 ";
 
         $aResults = false;
-        $oZ = new BxDolAlerts('grid', 'get_data', 0, false, array('object' => $this->_sObject, 'options' => $this->_aOptions, 'markers' => $this->_aMarkers, 'filter' => $sFilter, 'results' => &$aResults));
-    	$oZ->alert();
-
+        bx_alert('grid', 'get_data', 0, false, array('object' => $this->_sObject, 'options' => $this->_aOptions, 'markers' => $this->_aMarkers, 'filter' => $sFilter, 'browse_params' => $this->_aBrowseParams, 'results' => &$aResults));
     	if($aResults !== false)
     	    return $aResults;
 
@@ -463,8 +476,7 @@ class BxDolGrid extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
                 $sCond = rtrim($sCond, ' OR ');
             }
 
-            $oZ = new BxDolAlerts('grid', 'get_data_by_filter', 0, false, array('object' => $this->_sObject, 'options' => $this->_aOptions, 'markers' => $this->_aMarkers, 'filter' => $sFilter, 'conditions' => &$sCond));
-        	$oZ->alert();
+            bx_alert('grid', 'get_data_by_filter', 0, false, array('object' => $this->_sObject, 'options' => $this->_aOptions, 'markers' => $this->_aMarkers, 'filter' => $sFilter, 'browse_params' => $this->_aBrowseParams, 'conditions' => &$sCond));
 
             if ($sCond)
                 $sQuery .= ' AND (' . $sCond . ')';

+ 10 - 3
modules/boonex/elasticsearch/classes/BxElsApi.php

@@ -73,16 +73,23 @@ class BxElsApi extends BxDol
 
         $bTp = !empty($aCnd['tp']);
         $bFld = !empty($aCnd['fld']);
+        $bOprIn = !empty($aCnd['opr']) && $aCnd['opr'] == 'IN';
 
-        $sType = 'simple_query_string';
         if($bTp)
             $sType = $aCnd['tp'];
-        else if($bFld)
-            $sType = 'term';
+        else if($bFld) {
+            if($bOprIn)
+                $sType = 'terms';
+            else 
+                $sType = 'term';
+        }
+        else 
+            $sType = 'simple_query_string';
 
         $aQuery = array();
         switch ($sType) {
             case 'term':
+            case 'terms':
                 $aQuery = array($aCnd['fld'] => $aCnd['val']);
                 break;
 

+ 52 - 34
modules/boonex/elasticsearch/classes/BxElsResponse.php

@@ -24,12 +24,12 @@ class BxElsResponse extends BxDolAlertsResponse
 
     public function response($oAlert)
     {
+        if($oAlert->sUnit == 'grid' && $oAlert->sAction == 'get_data')
+            return $this->_processGridSearch($oAlert);
+
         if($oAlert->sUnit == 'grid' && $oAlert->sAction == 'get_data_by_filter')
             return $this->_processGridSearchFilter($oAlert);
-/*
-        if($oAlert->sUnit == 'grid' && $oAlert->sAction == 'get_data_by_conditions')
-            return $this->_processGridSearchConditions($oAlert);
-*/
+
         return $this->_processManageActions($oAlert);
     }
 
@@ -53,56 +53,74 @@ class BxElsResponse extends BxDolAlertsResponse
         }
     }
 
-    protected function _processGridSearchFilter(&$oAlert)
+    protected function _processGridSearch(&$oAlert)
     {
         $oContentInfo = BxDolContentInfo::getObjectInstanceByGrid($oAlert->aExtras['object']);
-        $sContentInfo = $oContentInfo->getName();
+        if(!$oContentInfo)
+            return;
 
-        $aGrid = $oContentInfo->getGrid();
+        $aCondition = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
+        $aSelection = array();
 
-        if(!empty($aGrid['condition']) || !empty($aGrid['order'])) {
-            $aFilter = array('grp' => true, 'opr' => 'AND', 'cnds' => array(
-                array('val' => $oAlert->aExtras['filter'])
-            ));
+        if(!empty($oAlert->aExtras['filter']))
+            $aCondition['cnds'][] = array('val' => $oAlert->aExtras['filter']);
 
-            $aCondition = unserialize($aGrid['condition']);
-            if(!empty($aCondition) && is_array($aCondition))
-                $aFilter['cnds'][] = $aCondition;
+        if(!empty($oAlert->aExtras['browse_params']['where']))
+            $aCondition['cnds'] = array_merge($aCondition['cnds'], $oAlert->aExtras['browse_params']['where']['cnds']);
 
-            $aSelection = unserialize($aGrid['selection']);
-            if(empty($aSelection) || !is_array($aSelection))
-                $aSelection = array();
+        $this->_updateSearchParamsByGrid($oContentInfo->getGrid(), $aCondition, $aSelection);
 
-            $aResults = $this->_oModule->serviceSearchExtended($aFilter, $aSelection, $sContentInfo);
-        }
-        else
-            $aResults = $this->_oModule->serviceSearchSimple($oAlert->aExtras['filter'], $sContentInfo);
+        $aResults = $this->_oModule->serviceSearchExtended($aCondition, $aSelection, $oContentInfo->getName());
+        if(empty($aResults) || !is_array($aResults) || (int)$aResults['total'] == 0)
+            return;
 
-        $aIds = array();
-        if((int)$aResults['total'] > 0 && !empty($aResults['hits']))
-            foreach($aResults['hits'] as $aResult)
-                $aIds[] = $aResult['_id'];
+        $aItems = array();
+        foreach($aResults['hits'] as $aResult)
+            $aItems[] = $aResult['_source'];
 
-        $oAlert->aExtras['conditions'] = "`" . $aGrid['grid_field_id'] . "` IN (" . $this->_oModule->_oDb->implode_escape($aIds) . ")";
+        $oAlert->aExtras['results'] = $aItems;
     }
 
-    /*
-    protected function _processGridSearchConditions(&$oAlert)
+    protected function _processGridSearchFilter(&$oAlert)
     {
         $oContentInfo = BxDolContentInfo::getObjectInstanceByGrid($oAlert->aExtras['object']);
+        if(!$oContentInfo)
+            return;
+
+        $aCondition = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
+        $aSelection = array();
+
+        if(!empty($oAlert->aExtras['filter']))
+            $aCondition['cnds'][] = array('val' => $oAlert->aExtras['filter']);
+
         $aGrid = $oContentInfo->getGrid();
+        $this->_updateSearchParamsByGrid($aGrid, $aCondition, $aSelection);
 
-        $aResults = $this->_oModule->serviceSearchSimple($oAlert->aExtras['filter'], $oContentInfo->getName());
-        if((int)$aResults['total'] == 0)
-            return;
+        $aResults = $this->_oModule->serviceSearchExtended($aCondition, $aSelection, $oContentInfo->getName());
 
         $aIds = array();
-        foreach($aResults['hits'] as $aResult)
-            $aIds[] = $aResult['_id'];
+        if((int)$aResults['total'] > 0 && !empty($aResults['hits']))
+            foreach($aResults['hits'] as $aResult)
+                $aIds[] = $aResult['_id'];
 
         $oAlert->aExtras['conditions'] = "`" . $aGrid['grid_field_id'] . "` IN (" . $this->_oModule->_oDb->implode_escape($aIds) . ")";
     }
-    */
+
+    protected function _updateSearchParamsByGrid($aGrid, &$aCondition, &$aSelection)
+    {
+        if(empty($aGrid['condition']) && empty($aGrid['selection']))
+            return;
+
+        //--- Update Condition params
+        $aGridCondition = unserialize($aGrid['condition']);
+        if(!empty($aGridCondition) && is_array($aGridCondition))
+            $aCondition['cnds'] = array_merge($aCondition['cnds'], $aGridCondition);
+
+        //--- Update Selection params: from, size and sort 
+        $aGridSelection = unserialize($aGrid['selection']);
+        if(empty($aGridSelection) || !is_array($aGridSelection))
+            $aSelection = array_merge($aSelection, $aGridSelection);
+    } 
 }
 
 /** @} */

+ 2 - 2
modules/boonex/elasticsearch/install/sql/enable.sql

@@ -19,5 +19,5 @@ INSERT INTO `sys_alerts_handlers`(`name`, `class`, `file`, `service_call`) VALUE
 SET @iHandler := LAST_INSERT_ID();
 
 INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
-('grid', 'get_data_by_filter', @iHandler),
-('grid', 'get_data_by_conditions', @iHandler);
+('grid', 'get_data', @iHandler),
+('grid', 'get_data_by_filter', @iHandler);

+ 12 - 22
modules/boonex/forum/classes/BxForumGrid.php

@@ -14,36 +14,27 @@ require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
 class BxForumGrid extends BxTemplGrid
 {
     protected $_oModule;
-    protected $_aParams;
     protected $_sDefaultSource; 
 
     public function __construct ($aOptions, $oTemplate = false)
     {
-        parent::__construct ($aOptions, $oTemplate);
-
         $this->_oModule = BxDolModule::getInstance('bx_forum');
 
+        parent::__construct ($aOptions, $oTemplate);
+
         $this->_aOptions['paginate_per_page'] = (int)$this->_oModule->_oDb->getParam('bx_forum_per_page_browse');
 
         $this->_sDefaultSortingOrder = 'DESC';
         $this->_sDefaultSource = $this->_aOptions['source'];
-
-	    $sParams = bx_get('params');
-        if(!empty($sParams)) {
-        	$aParams = unserialize(urldecode($sParams));
-        	if(!empty($aParams) && is_array($aParams))
-            	$this->setBrowseParams($aParams);
-        }
     }
 
     public function setBrowseParams($aParams)
     {
-    	$this->_aParams = $aParams;
-    	$this->_aQueryAppend['params'] = urlencode(serialize($this->_aParams));
+        parent::setBrowseParams($aParams);
 
     	$sField = 'added';
-    	if(!empty($this->_aParams['type']))
-	    	switch($this->_aParams['type']) {
+    	if(!empty($this->_aBrowseParams['type']))
+	    	switch($this->_aBrowseParams['type']) {
 	    		case 'new':
 	    		case 'author':
                 case 'favorite':
@@ -73,8 +64,7 @@ class BxForumGrid extends BxTemplGrid
 	    	}
 
 		$this->_aOptions['field_order'] = $sField;
-
-		$this->_aOptions['paginate_per_page'] = !empty($this->_aParams['per_page']) ? (int)$this->_aParams['per_page'] : (int)$this->_oModule->_oDb->getParam('bx_forum_per_page_browse');
+		$this->_aOptions['paginate_per_page'] = !empty($this->_aBrowseParams['per_page']) ? (int)$this->_aBrowseParams['per_page'] : (int)$this->_oModule->_oDb->getParam('bx_forum_per_page_browse');
     }
 
     public function getCode ($isDisplayHeader = true)
@@ -138,8 +128,8 @@ class BxForumGrid extends BxTemplGrid
 
     	//--- Check privacy
     	$iAuthorId = 0;
-    	if(!empty($this->_aParams['author'])) {
-    		$oProfileAuthor = BxDolProfile::getInstance((int)$this->_aParams['author']);
+    	if(!empty($this->_aBrowseParams['author'])) {
+    		$oProfileAuthor = BxDolProfile::getInstance((int)$this->_aBrowseParams['author']);
     		if($oProfileAuthor)
     			$iAuthorId = $oProfileAuthor->id();
     	}
@@ -153,14 +143,14 @@ class BxForumGrid extends BxTemplGrid
 			$sWhereClause .= $aCondition['where'];
 
 		//--- Check browse params
-        if(!empty($this->_aParams['join']) && is_array($this->_aParams['join'])) {
-			$sJoinClauseBrowse = $this->_getSqlJoinGroup($this->_aParams['join']);
+        if(!empty($this->_aBrowseParams['join']) && is_array($this->_aBrowseParams['join'])) {
+			$sJoinClauseBrowse = $this->_getSqlJoinGroup($this->_aBrowseParams['join']);
 			if(!empty($sJoinClauseBrowse))
 				$sJoinClause .= " " . $sJoinClauseBrowse;
 		}
 
-		if(!empty($this->_aParams['where']) && is_array($this->_aParams['where'])) {
-			$sWhereClauseBrowse = $this->_getSqlWhereFromGroup($this->_aParams['where']);
+		if(!empty($this->_aBrowseParams['where']) && is_array($this->_aBrowseParams['where'])) {
+			$sWhereClauseBrowse = $this->_getSqlWhereFromGroup($this->_aBrowseParams['where']);
 			if(!empty($sWhereClauseBrowse))
 				$sWhereClause .= " AND " . $sWhereClauseBrowse;
 		}