From e7279e9ecc8d95238c0204062fb64a5674e1bde5 Mon Sep 17 00:00:00 2001 From: AntonLV Date: Thu, 29 Aug 2024 15:38:47 +0300 Subject: [PATCH] Ticket #4329 - Browsings: Add possibility to easily change info fields in search results. --- inc/classes/BxDolSearch.php | 121 ++++++++++++------ .../classes/BxBaseModGeneralSearchResult.php | 37 ++++++ 2 files changed, 120 insertions(+), 38 deletions(-) diff --git a/inc/classes/BxDolSearch.php b/inc/classes/BxDolSearch.php index 0e4efd7503..032b083443 100644 --- a/inc/classes/BxDolSearch.php +++ b/inc/classes/BxDolSearch.php @@ -765,7 +765,9 @@ function getSearchData () 'mode' => $this->_sMode ]); - $this->aPseud = $this->_getPseud(); + if(($this->aPseud = $this->_getPseudFromParam()) === false) + $this->aPseud = $this->_getPseud(); + $this->setConditionParams(); $aData = $this->aCurrent['paginate']['num'] > 0 ? $this->getSearchDataByParams() : []; @@ -844,6 +846,16 @@ function getSearchQuery($sObject, $aParams = []) return $aQuery; } + function getFieldsOwn() + { + return false; + } + + function getFieldsJoin($sJoin) + { + return false; + } + /** * Get array with code for sql elements * @param $bRenameMode indicator of renmaing fields @@ -851,41 +863,55 @@ function getSearchQuery($sObject, $aParams = []) */ function getJoins ($bRenameMode = true) { - $aSql = array(); - // joinFields & join - if (isset($this->aCurrent['join']) && is_array($this->aCurrent['join'])) { - $aSql = array('join' => '', 'ownFields' => '', 'joinFields' => '', 'groupBy' => '', 'groupHaving' => ''); - foreach ($this->aCurrent['join'] as $sKey => $aValue) { - $sAlias = isset($aValue['table_alias']) ? $aValue['table_alias'] : $aValue['table']; - $sTableAlias = isset($aValue['table_alias']) ? " AS {$aValue['table_alias']} " : ''; - - if (is_array($aValue['joinFields'])) { - foreach ($aValue['joinFields'] as $sValue) { - $aSql['joinFields'] .= $this->setFieldUnit($sValue, $sAlias, isset($aValue['operator']) ? $aValue['operator'] : null, $bRenameMode); - } - } + if(!isset($this->aCurrent['join']) || !is_array($this->aCurrent['join'])) + return false; - // group by - if(isset($aValue['groupTable'])) - $aSql['groupBy'] .= "`{$aValue['groupTable']}`.`{$aValue['groupField']}`, "; - - // having - if(isset($aValue['groupHaving'])) - $aSql['groupHaving'] .= $aValue['groupHaving']; - - $sOn = isset($aValue['mainTable']) ? $aValue['mainTable'] : $this->aCurrent['table']; - $aSql['join'] .= " {$aValue['type']} JOIN `{$aValue['table']}` $sTableAlias ON " . (!empty($aValue['on_sql']) ? $aValue['on_sql'] : "`{$sAlias}`.`{$aValue['onField']}`=" . trim($this->setFieldUnit($aValue['mainField'], $sOn, isset($aValue['mainFieldFunc']) ? $aValue['mainFieldFunc'] : '', false), ", ")); - $aSql['ownFields'] .= $this->setFieldUnit($aValue['mainField'], $sOn, '', $bRenameMode); - } + $aSql = [ + 'join' => '', + 'ownFields' => '', + 'joinFields' => '', + 'groupBy' => '', + 'groupHaving' => '' + ]; + + foreach($this->aCurrent['join'] as $sKey => $aValue) { + $sAlias = isset($aValue['table_alias']) ? $aValue['table_alias'] : $aValue['table']; + $sTableAlias = isset($aValue['table_alias']) ? " AS {$aValue['table_alias']} " : ''; + $sOperator = isset($aValue['operator']) ? $aValue['operator'] : null; + + // joinFields + $aJoinFields = $this->getFieldsJoin($sKey); + if($aJoinFields === false && !empty($aValue['joinFields']) && is_array($aValue['joinFields'])) + $aJoinFields = $aValue['joinFields']; + + if($aJoinFields) + foreach($aJoinFields as $mixedField) { + list($sFldName, $sFldOperator) = is_array($mixedField) ? $mixedField : [$mixedField, $sOperator]; + + $aSql['joinFields'] .= $this->setFieldUnit($sFldName, $sAlias, $sFldOperator, $bRenameMode); + } - $aSql['joinFields'] = trim($aSql['joinFields'], ', '); + // group by + if(isset($aValue['groupTable'])) + $aSql['groupBy'] .= "`{$aValue['groupTable']}`.`{$aValue['groupField']}`, "; - if(!empty($aSql['groupBy'])) - $aSql['groupBy'] = trim('GROUP BY ' . $aSql['groupBy'], ', '); + // having + if(isset($aValue['groupHaving'])) + $aSql['groupHaving'] .= $aValue['groupHaving']; - if(!empty($aSql['groupHaving'])) - $aSql['groupHaving'] = 'HAVING ' . $aSql['groupHaving']; + $sOn = isset($aValue['mainTable']) ? $aValue['mainTable'] : $this->aCurrent['table']; + $aSql['join'] .= " {$aValue['type']} JOIN `{$aValue['table']}` $sTableAlias ON " . (!empty($aValue['on_sql']) ? $aValue['on_sql'] : "`{$sAlias}`.`{$aValue['onField']}`=" . trim($this->setFieldUnit($aValue['mainField'], $sOn, isset($aValue['mainFieldFunc']) ? $aValue['mainFieldFunc'] : '', false), ", ")); + $aSql['ownFields'] .= $this->setFieldUnit($aValue['mainField'], $sOn, '', $bRenameMode); } + + $aSql['joinFields'] = trim($aSql['joinFields'], ', '); + + if(!empty($aSql['groupBy'])) + $aSql['groupBy'] = trim('GROUP BY ' . $aSql['groupBy'], ', '); + + if(!empty($aSql['groupHaving'])) + $aSql['groupHaving'] = 'HAVING ' . $aSql['groupHaving']; + return $aSql; } @@ -898,7 +924,10 @@ function getSearchDataByParams ($aParams = '') { $bForUnion = isset($aParams['for_union']) && $aParams['for_union'] === true; - $aSql = array('ownFields'=>'', 'joinFields'=>'', 'order'=>''); + $aSql = [ + 'ownFields' => '', + 'order' => '' + ]; // searchFields if($bForUnion) { @@ -908,13 +937,20 @@ function getSearchDataByParams ($aParams = '') $aSql['ownFields'] .= $this->setFieldUnit(!empty($this->aCurrent['added']) ? $this->aCurrent['added'] : 'added', $sTable); $aSql['ownFields'] .= "'" . $this->getContentInfoName() . "' AS `content_info`"; } - else - foreach ($this->aCurrent['ownFields'] as $sValue) - $aSql['ownFields'] .= $this->setFieldUnit($sValue, $this->aCurrent['table']); + else { + $aOwnFields = $this->getFieldsOwn(); + if($aOwnFields === false) + $aOwnFields = $this->aCurrent['ownFields']; + + foreach($aOwnFields as $mixedField) { + list($sFldName, $sFldOperator) = is_array($mixedField) ? $mixedField : [$mixedField, '']; + + $aSql['ownFields'] .= $this->setFieldUnit($sFldName, $this->aCurrent['table'], $sFldOperator); + } + } // joinFields & join - $aJoins = $this->getJoins(); - if (!empty($aJoins)) { + if(($aJoins = $this->getJoins()) !== false) { if(!$bForUnion) { $aSql['ownFields'] .= $aJoins['ownFields']; $aSql['ownFields'] .= $aJoins['joinFields']; @@ -923,7 +959,7 @@ function getSearchDataByParams ($aParams = '') $aSql['join'] = $aJoins['join']; $aSql['groupBy'] = $aJoins['groupBy']; $aSql['groupHaving'] = $aJoins['groupHaving']; - } + } $aSql['ownFields'] = trim($aSql['ownFields'], ', '); @@ -1345,6 +1381,15 @@ function getMultiValues ($aValues) return $oDb->implode_escape($aValues); } + /** + * System method for filling aPseud array. Fill field aPseud for current class from system option. + * It may be useful when Fields are taken from system option too. + */ + function _getPseudFromParam () + { + return false; + } + /** * System method for filling aPseud array. * Fill field aPseud for current class (if you will use own getSearchUnit methods then not necessary to redeclare). diff --git a/modules/base/general/classes/BxBaseModGeneralSearchResult.php b/modules/base/general/classes/BxBaseModGeneralSearchResult.php index b06b44aea8..6683c12c0c 100644 --- a/modules/base/general/classes/BxBaseModGeneralSearchResult.php +++ b/modules/base/general/classes/BxBaseModGeneralSearchResult.php @@ -41,6 +41,31 @@ function getContentInfoObject() { return BxDolContentInfo::getObjectInstance($this->getContentInfoName()); } + + function getFieldsOwn() + { + $mixedFields = getParam($this->getModuleName() . '_browse_fields_own'); + if(empty($mixedFields)) + return false; + + if(($aFields = json_decode($mixedFields, true)) !== null) + return $aFields; + + $sDelimiter = ','; + if(strpos($mixedFields, $sDelimiter) !== false) + return explode($sDelimiter, $mixedFields); + + return false; + } + + function getFieldsJoin($sJoin) + { + $mixedFields = getParam($this->getModuleName() . '_browse_fields_join'); + if(empty($mixedFields)) + return false; + + return ($aJoins = json_decode($mixedFields, true)) !== null && !empty($aJoins[$sJoin]) ? $aJoins[$sJoin] : false; + } function getRssUnitLink (&$a) { @@ -268,6 +293,18 @@ function decodeDataAPI($a) return $a; } + + function _getPseudFromParam () + { + $mixedPseud = getParam($this->getModuleName() . '_browse_pseud'); + if(empty($mixedPseud)) + return false; + + if(($aPseud = json_decode($mixedPseud, true)) !== null) + return $aPseud; + + return false; + } } /** @} */