Index: branches/5.3.x/core/units/helpers/count_helper.php =================================================================== diff -u -N -r15698 -r15902 --- branches/5.3.x/core/units/helpers/count_helper.php (.../count_helper.php) (revision 15698) +++ branches/5.3.x/core/units/helpers/count_helper.php (.../count_helper.php) (revision 15902) @@ -1,6 +1,6 @@ Application->getUnitConfig($prefix)->getTableName(); if ( !isset($count_sql) ) { $count_sql = 'COUNT(*)'; } + $where_clause = array( + 'ci.PrimaryCat = 1', + 'c.Status = ' . STATUS_ACTIVE, + ); + $sql = 'SELECT ' . $count_sql . ' FROM ' . $table_name . ' item_table INNER JOIN ' . TABLE_PREFIX . 'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId - INNER JOIN ' . TABLE_PREFIX . 'Categories c ON c.CategoryId = ci.CategoryId - INNER JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm_cache ON ci.CategoryId = perm_cache.CategoryId'; + INNER JOIN ' . TABLE_PREFIX . 'Categories c ON c.CategoryId = ci.CategoryId'; - list ($view_perm, $view_filter) = $this->GetPermissionClause($prefix, 'perm_cache'); + list ($sql, $where_clause) = $this->attachViewPermissionCheck($prefix, $sql, $where_clause, 'ci.CategoryId'); - $where_clauses = Array ( - $view_filter, 'perm_cache.PermId = ' . $view_perm, - 'ci.PrimaryCat = 1', - 'c.Status = ' . STATUS_ACTIVE, - ); - if ( $today ) { $today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); - $where_clauses[] = 'item_table.CreatedOn >= ' . $today_date; + $where_clause[] = 'item_table.CreatedOn >= ' . $today_date; } - $sql .= ' WHERE (' . implode(') AND (', $where_clauses) . ')'; + $sql .= ' WHERE (' . implode(') AND (', $where_clause) . ')'; return (int)$this->Conn->GetOne($sql); } @@ -227,23 +227,20 @@ $count = $this->Application->getCache($cache_key); if ( $count === false ) { - $sql = 'SELECT COUNT(*) - FROM ' . $this->Application->getUnitConfig('c')->getTableName() . ' c - INNER JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm_cache ON c.CategoryId = perm_cache.CategoryId'; - - list ($view_perm, $view_filter) = $this->GetPermissionClause('c', 'perm_cache'); - - $where_clauses = Array ( - $view_filter, - 'perm_cache.PermId = ' . $view_perm, + $where_clause = Array ( 'c.Status = ' . STATUS_ACTIVE, ); + $sql = 'SELECT COUNT(*) + FROM ' . $this->Application->getUnitConfig('c')->getTableName() . ' c'; + + list ($sql, $where_clause) = $this->attachViewPermissionCheck('c', $sql, $where_clause); + if ( $today ) { - $where_clauses[] = 'c.CreatedOn >= ' . $today_date; + $where_clause[] = 'c.CreatedOn >= ' . $today_date; } - $sql .= ' WHERE (' . implode(') AND (', $where_clauses) . ')'; + $sql .= ' WHERE (' . implode(') AND (', $where_clause) . ')'; $count = $this->Conn->GetOne($sql); @@ -256,6 +253,33 @@ } /** + * Updates given data to make SQL use view permission check. + * + * @param string $prefix Unit config prefix. + * @param string $sql Sql. + * @param array $where_clause Where clause. + * @param string $category_field Field, where to get Category ID. + * + * @return array + * @access public + */ + public function attachViewPermissionCheck($prefix, $sql, $where_clause, $category_field = 'c.CategoryId') + { + if ( !$this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) { + return array($sql, $where_clause); + } + + list ($view_perm, $view_filter) = $this->GetPermissionClause($prefix, 'perm_cache'); + + $where_clause[] = $view_filter; + $where_clause[] = 'perm_cache.PermId = ' . $view_perm; + + $sql .= ' INNER JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm_cache ON ' . $category_field . ' = perm_cache.CategoryId'; + + return array($sql, $where_clause); + } + + /** * Returns permission limitation clause for category item lists * * @param string $prefix