diff --git a/include/ws_functions/pwg.categories.php b/include/ws_functions/pwg.categories.php index ea8f7f395..287c84f47 100644 --- a/include/ws_functions/pwg.categories.php +++ b/include/ws_functions/pwg.categories.php @@ -608,14 +608,37 @@ SELECT category_id, COUNT(*) AS counter // pwg_db_real_escape_string + $where = array('1=1'); + + if (!$params['recursive']) + { + if ($params['cat_id']>0) + { + $where[] = '( + id_uppercat = '. (int)($params['cat_id']) .' + OR id='.(int)($params['cat_id']).' + )'; + } + else + { + $where[] = 'id_uppercat IS NULL'; + } + } + elseif ($params['cat_id']>0) + { + $where[] = 'uppercats '. DB_REGEX_OPERATOR .' \'(^|,)'. + (int)($params['cat_id']) .'(,|$)\''; + } + $query = ' SELECT SQL_CALC_FOUND_ROWS id, name, comment, uppercats, global_rank, dir, status, image_order - FROM '. CATEGORIES_TABLE; + FROM '. CATEGORIES_TABLE .' + WHERE '. implode("\n AND ", $where); if (isset($params["search"]) and $params['search'] != "") { - $query .= ' - WHERE name LIKE \'%'.pwg_db_real_escape_string($params["search"]).'%\' + $query .= ' + AND name LIKE \'%'.pwg_db_real_escape_string($params["search"]).'%\' LIMIT '.$conf["linked_album_search_limit"]; } @@ -666,8 +689,33 @@ SELECT SQL_CALC_FOUND_ROWS id, name, comment, uppercats, global_rank, dir, statu $cats[] = $row; } + if (!$params['recursive']) + { + $cats_ids = array_column($cats, 'id'); + $nb_subcats_of = array(); + if (!empty($cats_ids)) + { + $query = ' +SELECT + id_uppercat, + COUNT(*) AS nb_subcats + FROM '. CATEGORIES_TABLE .' + WHERE id_uppercat IN ('. implode(',', $cats_ids ) .') + GROUP BY id_uppercat +'; + + $nb_subcats_of = query2array($query, 'id_uppercat', 'nb_subcats'); + } + + foreach ($cats as $idx => $cat) + { + $cats[$idx]['nb_categories'] = intval($nb_subcats_of[ $cat['id'] ] ?? 0); + } + } + $limit_reached = false; - if ($counter > $conf["linked_album_search_limit"]) { + if ($counter > $conf["linked_album_search_limit"]) + { $limit_reached = true; } diff --git a/ws.php b/ws.php index 07c9efbbb..69d438dd2 100644 --- a/ws.php +++ b/ws.php @@ -603,7 +603,13 @@ function ws_addDefaultMethods( $arr ) 'pwg.categories.getAdminList', 'ws_categories_getAdminList', array( + 'cat_id' => array('default'=>null, + 'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE, + 'info'=>'Parent category. "0" or empty for root.'), 'search' => array('default' => null), + 'recursive' => array( + 'default' => true, + 'type' => WS_TYPE_BOOL), 'additional_output' => array('default'=>null, 'info'=>'Comma saparated list (see method description)'), ),