Skip to content

Commit

Permalink
Ticket #4384 - Timeline: Make all joins overwritable.
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLV committed Jul 25, 2023
1 parent 796594e commit 5b08d1a
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions modules/boonex/timeline/classes/BxTimelineDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,10 @@ public function getEvents($aParams)
'limit_clause' => &$sLimitClause
));

$sJoinClauseAddon = '';
if($this->_oConfig->isSortByUnread() && $this->_isList($aParams))
$sJoinClauseAddon = $this->prepareAsString(" LEFT JOIN `{$this->_sTableEvent2User}` AS `teu` ON `{$sTableAlias}`.`id`=`teu`.`event_id` AND `teu`.`user_id`=? ", $aParams['viewer_id']);

$sSqlMask = "SELECT {select}
FROM `{$sTable}` AS `{$sTableAlias}`
LEFT JOIN `{$this->_sTableHandlers}` ON `{$sTableAlias}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$sTableAlias}`.`action`=`{$this->_sTableHandlers}`.`alert_action`
INNER JOIN `sys_profiles` ON ABS(`{$sTableAlias}`.`object_owner_id`)=`sys_profiles`.`id` {$sJoinClauseAddon} {join}
WHERE 1 AND `sys_profiles`.`status`='active' {where} {order} {limit}";
LEFT JOIN `{$this->_sTableHandlers}` ON `{$sTableAlias}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$sTableAlias}`.`action`=`{$this->_sTableHandlers}`.`alert_action` {join}
WHERE 1 {where} {order} {limit}";

