Found a way to solve the second post from above:
function mc_get_search_categories_stats()
{
$categoryItems = array();
$getcategoriesStats = false;
foreach(Params::getParamsAsArray() as $param => $value) {
if (in_array($param, array('page', 'sOrder', 'iOrderType', 'sCategory', 'bPic'), true) === false) {
$getcategoriesStats = true;
break;
}
}
if ($getcategoriesStats === true) {
$actualSearch = osc_search();
$conditions = (array) json_decode($actualSearch->toJson());
$conditions['limit_init'] = 0;
$conditions['results_per_page'] = 500;
$conditions['aCategories'] = array();
$categoryStatsSearch = new Search();
$categoryStatsSearch->setJsonAlert($conditions);
$categoryStatsSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.fk_i_category_id');
$categoryStatsSearch->addField('COUNT('.DB_TABLE_PREFIX.'t_item.pk_i_id) as category_num_items_search');
$categoryStats = $categoryStatsSearch->doSearch(false, true);
unset($categoryStatsSearch);
foreach($categoryStats as $x){
$categoryItems[$x['fk_i_category_id']] = $x['category_num_items_search'];
}
$categorySearch = new Category();
foreach ($categorySearch->listAll() as $category){
$categoryParent[$category['pk_i_id']] = $category['fk_i_parent_id'];
}
foreach($categoryItems as $catId => $nr){
$id = $catId;
while($id !== null) {
$id = $categoryParent[$id];
if($id !== null) {
if(isset($categoryItems[$id]) === false) {
$categoryItems[$id] = $nr;
} else {
$categoryItems[$id] += $nr;
}
}
}
}
}
return $categoryItems;
}
But when the param bPic (seach only ads with picture is checked) => 2 group by statements and the results are wrong...
SOLUTION (that disregards the picture parameter for stats)
Core mod
THIS CAN BE CONSIDERED A BUG!
/oc-includes/osclass/model/Search.php
if($this->withPicture && $this->groupBy === '') {
$this->dao->join(sprintf('%st_item_resource', DB_TABLE_PREFIX), sprintf('%st_item_resource.fk_i_item_id = %st_item.pk_i_id', DB_TABLE_PREFIX, DB_TABLE_PREFIX), 'LEFT');
$this->dao->where(sprintf("%st_item_resource.s_content_type LIKE '%%image%%' ", DB_TABLE_PREFIX, DB_TABLE_PREFIX, DB_TABLE_PREFIX));
$this->dao->groupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
}
instead of
if($this->withPicture) {
$this->dao->join(sprintf('%st_item_resource', DB_TABLE_PREFIX), sprintf('%st_item_resource.fk_i_item_id = %st_item.pk_i_id', DB_TABLE_PREFIX, DB_TABLE_PREFIX), 'LEFT');
$this->dao->where(sprintf("%st_item_resource.s_content_type LIKE '%%image%%' ", DB_TABLE_PREFIX, DB_TABLE_PREFIX, DB_TABLE_PREFIX));
$this->dao->groupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
}