Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/kernel/db/dblist.php
Show First 20 Lines • Show All 719 Lines • ▼ Show 20 Line(s) | |||||
* @param string $clause where clause to extract calculated fields from | * @param string $clause where clause to extract calculated fields from | ||||
* @param int $aggregated 0 - having + aggregated, 1 - having only, 2 - aggregated only | * @param int $aggregated 0 - having + aggregated, 1 - having only, 2 - aggregated only | ||||
* @param bool $replace_table | * @param bool $replace_table | ||||
* @return string | * @return string | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
public function extractCalculatedFields($clause, $aggregated = 1, $replace_table = false) | public function extractCalculatedFields($clause, $aggregated = 1, $replace_table = false) | ||||
{ | { | ||||
if ( !$clause ) { | |||||
return ''; | |||||
} | |||||
$fields = $this->getCalculatedFields($aggregated); | $fields = $this->getCalculatedFields($aggregated); | ||||
if ( is_array($fields) && count($fields) > 0 ) { | if ( is_array($fields) && count($fields) > 0 ) { | ||||
$fields = str_replace('%2$s', $this->Application->GetVar('m_lang'), $fields); | $fields = str_replace('%2$s', $this->Application->GetVar('m_lang'), $fields); | ||||
foreach ($fields as $field_name => $field_expression) { | foreach ($fields as $field_name => $field_expression) { | ||||
$clause = preg_replace('/(\\(+)[(,` ]*' . $field_name . '[` ]{1}/', '\1 (' . $field_expression . ') ', $clause); | $clause = preg_replace('/(\\(+)[(,` ]*' . $field_name . '[` ]{1}/', '\1 (' . $field_expression . ') ', $clause); | ||||
$clause = preg_replace('/[,` ]{1}' . $field_name . '[` ]{1}/', ' (' . $field_expression . ') ', $clause); | $clause = preg_replace('/[,` ]{1}' . $field_name . '[` ]{1}/', ' (' . $field_expression . ') ', $clause); | ||||
Show All 18 Lines | |||||
$where->addFilter( | $where->addFilter( | ||||
'system_where', | 'system_where', | ||||
$this->extractCalculatedFields($this->WhereFilter[self::FLT_SYSTEM]->getSQL()) | $this->extractCalculatedFields($this->WhereFilter[self::FLT_SYSTEM]->getSQL()) | ||||
); | ); | ||||
if (!$system_filters_only) { | if (!$system_filters_only) { | ||||
$where->addFilter('view_where', $this->WhereFilter[self::FLT_VIEW] ); | $where->addFilter('view_where', $this->WhereFilter[self::FLT_VIEW] ); | ||||
$search_w = $this->WhereFilter[self::FLT_SEARCH]->getSQL(); | |||||
if ($search_w || $for_counting) { // move search_having to search_where in case search_where isset or we are counting | // Handle general grid search filter. Append transformed having clause into where clause. | ||||
$search_w = $this->WhereFilter[self::FLT_SEARCH]->getSQL(); | |||||
$search_h = $this->extractCalculatedFields( $this->HavingFilter[self::FLT_SEARCH]->getSQL() ); | $search_h = $this->extractCalculatedFields($this->HavingFilter[self::FLT_SEARCH]->getSQL()); | ||||
$search_w = ($search_w && $search_h) ? $search_w.' OR '.$search_h : $search_w.$search_h; | $search_w = ($search_w && $search_h) ? $search_w . ' OR ' . $search_h : $search_w . $search_h; | ||||
$where->addFilter('search_where', $search_w ); | $where->addFilter('search_where', $search_w); | ||||
} | |||||
// CUSTOM | // Handle custom per-column grid filters. Append transformed having clause into where clause. | ||||
$search_w = $this->WhereFilter[self::FLT_CUSTOM]->getSQL(); | $search_w = $this->WhereFilter[self::FLT_CUSTOM]->getSQL(); | ||||
if ($search_w || $for_counting) { // move search_having to search_where in case search_where isset or we are counting | |||||
$search_h = $this->extractCalculatedFields( $this->HavingFilter[self::FLT_CUSTOM]->getSQL() ); | $search_h = $this->extractCalculatedFields($this->HavingFilter[self::FLT_CUSTOM]->getSQL()); | ||||
$search_w = ($search_w && $search_h) ? $search_w.' AND '.$search_h : $search_w.$search_h; | $search_w = ($search_w && $search_h) ? $search_w . ' AND ' . $search_h : $search_w . $search_h; | ||||
$where->addFilter('custom_where', $search_w ); | $where->addFilter('custom_where', $search_w); | ||||
} | } | ||||
// CUSTOM | |||||
} | |||||
if( $for_counting ) // add system_having and view_having to where | if( $for_counting ) // add system_having and view_having to where | ||||
{ | { | ||||
$where->addFilter('system_having', $this->extractCalculatedFields($this->HavingFilter[kDBList::FLT_SYSTEM]->getSQL()) ); | $where->addFilter('system_having', $this->extractCalculatedFields($this->HavingFilter[kDBList::FLT_SYSTEM]->getSQL()) ); | ||||
if (!$system_filters_only) $where->addFilter('view_having', $this->extractCalculatedFields( $this->HavingFilter[kDBList::FLT_VIEW]->getSQL() ) ); | if (!$system_filters_only) $where->addFilter('view_having', $this->extractCalculatedFields( $this->HavingFilter[kDBList::FLT_VIEW]->getSQL() ) ); | ||||
} | } | ||||
return $where->getSQL(); | return $where->getSQL(); | ||||
Show All 28 Lines | |||||
if ($aggregated == 0) { | if ($aggregated == 0) { | ||||
if (!$system_filters_only) { | if (!$system_filters_only) { | ||||
$having->addFilter('view_aggregated', $this->AggregateFilter[kDBList::FLT_VIEW] ); | $having->addFilter('view_aggregated', $this->AggregateFilter[kDBList::FLT_VIEW] ); | ||||
} | } | ||||
$having->addFilter('system_aggregated', $this->AggregateFilter[kDBList::FLT_SYSTEM]); | $having->addFilter('system_aggregated', $this->AggregateFilter[kDBList::FLT_SYSTEM]); | ||||
} | } | ||||
if (!$system_filters_only) { | if (!$system_filters_only) { | ||||
// Don't add search/custom having filters here, because they're added to the where clause. | |||||
$having->addFilter('view_having', $this->HavingFilter[kDBList::FLT_VIEW] ); | $having->addFilter('view_having', $this->HavingFilter[kDBList::FLT_VIEW] ); | ||||
$having->addFilter('custom_having', $this->HavingFilter[kDBList::FLT_CUSTOM] ); | |||||
$search_w = $this->WhereFilter[kDBList::FLT_SEARCH]->getSQL(); | |||||
if (!$search_w) { | |||||
$having->addFilter('search_having', $this->HavingFilter[kDBList::FLT_SEARCH] ); | |||||
} | |||||
} | } | ||||
return $having->getSQL(); | return $having->getSQL(); | ||||
} | } | ||||
/** | /** | ||||
* Returns GROUP BY clause of the query | * Returns GROUP BY clause of the query | ||||
* | * | ||||
▲ Show 20 Lines • Show All 952 Lines • Show Last 20 Lines |