if(is_string($mixedWhereClause)) {
$aSqlMarkers = array(
Expand Down Expand Up @@ -889,6 +884,11 @@ protected function _getSqlPartsEventsList($aParams)
$sTableAlias = $this->getTableAlias();
$mixedJoinClause = $mixedWhereClause = "";

$mixedJoinClause = "INNER JOIN `sys_profiles` AS `tpoo` ON ABS(`{$sTableAlias}`.`object_owner_id`)=`tpoo`.`id` AND `tpoo`.`status`='active'";

if($this->_oConfig->isSortByUnread())
$mixedJoinClause .= $this->prepareAsString(" LEFT JOIN `{$this->_sTableEvent2User}` AS `teu` ON `{$sTableAlias}`.`id`=`teu`.`event_id` AND `teu`.`user_id`=? ", $aParams['viewer_id']);

$sWhereClauseStatus = "AND `{$sTableAlias}`.`active`='1' ";
$sWhereClauseStatus .= $this->prepareAsString("AND `{$sTableAlias}`.`status`=? ", isset($aParams['status']) ? $aParams['status'] : BX_TIMELINE_STATUS_ACTIVE);
$sWhereClauseStatus .= $this->_getSqlPartsEventsListStatusAdmin($aParams);
Expand Down Expand Up @@ -937,7 +937,7 @@ protected function _getSqlPartsEventsList($aParams)
$sWhereClauseCf = $oCf->isEnabled() ? $oCf->getSQLParts($sTableAlias, 'object_cf') . ' ' : '';

//--- Check type
$mixedWhereSubclause = "";
$mixedJoinSubclause = $mixedWhereSubclause = "";
switch($aParams['type']) {
//--- Feed: Hot
case BX_TIMELINE_TYPE_HOT:
Expand All @@ -950,7 +950,7 @@ protected function _getSqlPartsEventsList($aParams)
$mixedWhereClause .= $aQueryParts['where'] . " ";

//--- Select Hot posts.
$mixedJoinClause .= " LEFT JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
$mixedJoinSubclause = "LEFT JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
$mixedWhereSubclause = "NOT ISNULL(`{$this->_sTableHotTrack}`.`value`)";

//--- Select Promoted posts.
Expand Down Expand Up @@ -997,7 +997,7 @@ protected function _getSqlPartsEventsList($aParams)
if(empty($aParams['owner_id']))
break;

$mixedJoinClause = array();
$mixedJoinSubclause = array();
$mixedWhereSubclause = array();

$oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
Expand All @@ -1006,7 +1006,7 @@ protected function _getSqlPartsEventsList($aParams)
$aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
$aJoin1 = $aQueryParts['join'];

$mixedJoinClause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedWhereSubclause['p1'] = "1";

//--- Exclude Own (Direct) posts on timelines of following members.
Expand All @@ -1021,7 +1021,7 @@ protected function _getSqlPartsEventsList($aParams)
$aJoin2['table_alias'] = 'cc';
$aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);

$mixedJoinClause['p2'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
$mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
$mixedWhereSubclause['p2'] = "1";

//--- Select Promoted posts.
Expand All @@ -1033,19 +1033,19 @@ protected function _getSqlPartsEventsList($aParams)
if(empty($aParams['owner_id']) || empty($aParams['context']))
break;

$mixedJoinClause = [];
$mixedJoinSubclause = [];
$mixedWhereSubclause = [];

//--- Filter out unnecessary contexts.
$aContexts = is_array($aParams['context']) ? $aParams['context'] : [$aParams['context']];
$mixedJoinClause['p1'] = "INNER JOIN `sys_profiles` AS `tpo` ON ABS(`{$sTableAlias}`.`owner_id`)=`tpo`.`id` AND `tpo`.`type` IN (" . $this->implode_escape($aContexts) . ") AND `tpo`.`status`='active' ";
$mixedJoinSubclause['p1'] = "INNER JOIN `sys_profiles` AS `tpo` ON ABS(`{$sTableAlias}`.`owner_id`)=`tpo`.`id` AND `tpo`.`type` IN (" . $this->implode_escape($aContexts) . ") AND `tpo`.`status`='active' ";

//--- Join System posts received by following contexts.
$oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
$aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
$aJoin1 = $aQueryParts['join'];

$mixedJoinClause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedWhereSubclause['p1'] = "1";

//--- Select Promoted posts.
Expand All @@ -1057,7 +1057,7 @@ protected function _getSqlPartsEventsList($aParams)
if(empty($aParams['owner_id']))
break;

$mixedJoinClause = [];
$mixedJoinSubclause = [];
$mixedWhereSubclause = [];

$oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
Expand All @@ -1066,7 +1066,7 @@ protected function _getSqlPartsEventsList($aParams)
$aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
$aJoin1 = $aQueryParts['join'];

$mixedJoinClause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedWhereSubclause['p1'] = "`{$sTableAlias}`.`type`='bx_channels'";

//--- Exclude Own (Direct) posts on timelines of following members.
Expand All @@ -1083,7 +1083,7 @@ protected function _getSqlPartsEventsList($aParams)
if(empty($aParams['owner_id']))
break;

$mixedJoinClause = array();
$mixedJoinSubclause = array();
$mixedWhereSubclause = array();

$oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
Expand All @@ -1100,7 +1100,7 @@ protected function _getSqlPartsEventsList($aParams)
$aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
$aJoin1 = $aQueryParts['join'];

$mixedJoinClause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`type`<>'bx_channels'";

//--- Exclude Own (Direct) posts on timelines of following members.
Expand All @@ -1112,15 +1112,15 @@ protected function _getSqlPartsEventsList($aParams)
$aJoin2['table_alias'] = 'cc';
$aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);

$mixedJoinClause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system` = 0 AND `" . $sTableAlias . "`.`object_privacy_view` > 0 AND " . $aJoin2['condition'];
$mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system` = 0 AND `" . $sTableAlias . "`.`object_privacy_view` > 0 AND " . $aJoin2['condition'];
$mixedWhereSubclause['p3'] = "1";

//--- Select Promoted posts.
$mixedWhereSubclause['p4'] = "`{$sTableAlias}`.`promoted` <> '0'";

//--- Select Hot posts.
if($aParams['type'] == BX_TIMELINE_TYPE_FEED_AND_HOT) {
$mixedJoinClause['p5'] = "INNER JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
$mixedJoinSubclause['p5'] = "INNER JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
$mixedWhereSubclause['p5'] = "1";
}
break;
Expand All @@ -1130,7 +1130,7 @@ protected function _getSqlPartsEventsList($aParams)
if(empty($aParams['owner_id']))
break;

$mixedJoinClause = array();
$mixedJoinSubclause = array();
$mixedWhereSubclause = array();

$oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
Expand All @@ -1147,7 +1147,7 @@ protected function _getSqlPartsEventsList($aParams)
$aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
$aJoin1 = $aQueryParts['join'];

$mixedJoinClause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedWhereSubclause['p2'] = "1";

//--- Exclude Own (Direct) posts on timelines of following members.
Expand All @@ -1159,7 +1159,7 @@ protected function _getSqlPartsEventsList($aParams)
$aJoin2['table_alias'] = 'cc';
$aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);

$mixedJoinClause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
$mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
$mixedWhereSubclause['p3'] = "1";

//--- Select Promoted posts.
Expand All @@ -1171,7 +1171,7 @@ protected function _getSqlPartsEventsList($aParams)
if(empty($aParams['owner_id']))
break;

$mixedJoinClause = [];
$mixedJoinSubclause = [];
$mixedWhereSubclause = [];

$oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
Expand All @@ -1180,8 +1180,8 @@ protected function _getSqlPartsEventsList($aParams)
$aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
$aJoin1 = $aQueryParts['join'];

$mixedJoinClause['p1'] = "INNER JOIN `sys_profiles` AS `p` ON `" . $sTableAlias ."`.`owner_id`=`p`.`id` AND `p`.`type`='" . $aParams['type'] . "' ";
$mixedJoinClause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
$mixedJoinSubclause['p1'] = "INNER JOIN `sys_profiles` AS `p` ON `" . $sTableAlias ."`.`owner_id`=`p`.`id` AND `p`.`type`='" . $aParams['type'] . "' ";
$mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];

