Index: branches/RC/core/units/general/helpers/count_helper.php =================================================================== diff -u -r8993 -r9001 --- branches/RC/core/units/general/helpers/count_helper.php (.../count_helper.php) (revision 8993) +++ branches/RC/core/units/general/helpers/count_helper.php (.../count_helper.php) (revision 9001) @@ -94,6 +94,14 @@ $this->Conn->Query($sql); } + /** + * Counts items (of specific type) in category & subcategories + * + * @param string $prefix + * @param Array $params + * @param mixed $count_sql + * @return int + */ function CategoryItemCount($prefix, $params, $count_sql = null) { if (!$this->Application->findModule('Var', $prefix)) { @@ -102,7 +110,7 @@ } // 1. get root category for counting - $category_id = isset($params['_catid']) ? $params['_catid'] : false; + $category_id = isset($params['cat_id']) ? $params['cat_id'] : false; if (!is_numeric($category_id)) { $category_id = $this->Application->GetVar('m_cat_id'); } @@ -114,33 +122,24 @@ $where_clauses = Array (); $table_name = $this->Application->getUnitOption($prefix, 'TableName'); + // 1. count category items $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.'CategoryItems ci ON (ci.ItemResourceId = item_table.ResourceId) + INNER JOIN '.TABLE_PREFIX.'Category c ON (ci.CategoryId = c.CategoryId)'; - if (isset($params['_subcats']) && $params['_subcats']) { - $sql .= ' INNER JOIN '.TABLE_PREFIX.'Category c ON (ci.CategoryId = c.CategoryId)'; - - if ($category_id > 0) { - // get subcategories of required category - $tmp_sql = 'SELECT TreeLeft, TreeRight - FROM '.TABLE_PREFIX.'Category - WHERE CategoryId = '.$category_id; - $tree_info = $this->Conn->GetRow($tmp_sql); - $where_clauses[] = 'c.TreeLeft BETWEEN '.$tree_info['TreeLeft'].' AND '.$tree_info['TreeRight']; - } - - $count_current = isset($params['_countcurrent']) && $params['_countcurrent'] ? true : false; - if (!$count_current) { - $where_clauses[] = 'c.CategoryId <> '.$category_id.')'; - } + // 2. count items from subcategories + if ($category_id > 0) { + // get subcategories of required category + $tmp_sql = 'SELECT TreeLeft, TreeRight + FROM '.TABLE_PREFIX.'Category + WHERE CategoryId = '.$category_id; + $tree_info = $this->Conn->GetRow($tmp_sql); + $where_clauses[] = 'c.TreeLeft BETWEEN '.$tree_info['TreeLeft'].' AND '.$tree_info['TreeRight']; } - else { - $where_clauses[] = 'ci.CategoryId = '.$category_id; - } $where_clauses[] = 'item_table.Status = '.STATUS_ACTIVE; - if (isset($params['_today']) && $params['_today']) { + if (isset($params['today']) && $params['today']) { $today = adodb_mktime(0,0,0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); $where_clauses[] = 'item_table.CreatedOn >= '.$today; } @@ -150,6 +149,91 @@ return (int)$this->Conn->GetOne($sql); } + /** + * Counts items (of specific type) from all categories + * + * @param string $prefix + * @param bool $today + * @return int + */ + function ItemCount($prefix, $today = false) + { + $table_name = $this->Application->getUnitOption($prefix, 'TableName'); + + $sql = 'SELECT COUNT(*) + FROM '.$table_name.' item_table + INNER JOIN '.TABLE_PREFIX.'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId + INNER JOIN '.TABLE_PREFIX.'Category c ON c.CategoryId = ci.CategoryId + INNER JOIN '.TABLE_PREFIX.'PermCache perm_cache ON ci.CategoryId = perm_cache.CategoryId'; + + list ($view_perm, $view_filter) = $this->GetPermissionClause($prefix, 'perm_cache'); + $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; + } + + $sql .= ' WHERE ('.implode(') AND (', $where_clauses).')'; + + return $this->Conn->GetOne($sql); + } + + /** + * Returns categories count in system + * + * @param bool $today + * @return int + */ + function CategoryCount($today = false) + { + $table_name = $this->Application->getUnitOption('c', 'TableName'); + + $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) { + $today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); + $where_clauses[] = 'c.CreatedOn >= '.$today_date; + } + + $sql .= ' WHERE ('.implode(') AND (', $where_clauses).')'; + + return $this->Conn->GetOne($sql); + } + + /** + * Returns permission limitation clause for category item lists + * + * @param string $prefix + * @param string $table_alias + * @return Array + */ + function GetPermissionClause($prefix, $table_alias) + { + $sql = 'SELECT PermissionConfigId + FROM '.TABLE_PREFIX.'PermissionConfig + WHERE PermissionName = "'.$this->Application->getUnitOption($prefix, 'PermItemPrefix').'.VIEW"'; + $view_perm = $this->Conn->GetOne($sql); + + $groups = explode(',', $this->Application->RecallVar('UserGroups')); + foreach ($groups as $group) { + $view_filters[] = 'FIND_IN_SET('.$group.', '.$table_alias.'.acl)'; + } + + $view_filter = implode(' OR ', $view_filters); + + return Array ($view_perm, $view_filter); + } + } ?> \ No newline at end of file