Index: branches/RC/core/units/general/helpers/count_helper.php =================================================================== diff -u -r8929 -r8993 --- branches/RC/core/units/general/helpers/count_helper.php (.../count_helper.php) (revision 8929) +++ branches/RC/core/units/general/helpers/count_helper.php (.../count_helper.php) (revision 8993) @@ -1,19 +1,19 @@ 0) AND (LastCounted < '.adodb_mktime().' - LifeTime)'; $this->Conn->Query($sql); } - + /** * Returns counter value * @@ -31,7 +31,7 @@ FROM '.TABLE_PREFIX.'Counters WHERE Name = '.$this->Conn->qstr($query_name); $count_data = $this->Conn->GetRow($sql); - + if (!$count_data && ($query_name != $name)) { // cloned record not found -> search for origial one $sql = 'SELECT * @@ -40,36 +40,36 @@ $count_data = $this->Conn->GetRow($sql); $clone_counter = true; } - + if (is_null($count_data['CountValue'])) { $params['PREFIX'] = TABLE_PREFIX; $count_sql = $count_data['CountQuery']; - + foreach ($params as $replace_from => $replace_to) { $count_sql = str_replace('<%'.$replace_from.'%>', $replace_to, $count_sql); } - + if ($multiple_results) { $count_data['CountValue'] = serialize($this->Conn->GetCol($count_sql)); } else { $count_data['CountValue'] = $this->Conn->GetOne($count_sql); } - + if ($clone_counter && !$count_data['IsClone']) { // don't clone clones $count_data['CounterId'] = 0; $count_data['Name'] = $query_name; $count_data['IsClone'] = 1; } - + $count_data['LastCounted'] = adodb_mktime(); $this->Conn->doInsert($count_data, TABLE_PREFIX.'Counters', 'REPLACE'); } - + return $multiple_results ? unserialize($count_data['CountValue']) : $count_data['CountValue']; } - + /** * Resets counter, whitch are affected by one of specified tables * @@ -86,14 +86,70 @@ } $tables[$index] = '(TablesAffected LIKE "%|'.$table.'|%")'; } - + $sql = 'UPDATE '.TABLE_PREFIX.'Counters SET CountValue = NULL WHERE '.implode(' OR ', $tables); - + $this->Conn->Query($sql); } - + + function CategoryItemCount($prefix, $params, $count_sql = null) + { + if (!$this->Application->findModule('Var', $prefix)) { + // this is not module item + return $this->Application->ProcessParsedTag($prefix, 'CategoryItemCount', $params); + } + + // 1. get root category for counting + $category_id = isset($params['_catid']) ? $params['_catid'] : false; + if (!is_numeric($category_id)) { + $category_id = $this->Application->GetVar('m_cat_id'); + } + + if (!isset($count_sql)) { + $count_sql = 'COUNT(*)'; + } + + $where_clauses = Array (); + $table_name = $this->Application->getUnitOption($prefix, 'TableName'); + + $sql = 'SELECT '.$count_sql.' + FROM '.$table_name.' item_table + INNER JOIN '.TABLE_PREFIX.'CategoryItems ci ON (ci.ItemResourceId = item_table.ResourceId)'; + + 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.')'; + } + } + else { + $where_clauses[] = 'ci.CategoryId = '.$category_id; + } + $where_clauses[] = 'item_table.Status = '.STATUS_ACTIVE; + + 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; + } + + $sql .= ' WHERE ('.implode(') AND (', $where_clauses).')'; + + return (int)$this->Conn->GetOne($sql); + } + } ?> \ No newline at end of file