$mixedWhereSubclause['p1'] = "1";
if(!empty($aParams['context']))
Expand All @@ -1205,6 +1205,7 @@ protected function _getSqlPartsEventsList($aParams)
'params' => $aAlertParams,
'table' => isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable,
'join_clause' => &$mixedJoinClause,
'join_subclause' => &$mixedJoinSubclause,
'where_clause' => &$mixedWhereClause,
'where_clause_status' => &$sWhereClauseStatus,
'where_clause_filter' => &$sWhereClauseFilter,
Expand All @@ -1226,9 +1227,22 @@ protected function _getSqlPartsEventsList($aParams)
$mixedWhereClause .= $sWhereClauseUnpublished;
$mixedWhereClause .= $sWhereClauseCf;

//--- Combine general Join with SubJoins.
if(!empty($mixedJoinSubclause)) {
if(is_array($mixedJoinSubclause)) {
$aJoinClause = [];
foreach($mixedJoinSubclause as $sKey => $sValue)
$aJoinClause[$sKey] = $mixedJoinClause . " " . $sValue . " ";
$mixedJoinClause = $aJoinClause;
}
else
$mixedJoinClause .= " " . $mixedJoinSubclause . " ";
}

//--- Combine general Where with SubWheres.
if(!empty($mixedWhereSubclause)) {
if(is_array($mixedWhereSubclause)) {
$aWhereClause = array();
$aWhereClause = [];
foreach($mixedWhereSubclause as $sKey => $sValue)
$aWhereClause[$sKey] = $mixedWhereClause . "AND " . $sValue . " ";
$mixedWhereClause = $aWhereClause;
Expand All @@ -1237,7 +1251,7 @@ protected function _getSqlPartsEventsList($aParams)
$mixedWhereClause .= "AND (" . $mixedWhereSubclause . ") ";
}

return array($mixedJoinClause, $mixedWhereClause);
return [$mixedJoinClause, $mixedWhereClause];
}

protected function _isList($aParams)
Expand Down

0 comments on commit 5b08d1a

Please sign in to comment.