Index: branches/5.1.x/core/units/helpers/count_helper.php =================================================================== diff -u -N -r13086 -r13168 --- branches/5.1.x/core/units/helpers/count_helper.php (.../count_helper.php) (revision 13086) +++ branches/5.1.x/core/units/helpers/count_helper.php (.../count_helper.php) (revision 13168) @@ -1,6 +1,6 @@ 0) AND (LastCounted < '.adodb_mktime().' - LifeTime)'; - $this->Conn->Query($sql); - } - /** * Returns counter value * @@ -38,6 +27,8 @@ */ function getCounter($name, $params = Array (), $query_name = null, $multiple_results = false) { + $this->resetExpiredCounters(); + $clone_counter = false; $query_name = isset($query_name) ? $query_name : rtrim($name.'-'.implode('-', array_values($params)), '-'); $sql = 'SELECT * @@ -107,6 +98,20 @@ $this->Conn->Query($sql); } + function resetExpiredCounters() + { + static $reset = false; + + if (!$reset) { + // reset expired counts + $sql = 'UPDATE '.TABLE_PREFIX.'Counters + SET CountValue = NULL + WHERE (LifeTime > 0) AND (LastCounted < '.adodb_mktime().' - LifeTime)'; + $this->Conn->Query($sql); + $reset = true; + } + } + /** * Counts items (of specific type) in category & subcategories * @@ -206,25 +211,42 @@ */ function CategoryCount($today = false) { - $table_name = $this->Application->getUnitOption('c', 'TableName'); + $cache_key = 'category_count[%CSerial%]'; - $sql = 'SELECT COUNT(*) - FROM '.$table_name.' c - INNER JOIN '.TABLE_PREFIX.'PermCache 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, 'c.Status = '.STATUS_ACTIVE, - ); - - if ($today) { + if ($today) { $today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); - $where_clauses[] = 'c.CreatedOn >= '.$today_date; + $cache_key .= ':date=' . $today_date; } - $sql .= ' WHERE ('.implode(') AND (', $where_clauses).')'; + $count = $this->Application->getCache($cache_key); - return $this->Conn->GetOne($sql); + if ($count === false) { + $sql = 'SELECT COUNT(*) + FROM ' . $this->Application->getUnitOption('c', 'TableName') . ' c + INNER JOIN ' . TABLE_PREFIX . 'PermCache 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, + 'c.Status = ' . STATUS_ACTIVE, + ); + + if ($today) { + $where_clauses[] = 'c.CreatedOn >= ' . $today_date; + } + + $sql .= ' WHERE ('.implode(') AND (', $where_clauses).')'; + + $count = $this->Conn->GetOne($sql); + + if ($count !== false) { + $this->Application->setCache($cache_key, $count); + } + } + + return $count; } /** @@ -236,17 +258,24 @@ */ function GetPermissionClause($prefix, $table_alias) { - $view_perm = $this->Application->getCache(__CLASS__ . __FUNCTION__, $prefix); - if ($view_perm === false) { - $sql = 'SELECT PermissionConfigId + $permissions_ids = $this->Application->getCache(__CLASS__ . '::' . __FUNCTION__); + + if ($permissions_ids === false) { + $this->Conn->nextQueryCachable = true; + + $sql = 'SELECT PermissionConfigId, PermissionName FROM '.TABLE_PREFIX.'PermissionConfig - WHERE PermissionName = "'.$this->Application->getUnitOption($prefix, 'PermItemPrefix').'.VIEW"'; - $view_perm = $this->Conn->GetOne($sql); + WHERE PermissionName LIKE "%.VIEW"'; + $permissions_ids = $this->Conn->GetCol($sql, 'PermissionName'); - $this->Application->setCache(__CLASS__ . __FUNCTION__, $prefix, $view_perm); + $this->Application->setCache(__CLASS__ . '::' . __FUNCTION__, $permissions_ids); } + $permission_prefix = $this->Application->getUnitOption($prefix, 'PermItemPrefix'); + $view_perm = $permissions_ids[$permission_prefix . '.VIEW']; + $groups = explode(',', $this->Application->RecallVar('UserGroups')); + foreach ($groups as $group) { $view_filters[] = 'FIND_IN_SET('.$group.', '.$table_alias.'.acl)'; }