Skip to content

Commit

Permalink
Ticket #4329 - Browsings: Add possibility to easily change info field…
Browse files Browse the repository at this point in the history
…s in search results.
  • Loading branch information
AntonLV committed Aug 29, 2024
1 parent 60e6422 commit e7279e9
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 38 deletions.
121 changes: 83 additions & 38 deletions inc/classes/BxDolSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() : [];

Expand Down Expand Up @@ -844,48 +846,72 @@ 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
* return array with joinFields, ownFields, groupBy and join elements
*/
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;
}

Expand All @@ -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) {
Expand All @@ -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'];
Expand All @@ -923,7 +959,7 @@ function getSearchDataByParams ($aParams = '')
$aSql['join'] = $aJoins['join'];
$aSql['groupBy'] = $aJoins['groupBy'];
$aSql['groupHaving'] = $aJoins['groupHaving'];
}
}

$aSql['ownFields'] = trim($aSql['ownFields'], ', ');

Expand Down Expand Up @@ -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).
Expand Down
37 changes: 37 additions & 0 deletions modules/base/general/classes/BxBaseModGeneralSearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}

/** @} */

0 comments on commit e7279e9

Please sign in to comment.